Skip to content

Commit 1af97c1

Browse files
committed
Update selective build example + CI for top-level targets
1 parent 9ffcf4b commit 1af97c1

File tree

4 files changed

+119
-70
lines changed

4 files changed

+119
-70
lines changed

examples/selective_build/README.md

Lines changed: 13 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

@@ -12,12 +14,20 @@ cd executorch
1214
bash examples/selective_build/test_selective_build.sh cmake
1315
```
1416

17+
## Selective Build APIs
18+
19+
As mentioned above, these examples showcases two flows for enabling kernel selective build - via CMake options or via a user-defined
20+
kernel target. When building the code in this directory, the `EXECUTORCH_EXAMPLE_DEFINE_CUSTOM_TARGET` CMake option controls which
21+
approach is used. When unset or set to `OFF`, the
22+
23+
### CMake Options
24+
1525
Check out `CMakeLists.txt` for demo of selective build APIs:
1626
1. `SELECT_ALL_OPS`: Select all ops from the dependency kernel libraries, register all of them into ExecuTorch runtime.
1727
2. `SELECT_OPS_LIST`: Only select operators from a list.
1828
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.
29+
4. `SELECT_OPS_MODEL`: Only select operators from a from an exported model pte.
30+
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.
2131

2232
Other configs:
2333
- `MAX_KERNEL_NUM=N`: Only allocate memory for N operators.

examples/selective_build/CMakeLists.txt renamed to examples/selective_build/advanced/CMakeLists.txt

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,56 +37,39 @@ 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
57-
)
58-
59-
# Option to register op list
60-
option(EXECUTORCH_SELECT_OPS_LIST "Register a list of ops, separated by comma"
61-
OFF
62-
)
63-
6444
# Selective build options.
65-
option(EXECUTORCH_SELECT_ALL_OPS
45+
option(EXECUTORCH_EXAMPLE_SELECT_ALL_OPS
6646
"Whether to register all ops defined in portable kernel library." OFF
6747
)
6848

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
49+
option(
50+
EXECUTORCH_EXAMPLE_USE_CUSTOM_OPS
51+
"Whether to include custom ops in the example."
52+
OFF
7253
)
7354

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)
55+
# Note that the following options are defined by the core framework and are also
56+
# used by this example when defining a custom operator target:
57+
#
58+
# EXECUTORCH_SELECT_OPS_YAML EXECUTORCH_SELECT_OPS_LIST
59+
# EXECUTORCH_SELECT_OPS_MODEL EXECUTORCH_DTYPE_SELECTIVE_BUILD
60+
7761
# ------------------------------- OPTIONS END --------------------------------
7862

7963
#
8064
# The `_<target>_srcs` lists are defined by executorch_load_build_variables.
8165
#
8266
executorch_load_build_variables()
8367

84-
#
85-
# select_build_lib: C++ library to register selected ops in custom kernel
86-
# library
87-
#
68+
# For advanced use cases, we can define a custom operator target. This is
69+
# useful when using custom operators.
8870
set(_kernel_lib)
89-
if(EXECUTORCH_SELECT_OPS_YAML)
71+
72+
if(EXECUTORCH_EXAMPLE_USE_CUSTOM_OPS)
9073
set(_custom_ops_yaml
9174
${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops.yaml
9275
)
@@ -116,7 +99,7 @@ gen_selected_ops(
11699
INCLUDE_ALL_OPS
117100
"${EXECUTORCH_SELECT_ALL_OPS}"
118101
OPS_FROM_MODEL
119-
"${EXECUTORCH_SELECT_OPS_FROM_MODEL}"
102+
"${EXECUTORCH_SELECT_OPS_MODEL}"
120103
DTYPE_SELECTIVE_BUILD
121104
"${EXECUTORCH_DTYPE_SELECTIVE_BUILD}"
122105
)
@@ -143,6 +126,9 @@ gen_operators_lib(
143126
"${EXECUTORCH_DTYPE_SELECTIVE_BUILD}"
144127
)
145128

129+
executorch_target_link_options_shared_lib(select_build_lib)
130+
set(selected_kernel_target select_build_lib)
131+
146132
list(TRANSFORM _executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
147133

148134
#
@@ -154,8 +140,8 @@ if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
154140
target_link_options_gc_sections(selective_build_test)
155141
endif()
156142
target_link_libraries(
157-
selective_build_test PRIVATE executorch_core extension_evalue_util
158-
extension_runner_util gflags select_build_lib
143+
selective_build_test
144+
PRIVATE executorch_core extension_evalue_util extension_runner_util
145+
gflags::gflags ${selected_kernel_target}
159146
)
160-
executorch_target_link_options_shared_lib(select_build_lib)
161147
target_compile_options(selective_build_test PUBLIC ${_common_compile_options})
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
#
8+
# Simple CMake build system for selective build demo.
9+
#
10+
# ### Editing this file ###
11+
#
12+
# This file should be formatted with
13+
# ~~~
14+
# cmake-format -i CMakeLists.txt
15+
# ~~~
16+
# It should also be cmake-lint clean.
17+
#
18+
cmake_minimum_required(VERSION 3.19)
19+
project(selective_build_example)
20+
21+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
22+
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
23+
24+
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
25+
include(${EXECUTORCH_ROOT}/tools/cmake/Codegen.cmake)
26+
27+
if(NOT PYTHON_EXECUTABLE)
28+
resolve_python_executable()
29+
endif()
30+
31+
if(NOT CMAKE_CXX_STANDARD)
32+
set(CMAKE_CXX_STANDARD 17)
33+
# Can't set to 11 due to executor_runner.cpp make_unique
34+
endif()
35+
36+
set(_common_compile_options -Wno-deprecated-declarations -fPIC
37+
-ffunction-sections -fdata-sections
38+
)
39+
40+
add_subdirectory(${EXECUTORCH_ROOT} ${CMAKE_CURRENT_BINARY_DIR}/executorch)
41+
42+
# ------------------------------ OPTIONS BEGIN -------------------------------
43+
44+
# The following options are defined by the core framework and are also used in
45+
# the generated kernel target.
46+
#
47+
# EXECUTORCH_SELECT_OPS_YAML EXECUTORCH_SELECT_OPS_LIST
48+
# EXECUTORCH_SELECT_OPS_MODEL EXECUTORCH_DTYPE_SELECTIVE_BUILD
49+
50+
# ------------------------------- OPTIONS END --------------------------------
51+
52+
#
53+
# The `_<target>_srcs` lists are defined by executorch_load_build_variables.
54+
#
55+
executorch_load_build_variables()
56+
57+
# For most use cases, we can configure the ExecuTorch kernel library build
58+
# using the EXECUTORCH_SELECT_OPS_* variables. This will reflect in the
59+
# executorch_kernels target, which includes the configured kernel libraries,
60+
# including selective build, where supported.
61+
62+
list(TRANSFORM _executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
63+
64+
#
65+
# selective_build_test: test binary to allow different operator libraries to
66+
# link to
67+
#
68+
add_executable(selective_build_test ${_executor_runner__srcs})
69+
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
70+
target_link_options_gc_sections(selective_build_test)
71+
endif()
72+
target_link_libraries(
73+
selective_build_test
74+
PRIVATE executorch_core extension_evalue_util extension_runner_util
75+
gflags::gflags executorch_kernels
76+
)
77+
target_compile_options(selective_build_test PUBLIC ${_common_compile_options})

examples/selective_build/test_selective_build.sh

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,35 +85,11 @@ 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"
11591

116-
local example_dir=examples/selective_build
92+
local example_dir=examples/selective_build/basic
11793
local build_dir=cmake-out/${example_dir}
11894
# set MAX_KERNEL_NUM=22: 19 primops, add, mul
11995
rm -rf ${build_dir}
@@ -141,11 +117,12 @@ aten,aten::clone.out" \
141117
test_cmake_select_ops_in_yaml() {
142118
echo "Exporting custom_op_1"
143119
${PYTHON_EXECUTABLE} -m examples.portable.custom_ops.custom_ops_1
144-
local example_dir=examples/selective_build
120+
local example_dir=examples/selective_build/advanced
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} \
@@ -166,11 +143,11 @@ test_cmake_select_ops_in_model() {
166143
local model_export_name="${model_name}.pte"
167144
echo "Exporting ${model_name}"
168145
${PYTHON_EXECUTABLE} -m examples.portable.scripts.export --model_name="${model_name}"
169-
local example_dir=examples/selective_build
146+
local example_dir=examples/selective_build/basic
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

0 commit comments

Comments
 (0)