Skip to content

Commit 20fabb1

Browse files
committed
[manual reg]
Summary: Test Plan: Reviewers: Subscribers: Tasks: Tags:
1 parent 578fdf3 commit 20fabb1

File tree

11 files changed

+130
-26
lines changed

11 files changed

+130
-26
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ if(BUILD_SELECTIVE_BUILD_TEST)
103103

104104
# Option to register ops from yaml file
105105
option(SELECT_OPS_YAML "Register all the ops from a given yaml file" OFF)
106+
107+
# Option to manually register kernels
108+
option(MANUAL_REGISTRATION "Manually register kernels" OFF)
106109
endif()
107110

108111
# Build xnn_executor_runner which depends on XNNPACK

build/Codegen.cmake

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,27 @@ function(gen_selected_ops ops_schema_yaml root_ops include_all_ops)
3636

3737
endfunction()
3838

39+
# Return generated source files
40+
function(get_generated_sources list_name manual_registration)
41+
set(_temp_list
42+
${CMAKE_CURRENT_BINARY_DIR}/Functions.h
43+
${CMAKE_CURRENT_BINARY_DIR}/NativeFunctions.h)
44+
# If manually register ops, change source files
45+
if(manual_registration)
46+
list(APPEND _temp_list
47+
${CMAKE_CURRENT_BINARY_DIR}/RegisterKernelsEverything.cpp)
48+
list(APPEND _temp_list ${CMAKE_CURRENT_BINARY_DIR}/RegisterKernels.h)
49+
else()
50+
list(APPEND _temp_list
51+
${CMAKE_CURRENT_BINARY_DIR}/RegisterCodegenUnboxedKernelsEverything.cpp)
52+
endif()
53+
54+
set(${list_name} ${_temp_list} PARENT_SCOPE)
55+
endfunction()
56+
3957
# Codegen for registering kernels. Kernels are defined in functions_yaml and
4058
# custom_ops_yaml
41-
function(generate_bindings_for_kernels functions_yaml custom_ops_yaml)
59+
function(generate_bindings_for_kernels functions_yaml custom_ops_yaml manual)
4260
# Command to generate selected_operators.yaml from custom_ops.yaml.
4361
file(GLOB_RECURSE _codegen_templates "${EXECUTORCH_ROOT}/codegen/templates/*")
4462
file(GLOB_RECURSE _torchgen_srcs "${TORCH_ROOT}/torchgen/*.py")
@@ -55,10 +73,12 @@ function(generate_bindings_for_kernels functions_yaml custom_ops_yaml)
5573
--aten-yaml-path=${TORCH_ROOT}/aten/src/ATen/native/native_functions.yaml
5674
--op-selection-yaml-path=${_oplist_yaml})
5775

58-
set(_gen_command_sources
59-
${CMAKE_CURRENT_BINARY_DIR}/RegisterCodegenUnboxedKernelsEverything.cpp
60-
${CMAKE_CURRENT_BINARY_DIR}/Functions.h
61-
${CMAKE_CURRENT_BINARY_DIR}/NativeFunctions.h)
76+
get_generated_sources("_gen_command_sources" "${manual}")
77+
78+
# If manually register ops, append --manual_registration and change out files
79+
if(manual)
80+
list(APPEND _gen_command --manual_registration)
81+
endif()
6282

6383
if(functions_yaml)
6484
list(APPEND _gen_command --functions-yaml-path=${functions_yaml})
@@ -105,14 +125,10 @@ function(gen_custom_ops_aot_lib lib_name kernel_sources)
105125
endfunction()
106126

