Skip to content

Commit ada2eff

Browse files
committed
Dtype selective build: enable in fbcode
Pull Request resolved: #11016 ^ By removing `header_namespace` and depending on #include "selected_op_variants.h" instead of the full path #include <executorch/kernels/portable/cpu/selected_op_variants.h> Note: expose_operator_symbols=False only works in xplat, so add a failure message for that too. I don't think we should recommend for users to set it to true, as it prevents a library from linking multiple executorch_generated_libs (symbols will clash). Differential Revision: [D75082395](https://our.internmc.facebook.com/intern/diff/D75082395/) ghstack-source-id: 285224490
1 parent 6e8e8b1 commit ada2eff

File tree

7 files changed

+77
-74
lines changed

7 files changed

+77
-74
lines changed

kernels/portable/cpu/pattern/targets.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def define_common_targets():
1111
# build, where the portable ops are built from source and linked with :all_deps
1212
runtime.cxx_library(
1313
name = "all_deps",
14-
deps = [
14+
exported_deps = [
1515
"//executorch/kernels/portable/cpu/pattern:pattern",
1616
"//executorch/kernels/portable/cpu/pattern:bitwise_op",
1717
"//executorch/kernels/portable/cpu/pattern:comparison_op",
@@ -58,7 +58,7 @@ def define_common_targets():
5858
"pattern.h",
5959
],
6060
compiler_flags = ["-Wno-missing-prototypes"],
61-
deps = [
61+
exported_deps = [
6262
"//executorch/kernels/portable/cpu/util:broadcast_util",
6363
"//executorch/kernels/portable/cpu/util:functional_util",
6464
"//executorch/runtime/kernel:kernel_includes",

kernels/portable/cpu/selective_build.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#ifdef EXECUTORCH_SELECTIVE_BUILD_DTYPE
1414
// include header generated by
1515
// executorch/codegen/tools/gen_selected_op_variants.py
16-
#include <executorch/kernels/portable/cpu/selected_op_variants.h>
16+
#include "selected_op_variants.h"
1717
#else
1818
// dummy implementation
1919
inline constexpr bool should_include_kernel_dtype(

kernels/portable/cpu/targets.bzl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ def define_common_targets():
4646
],
4747
srcs = [],
4848
exported_headers = ["vec_ops.h"],
49-
visibility = ["//executorch/kernels/portable/cpu/...", "//executorch/kernels/quantized/..."],
49+
visibility = [
50+
"//executorch/kernels/portable/...",
51+
"//executorch/kernels/quantized/...",
52+
"@EXECUTORCH_CLIENTS",
53+
],
5054
)
5155

5256
# Only for use by targets in this directory. Defines constants like M_PI
@@ -58,7 +62,7 @@ def define_common_targets():
5862
"math_constants.h",
5963
],
6064
visibility = [
61-
"//executorch/kernels/portable/cpu/...",
65+
"//executorch/kernels/portable/...", "@EXECUTORCH_CLIENTS",
6266
],
6367
)
6468

kernels/portable/cpu/util/targets.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def define_common_targets():
1111
# build, where the portable ops are built from source and linked with :all_deps
1212
runtime.cxx_library(
1313
name = "all_deps",
14-
deps = [
14+
exported_deps = [
1515
"//executorch/extension/threadpool:threadpool",
1616
"//executorch/kernels/portable/cpu/util:functional_util",
1717
"//executorch/kernels/portable/cpu/util:broadcast_util",

shim_et/xplat/executorch/codegen/codegen.bzl

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "get_default_executorch_platforms", "is_xplat", "runtime", "struct_to_json")
22
load("@fbsource//xplat/executorch/build:selects.bzl", "selects")
3-
load("@fbsource//xplat/executorch/kernels/portable:op_registration_util.bzl", "portable_header_list", "portable_source_list")
4-
load("@fbsource//xplat/executorch/kernels/optimized:op_registration_util.bzl", "optimized_header_list", "optimized_source_list")
3+
load("@fbsource//xplat/executorch/kernels/portable:op_registration_util.bzl", "portable_source_list")
4+
load("@fbsource//xplat/executorch/kernels/optimized:op_registration_util.bzl", "optimized_source_list")
55
load(
66
"@fbsource//xplat/executorch/kernels/optimized:lib_defs.bzl",
77
"get_vec_deps",
@@ -407,29 +407,40 @@ def copy_files(genrule_name, target, file_list):
407407
default_outs = ["."],
408408
)
409409

410+
def get_portable_lib_deps():
411+
return [
412+
"//executorch/kernels/portable/cpu:math_constants",
413+
"//executorch/kernels/portable/cpu:scalar_utils",
414+
"//executorch/kernels/portable/cpu:vec_ops",
415+
"//executorch/kernels/portable/cpu/pattern:all_deps",
416+
"//executorch/kernels/portable/cpu/util:all_deps",
417+
]
418+
419+
def get_optimized_lib_deps():
420+
return [
421+
"//executorch/kernels/optimized/cpu:add_sub_impl",
422+
"//executorch/kernels/optimized/cpu:binary_ops",
423+
"//executorch/kernels/optimized/cpu:fft_utils",
424+
"//executorch/kernels/optimized/cpu:moments_utils",
425+
"//executorch/kernels/optimized:libblas",
426+
"//executorch/kernels/optimized:libutils",
427+
"//executorch/kernels/optimized:libvec",
428+
"//executorch/runtime/core/portable_type/c10/c10:aten_headers_for_executorch",
429+
"//executorch/runtime/kernel:kernel_includes",
430+
] + get_vec_deps()
431+
410432
def build_portable_header_lib(name, oplist_header_name, feature = None):
411433
"""Build the portable headers into a header-only library.
412434
Ensures that includes work across portable and optimized libs.
413-
#include "executorch/kernels/portable/cpu/<header.h>"
414435
"""
415-
# Copy portable header files.
416-
portable_header_files = {}
417-
genrule_name = name + "_copy_portable_header"
418-
copy_files(genrule_name, "//executorch/kernels/portable/cpu:portable_header_files", portable_header_list())
419-
for header in portable_header_list():
420-
portable_header_files[header] = ":{}[{}]".format(genrule_name, header)
421-
422-
# Include dtype header.
423-
portable_header_files["selected_op_variants.h"] = ":{}[selected_op_variants]".format(oplist_header_name)
424-
425-
# Build portable headers lib.
426436
runtime.cxx_library(
427437
name = name,
428438
srcs = [],
429-
exported_headers = portable_header_files,
439+
exported_headers = {
440+
"selected_op_variants.h":":{}[selected_op_variants]".format(oplist_header_name),
441+
},
430442
exported_preprocessor_flags = ["-DEXECUTORCH_SELECTIVE_BUILD_DTYPE"],
431-
# header_namespace is only available in xplat. See https://fburl.com/code/we2gvopk
432-
header_namespace = "executorch/kernels/portable/cpu",
443+
header_namespace = "",
433444
feature = feature,
434445
)
435446

@@ -464,9 +475,7 @@ def build_portable_lib(name, oplist_header_name, portable_header_lib, feature =
464475
name = name,
465476
srcs = portable_source_files,
466477
exported_preprocessor_flags = ["-DEXECUTORCH_SELECTIVE_BUILD_DTYPE"],
467-
deps = ["//executorch/kernels/portable/cpu/pattern:all_deps", "//executorch/kernels/portable/cpu/util:all_deps"] + [":" + portable_header_lib],
468-
# header_namespace is only available in xplat. See https://fburl.com/code/we2gvopk
469-
header_namespace = "executorch/kernels/portable/cpu",
478+
deps = get_portable_lib_deps() + [":" + portable_header_lib],
470479
compiler_flags = compiler_flags,
471480
# WARNING: using a deprecated API to avoid being built into a shared
472481
# library. In the case of dynamically loading so library we don't want
@@ -492,13 +501,6 @@ def build_optimized_lib(name, oplist_header_name, portable_header_lib, feature =
492501
for op in optimized_source_list():
493502
optimized_source_files.append(":{}[{}]".format(source_genrule, op))
494503

495-
# Copy optimized header files.
496-
optimized_header_files = {}
497-
header_genrule = name + "_copy_optimized_header"
498-
copy_files(header_genrule, "//executorch/kernels/optimized/cpu:optimized_header_files", optimized_header_list())
499-
for header in optimized_header_list():
500-
optimized_header_files[header] = ":{}[{}]".format(header_genrule, header)
501-
502504
# For shared library build, we don't want to expose symbols of
503505
# kernel implementation (ex torch::executor::native::tanh_out)
504506
# to library users. They should use kernels through registry only.
@@ -513,30 +515,12 @@ def build_optimized_lib(name, oplist_header_name, portable_header_lib, feature =
513515
# This allows operators to be called outside of the kernel registry.
514516
compiler_flags += ["-fvisibility=hidden"]
515517

516-
# Set up dependencies.
517-
optimized_lib_deps = [
518-
"//executorch/kernels/optimized/cpu:add_sub_impl",
519-
"//executorch/kernels/optimized/cpu:binary_ops",
520-
"//executorch/kernels/optimized/cpu:fft_utils",
521-
"//executorch/kernels/optimized/cpu:moments_utils",
522-
"//executorch/kernels/optimized:libblas",
523-
"//executorch/kernels/optimized:libutils",
524-
"//executorch/kernels/optimized:libvec",
525-
"//executorch/kernels/portable/cpu/pattern:all_deps",
526-
"//executorch/kernels/portable/cpu/util:all_deps",
527-
"//executorch/runtime/core/portable_type/c10/c10:aten_headers_for_executorch",
528-
"//executorch/runtime/kernel:kernel_includes",
529-
":" + portable_header_lib,
530-
] + get_vec_deps()
531-
532518
# Build optimized lib.
533519
runtime.cxx_library(
534520
name = name,
535521
srcs = optimized_source_files,
536522
exported_preprocessor_flags = ["-DEXECUTORCH_SELECTIVE_BUILD_DTYPE"],
537-
deps = optimized_lib_deps,
538-
# header_namespace is only available in xplat. See https://fburl.com/code/we2gvopk
539-
header_namespace = "executorch/kernels/optimized/cpu",
523+
deps = get_portable_lib_deps() + get_optimized_lib_deps() + [":" + portable_header_lib],
540524
compiler_flags = compiler_flags,
541525
preprocessor_flags = get_vec_preprocessor_flags(),
542526
# sleef needs to be added as a direct dependency of the operator target when building for Android,
@@ -627,21 +611,46 @@ def executorch_generated_lib(
627611
deps: Additinal deps of the main C++ library. Needs to be in either `//executorch` or `//caffe2` module.
628612
platforms: platforms args to runtime.cxx_library (only used when in xplat)
629613
manual_registration: if true, generate RegisterKernels.cpp and RegisterKernels.h.
630-
use_default_aten_ops_lib: If `aten_mode` is True AND this flag is True, use `torch_mobile_all_ops_et` for ATen operator library.
614+
use_default_aten_ops_lib: If `aten_mode` is True AND this flag is True,
615+
use `torch_mobile_all_ops_et` for ATen operator library.
631616
xplat_deps: Additional xplat deps, can be used to provide custom operator library.
632617
fbcode_deps: Additional fbcode deps, can be used to provide custom operator library.
633618
compiler_flags: compiler_flags args to runtime.cxx_library
634-
dtype_selective_build: In additional to operator selection, dtype selective build further selects the dtypes for each operator. Can be used with model or dict selective build APIs, where dtypes can be specified. Note: this is only available in xplat.
635-
feature: Product-Feature Hierarchy (PFH). For internal use only, required for FoA in production. See: https://fburl.com/wiki/2wzjpyqy
636-
support_exceptions: enable try/catch wrapper around operator implemntations to make sure exceptions thrown will not bring down the process. Disable if your use case disables exceptions in the build.
619+
dtype_selective_build: In additional to operator selection, dtype selective build
620+
further selects the dtypes for each operator. Can be used with model or dict
621+
selective build APIs, where dtypes can be specified.
622+
feature: Product-Feature Hierarchy (PFH). For internal use only, required
623+
for FoA in production. See: https://fburl.com/wiki/2wzjpyqy
624+
expose_operator_symbols: By default, fvisibility=hidden is set for executorch kernel
625+
libraries built with dtype selective build. This options removes the compiler
626+
flag and allows operators to be called outside of the kernel registry.
627+
NOTE: It is not recommended to set this to True, as symbols may clash (duplicate
628+
symbols errors) if multiple executorch_generated_libs are included by a parent library.
629+
support_exceptions: enable try/catch wrapper around operator implementations
630+
to make sure exceptions thrown will not bring down the process. Disable if your
631+
use case disables exceptions in the build.
637632
"""
638633
if functions_yaml_target and aten_mode:
639634
fail("{} is providing functions_yaml_target in ATen mode, it will be ignored. `native_functions.yaml` will be the source of truth.".format(name))
640635

641636
if not aten_mode and not functions_yaml_target and not custom_ops_yaml_target:
642637
fail("At least one of functions_yaml_target, custom_ops_yaml_target needs to be provided")
643638

639+
if expose_operator_symbols:
640+
if not dtype_selective_build:
641+
fail("""
642+
expose_operator_symbols is only available in dtype selective build mode.
643+
See: https://www.internalfb.com/wiki/PyTorch/Teams/Edge/PyTorch_Edge_Core_Team/Dtype_Selective_Build/""")
644+
644645
if dtype_selective_build:
646+
if not expose_operator_symbols and not is_xplat():
647+
fail("""
648+
Dtype selective build with expose_operator_symbols=False works only in xplat -
649+
there are undefined symbols otherwise. Please try to use xplat, or talk to the
650+
executorch team. Setting expose_operator_symbols=True is not recommended as the
651+
exposed symbols may clash (duplicate symbols errors) if multiple
652+
executorch_generated_libs are included by a parent library.""")
653+
645654
if (not "//executorch/kernels/portable:operators" in kernel_deps) and (not "//executorch/kernels/optimized:optimized_operators" in kernel_deps):
646655
fail("""
647656
!!WARNING!! Dtype selective build is available for the portable and optimized kernel libraries.
@@ -655,7 +664,7 @@ def executorch_generated_lib(
655664
If you have a custom kernel library, please remove `dtype_selective_build=True`
656665
and use regular selective build.
657666
""".format(kernel_deps))
658-
667+
659668
# Dtype selective build requires that the portable/optimized kernel libraries are not passed into `deps`.
660669
if ("//executorch/kernels/portable:operators" in kernel_deps):
661670
index = 0
@@ -755,30 +764,28 @@ def executorch_generated_lib(
755764
platforms = platforms,
756765
)
757766

758-
portable_lib = []
759-
optimized_lib = []
760-
if dtype_selective_build and is_xplat():
767+
if dtype_selective_build:
761768
# Build portable headers lib. Used for portable and optimized kernel libraries.
762769
portable_header_lib = name + "_portable_header_lib"
763770
build_portable_header_lib(portable_header_lib, oplist_header_name, feature)
764-
771+
765772
if "//executorch/kernels/portable:operators" in kernel_deps:
766773
# Remove portable from kernel_deps as we're building it from source.
767774
kernel_deps.remove("//executorch/kernels/portable:operators")
768775

769776
# Build portable lib.
770777
portable_lib_name = name + "_portable_lib"
771778
build_portable_lib(portable_lib_name, oplist_header_name, portable_header_lib, feature, expose_operator_symbols)
772-
portable_lib = [":{}".format(portable_lib_name)]
773-
779+
kernel_deps.append(":{}".format(portable_lib_name))
780+
774781
if "//executorch/kernels/optimized:optimized_operators" in kernel_deps:
775782
# Remove optimized from kernel_deps as we're building it from source.
776783
kernel_deps.remove("//executorch/kernels/optimized:optimized_operators")
777-
784+
778785
# Build optimized lib.
779786
optimized_lib_name = name + "_optimized_lib"
780787
build_optimized_lib(optimized_lib_name, oplist_header_name, portable_header_lib, feature, expose_operator_symbols)
781-
optimized_lib = [":{}".format(optimized_lib_name)]
788+
kernel_deps.append(":{}".format(optimized_lib_name))
782789

783790
# Exports headers that declare the function signatures of the C++ functions
784791
# that map to entries in `functions.yaml` and `custom_ops.yaml`.
@@ -832,7 +839,7 @@ def executorch_generated_lib(
832839
"//executorch/kernels/prim_ops:prim_ops_registry" + aten_suffix,
833840
"//executorch/runtime/core:evalue" + aten_suffix,
834841
"//executorch/codegen:macros",
835-
] + deps + kernel_deps + portable_lib + optimized_lib,
842+
] + deps + kernel_deps,
836843
exported_deps = [
837844
"//executorch/runtime/core/exec_aten:lib" + aten_suffix,
838845
"//executorch/runtime/kernel:kernel_runtime_context" + aten_suffix,

shim_et/xplat/executorch/kernels/optimized/op_registration_util.bzl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,3 @@ OPTIMIZED_ATEN_OPS = (
269269
def optimized_source_list():
270270
"""All the source file names from //executorch/kernels/optimized/cpu"""
271271
return [op["name"] + ".cpp" for op in OPTIMIZED_ATEN_OPS]
272-
273-
def optimized_header_list():
274-
"""All the header file names from //executorch/kernels/optimized/cpu"""
275-
return ["binary_ops.h", "fft_utils.h", "moments_utils.h", "op_add_sub_impl.h",]

shim_et/xplat/executorch/kernels/portable/op_registration_util.bzl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,3 @@ CUSTOM_OPS = (
13331333
def portable_source_list():
13341334
"""All the source file names from //executorch/kernels/portable/cpu/"""
13351335
return [op["name"] + ".cpp" for op in ATEN_OPS + CUSTOM_OPS]
1336-
1337-
def portable_header_list():
1338-
"""All the header file names from //executorch/kernels/portable/cpu/"""
1339-
return ["selective_build.h", "scalar_utils.h", "math_constants.h", "vec_ops.h"]

0 commit comments

Comments
 (0)