Skip to content

Integrate torchgen exception boundary with ExecuTorch #7546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
238d6f0
Build optimized operators lib with -fexceptions
swolchok Jan 7, 2025
eacf0db
Update on "Build optimized operators lib with -fexceptions"
swolchok Jan 7, 2025
85f1f22
Update on "Build optimized operators lib with -fexceptions"
swolchok Jan 8, 2025
f375188
Update on "Build optimized operators lib with -fexceptions"
swolchok Jan 9, 2025
b405dbd
Update on "Build optimized operators lib with -fexceptions"
swolchok Jan 9, 2025
b34b10d
Update on "Build optimized operators lib with -fexceptions"
swolchok Jan 9, 2025
63119b2
Update on "Build optimized operators lib with -fexceptions"
Jan 10, 2025
a400808
Update on "Build optimized operators lib with -fexceptions"
Jan 10, 2025
d0aeeda
Update on "Build optimized operators lib with -fexceptions"
Jan 10, 2025
3805417
Update on "Build optimized operators lib with -fexceptions"
Jan 10, 2025
3e3a3e9
Update on "Build optimized operators lib with -fexceptions"
Jan 11, 2025
11f2eb0
Update on "Build optimized operators lib with -fexceptions"
Jan 13, 2025
9fad43d
Update on "Build optimized operators lib with -fexceptions"
swolchok Jan 15, 2025
672b94d
Update on "Build optimized operators lib with -fexceptions"
swolchok Jan 15, 2025
8e042ac
Update on "Build optimized operators lib with -fexceptions"
swolchok Jan 17, 2025
97972bd
Update on "Build optimized operators lib with -fexceptions"
swolchok Jan 24, 2025
f2fb0b1
Update on "Build optimized operators lib with -fexceptions"
swolchok Jan 30, 2025
de1cc23
Update on "Integrate torchgen exception boundary with ExecuTorch"
swolchok Jan 30, 2025
3ffb5d2
Update on "Integrate torchgen exception boundary with ExecuTorch"
swolchok Jan 30, 2025
00f6b85
Update on "Integrate torchgen exception boundary with ExecuTorch"
swolchok Jan 30, 2025
aff01f5
Update on "Integrate torchgen exception boundary with ExecuTorch"
Feb 4, 2025
59b5088
Update on "Integrate torchgen exception boundary with ExecuTorch"
Feb 4, 2025
99b4a3d
Update on "Integrate torchgen exception boundary with ExecuTorch"
Feb 5, 2025
078941b
Update on "Integrate torchgen exception boundary with ExecuTorch"
Feb 6, 2025
6339d09
Update on "Integrate torchgen exception boundary with ExecuTorch"
Feb 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions build/Codegen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ endfunction()
# Invoked as generate_bindings_for_kernels( LIB_NAME lib_name FUNCTIONS_YAML
# functions_yaml CUSTOM_OPS_YAML custom_ops_yaml )
function(generate_bindings_for_kernels)
set(options ADD_EXCEPTION_BOUNDARY)
set(arg_names LIB_NAME FUNCTIONS_YAML CUSTOM_OPS_YAML)
cmake_parse_arguments(GEN "" "${arg_names}" "" ${ARGN})
cmake_parse_arguments(GEN "${options}" "${arg_names}" "" ${ARGN})

message(STATUS "Generating kernel bindings:")
message(STATUS " LIB_NAME: ${GEN_LIB_NAME}")
message(STATUS " FUNCTIONS_YAML: ${GEN_FUNCTIONS_YAML}")
message(STATUS " CUSTOM_OPS_YAML: ${GEN_CUSTOM_OPS_YAML}")
message(STATUS " ADD_EXCEPTION_BOUNDARY: ${GEN_ADD_EXCEPTION_BOUNDARY}")

# Command to generate selected_operators.yaml from custom_ops.yaml.
file(GLOB_RECURSE _codegen_templates "${EXECUTORCH_ROOT}/codegen/templates/*")
Expand Down Expand Up @@ -93,7 +95,10 @@ function(generate_bindings_for_kernels)
--tags-path=${site-packages-out}/torchgen/packaged/ATen/native/tags.yaml
--aten-yaml-path=${site-packages-out}/torchgen/packaged/ATen/native/native_functions.yaml
--op-selection-yaml-path=${_oplist_yaml}
)
)
if(GEN_ADD_EXCEPTION_BOUNDARY)
set(_gen_command "${_gen_command}" --add-exception-boundary)
endif()

set(_gen_command_sources
${_out_dir}/RegisterCodegenUnboxedKernelsEverything.cpp
Expand Down
2 changes: 1 addition & 1 deletion configurations/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)

generate_bindings_for_kernels(
LIB_NAME "optimized_native_cpu_ops_lib" FUNCTIONS_YAML
${CMAKE_CURRENT_BINARY_DIR}/merged.yaml
${CMAKE_CURRENT_BINARY_DIR}/merged.yaml ADD_EXCEPTION_BOUNDARY
)
message("Generated files ${gen_command_sources}")