107127
# Generate a runtime lib for registering operators in Executorch
108-
function(gen_operators_lib lib_name kernel_lib deps)
128+
function(gen_operators_lib lib_name kernel_lib deps manual_registration)
109129
add_library(${lib_name})
110-
target_sources(
111-
${lib_name}
112-
PRIVATE
113-
${CMAKE_CURRENT_BINARY_DIR}/RegisterCodegenUnboxedKernelsEverything.cpp
114-
${CMAKE_CURRENT_BINARY_DIR}/Functions.h
115-
${CMAKE_CURRENT_BINARY_DIR}/NativeFunctions.h)
130+
get_generated_sources("_sources" "${manual_registration}")
131+
target_sources(${lib_name} PRIVATE ${_sources})
116132
target_link_libraries(${lib_name} PRIVATE ${deps})
117133
if(kernel_lib)
118134
target_link_libraries(${lib_name} INTERFACE ${kernel_lib})

codegen/templates/RegisterKernels.cpp

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

18+
static const Kernel kernels_to_register[] = {
19+
${unboxed_kernels} // Generated kernels
20+
};
21+
1822
Error register_all_kernels() {
19-
Kernel kernels_to_register[] = {
20-
${unboxed_kernels} // Generated kernels
21-
};
2223
Error success_with_kernel_reg = register_kernels(kernels_to_register);
2324
if (success_with_kernel_reg != Error::Ok) {
2425
ET_LOG(Error, "Failed register all kernels");

examples/custom_ops/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ elseif(REGISTER_EXAMPLE_CUSTOM_OP EQUAL 2)
4343
gen_selected_ops("" "my_ops::mul4.out" "")
4444
endif()
4545
# Expect gen_selected_ops output file to be selected_operators.yaml
46-
generate_bindings_for_kernels("" ${CMAKE_CURRENT_LIST_DIR}/custom_ops.yaml)
46+
generate_bindings_for_kernels("" ${CMAKE_CURRENT_LIST_DIR}/custom_ops.yaml "")
4747
message("Generated files ${gen_command_sources}")
4848

4949
# Prepare for C++ libraries.
@@ -71,4 +71,4 @@ add_library(custom_kernels ${kernel_sources})
7171
target_link_libraries(custom_kernels PRIVATE executorch)
7272
target_compile_options(custom_kernels PUBLIC ${_common_compile_options})
7373

74-
gen_operators_lib("custom_ops_lib" custom_kernels executorch)
74+
gen_operators_lib("custom_ops_lib" custom_kernels executorch "")

examples/executor_runner/executor_runner.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
#include <executorch/runtime/platform/profiler.h>
3131
#include <executorch/runtime/platform/runtime.h>
3232
#include <executorch/util/util.h>
33-
33+
#if defined(EXECUTORCH_MANUAL_KERNEL_REG)
34+
#include <executorch/examples/selective_build/RegisterKernels.h>
35+
#endif
3436
static uint8_t method_allocator_pool[4 * 1024U * 1024U]; // 4 MB
3537

3638
DEFINE_string(
@@ -47,7 +49,9 @@ using torch::executor::util::FileDataLoader;
4749

4850
int main(int argc, char** argv) {
4951
runtime_init();
50-
52+
#if defined(EXECUTORCH_MANUAL_KERNEL_REG)
53+
register_all_kernels();
54+
#endif
5155
gflags::ParseCommandLineFlags(&argc, &argv, true);
5256
if (argc != 1) {
5357
std::string msg = "Extra commandline args:";

examples/executor_runner/targets.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ def define_common_targets():
99

1010
# Wraps a commandline executable that can be linked against any desired
1111
# kernel or backend implementations. Contains a main() function.
12+
select_ops = native.read_config("executorch", "select_ops", None)
13+
1214
runtime.cxx_library(
1315
name = "executor_runner_lib",
1416
srcs = ["executor_runner.cpp"],
1517
deps = [
1618
"//executorch/runtime/executor:program",
19+
"//executorch/runtime/kernel:operator_registry",
1720
"//executorch/extension/data_loader:file_data_loader",
1821
"//executorch/extension/evalue_util:print_evalue",
1922
"//executorch/util:util",
20-
],
23+
] + (["//executorch/examples/selective_build:select_add_out_manual_lib"] if select_ops == "add_manual" else []),
24+
preprocessor_flags = ["-DEXECUTORCH_MANUAL_KERNEL_REG"] if select_ops == "add_manual" else [],
2125
external_deps = [
2226
"gflags",
2327
],

examples/selective_build/CMakeLists.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,28 @@ elseif(SELECT_OPS_YAML)
4949
list(APPEND _kernel_lib custom_kernels)
5050
endif()
5151
generate_bindings_for_kernels(${EXECUTORCH_ROOT}/kernels/portable/functions.yaml
52-
"${_custom_ops_yaml}")
53-
gen_operators_lib("select_build_lib" ${_kernel_lib} executorch)
52+
"${_custom_ops_yaml}" ${MANUAL_REGISTRATION})
53+
gen_operators_lib(
54+
"select_build_lib" ${_kernel_lib} executorch ${MANUAL_REGISTRATION})
5455

5556
set(_updated__srcs)
5657
foreach(_src ${_executor_runner__srcs})
5758
list(APPEND _updated__srcs "${EXECUTORCH_ROOT}/${_src}")
5859
endforeach()
5960

61+
if(${MANUAL_REGISTRATION})
62+
# Create include directory with the header RegisterKernels.h
63+
set(_include_dir "${CMAKE_BINARY_DIR}/examples/selective_build/include")
64+
add_custom_command(TARGET select_build_lib POST_BUILD
65+
COMMAND ${CMAKE_COMMAND} -E copy
66+
${CMAKE_CURRENT_BINARY_DIR}/RegisterKernels.h
67+
${_include_dir}/executorch/examples/selective_build/RegisterKernels.h)
68+
69+
target_include_directories(
70+
select_build_lib INTERFACE ${_include_dir})
71+
# Set preprocessor flags for manual registration
72+
add_definitions(-DEXECUTORCH_MANUAL_KERNEL_REG)
73+
endif()
6074
#
6175
# selective_build_test: test binary to allow different operator libraries to
6276
# link to

examples/selective_build/targets.bzl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ def define_common_targets():
6060
# Select all ops from a given model
6161
# TODO(larryliu0820): Add this
6262

63+
# Select add.out and manually register
64+
et_operator_library(
65+
name = "select_add_out",
66+
ops = [
67+
"aten::add.out",
68+
],
69+
)
70+
71+
executorch_generated_lib(
72+
name = "select_add_out_manual_lib",
73+
functions_yaml_target = "//executorch/kernels/portable:functions.yaml",
74+
manual_registration = True,
75+
deps = [
76+
"//executorch/kernels/portable:operators",
77+
":select_add_out",
78+
],
79+
visibility = ["PUBLIC"],
80+
)
81+
6382
# ~~~ Test binary for selective build ~~~
6483
select_ops = native.read_config("executorch", "select_ops", None)
6584
lib = []
@@ -69,8 +88,13 @@ def define_common_targets():
6988
lib.append(":select_ops_in_list_lib")
7089
elif select_ops == "yaml":
7190
lib.append(":select_ops_from_yaml_lib")
91+
elif select_ops == "add_manual":
92+
lib.append(":select_add_out_manual_lib")
7293
runtime.cxx_binary(
7394
name = "selective_build_test",
95+
include_directories = [
96+
"$(location :select_add_out_manual_lib)"
97+
],
7498
srcs = [],
7599
deps = [
76100
"//executorch/examples/executor_runner:executor_runner_lib",

examples/selective_build/test_selective_build.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ test_buck2_select_ops_from_yaml() {
5353
rm "./custom_ops_1.pte"
5454
}
5555

56+
test_buck2_select_add_manual() {
57+
echo "Exporting add"
58+
${PYTHON_EXECUTABLE} -m examples.export.export_example --model_name="add"
59+
60+
echo "Running selective build test"
61+
$BUCK run //examples/selective_build:selective_build_test \
62+
--config=executorch.select_ops=add_manual -- --model_path=./add.pte
63+
64+
echo "Removing add.pte"
65+
rm "./add.pte"
66+
}
67+
5668
test_cmake_select_all_ops() {
5769
echo "Exporting MobilenetV3"
5870
${PYTHON_EXECUTABLE} -m examples.export.export_example --model_name="mv3"
@@ -127,6 +139,30 @@ test_cmake_select_ops_in_yaml() {
127139
rm "./custom_ops_1.pte"
128140
}
129141

142+
test_cmake_select_add_manual() {
143+
echo "Exporting add"
144+
${PYTHON_EXECUTABLE} -m examples.export.export_example --model_name="add"
145+
# -DCMAKE_BUILD_TYPE=Release \
146+
147+
(rm -rf cmake-out \
148+
&& mkdir cmake-out \
149+
&& cd cmake-out \
150+
&& retry cmake -DBUCK2="$BUCK" \
151+
-DBUILD_SELECTIVE_BUILD_TEST=ON \
152+
-DSELECT_OPS_LIST="aten::add.out" \
153+
-DMANUAL_REGISTRATION=ON \
154+
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..)
155+
156+
echo "Build selective build test"
157+
cmake --build cmake-out -j9
158+
159+
echo 'Running selective build test'
160+
cmake-out/examples/selective_build/selective_build_test --model_path="./add.pte"
161+
162+
echo "Removing add.pte"
163+
rm "./add.pte"
164+
}
165+
130166
if [[ -z $BUCK ]];
131167
then
132168
BUCK=buck2
@@ -142,9 +178,11 @@ then
142178
test_cmake_select_all_ops
143179
test_cmake_select_ops_in_list
144180
test_cmake_select_ops_in_yaml
181+
test_cmake_select_add_manual
145182
elif [[ $1 == "buck2" ]];
146183
then
147184
test_buck2_select_all_ops
148185
test_buck2_select_ops_in_list
149186
test_buck2_select_ops_from_yaml
187+
test_buck2_select_add_manual
150188
fi

kernels/portable/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ list(FILTER _portable_kernels__srcs EXCLUDE REGEX "codegen")
4141
# Executorch (for runtime). Here select all ops in functions.yaml
4242
gen_selected_ops("${CMAKE_CURRENT_LIST_DIR}/functions.yaml" "" "")
4343
# Expect gen_selected_ops output file to be selected_operators.yaml
44-
generate_bindings_for_kernels(${CMAKE_CURRENT_SOURCE_DIR}/functions.yaml "")
44+
generate_bindings_for_kernels(${CMAKE_CURRENT_SOURCE_DIR}/functions.yaml "" "")
4545
message("Generated files ${gen_command_sources}")
4646

4747
#
@@ -57,4 +57,4 @@ target_compile_options(portable_kernels PUBLIC ${_common_compile_options})
5757
#
5858
# portable_ops_lib: Register portable_ops_lib ops kernels into Executorch
5959
# runtime
60-
gen_operators_lib("portable_ops_lib" portable_kernels executorch)
60+
gen_operators_lib("portable_ops_lib" portable_kernels executorch "")

kernels/quantized/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ file(GLOB_RECURSE _quantized_kernels__srcs
3939
# Executorch (for runtime). Here select all ops in quantized.yaml
4040
gen_selected_ops("${CMAKE_CURRENT_LIST_DIR}/quantized.yaml" "" "")
4141
# Expect gen_selected_ops output file to be selected_operators.yaml
42-
generate_bindings_for_kernels("" ${CMAKE_CURRENT_SOURCE_DIR}/quantized.yaml)
42+
generate_bindings_for_kernels("" ${CMAKE_CURRENT_SOURCE_DIR}/quantized.yaml "")
4343
message("Generated files ${gen_command_sources}")
4444

4545
# Build a AOT library to register quantized ops into PyTorch.
@@ -56,4 +56,4 @@ target_compile_options(quantized_kernels PUBLIC ${_common_compile_options})
5656
# Build a library for _quantized_kernels_srcs
5757
#
5858
# quantized_ops_lib: Register quantized ops kernels into Executorch runtime
59-
gen_operators_lib("quantized_ops_lib" quantized_kernels executorch)
59+
gen_operators_lib("quantized_ops_lib" quantized_kernels executorch "")

0 commit comments

Comments
 (0)