Skip to content

Commit 4e0bac5

Browse files
committed
Final changes done based on review comments
1 parent ab3400d commit 4e0bac5

File tree

6 files changed

+72
-17
lines changed

6 files changed

+72
-17
lines changed

codegen/gen.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,7 @@ def key_func(
340340

341341
header = ["Functions.h" if use_aten_lib else "NativeFunctions.h"]
342342
filename = (
343-
f"register_{lib_name}_kernels.cpp"
344-
if manual_registration and lib_name
345-
else "RegisterKernels.cpp"
343+
"RegisterKernels.cpp"
346344
if manual_registration
347345
else "RegisterCodegenUnboxedKernels.cpp"
348346
)
@@ -359,10 +357,11 @@ def key_func(
359357
"fn_header": (
360358
header if unbox_kernel_entry == items[0] else []
361359
), # Only write header once
362-
"lib_name": lib_name or "all",
360+
"lib_name": lib_name or "",
361+
"use_lib_name_in_register": bool(lib_name),
363362
},
364363
num_shards=1,
365-
sharded_keys={"unboxed_kernels", "fn_header", "lib_name"},
364+
sharded_keys={"unboxed_kernels", "fn_header", "lib_name", "use_lib_name_in_register"},
366365
)
367366

368367

@@ -1015,7 +1014,8 @@ def main() -> None:
10151014
cpu_fm=cpu_fm,
10161015
use_aten_lib=options.use_aten_lib,
10171016
)
1018-
1017+
if options.lib_name and not options.manual_registration:
1018+
raise ValueError("--lib-name can only be used with --manual-registration")
10191019
if "sources" in options.generate:
10201020
gen_unboxing(
10211021
native_functions=native_functions,

codegen/templates/RegisterKernels.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,30 @@
1515
namespace torch {
1616
namespace executor {
1717

18-
Error register_${lib_name}_kernels() {
18+
#if USE_LIB_NAME_IN_REGISTER
19+
Error register_kernels_${lib_name}() {
20+
#else
21+
Error register_all_kernels() {
22+
#endif
23+
1924
Kernel kernels_to_register[] = {
2025
${unboxed_kernels} // Generated kernels
2126
};
2227
Error success_with_kernel_reg =
2328
::executorch::runtime::register_kernels({kernels_to_register});
2429
if (success_with_kernel_reg != Error::Ok) {
25-
ET_LOG(Error, "Failed to register ${lib_name} kernels");
30+
#if USE_LIB_NAME_IN_REGISTER
31+
ET_LOG(Error, "Failed to register %zu kernels for %s (from %s)",
32+
sizeof(kernels_to_register) / sizeof(Kernel),
33+
"${lib_name}",
34+
__FILE__);
35+
#else
36+
ET_LOG(Error, "Failed to register %zu kernels (from %s)",
37+
sizeof(kernels_to_register) / sizeof(Kernel),
38+
__FILE__);
39+
#endif
2640
return success_with_kernel_reg;
27-
}
41+
}
2842
return Error::Ok;
2943
}
3044

codegen/templates/RegisterKernels.h

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

19-
Error register_${lib_name}_kernels();
19+
#if USE_LIB_NAME_IN_REGISTER
20+
Error register_kernels_${lib_name}();
21+
#else
22+
Error register_all_kernels();
23+
#endif
2024

2125
} // namespace executor
2226
} // namespace torch

codegen/test/test_executorch_gen.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import os
1010
import tempfile
1111
import unittest
12+
import shutil
13+
import subprocess
14+
import sys
1215

1316
import yaml
1417
from executorch.codegen.gen import (
@@ -693,3 +696,31 @@ def test_codegen_unboxed_default_kernel_key_selected(self) -> None:
693696
)
694697

695698
self.assertEqual(expected_str, result)
699+
700+
class TestGenMainArgumentChecks(unittest.TestCase):
701+
def setUp(self):
702+
self.temp_dir = tempfile.mkdtemp()
703+
self.dummy_yaml = os.path.join(self.temp_dir, "dummy.yaml")
704+
with open(self.dummy_yaml, "w") as f:
705+
f.write("- tag: dummy\n")
706+
707+
def tearDown(self):
708+
shutil.rmtree(self.temp_dir)
709+
710+
def test_lib_name_without_manual_registration_raises(self):
711+
script_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../gen.py"))
712+
result = subprocess.run(
713+
[
714+
sys.executable,
715+
script_path,
716+
"--lib-name", "foo",
717+
"--tags-path", self.dummy_yaml,
718+
"--aten-yaml-path", self.dummy_yaml,
719+
"--functions-yaml-path", self.dummy_yaml,
720+
],
721+
stdout=subprocess.PIPE,
722+
stderr=subprocess.PIPE,
723+
text=True,
724+
)
725+
self.assertNotEqual(result.returncode, 0)
726+
self.assertIn("--lib-name can only be used with --manual-registration", result.stderr)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ gen_selected_ops(
4545
ROOT_OPS # comma separated operator names to be selected
4646
INCLUDE_ALL_OPS # boolean flag to include all operators
4747
OPS_FROM_MODEL # path to a pte file of model to select operators from
48-
DTYPE_SELECTIVE_BUILD # boolean flag to enable dtye selection
48+
DTYPE_SELECTIVE_BUILD # boolean flag to enable dtype selection
4949
)
5050
```
5151

@@ -112,14 +112,14 @@ python -m codegen.gen \
112112
```
113113
This will generate:
114114

115-
`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
115+
`register_kernels_custom.cpp` defines `register_kernels_custom()` with only the kernels selected and `register_kernels_custom.h` declares the function for inclusion in your application
116116

117117
Then in your application, call:
118118

119119
```
120-
#include "register_custom_kernels.h"
120+
#include "register_kernels_custom.h"
121121
122-
register_custom_kernels(); // Registers only the "custom" kernels
122+
register_kernels_custom(); // Registers only the "custom" kernels
123123
```
124124

125125
This avoids relying on static initialization and enables you to register only the kernels you want.

tools/cmake/Codegen.cmake

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ endfunction()
9292
# Invoked as generate_bindings_for_kernels( LIB_NAME lib_name FUNCTIONS_YAML
9393
# functions_yaml CUSTOM_OPS_YAML custom_ops_yaml )
9494
function(generate_bindings_for_kernels)
95-
set(options ADD_EXCEPTION_BOUNDARY)
95+
set(options ADD_EXCEPTION_BOUNDARY MANUAL_REGISTRATION)
9696
set(arg_names LIB_NAME FUNCTIONS_YAML CUSTOM_OPS_YAML DTYPE_SELECTIVE_BUILD)
9797
cmake_parse_arguments(GEN "${options}" "${arg_names}" "" ${ARGN})
9898

@@ -102,11 +102,17 @@ function(generate_bindings_for_kernels)
102102
message(STATUS " CUSTOM_OPS_YAML: ${GEN_CUSTOM_OPS_YAML}")
103103
message(STATUS " ADD_EXCEPTION_BOUNDARY: ${GEN_ADD_EXCEPTION_BOUNDARY}")
104104
message(STATUS " DTYPE_SELECTIVE_BUILD: ${GEN_DTYPE_SELECTIVE_BUILD}")
105+
message(STATUS " MANUAL_REGISTRATION: ${GEN_MANUAL_REGISTRATION}")
105106

106107
# Command to generate selected_operators.yaml from custom_ops.yaml.
107108
file(GLOB_RECURSE _codegen_templates "${EXECUTORCH_ROOT}/codegen/templates/*")
108109

109-
set(_out_dir ${CMAKE_CURRENT_BINARY_DIR}/${GEN_LIB_NAME})
110+
if(GEN_LIB_NAME)
111+
set(_out_dir ${CMAKE_CURRENT_BINARY_DIR}/${GEN_LIB_NAME})
112+
else()
113+
set(_out_dir ${CMAKE_CURRENT_BINARY_DIR}/codegen_output)
114+
endif()
115+
110116
# By default selective build output is selected_operators.yaml
111117
set(_oplist_yaml ${_out_dir}/selected_operators.yaml)
112118

@@ -142,7 +148,7 @@ function(generate_bindings_for_kernels)
142148
set(_gen_command "${_gen_command}" --add-exception-boundary)
143149
endif()
144150

145-
if(GEN_LIB_NAME)
151+
if(GEN_LIB_NAME AND GEN_MANUAL_REGISTRATION)
146152
list(APPEND _gen_command --lib-name=${GEN_LIB_NAME})
147153
endif()
148154

0 commit comments

Comments
 (0)