Skip to content

Commit f1a23fa

Browse files
authored
Support for Model API for Selective Build in OSS (#11601)
### Summary Added support for parsing the selected operators and dtypes directly from an exported model to a `selected_operators.yaml` file. Previously, operators had to be specified in string list or in a schema yaml file for the selective build process. Now, a model can be specified at build time (e.g. `-DEXECUTORCH_SELECT_OPS_FROM_MODEL="./mv2.pte"`) to parse and select operators. ### Test plan Added a test for this in `examples/selective_build/test_selective_build.sh`. When run with `bash examples/selective_build/test_selective_build.sh cmake` a resultant `selected_operators.yaml` file is created at `cmake-out/examples/selective_build/select_build_lib/selected_operators.yaml` that conatins the selected operators as well as their dtypes.
1 parent b59f5cc commit f1a23fa

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

examples/selective_build/CMakeLists.txt

Lines changed: 6 additions & 0 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 parsing ops and dtypes directly from model pte file
66+
option(EXECUTORCH_SELECT_OPS_FROM_MODEL "Enable op selection from pte during build." OFF
67+
)
6468
# ------------------------------- OPTIONS END --------------------------------
6569

6670
#
@@ -108,6 +112,8 @@ gen_selected_ops(
108112
"${EXECUTORCH_SELECT_OPS_LIST}"
109113
INCLUDE_ALL_OPS
110114
"${EXECUTORCH_SELECT_ALL_OPS}"
115+
OPS_FROM_MODEL
116+
"${EXECUTORCH_SELECT_OPS_FROM_MODEL}"
111117
)
112118

113119
generate_bindings_for_kernels(

examples/selective_build/test_selective_build.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,29 @@ test_cmake_select_ops_in_yaml() {
161161
rm "./custom_ops_1.pte"
162162
}
163163

164+
test_cmake_select_ops_in_model() {
165+
echo "Exporting MobilenetV2"
166+
${PYTHON_EXECUTABLE} -m examples.portable.scripts.export --model_name="mv2"
167+
local example_dir=examples/selective_build
168+
local build_dir=cmake-out/${example_dir}
169+
rm -rf ${build_dir}
170+
retry cmake -DCMAKE_BUILD_TYPE=Release \
171+
-DEXECUTORCH_SELECT_OPS_FROM_MODEL="./mv2.pte" \
172+
-DCMAKE_INSTALL_PREFIX=cmake-out \
173+
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
174+
-B${build_dir} \
175+
${example_dir}
176+
177+
echo "Building ${example_dir}"
178+
cmake --build ${build_dir} -j9 --config Release
179+
180+
echo 'Running selective build test'
181+
${build_dir}/selective_build_test --model_path="./mv2.pte"
182+
183+
echo "Removing mv2.pte"
184+
rm "./mv2.pte"
185+
}
186+
164187
if [[ -z $BUCK ]];
165188
then
166189
BUCK=buck2
@@ -177,6 +200,7 @@ then
177200
test_cmake_select_all_ops
178201
test_cmake_select_ops_in_list
179202
test_cmake_select_ops_in_yaml
203+
test_cmake_select_ops_in_model
180204
elif [[ $1 == "buck2" ]];
181205
then
182206
test_buck2_select_all_ops

tools/cmake/Codegen.cmake

Lines changed: 6 additions & 1 deletion
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 OPS_FROM_MODEL)
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 " OPS_FROM_MODEL: ${GEN_OPS_FROM_MODEL}")
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")
@@ -43,6 +45,9 @@ function(gen_selected_ops)
4345
if(GEN_INCLUDE_ALL_OPS)
4446
list(APPEND _gen_oplist_command --include_all_operators)
4547
endif()
48+
if(GEN_OPS_FROM_MODEL)
49+
list(APPEND _gen_oplist_command --model_file_path="${GEN_OPS_FROM_MODEL}")
50+
endif()
4651

4752
message("Command - ${_gen_oplist_command}")
4853
add_custom_command(

0 commit comments

Comments
 (0)