Skip to content

Commit 76cc4d4

Browse files
committed
Plumbing for dtype selection in cmake flow - header generation
1 parent 439aab7 commit 76cc4d4

File tree

3 files changed

+80
-13
lines changed

3 files changed

+80
-13
lines changed

examples/selective_build/CMakeLists.txt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ option(EXECUTORCH_SELECT_OPS_LIST "Register a list of ops, separated by comma"
6161
option(EXECUTORCH_SELECT_ALL_OPS
6262
"Whether to register all ops defined in portable kernel library." OFF
6363
)
64+
65+
# Option to enable dtype selective build
66+
option(EXECUTORCH_SELECT_DTYPE "Enable dtype selection during build." OFF
67+
)
6468
# ------------------------------- OPTIONS END --------------------------------
6569

6670
#
@@ -108,16 +112,30 @@ gen_selected_ops(
108112
"${EXECUTORCH_SELECT_OPS_LIST}"
109113
INCLUDE_ALL_OPS
110114
"${EXECUTORCH_SELECT_ALL_OPS}"
115+
DTYPE_SELECT
116+
"${EXECUTORCH_SELECT_DTYPE}"
111117
)
112118

113119
generate_bindings_for_kernels(
114-
LIB_NAME "select_build_lib" FUNCTIONS_YAML
115-
${EXECUTORCH_ROOT}/kernels/portable/functions.yaml CUSTOM_OPS_YAML
120+
LIB_NAME
121+
"select_build_lib"
122+
FUNCTIONS_YAML
123+
${EXECUTORCH_ROOT}/kernels/portable/functions.yaml
124+
CUSTOM_OPS_YAML
116125
"${_custom_ops_yaml}"
126+
DTYPE_SELECT
127+
"${EXECUTORCH_SELECT_DTYPE}"
117128
)
118129

119130
gen_operators_lib(
120-
LIB_NAME "select_build_lib" KERNEL_LIBS ${_kernel_lib} DEPS executorch_core
131+
LIB_NAME
132+
"select_build_lib"
133+
KERNEL_LIBS
134+
${_kernel_lib}
135+
DEPS
136+
executorch_core
137+
DTYPE_SELECT
138+
"${EXECUTORCH_SELECT_DTYPE}"
121139
)
122140

123141
list(TRANSFORM _executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")

examples/selective_build/test_selective_build.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ test_cmake_select_all_ops() {
9494
rm -rf ${build_dir}
9595
retry cmake -DCMAKE_BUILD_TYPE=Release \
9696
-DEXECUTORCH_SELECT_ALL_OPS=ON \
97+
-DEXECUTORCH_SELECT_DTYPE=ON \
9798
-DCMAKE_INSTALL_PREFIX=cmake-out \
9899
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
99100
-B${build_dir} \
@@ -123,6 +124,7 @@ test_cmake_select_ops_in_list() {
123124
aten::_native_batch_norm_legit_no_training.out,aten::hardtanh.out,aten::add.out,\
124125
aten::mean.out,aten::view_copy.out,aten::permute_copy.out,aten::addmm.out,\
125126
aten,aten::clone.out" \
127+
-DEXECUTORCH_SELECT_DTYPE=ON \
126128
-DCMAKE_INSTALL_PREFIX=cmake-out \
127129
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
128130
-B${build_dir} \
@@ -146,6 +148,7 @@ test_cmake_select_ops_in_yaml() {
146148
rm -rf ${build_dir}
147149
retry cmake -DCMAKE_BUILD_TYPE=Release \
148150
-DEXECUTORCH_SELECT_OPS_YAML=ON \
151+
-DEXECUTORCH_SELECT_DTYPE=ON \
149152
-DCMAKE_INSTALL_PREFIX=cmake-out \
150153
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
151154
-B${build_dir} \

tools/cmake/Codegen.cmake

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@
1212
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
1313

1414
function(gen_selected_ops)
15-
set(arg_names LIB_NAME OPS_SCHEMA_YAML ROOT_OPS INCLUDE_ALL_OPS)
15+
set(arg_names LIB_NAME OPS_SCHEMA_YAML ROOT_OPS INCLUDE_ALL_OPS DTYPE_SELECT)
1616
cmake_parse_arguments(GEN "" "" "${arg_names}" ${ARGN})
1717

1818
message(STATUS "Generating operator lib:")
1919
message(STATUS " LIB_NAME: ${GEN_LIB_NAME}")
2020
message(STATUS " OPS_SCHEMA_YAML: ${GEN_OPS_SCHEMA_YAML}")
2121
message(STATUS " ROOT_OPS: ${GEN_ROOT_OPS}")
2222
message(STATUS " INCLUDE_ALL_OPS: ${GEN_INCLUDE_ALL_OPS}")
23+
message(STATUS " DTYPE_SELECT: ${GEN_DTYPE_SELECT}")
2324

2425
set(_oplist_yaml
2526
${CMAKE_CURRENT_BINARY_DIR}/${GEN_LIB_NAME}/selected_operators.yaml
2627
)
28+
2729
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${GEN_LIB_NAME})
2830

2931
file(GLOB_RECURSE _codegen_tools_srcs "${EXECUTORCH_ROOT}/codegen/tools/*.py")
@@ -53,6 +55,23 @@ function(gen_selected_ops)
5355
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
5456
)
5557

58+
if(GEN_DTYPE_SELECT)
59+
set(_opvariant_h
60+
${CMAKE_CURRENT_BINARY_DIR}/${GEN_LIB_NAME}/selected_op_variants.h
61+
)
62+
set(_gen_opvariant_command "${PYTHON_EXECUTABLE}" -m codegen.tools.gen_selected_op_variants
63+
--yaml-file=${_oplist_yaml}
64+
--output-dir=${CMAKE_CURRENT_BINARY_DIR}/${GEN_LIB_NAME}/
65+
)
66+
message("Command - ${_gen_opvariant_command}")
67+
add_custom_command(
68+
COMMENT "Generating selected_op_variants.h for ${GEN_LIB_NAME}"
69+
OUTPUT ${_opvariant_h}
70+
COMMAND ${_gen_opvariant_command}
71+
DEPENDS ${_optlist_yaml} ${_codegen_tools_srcs}
72+
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
73+
)
74+
endif()
5675
endfunction()
5776

5877
# Codegen for registering kernels. Kernels are defined in functions_yaml and
@@ -62,14 +81,15 @@ endfunction()
6281
# functions_yaml CUSTOM_OPS_YAML custom_ops_yaml )
6382
function(generate_bindings_for_kernels)
6483
set(options ADD_EXCEPTION_BOUNDARY)
65-
set(arg_names LIB_NAME FUNCTIONS_YAML CUSTOM_OPS_YAML)
84+
set(arg_names LIB_NAME FUNCTIONS_YAML CUSTOM_OPS_YAML DTYPE_SELECT)
6685
cmake_parse_arguments(GEN "${options}" "${arg_names}" "" ${ARGN})
6786

6887
message(STATUS "Generating kernel bindings:")
6988
message(STATUS " LIB_NAME: ${GEN_LIB_NAME}")
7089
message(STATUS " FUNCTIONS_YAML: ${GEN_FUNCTIONS_YAML}")
7190
message(STATUS " CUSTOM_OPS_YAML: ${GEN_CUSTOM_OPS_YAML}")
7291
message(STATUS " ADD_EXCEPTION_BOUNDARY: ${GEN_ADD_EXCEPTION_BOUNDARY}")
92+
message(STATUS " DTYPE_SELECT: ${GEN_DTYPE_SELECT}")
7393

7494
# Command to generate selected_operators.yaml from custom_ops.yaml.
7595
file(GLOB_RECURSE _codegen_templates "${EXECUTORCH_ROOT}/codegen/templates/*")
@@ -78,6 +98,13 @@ function(generate_bindings_for_kernels)
7898
# By default selective build output is selected_operators.yaml
7999
set(_oplist_yaml ${_out_dir}/selected_operators.yaml)
80100

101+
# If dtype selective build is enable, force header file to be preserved
102+
if(GEN_DTYPE_SELECT)
103+
set(_opvariant_h ${_out_dir}/selected_op_variants.h)
104+
else()
105+
set(_opvariant_h "")
106+
endif()
107+
81108
# Command to codegen C++ wrappers to register custom ops to both PyTorch and
82109
# Executorch runtime.
83110
execute_process(
@@ -108,6 +135,10 @@ function(generate_bindings_for_kernels)
108135
${_out_dir}/Functions.h ${_out_dir}/NativeFunctions.h
109136
)
110137

138+
if(GEN_DTYPE_SELECT)
139+
list(APPEND _gen_command_sources ${_out_dir}/selected_op_variants.h)
140+
endif()
141+
111142
if(GEN_FUNCTIONS_YAML)
112143
list(APPEND _gen_command --functions-yaml-path=${GEN_FUNCTIONS_YAML})
113144
endif()
@@ -122,8 +153,9 @@ function(generate_bindings_for_kernels)
122153
COMMENT "Generating code for kernel registration"
123154
OUTPUT ${_gen_command_sources}
124155
COMMAND ${_gen_command}
125-
DEPENDS ${_oplist_yaml} ${GEN_CUSTOM_OPS_YAML} ${GEN_FUNCTIONS_YAML}
126-
${_codegen_templates} ${_torchgen_srcs}
156+
DEPENDS ${_oplist_yaml} ${_opvariants_h} ${GEN_CUSTOM_OPS_YAML}
157+
${GEN_FUNCTIONS_YAML} ${_codegen_templates}
158+
${_torchgen_srcs}
127159
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
128160
)
129161
# Make generated file list available in parent scope
@@ -165,29 +197,43 @@ endfunction()
165197

166198
# Generate a runtime lib for registering operators in Executorch
167199
function(gen_operators_lib)
168-
set(multi_arg_names LIB_NAME KERNEL_LIBS DEPS)
200+
set(multi_arg_names LIB_NAME KERNEL_LIBS DEPS DTYPE_SELECT)
169201
cmake_parse_arguments(GEN "" "" "${multi_arg_names}" ${ARGN})
170202

171203
message(STATUS "Generating operator lib:")
172204
message(STATUS " LIB_NAME: ${GEN_LIB_NAME}")
173205
message(STATUS " KERNEL_LIBS: ${GEN_KERNEL_LIBS}")
174206
message(STATUS " DEPS: ${GEN_DEPS}")
207+
message(STATUS " DTYPE_SELECT: ${GEN_DTYPE_SELECT}")
175208

176209
set(_out_dir ${CMAKE_CURRENT_BINARY_DIR}/${GEN_LIB_NAME})
177210

178211
add_library(${GEN_LIB_NAME})
179-
target_sources(
180-
${GEN_LIB_NAME}
181-
PRIVATE ${_out_dir}/RegisterCodegenUnboxedKernelsEverything.cpp
182-
${_out_dir}/Functions.h ${_out_dir}/NativeFunctions.h
183-
)
212+
if(GEN_DTYPE_SELECT)
213+
target_sources(
214+
${GEN_LIB_NAME}
215+
PRIVATE ${_out_dir}/RegisterCodegenUnboxedKernelsEverything.cpp
216+
${_out_dir}/Functions.h ${_out_dir}/NativeFunctions.h
217+
${_out_dir}/selected_op_variants.h
218+
)
219+
else()
220+
target_sources(
221+
${GEN_LIB_NAME}
222+
PRIVATE ${_out_dir}/RegisterCodegenUnboxedKernelsEverything.cpp
223+
${_out_dir}/Functions.h ${_out_dir}/NativeFunctions.h
224+
)
225+
endif()
226+
184227
target_link_libraries(${GEN_LIB_NAME} PRIVATE ${GEN_DEPS})
185228
if(GEN_KERNEL_LIBS)
186229
target_link_libraries(${GEN_LIB_NAME} PUBLIC ${GEN_KERNEL_LIBS})
187230
endif()
188231

189232
target_link_options_shared_lib(${GEN_LIB_NAME})
190233
set(_generated_headers ${_out_dir}/Functions.h ${_out_dir}/NativeFunctions.h)
234+
if(GEN_DTYPE_SELECT)
235+
list(APPEND _generated_headers ${_out_dir}/selected_op_variants.h)
236+
endif()
191237
set_target_properties(
192238
${GEN_LIB_NAME} PROPERTIES PUBLIC_HEADER "${_generated_headers}"
193239
)

0 commit comments

Comments
 (0)