|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import argparse |
| 8 | +import os |
| 9 | + |
| 10 | +from typing import Dict |
| 11 | + |
| 12 | +from executorch.backends.vulkan.test.op_tests.cases import test_suites |
| 13 | + |
| 14 | +from executorch.backends.vulkan.test.op_tests.utils.gen_benchmark_vk import ( |
| 15 | + VkBenchmarkFileGen, |
| 16 | +) |
| 17 | +from executorch.backends.vulkan.test.op_tests.utils.gen_computegraph import ( |
| 18 | + ComputeGraphGen, |
| 19 | +) |
| 20 | +from executorch.backends.vulkan.test.op_tests.utils.test_suite import TestSuite |
| 21 | +from torchgen import local |
| 22 | + |
| 23 | +from torchgen.gen import parse_native_yaml, ParsedYaml |
| 24 | +from torchgen.model import DispatchKey, NativeFunction |
| 25 | + |
| 26 | + |
| 27 | +def registry_name(f: NativeFunction) -> str: |
| 28 | + name = str(f.namespace) + "." + str(f.func.name) |
| 29 | + if len(f.func.name.overload_name) == 0: |
| 30 | + name += ".default" |
| 31 | + return name |
| 32 | + |
| 33 | + |
| 34 | +def construct_f_map(parsed_yaml: ParsedYaml) -> Dict[str, NativeFunction]: |
| 35 | + f_map: Dict[str, NativeFunction] = {} |
| 36 | + for f in parsed_yaml.native_functions: |
| 37 | + f_map[registry_name(f)] = f |
| 38 | + return f_map |
| 39 | + |
| 40 | + |
| 41 | +def process_test_suites( |
| 42 | + cpp_generator: VkBenchmarkFileGen, |
| 43 | + f_map: Dict[str, NativeFunction], |
| 44 | + test_suites: Dict[str, TestSuite], |
| 45 | +) -> None: |
| 46 | + for registry_name, op_test_suite in test_suites.items(): |
| 47 | + f = f_map[registry_name] |
| 48 | + cpp_generator.add_suite(registry_name, f, op_test_suite) |
| 49 | + |
| 50 | + |
| 51 | +@local.parametrize( |
| 52 | + use_const_ref_for_mutable_tensors=False, use_ilistref_for_tensor_lists=False |
| 53 | +) |
| 54 | +def generate_cpp( |
| 55 | + native_functions_yaml_path: str, tags_path: str, output_dir: str |
| 56 | +) -> None: |
| 57 | + output_file = os.path.join(output_dir, "op_benchmarks.cpp") |
| 58 | + cpp_generator = VkBenchmarkFileGen(output_file) |
| 59 | + |
| 60 | + parsed_yaml = parse_native_yaml(native_functions_yaml_path, tags_path) |
| 61 | + f_map = construct_f_map(parsed_yaml) |
| 62 | + |
| 63 | + ComputeGraphGen.backend_key = parsed_yaml.backend_indices[DispatchKey.CPU] |
| 64 | + |
| 65 | + process_test_suites(cpp_generator, f_map, test_suites) |
| 66 | + |
| 67 | + with open(output_file, "w") as file: |
| 68 | + file.write(cpp_generator.generate_cpp()) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + parser = argparse.ArgumentParser() |
| 73 | + parser.add_argument( |
| 74 | + "--aten-yaml-path", |
| 75 | + help="path to native_functions.yaml file.", |
| 76 | + ) |
| 77 | + parser.add_argument( |
| 78 | + "--tags-path", |
| 79 | + help="Path to tags.yaml. Required by yaml parsing in gen_correctness_vk system.", |
| 80 | + ) |
| 81 | + |
| 82 | + parser.add_argument("-o", "--output", help="Output directory", required=True) |
| 83 | + args = parser.parse_args() |
| 84 | + generate_cpp(args.aten_yaml_path, args.tags_path, args.output) |
0 commit comments