Skip to content

Commit 4653028

Browse files
authored
Integrate torchgen exception boundary with ExecuTorch
Differential Revision: D67904052 Pull Request resolved: #7546
1 parent dc7d200 commit 4653028

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

build/Codegen.cmake

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ endfunction()
5959
# Invoked as generate_bindings_for_kernels( LIB_NAME lib_name FUNCTIONS_YAML
6060
# functions_yaml CUSTOM_OPS_YAML custom_ops_yaml )
6161
function(generate_bindings_for_kernels)
62+
set(options ADD_EXCEPTION_BOUNDARY)
6263
set(arg_names LIB_NAME FUNCTIONS_YAML CUSTOM_OPS_YAML)
63-
cmake_parse_arguments(GEN "" "${arg_names}" "" ${ARGN})
64+
cmake_parse_arguments(GEN "${options}" "${arg_names}" "" ${ARGN})
6465

6566
message(STATUS "Generating kernel bindings:")
6667
message(STATUS " LIB_NAME: ${GEN_LIB_NAME}")
6768
message(STATUS " FUNCTIONS_YAML: ${GEN_FUNCTIONS_YAML}")
6869
message(STATUS " CUSTOM_OPS_YAML: ${GEN_CUSTOM_OPS_YAML}")
70+
message(STATUS " ADD_EXCEPTION_BOUNDARY: ${GEN_ADD_EXCEPTION_BOUNDARY}")
6971

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

98103
set(_gen_command_sources
99104
${_out_dir}/RegisterCodegenUnboxedKernelsEverything.cpp

configurations/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
4242

4343
generate_bindings_for_kernels(
4444
LIB_NAME "optimized_native_cpu_ops_lib" FUNCTIONS_YAML
45-
${CMAKE_CURRENT_BINARY_DIR}/merged.yaml
45+
${CMAKE_CURRENT_BINARY_DIR}/merged.yaml ADD_EXCEPTION_BOUNDARY
4646
)
4747
message("Generated files ${gen_command_sources}")
4848

kernels/optimized/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ gen_selected_ops(LIB_NAME "optimized_ops_lib" OPS_SCHEMA_YAML "${_yaml}")
5555
generate_bindings_for_kernels(
5656
LIB_NAME "optimized_ops_lib" FUNCTIONS_YAML
5757
${CMAKE_CURRENT_SOURCE_DIR}/optimized-oss.yaml
58+
ADD_EXCEPTION_BOUNDARY
5859
)
5960
message("Generated files ${gen_command_sources}")
6061

shim/xplat/executorch/codegen/codegen.bzl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def _prepare_genrule_and_lib(
116116
custom_ops_yaml_path = None,
117117
custom_ops_requires_runtime_registration = True,
118118
manual_registration = False,
119-
aten_mode = False):
119+
aten_mode = False,
120+
support_exceptions = True):
120121
"""
121122
This function returns two dicts `genrules` and `libs`, derived from the arguments being passed
122123
to `executorch_generated_lib`. `genrules` contains all information related to what genrules to
@@ -156,6 +157,10 @@ def _prepare_genrule_and_lib(
156157
# actually-generated files matches GENERATED_FILES.
157158
]
158159

160+
if support_exceptions:
161+
genrule_cmd.append("--add-exception-boundary")
162+
163+
159164
# Sources for generated kernel registration lib
160165
sources = MANUAL_REGISTRATION_SOURCES if manual_registration else GENERATED_SOURCES
161166

@@ -217,6 +222,7 @@ def _prepare_genrule_and_lib(
217222
def _prepare_custom_ops_genrule_and_lib(
218223
name,
219224
custom_ops_yaml_path = None,
225+
support_exceptions = True,
220226
deps = [],
221227
kernels = []):
222228
"""Similar to _prepare_genrule_and_lib but for custom ops."""
@@ -250,6 +256,8 @@ def _prepare_custom_ops_genrule_and_lib(
250256
"--install_dir=${OUT}",
251257
"--op_selection_yaml_path=$(location :{}[selected_operators.yaml])".format(oplist_dir_name),
252258
]
259+
if support_exceptions:
260+
genrule_cmd.append("--add-exception-boundary")
253261

254262
# Determine what sources custom_ops_<name> target should include
255263
custom_ops_sources = CUSTOM_OPS_SCHEMA_REGISTRATION_SOURCES + (
@@ -281,6 +289,7 @@ def exir_custom_ops_aot_lib(
281289
deps = [],
282290
compiler_flags = [],
283291
define_static_target = False,
292+
support_exceptions = True,
284293
platforms = get_default_executorch_platforms()):
285294
"""Generates a C++ library that helps to register the custom ops into PyTorch,
286295
so they are visible to EXIR. To use this, we need to load the generated so file:
@@ -297,11 +306,13 @@ def exir_custom_ops_aot_lib(
297306
visibility: visibility of the generated library.
298307
kernels: C++ kernels for these custom ops. They need to be implemented using ATen/c10 basics.
299308
deps: dependencies of the generated library.
309+
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.
300310
"""
301311
genrules, libs = _prepare_custom_ops_genrule_and_lib(
302312
name = name,
303313
custom_ops_yaml_path = selects.apply(yaml_target, lambda y: "$(location {})".format(y)),
304314
kernels = kernels,
315+
support_exceptions = support_exceptions,
305316
deps = deps,
306317
)
307318
for genrule in genrules:
@@ -368,7 +379,7 @@ def copy_portable_header_files(name):
368379
)
369380

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

@@ -446,7 +457,8 @@ def executorch_generated_lib(
446457
kernel_deps = [],
447458
dtype_selective_build = False,
448459
feature = None,
449-
expose_operator_symbols = False):
460+
expose_operator_symbols = False,
461+
support_exceptions = True):
450462
"""Emits 0-3 C++ library targets (in fbcode or xplat) containing code to
451463
dispatch the operators specified in the provided yaml files.
452464
@@ -495,6 +507,7 @@ def executorch_generated_lib(
495507
compiler_flags: compiler_flags args to runtime.cxx_library
496508
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.
497509
feature: Product-Feature Hierarchy (PFH). For internal use only, required for FoA in production. See: https://fburl.com/wiki/2wzjpyqy
510+
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.
498511
"""
499512
if functions_yaml_target and aten_mode:
500513
fail("{} is providing functions_yaml_target in ATen mode, it will be ignored. `native_functions.yaml` will be the source of truth.".format(name))
@@ -534,6 +547,7 @@ def executorch_generated_lib(
534547
custom_ops_requires_runtime_registration = custom_ops_requires_runtime_registration,
535548
aten_mode = aten_mode,
536549
manual_registration = manual_registration,
550+
support_exceptions = support_exceptions,
537551
)
538552

539553
# genrule for selective build from static operator list
@@ -672,7 +686,7 @@ def executorch_generated_lib(
672686
platforms = platforms,
673687
)
674688

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

0 commit comments

Comments
 (0)