|
| 1 | +load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime") |
| 2 | +load("@fbsource//xplat/executorch/build:selects.bzl", "selects") |
| 3 | +load( |
| 4 | + "@fbsource//xplat/executorch/kernels/optimized:lib_defs.bzl", |
| 5 | + "get_vec_android_preprocessor_flags", |
| 6 | +) |
| 7 | + |
| 8 | +def op_target(name, deps = []): |
| 9 | + """Registers an optimized implementation for an operator overload group. |
| 10 | +
|
| 11 | + An operator overload group is a set of operator overloads with a common |
| 12 | + operator name. That common operator name should be the base name of this |
| 13 | + target. |
| 14 | +
|
| 15 | + E.g., the "add" operator overload group, named "op_add" in this target, |
| 16 | + might implement: |
| 17 | + - add.Tensor |
| 18 | + - add_.Tensor |
| 19 | + - add.out |
| 20 | + - add.Scalar |
| 21 | +
|
| 22 | + If an op target would like to share a header/sources with a different op |
| 23 | + target (e.g., helpers/utilities), it should declare a separate cxx_library |
| 24 | + and add it as a dep. |
| 25 | +
|
| 26 | + Args: |
| 27 | + name: The name of the operator overload group; e.g., |
| 28 | + "op_add". This directory must contain a source file named |
| 29 | + "<name>.cpp"; e.g., "op_add.cpp". |
| 30 | + deps: Optional extra deps to add to the cxx_library(). Note: |
| 31 | + - op targets may not depend on other op targets, to keep the |
| 32 | + dependencies manageable. If two op targets would like to share |
| 33 | + code, define a separate runtime.cxx_library that they both depend |
| 34 | + on. |
| 35 | + """ |
| 36 | + |
| 37 | + # Note that this doesn't actually define the target, but helps register |
| 38 | + # it in a table that's used to define the target. |
| 39 | + return { |
| 40 | + "deps": deps, |
| 41 | + "name": name, |
| 42 | + } |
| 43 | + |
| 44 | +def _enforce_deps(deps, name): |
| 45 | + """Fails if any of the deps are not allowed. |
| 46 | +
|
| 47 | + Args: |
| 48 | + deps: A list of build target strings. |
| 49 | + name: The name of the target; e.g., "op_add" |
| 50 | + """ |
| 51 | + for dep in deps: |
| 52 | + if dep.startswith(":op_"): |
| 53 | + # op targets may not depend on other op targets, to keep the |
| 54 | + # dependencies manageable. If two op targets would like to share |
| 55 | + # code, define a separate runtime.cxx_library that they both depend |
| 56 | + # on. |
| 57 | + fail("op_target {} may not depend on other op_target {}".format( |
| 58 | + name, |
| 59 | + dep, |
| 60 | + )) |
| 61 | + |
| 62 | +def define_op_library(name, deps): |
| 63 | + """Defines a cxx_library target for the named operator overload group. |
| 64 | +
|
| 65 | + Args: |
| 66 | + name: The name of the target; e.g., "op_add" |
| 67 | + deps: List of deps for the target. |
| 68 | + """ |
| 69 | + selects.apply(obj = deps, function = native.partial(_enforce_deps, name = name)) |
| 70 | + |
| 71 | + augmented_deps = deps + [ |
| 72 | + "//executorch/kernels/optimized:libvec", |
| 73 | + "//executorch/kernels/optimized:libutils", |
| 74 | + ] |
| 75 | + |
| 76 | + runtime.cxx_library( |
| 77 | + name = "{}".format(name), |
| 78 | + srcs = [ |
| 79 | + "{}.cpp".format(name), |
| 80 | + ], |
| 81 | + visibility = [ |
| 82 | + "//executorch/kernels/portable/test/...", |
| 83 | + "//executorch/kernels/quantized/test/...", |
| 84 | + "//executorch/kernels/optimized/test/...", |
| 85 | + "//executorch/kernels/test/...", |
| 86 | + "@EXECUTORCH_CLIENTS", |
| 87 | + ], |
| 88 | + # kernels often have helpers with no prototypes just disabling the warning here as the headers |
| 89 | + # are codegend and linked in later |
| 90 | + compiler_flags = ["-Wno-missing-prototypes"], |
| 91 | + deps = [ |
| 92 | + "//executorch/runtime/kernel:kernel_includes", |
| 93 | + ] + augmented_deps, |
| 94 | + fbandroid_platform_preprocessor_flags = get_vec_android_preprocessor_flags(), |
| 95 | + # sleef needs to be added as a direct dependency of the operator target when building for Android, |
| 96 | + # or a linker error may occur. Not sure why this happens; it seems that fbandroid_platform_deps of |
| 97 | + # dependencies are not transitive |
| 98 | + fbandroid_platform_deps = [ |
| 99 | + ( |
| 100 | + "^android-arm64.*$", |
| 101 | + [ |
| 102 | + "fbsource//third-party/sleef:sleef_arm", |
| 103 | + ], |
| 104 | + ), |
| 105 | + ], |
| 106 | + # link_whole is necessary because the operators register themselves |
| 107 | + # via static initializers that run at program startup. |
| 108 | + # @lint-ignore BUCKLINT link_whole |
| 109 | + link_whole = True, |
| 110 | + ) |
| 111 | + |
| 112 | +def define_op_target(name, deps): |
| 113 | + """Possibly defines cxx_library targets for the named operator group. |
| 114 | +
|
| 115 | + Args: |
| 116 | + name: The base name of the target; e.g., "op_add" |
| 117 | + deps: List of deps for the targets. |
| 118 | + """ |
| 119 | + |
| 120 | + # When building in ATen mode, ATen-compatible (non-custom) operators will |
| 121 | + # use the implementations provided by ATen, so we should not build the |
| 122 | + # versions defined here. |
| 123 | + define_op_library( |
| 124 | + name = name, |
| 125 | + deps = deps, |
| 126 | + ) |
| 127 | + |
| 128 | +def is_op_disabled(name): |
| 129 | + # TODO (gjcomer) Enable ops with sleef dependency in OSS |
| 130 | + disabled_ops = ["op_gelu", "op_log_softmax"] |
| 131 | + return name in disabled_ops |
0 commit comments