Skip to content

Commit 8ebb8eb

Browse files
committed
Added support for --lib-name in manual kernel registration
1 parent 1b42512 commit 8ebb8eb

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

codegen/gen.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ def gen_unboxing(
323323
use_aten_lib: bool,
324324
kernel_index: ETKernelIndex,
325325
manual_registration: bool,
326+
lib_name: Optional[str] = None,
326327
add_exception_boundary: bool = False,
327328
) -> None:
328329
# Iterable type for write_sharded is a Tuple of (native_function, (kernel_key, metadata))
@@ -339,7 +340,9 @@ def key_func(
339340

340341
header = ["Functions.h" if use_aten_lib else "NativeFunctions.h"]
341342
filename = (
342-
"RegisterKernels.cpp"
343+
f"register_{lib_name}_kernels.cpp"
344+
if manual_registration and lib_name
345+
else "RegisterKernels.cpp"
343346
if manual_registration
344347
else "RegisterCodegenUnboxedKernels.cpp"
345348
)
@@ -356,9 +359,10 @@ def key_func(
356359
"fn_header": (
357360
header if unbox_kernel_entry == items[0] else []
358361
), # Only write header once
362+
"lib_name": lib_name or "all",
359363
},
360364
num_shards=1,
361-
sharded_keys={"unboxed_kernels", "fn_header"},
365+
sharded_keys={"unboxed_kernels", "fn_header", "lib_name"},
362366
)
363367

364368

@@ -953,6 +957,12 @@ def main() -> None:
953957
help="a boolean flag to indicate whether we want to manually call"
954958
"register_kernels() or rely on static init. ",
955959
)
960+
parser.add_argument(
961+
"--lib-name",
962+
type=str,
963+
default=None,
964+
help="Optional library name used to customize the generated register_<lib>_kernels() function and file names.",
965+
)
956966
parser.add_argument(
957967
"--generate",
958968
type=str,
@@ -1015,6 +1025,7 @@ def main() -> None:
10151025
kernel_index=kernel_index,
10161026
manual_registration=options.manual_registration,
10171027
add_exception_boundary=options.add_exception_boundary,
1028+
lib_name=options.lib_name,
10181029
)
10191030
if custom_ops_native_functions:
10201031
gen_custom_ops(

codegen/templates/RegisterKernels.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
namespace torch {
1616
namespace executor {
1717

18-
Error register_all_kernels() {
18+
Error register_${lib_name}_kernels() {
1919
Kernel kernels_to_register[] = {
2020
${unboxed_kernels} // Generated kernels
2121
};
2222
Error success_with_kernel_reg =
2323
::executorch::runtime::register_kernels({kernels_to_register});
2424
if (success_with_kernel_reg != Error::Ok) {
25-
ET_LOG(Error, "Failed register all kernels");
25+
ET_LOG(Error, "Failed to register ${lib_name} kernels");
2626
return success_with_kernel_reg;
2727
}
2828
return Error::Ok;

codegen/templates/RegisterKernels.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace torch {
1717
namespace executor {
1818

19-
Error register_all_kernels();
19+
Error register_${lib_name}_kernels();
2020

2121
} // namespace executor
2222
} // namespace torch

docs/source/kernel-library-selective-build.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,37 @@ cmake -D… -DSELECT_OPS_YAML=ON
9090
```
9191

9292
To select from either an operator name list or a schema yaml from kernel library.
93+
94+
## Manual Kernel Registration with '--lib-name'
95+
ExecuTorch now supports generating library-specific kernel registration APIs using the '--lib-name' option along with '--manual-registration' during codegen. This allows applications to avoid using static initialization or linker flags like '-force_load' when linking in kernel libraries.
96+
97+
## Motivation
98+
In environments like Xcode, using static libraries requires developers to manually specify '-force_load' flags to ensure kernel registration code is executed. This is inconvenient and error-prone.
99+
100+
By passing a library name to the codegen script, developers can generate explicit registration functions and headers, which they can call directly in their application.
101+
102+
## How to Use
103+
Run the codegen script like this:
104+
105+
```
106+
python -m codegen.gen \
107+
--functions-yaml-path=path/to/functions.yaml \
108+
--manual-registration \
109+
--lib-name=custom
110+
```
111+
This will generate:
112+
113+
'register_custom_kernels.cpp' defines 'register_custom_kernels()' with only the kernels selected and 'register_custom_kernels.h' declares the function for inclusion in your application
114+
115+
Then in your application, call:
116+
117+
```
118+
#include "register_custom_kernels.h"
119+
120+
register_custom_kernels(); // Registers only the "custom" kernels
121+
```
122+
123+
This avoids relying on static initialization and enables you to register only the kernels you want.
124+
125+
### Compatibility
126+
If '--lib-name' is not passed, the default behavior remains unchanged, the codegen script will generate a general 'RegisterKernels.cpp' and 'register_all_kernels()' function.

tools/cmake/Codegen.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ function(generate_bindings_for_kernels)
142142
set(_gen_command "${_gen_command}" --add-exception-boundary)
143143
endif()
144144

145+
if(GEN_LIB_NAME)
146+
list(APPEND _gen_command --lib-name=${GEN_LIB_NAME})
147+
endif()
148+
145149
set(_gen_command_sources
146150
${_out_dir}/RegisterCodegenUnboxedKernelsEverything.cpp
147151
${_out_dir}/Functions.h ${_out_dir}/NativeFunctions.h

0 commit comments

Comments
 (0)