Skip to content

Commit efea31b

Browse files
committed
Update
[ghstack-poisoned]
1 parent b4e1145 commit efea31b

File tree

4 files changed

+95
-118
lines changed

4 files changed

+95
-118
lines changed

examples/selective_build/CMakeLists.txt

Lines changed: 86 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -37,111 +37,110 @@ set(_common_compile_options -Wno-deprecated-declarations -fPIC
3737
-ffunction-sections -fdata-sections
3838
)
3939

40-
# Let files say "include <executorch/path/to/header.h>".
41-
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
42-
43-
find_package(executorch CONFIG REQUIRED)
44-
find_package(
45-
gflags REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/../../third-party
46-
)
47-
48-
target_include_directories(
49-
executorch_core INTERFACE ${_common_include_directories}
50-
)
40+
add_subdirectory(${EXECUTORCH_ROOT} ${CMAKE_CURRENT_BINARY_DIR}/executorch)
5141

5242
# ------------------------------ OPTIONS BEGIN -------------------------------
5343

54-
# Option to register ops from yaml file
55-
option(EXECUTORCH_SELECT_OPS_YAML "Register all the ops from a given yaml file"
56-
OFF
44+
# Selective build options.
45+
option(EXECUTORCH_EXAMPLE_SELECT_ALL_OPS
46+
"Whether to register all ops defined in portable kernel library." OFF
5747
)
5848

59-
# Option to register op list
60-
option(EXECUTORCH_SELECT_OPS_LIST "Register a list of ops, separated by comma"
61-
OFF
49+
option(
50+
EXECUTORCH_EXAMPLE_DEFINE_CUSTOM_TARGET
51+
"Whether to define a custom operator target for advanced use cases. If false, the top-level executorch_kernels target is linked."
52+
OFF
6253
)
6354

64-
# Selective build options.
65-
option(EXECUTORCH_SELECT_ALL_OPS
66-
"Whether to register all ops defined in portable kernel library." OFF
55+
option(EXECUTORCH_EXAMPLE_USE_CUSTOM_OPS
56+
"Whether to include custom ops in the example." OFF
6757
)
6858

69-
# Option to enable parsing ops and dtypes directly from model pte file
70-
option(EXECUTORCH_SELECT_OPS_FROM_MODEL
71-
"Enable op selection from pte during build." OFF
72-
)
59+
# Note that the following options are defined by the core framework and are also
60+
# used by this example when defining a custom operator target:
61+
#
62+
# EXECUTORCH_SELECT_OPS_YAML EXECUTORCH_SELECT_OPS_LIST
63+
# EXECUTORCH_SELECT_OPS_MODEL EXECUTORCH_DTYPE_SELECTIVE_BUILD
7364

74-
# Option to enable dtype selective build. Note: must be using selective build
75-
# model API.
76-
option(EXECUTORCH_DTYPE_SELECTIVE_BUILD "Enable dtype selective build." OFF)
7765
# ------------------------------- OPTIONS END --------------------------------
7866

7967
#
8068
# The `_<target>_srcs` lists are defined by executorch_load_build_variables.
8169
#
8270
executorch_load_build_variables()
8371

84-
#
85-
# select_build_lib: C++ library to register selected ops in custom kernel
86-
# library
87-
#
88-
set(_kernel_lib)
89-
if(EXECUTORCH_SELECT_OPS_YAML)
90-
set(_custom_ops_yaml
91-
${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops.yaml
92-
)
93-
set(kernel_sources
94-
${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops_1_out.cpp
95-
${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops_2_out.cpp
72+
if(NOT EXECUTORCH_EXAMPLE_DEFINE_CUSTOM_TARGET)
73+
# For standard use cases, we can configure the ExecuTorch kernel library build
74+
# using the EXECUTORCH_SELECT_OPS_* variables. This will reflect in the
75+
# executorch_kernels target, which includes the configured kernel libraries,
76+
# including selective build, where supported.
77+
78+
set(selected_kernel_target executorch_kernels)
79+
else() # EXECUTORCH_EXAMPLE_DEFINE_CUSTOM_TARGET
80+
# For advanced use cases, we can define a custom operator target. This is
81+
# useful when using custom operators.
82+
set(_kernel_lib)
83+
84+
if(EXECUTORCH_EXAMPLE_USE_CUSTOM_OPS)
85+
set(_custom_ops_yaml
86+
${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops.yaml
87+
)
88+
set(kernel_sources
89+
${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops_1_out.cpp
90+
${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops_2_out.cpp
91+
)
92+
#
93+
# custom_kernels: C++ kernel implementations of custom ops
94+
#
95+
add_library(custom_kernels ${kernel_sources})
96+
target_link_libraries(custom_kernels PRIVATE executorch_core)
97+
target_compile_options(custom_kernels PUBLIC ${_common_compile_options})
98+
99+
list(APPEND _kernel_lib custom_kernels)
100+
else()
101+
list(APPEND _kernel_lib portable_kernels)
102+
endif()
103+
104+
gen_selected_ops(
105+
LIB_NAME
106+
"select_build_lib"
107+
OPS_SCHEMA_YAML
108+
"${_custom_ops_yaml}"
109+
ROOT_OPS
110+
"${EXECUTORCH_SELECT_OPS_LIST}"
111+
INCLUDE_ALL_OPS
112+
"${EXECUTORCH_SELECT_ALL_OPS}"
113+
OPS_FROM_MODEL
114+
"${EXECUTORCH_SELECT_OPS_MODEL}"
115+
DTYPE_SELECTIVE_BUILD
116+
"${EXECUTORCH_DTYPE_SELECTIVE_BUILD}"
96117
)
97-
#
98-
# custom_kernels: C++ kernel implementations of custom ops
99-
#
100-
add_library(custom_kernels ${kernel_sources})
101-
target_link_libraries(custom_kernels PRIVATE executorch_core)
102-
target_compile_options(custom_kernels PUBLIC ${_common_compile_options})
103-
104-
list(APPEND _kernel_lib custom_kernels)
105-
else()
106-
list(APPEND _kernel_lib portable_kernels)
107-
endif()
108118

109-
gen_selected_ops(
110-
LIB_NAME
111-
"select_build_lib"
112-
OPS_SCHEMA_YAML
113-
"${_custom_ops_yaml}"
114-
ROOT_OPS
115-
"${EXECUTORCH_SELECT_OPS_LIST}"
116-
INCLUDE_ALL_OPS
117-
"${EXECUTORCH_SELECT_ALL_OPS}"
118-
OPS_FROM_MODEL
119-
"${EXECUTORCH_SELECT_OPS_FROM_MODEL}"
120-
DTYPE_SELECTIVE_BUILD
121-
"${EXECUTORCH_DTYPE_SELECTIVE_BUILD}"
122-
)
119+
generate_bindings_for_kernels(
120+
LIB_NAME
121+
"select_build_lib"
122+
FUNCTIONS_YAML
123+
${EXECUTORCH_ROOT}/kernels/portable/functions.yaml
124+
CUSTOM_OPS_YAML
125+
"${_custom_ops_yaml}"
126+
DTYPE_SELECTIVE_BUILD
127+
"${EXECUTORCH_DTYPE_SELECTIVE_BUILD}"
128+
)
123129

124-
generate_bindings_for_kernels(
125-
LIB_NAME
126-
"select_build_lib"
127-
FUNCTIONS_YAML
128-
${EXECUTORCH_ROOT}/kernels/portable/functions.yaml
129-
CUSTOM_OPS_YAML
130-
"${_custom_ops_yaml}"
131-
DTYPE_SELECTIVE_BUILD
132-
"${EXECUTORCH_DTYPE_SELECTIVE_BUILD}"
133-
)
130+
gen_operators_lib(
131+
LIB_NAME
132+
"select_build_lib"
133+
KERNEL_LIBS
134+
${_kernel_lib}
135+
DEPS
136+
executorch_core
137+
DTYPE_SELECTIVE_BUILD
138+
"${EXECUTORCH_DTYPE_SELECTIVE_BUILD}"
139+
)
134140

135-
gen_operators_lib(
136-
LIB_NAME
137-
"select_build_lib"
138-
KERNEL_LIBS
139-
${_kernel_lib}
140-
DEPS
141-
executorch_core
142-
DTYPE_SELECTIVE_BUILD
143-
"${EXECUTORCH_DTYPE_SELECTIVE_BUILD}"
144-
)
141+
executorch_target_link_options_shared_lib(select_build_lib)
142+
set(selected_kernel_target select_build_lib)
143+
endif()
145144

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

@@ -154,8 +153,8 @@ if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
154153
target_link_options_gc_sections(selective_build_test)
155154
endif()
156155
target_link_libraries(
157-
selective_build_test PRIVATE executorch_core extension_evalue_util
158-
extension_runner_util gflags select_build_lib
156+
selective_build_test
157+
PRIVATE executorch_core extension_evalue_util extension_runner_util
158+
gflags::gflags ${selected_kernel_target}
159159
)
160-
executorch_target_link_options_shared_lib(select_build_lib)
161160
target_compile_options(selective_build_test PUBLIC ${_common_compile_options})

examples/selective_build/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Selective Build Examples
2-
To optimize binary size of ExecuTorch runtime, selective build can be used. This folder contains examples to select only the operators needed for ExecuTorch build. This example will demonstrate the CMake build.
2+
To optimize binary size of ExecuTorch runtime, selective build can be used. This folder contains examples to select only the operators needed for ExecuTorch build.
3+
4+
These examples showcase two flows - the simple way, using CMake options to configure the framework build, and an advanced flow - showcasing user-defined kernel targets including custom operators.
35

46
## How to run
57

@@ -16,8 +18,8 @@ Check out `CMakeLists.txt` for demo of selective build APIs:
1618
1. `SELECT_ALL_OPS`: Select all ops from the dependency kernel libraries, register all of them into ExecuTorch runtime.
1719
2. `SELECT_OPS_LIST`: Only select operators from a list.
1820
3. `SELECT_OPS_YAML`: Only select operators from a yaml file.
19-
4. `SELECT_OPS_FROM_MODEL`: Only select operators from a from an exported model pte.
20-
5. `DTYPE_SELECTIVE_BUILD`: Enable rebuild of `portable_kernels` to use dtype selection. Currently only supported for `SELECTED_OPS_FROM_MODEL` API and `portable_kernels` lib.
21+
4. `SELECT_OPS_MODEL`: Only select operators from a from an exported model pte.
22+
5. `DTYPE_SELECTIVE_BUILD`: Enable rebuild of `portable_kernels` to use dtype selection. Currently only supported for `SELECTED_OPS_MODEL` API and `portable_kernels` lib.
2123

2224
Other configs:
2325
- `MAX_KERNEL_NUM=N`: Only allocate memory for N operators.

examples/selective_build/test_selective_build.sh

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,30 +85,6 @@ test_buck2_select_ops_from_yaml() {
8585
}
8686

8787
# CMake examples; test in OSS. Check the README for more information.
88-
test_cmake_select_all_ops() {
89-
echo "Exporting MobilenetV3"
90-
${PYTHON_EXECUTABLE} -m examples.portable.scripts.export --model_name="mv3"
91-
92-
local example_dir=examples/selective_build
93-
local build_dir=cmake-out/${example_dir}
94-
rm -rf ${build_dir}
95-
retry cmake -DCMAKE_BUILD_TYPE=Release \
96-
-DEXECUTORCH_SELECT_ALL_OPS=ON \
97-
-DCMAKE_INSTALL_PREFIX=cmake-out \
98-
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
99-
-B${build_dir} \
100-
${example_dir}
101-
102-
echo "Building ${example_dir}"
103-
cmake --build ${build_dir} -j9 --config Release
104-
105-
echo 'Running selective build test'
106-
${build_dir}/selective_build_test --model_path="./mv3.pte"
107-
108-
echo "Removing mv3.pte"
109-
rm "./mv3.pte"
110-
}
111-
11288
test_cmake_select_ops_in_list() {
11389
echo "Exporting MobilenetV2"
11490
${PYTHON_EXECUTABLE} -m examples.portable.scripts.export --model_name="mv2"
@@ -145,7 +121,8 @@ test_cmake_select_ops_in_yaml() {
145121
local build_dir=cmake-out/${example_dir}
146122
rm -rf ${build_dir}
147123
retry cmake -DCMAKE_BUILD_TYPE=Release \
148-
-DEXECUTORCH_SELECT_OPS_YAML=ON \
124+
-DEXECUTORCH_EXAMPLE_USE_CUSTOM_OPS=ON \
125+
-DEXECUTORCH_EXAMPLE_DEFINE_CUSTOM_TARGET=ON \
149126
-DCMAKE_INSTALL_PREFIX=cmake-out \
150127
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
151128
-B${build_dir} \
@@ -170,7 +147,7 @@ test_cmake_select_ops_in_model() {
170147
local build_dir=cmake-out/${example_dir}
171148
rm -rf ${build_dir}
172149
retry cmake -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \
173-
-DEXECUTORCH_SELECT_OPS_FROM_MODEL="./${model_export_name}" \
150+
-DEXECUTORCH_SELECT_OPS_MODEL="./${model_export_name}" \
174151
-DEXECUTORCH_DTYPE_SELECTIVE_BUILD=ON \
175152
-DEXECUTORCH_OPTIMIZE_SIZE=ON \
176153
-DCMAKE_INSTALL_PREFIX=cmake-out \
@@ -206,7 +183,6 @@ fi
206183
if [[ $1 == "cmake" ]];
207184
then
208185
cmake_install_executorch_lib $CMAKE_BUILD_TYPE
209-
test_cmake_select_all_ops
210186
test_cmake_select_ops_in_list
211187
test_cmake_select_ops_in_yaml
212188
test_cmake_select_ops_in_model

exir/operator/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def gen_out_variant_schema(func_op_schema: str) -> str:
4848
torch.ops.quantized_decomposed.dequantize_per_channel.default,
4949
torch.ops.quantized_decomposed.dequantize_per_tensor.default,
5050
torch.ops.quantized_decomposed.dequantize_per_tensor.tensor,
51-
torch.ops.quantized_decomposed.convert_element_type.no_fuse,
51+
#torch.ops.quantized_decomposed.convert_element_type.no_fuse,
5252
torch.ops.quantized_decomposed.quantize_per_tensor.default,
5353
torch.ops.quantized_decomposed.quantize_per_tensor.tensor,
5454
torch.ops.quantized_decomposed.quantize_per_channel.default,

0 commit comments

Comments
 (0)