|
| 1 | +#!/usr/bin/env fbpython |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +import os |
| 9 | + |
| 10 | +import yaml |
| 11 | + |
| 12 | +from torchgen.code_template import CodeTemplate |
| 13 | + |
| 14 | + |
| 15 | +ops_and_dtypes_template_str = """((string_view(operator_name).compare("$operator_name") == 0)\n && ($dtype_checks))""" |
| 16 | +ops_and_dtypes_template = CodeTemplate(ops_and_dtypes_template_str) |
| 17 | + |
| 18 | +selected_kernel_dtypes_h_template_str = """#pragma once |
| 19 | +/** |
| 20 | + * Generated by executorch/codegen/tools/gen_selected_mobile_ops_header.py |
| 21 | + */ |
| 22 | +
|
| 23 | +inline constexpr bool should_include_kernel_dtype( |
| 24 | + const char *operator_name, |
| 25 | + exec_aten::ScalarType scalar_type |
| 26 | +) { |
| 27 | + return $body; |
| 28 | +} |
| 29 | +""" |
| 30 | +selected_kernel_dtypes_h_template = CodeTemplate(selected_kernel_dtypes_h_template_str) |
| 31 | + |
| 32 | +# enum from: https://github.com/pytorch/executorch/blob/main/runtime/core/portable_type/scalar_type.h |
| 33 | +dtype_enum_to_type = { |
| 34 | + "0": "Byte", |
| 35 | + "1": "Char", |
| 36 | + "2": "Short", |
| 37 | + "3": "Int", |
| 38 | + "4": "Long", |
| 39 | + "5": "Half", |
| 40 | + "6": "Float", |
| 41 | + "7": "Double", |
| 42 | + "8": "ComplexHalf", |
| 43 | + "9": "ComplexFloat", |
| 44 | + "10": "ComplexDouble", |
| 45 | + "11": "Bool", |
| 46 | + "12": "QInt8", |
| 47 | + "13": "QUInt8", |
| 48 | + "14": "QInt32", |
| 49 | + "15": "BFloat16", |
| 50 | + "16": "QUInt4x2", |
| 51 | + "17": "QUInt2x4", |
| 52 | + "18": "Bits1x8", |
| 53 | + "19": "Bits2x4", |
| 54 | + "20": "Bits4x2", |
| 55 | + "21": "Bits8", |
| 56 | + "22": "Bits16", |
| 57 | +} |
| 58 | + |
| 59 | + |
| 60 | +def write_selected_mobile_ops(output_file_path: str) -> None: |
| 61 | + with open( |
| 62 | + os.path.join(output_file_path, "selected_operators.yaml"), "r" |
| 63 | + ) as selected_operators_file: |
| 64 | + # Collect et_kernel_metadata from selected_operators.yaml and extract dtypes |
| 65 | + # Example format: v1/6;0,1|6;0,1|6;0,1|6;0,1 # Float, 0, 1 |
| 66 | + selected_operators_dict = yaml.safe_load(selected_operators_file) |
| 67 | + et_kernel_metadata = selected_operators_dict.get("et_kernel_metadata", {}) |
| 68 | + assert isinstance(et_kernel_metadata, dict) |
| 69 | + body = "return true;" |
| 70 | + body_parts = [] |
| 71 | + for operator_name, kernel_metadata_str in et_kernel_metadata.items(): |
| 72 | + tensor_meta = [] |
| 73 | + for kernel_metadata in kernel_metadata_str: |
| 74 | + if kernel_metadata == "default": |
| 75 | + break |
| 76 | + else: |
| 77 | + x = kernel_metadata.split("/")[1] |
| 78 | + tensor_meta.extend(x.split("|")) |
| 79 | + conditions = ["true"] |
| 80 | + if len(tensor_meta) > 0: |
| 81 | + dtype_set = set([x.split(";")[0] for x in tensor_meta]) |
| 82 | + dtype_list = sorted([dtype_enum_to_type[x] for x in dtype_set]) |
| 83 | + conditions = [ |
| 84 | + "scalar_type == exec_aten::ScalarType::" + x for x in dtype_list |
| 85 | + ] |
| 86 | + body_parts.append( |
| 87 | + ops_and_dtypes_template.substitute( |
| 88 | + operator_name=operator_name.removeprefix("aten::"), |
| 89 | + dtype_checks=" || ".join(conditions), |
| 90 | + ), |
| 91 | + ) |
| 92 | + body = "\n || ".join(body_parts) |
| 93 | + header_contents = selected_kernel_dtypes_h_template.substitute(body=body) |
| 94 | + selected_mobile_ops_path = os.path.join( |
| 95 | + output_file_path, "selected_mobile_ops.h" |
| 96 | + ) |
| 97 | + with open(selected_mobile_ops_path, "wb") as out_file: |
| 98 | + out_file.write(header_contents.encode("utf-8")) |
0 commit comments