Skip to content

Commit d92a763

Browse files
pytorchbotGithub Executorch
andauthored
Integrate torchgen exception boundary with ExecuTorch (#8321)
Pull Request resolved: #7546 As of #7746, we build with exceptions by default, so we just need to use them. ghstack-source-id: 265190625 @exported-using-ghexport Differential Revision: [D67904052](https://our.internmc.facebook.com/intern/diff/D67904052/) --------- Co-authored-by: Github Executorch <[email protected]>
1 parent de64ea3 commit d92a763

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
@@ -149,7 +149,8 @@ def _prepare_genrule_and_lib(
149149
custom_ops_yaml_path = None,
150150
custom_ops_requires_runtime_registration = True,
151151
manual_registration = False,
152-
aten_mode = False):
152+
aten_mode = False,
153+
support_exceptions = True):
153154
"""
154155
This function returns two dicts `genrules` and `libs`, derived from the arguments being passed
155156
to `executorch_generated_lib`. `genrules` contains all information related to what genrules to
@@ -189,6 +190,10 @@ def _prepare_genrule_and_lib(
189190
# actually-generated files matches GENERATED_FILES.
190191
]
191192

193+
if support_exceptions:
194+
genrule_cmd.append("--add-exception-boundary")
195+
196+
192197
# Sources for generated kernel registration lib
193198
sources = MANUAL_REGISTRATION_SOURCES if manual_registration else GENERATED_SOURCES
194199

@@ -250,6 +255,7 @@ def _prepare_genrule_and_lib(
250255
def _prepare_custom_ops_genrule_and_lib(
251256
name,
252257
custom_ops_yaml_path = None,
258+
support_exceptions = True,
253259
deps = [],
254260
kernels = []):
255261
"""Similar to _prepare_genrule_and_lib but for custom ops."""
@@ -283,6 +289,8 @@ def _prepare_custom_ops_genrule_and_lib(
283289
"--install_dir=${OUT}",
284290
"--op_selection_yaml_path=$(location :{}[selected_operators.yaml])".format(oplist_dir_name),
285291
]
292+
if support_exceptions:
293+
genrule_cmd.append("--add-exception-boundary")
286294

287295
# Determine what sources custom_ops_<name> target should include
288296
custom_ops_sources = CUSTOM_OPS_SCHEMA_REGISTRATION_SOURCES + (
@@ -314,6 +322,7 @@ def exir_custom_ops_aot_lib(
314322
deps = [],
315323
compiler_flags = [],
316324
define_static_target = False,
325+
support_exceptions = True,
317326
platforms = get_default_executorch_platforms()):
318327
"""Generates a C++ library that helps to register the custom ops into PyTorch,
319328
so they are visible to EXIR. To use this, we need to load the generated so file:
@@ -330,11 +339,13 @@ def exir_custom_ops_aot_lib(
330339
visibility: visibility of the generated library.
331340
kernels: C++ kernels for these custom ops. They need to be implemented using ATen/c10 basics.
332341
deps: dependencies of the generated library.
342+
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.
333343
"""
334344
genrules, libs = _prepare_custom_ops_genrule_and_lib(
335345
name = name,
336346
custom_ops_yaml_path = selects.apply(yaml_target, lambda y: "$(location {})".format(y)),
337347
kernels = kernels,
348+
support_exceptions = support_exceptions,
338349
deps = deps,
339350
)
340351
for genrule in genrules:
@@ -401,7 +412,7 @@ def copy_portable_header_files(name):
401412
)
402413

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

@@ -479,7 +490,8 @@ def executorch_generated_lib(
479490
kernel_deps = [],
480491
dtype_selective_build = False,
481492
feature = None,
482-
expose_operator_symbols = False):
493+
expose_operator_symbols = False,
494+
support_exceptions = True):
483495
"""Emits 0-3 C++ library targets (in fbcode or xplat) containing code to
484496
dispatch the operators specified in the provided yaml files.
485497
@@ -528,6 +540,7 @@ def executorch_generated_lib(
528540
compiler_flags: compiler_flags args to runtime.cxx_library
529541
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.
530542
feature: Product-Feature Hierarchy (PFH). For internal use only, required for FoA in production. See: https://fburl.com/wiki/2wzjpyqy
543+
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.
531544
"""
532545
if functions_yaml_target and aten_mode:
533546
fail("{} is providing functions_yaml_target in ATen mode, it will be ignored. `native_functions.yaml` will be the source of truth.".format(name))
@@ -567,6 +580,7 @@ def executorch_generated_lib(
567580
custom_ops_requires_runtime_registration = custom_ops_requires_runtime_registration,
568581
aten_mode = aten_mode,
569582
manual_registration = manual_registration,
583+
support_exceptions = support_exceptions,
570584
)
571585

572586
# genrule for selective build from static operator list
@@ -705,7 +719,7 @@ def executorch_generated_lib(
705719
platforms = platforms,
706720
)
707721

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

0 commit comments

Comments
 (0)