Expand Down
1 change: 1 addition & 0 deletions kernels/optimized/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ gen_selected_ops(LIB_NAME "optimized_ops_lib" OPS_SCHEMA_YAML "${_yaml}")
generate_bindings_for_kernels(
LIB_NAME "optimized_ops_lib" FUNCTIONS_YAML
${CMAKE_CURRENT_SOURCE_DIR}/optimized-oss.yaml
ADD_EXCEPTION_BOUNDARY
)
message("Generated files ${gen_command_sources}")

Expand Down
22 changes: 18 additions & 4 deletions shim/xplat/executorch/codegen/codegen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def _prepare_genrule_and_lib(
custom_ops_yaml_path = None,
custom_ops_requires_runtime_registration = True,
manual_registration = False,
aten_mode = False):
aten_mode = False,
support_exceptions = True):
"""
This function returns two dicts `genrules` and `libs`, derived from the arguments being passed
to `executorch_generated_lib`. `genrules` contains all information related to what genrules to
Expand Down Expand Up @@ -156,6 +157,10 @@ def _prepare_genrule_and_lib(
# actually-generated files matches GENERATED_FILES.
]

if support_exceptions:
genrule_cmd.append("--add-exception-boundary")


# Sources for generated kernel registration lib
sources = MANUAL_REGISTRATION_SOURCES if manual_registration else GENERATED_SOURCES

Expand Down Expand Up @@ -217,6 +222,7 @@ def _prepare_genrule_and_lib(
def _prepare_custom_ops_genrule_and_lib(
name,
custom_ops_yaml_path = None,
support_exceptions = True,
deps = [],
kernels = []):
"""Similar to _prepare_genrule_and_lib but for custom ops."""
Expand Down Expand Up @@ -250,6 +256,8 @@ def _prepare_custom_ops_genrule_and_lib(
"--install_dir=${OUT}",
"--op_selection_yaml_path=$(location :{}[selected_operators.yaml])".format(oplist_dir_name),
]
if support_exceptions:
genrule_cmd.append("--add-exception-boundary")

# Determine what sources custom_ops_<name> target should include
custom_ops_sources = CUSTOM_OPS_SCHEMA_REGISTRATION_SOURCES + (
Expand Down Expand Up @@ -281,6 +289,7 @@ def exir_custom_ops_aot_lib(
deps = [],
compiler_flags = [],
define_static_target = False,
support_exceptions = True,
platforms = get_default_executorch_platforms()):
"""Generates a C++ library that helps to register the custom ops into PyTorch,
so they are visible to EXIR. To use this, we need to load the generated so file:
Expand All @@ -297,11 +306,13 @@ def exir_custom_ops_aot_lib(
visibility: visibility of the generated library.
kernels: C++ kernels for these custom ops. They need to be implemented using ATen/c10 basics.
deps: dependencies of the generated library.
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.
"""
genrules, libs = _prepare_custom_ops_genrule_and_lib(
name = name,
custom_ops_yaml_path = selects.apply(yaml_target, lambda y: "$(location {})".format(y)),
kernels = kernels,
support_exceptions = support_exceptions,
deps = deps,
)
for genrule in genrules:
Expand Down Expand Up @@ -368,7 +379,7 @@ def copy_portable_header_files(name):
)

def build_portable_lib(name, oplist_header_name, feature = None, expose_operator_symbols = False):
"""Build portable lib from source. We build from source so that the generated header file,
"""Build portable lib from source. We build from source so that the generated header file,
selected_op_variants.h, can be used to selectively build the lib for different dtypes.
"""

Expand Down Expand Up @@ -446,7 +457,8 @@ def executorch_generated_lib(
kernel_deps = [],
dtype_selective_build = False,
feature = None,
expose_operator_symbols = False):
expose_operator_symbols = False,
support_exceptions = True):
"""Emits 0-3 C++ library targets (in fbcode or xplat) containing code to
dispatch the operators specified in the provided yaml files.

Expand Down Expand Up @@ -495,6 +507,7 @@ def executorch_generated_lib(
compiler_flags: compiler_flags args to runtime.cxx_library
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.
feature: Product-Feature Hierarchy (PFH). For internal use only, required for FoA in production. See: https://fburl.com/wiki/2wzjpyqy
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.
"""
if functions_yaml_target and aten_mode:
fail("{} is providing functions_yaml_target in ATen mode, it will be ignored. `native_functions.yaml` will be the source of truth.".format(name))
Expand Down Expand Up @@ -534,6 +547,7 @@ def executorch_generated_lib(
custom_ops_requires_runtime_registration = custom_ops_requires_runtime_registration,
aten_mode = aten_mode,
manual_registration = manual_registration,
support_exceptions = support_exceptions,
)

# genrule for selective build from static operator list
Expand Down Expand Up @@ -672,7 +686,7 @@ def executorch_generated_lib(
platforms = platforms,
)

# Util macro that takes in a binary or a shared library, find targets ending with `_et_oplist` in the transitive closure of deps,
# Util macro that takes in a binary or a shared library, find targets ending with `_et_oplist` in the transitive closure of deps,
# get the `selected_operators.yaml` from those targets, try to merge them into a single yaml. This target will fail to build, if
# there are intersections of all `selected_operators.yaml` the `target` is depending on.
#
Expand Down
Loading