-
Notifications
You must be signed in to change notification settings - Fork 682
Update selective build example + CI for top-level targets #13741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
GregoryComer
merged 1 commit into
pytorch:main
from
GregoryComer:update-selective-build
Sep 11, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,137 @@ | ||
# Selective Build Examples | ||
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. | ||
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. | ||
|
||
## How to run | ||
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. | ||
|
||
Prerequisite: finish the [setting up wiki](https://pytorch.org/executorch/main/getting-started-setup). | ||
|
||
Run: | ||
## Example 1 - Basic Flow | ||
|
||
```bash | ||
cd executorch | ||
bash examples/selective_build/test_selective_build.sh cmake | ||
This example showcases using CMake options to control which operators are included. This approach should be preferred when not using | ||
custom operators or additional kernel libraries beyond the standard kernels provided by ExecuTorch. | ||
|
||
The code under the basic/ directory builds a minimal model runner binary which links to a selective kernel target. To build the | ||
example with operators needed for the MobileNetV2 model, run the following commands: | ||
``` | ||
# From the executorch directory | ||
python -m examples.portable.scripts.export --model_name="mv2" # Create a PTE file for MobileNetV2 | ||
cd examples/selective_build/basic | ||
mkdir cmake-out && cd cmake-out | ||
cmake .. -DEXECUTORCH_SELECT_OPS_MODEL="../../mv2.pte" # Build with kernels needed for mv2.pte | ||
cmake --build . -j8 | ||
./selective_build_test --model_path="../../mv2.pte" # Run the model with the selective kernel library | ||
``` | ||
|
||
### CMake Options | ||
|
||
The example commands above show use of the EXECUTORCH_SELECT_OPS_MODEL option to select operators used in a PTE file, but there are | ||
several ways to provide the operator list. The options can be passed to CMake in the same way (during configuration) and are mutually | ||
exclusive, meaning that only one of these options should be chosen. | ||
|
||
* `EXECUTORCH_SELECT_OPS_MODEL`: Select operators used in a .PTE file. Takes a path to the file. | ||
* `EXECUTORCH_SELECT_OPS_YAML`: Provide a list of operators from a .yml file, typically generated with the `codegen/tools/gen_oplist.py` script. See this script for usage information. | ||
* `EXECUTORCH_SELECT_OPS_LIST`: Provide a comma-separated list of operators to include. An example is included below. | ||
|
||
Example operator list specification (passed as a CLI arg to the CMake configure command): | ||
``` | ||
-DEXECUTORCH_SELECT_OPS_LIST="aten::convolution.out,\ | ||
aten::_native_batch_norm_legit_no_training.out,aten::hardtanh.out,aten::add.out,\ | ||
aten::mean.out,aten::view_copy.out,aten::permute_copy.out,aten::addmm.out,\ | ||
aten,aten::clone.out" | ||
``` | ||
|
||
#### DType-Selective Build | ||
|
||
To further reduce binary size, ExecuTorch can specialize the individual operators for only the dtypes (data types) used. For example, if | ||
the model only calls add with 32-bit floating point tensors, it can drop parts of the code that handle integer tensors or other floating point types. This option is controlled by passing `-DEXECUTORCH_DTYPE_SELECTIVE_BUILD=ON` to CMake. It is only supported in conjunction | ||
with the `EXECUTORCH_SELECT_OPS_MODEL` option and is not yet supported for other modes. It is recommended to enable this option when using `EXECUTORCH_SELECT_OPS_MODEL` as it provides significant size savings on top of the kernel selective build. | ||
|
||
### How it Works | ||
|
||
The CMake options described above are read by ExecuTorch framework build, which is referenced via `add_subdirectory` in basic/CMakeLists.txt. These options reflect in the `executorch_kernels` CMake target, which is linked against the example binary. | ||
|
||
```cmake | ||
# basic/CMakeLists.txt | ||
target_link_libraries( | ||
selective_build_test | ||
PRIVATE executorch_core extension_evalue_util extension_runner_util | ||
gflags::gflags executorch_kernels | ||
) | ||
``` | ||
|
||
To use selective build in a user CMake project, take the following steps: | ||
* Reference the executorch framework via `add_subdirectory`. | ||
* Add `executorch_kernels` as a dependency (via target_link_libraries). | ||
* Set CMake options at build time or in user CMake code. | ||
|
||
To use the CMake-build framework libraries from outside of the CMake ecosystem, link against libexecutorch_selected_kernels. | ||
|
||
## Example 2 - Advanced Flow for Custom Ops and Kernel Libraries | ||
|
||
This example showcases defined a custom kernel target. This option can be used when defining custom operators or integrating with | ||
kernel libraries not part of the standard ExecuTorch build. | ||
|
||
The code under the advanced/ directory builds a minimal model runner binary which links to a user-defined kernel library target. To run a model with a simple custom operator, run the following commands: | ||
``` | ||
# From the executorch directory | ||
python -m examples.portable.custom_ops.custom_ops_1 # Create a model PTE file | ||
cd examples/selective_build/basic | ||
mkdir cmake-out && cd cmake-out | ||
cmake .. -DEXECUTORCH_SELECT_OPS_MODEL="../../custom_ops_1.pte" -DEXECUTORCH_EXAMPLE_USE_CUSTOM_OPS=ON # Build with kernels needed for the model | ||
cmake --build . -j8 | ||
./selective_build_test --model_path="../../custom_ops_1.pte" # Run the model with the selective kernel library | ||
``` | ||
|
||
### CMake Options | ||
|
||
The CMake logic in `advanced/CMakeLists.txt` respects the CMake options described in the basic flow, as well as the following options: | ||
|
||
* `EXECUTORCH_EXAMPLE_USE_CUSTOM_OPS`: Build and link some simple custom operators. | ||
* `EXECUTORCH_EXAMPLE_SELECT_ALL_OPS`: Build a kernel target with all available operators. | ||
|
||
### How it Works | ||
|
||
The build logic in `advanced/CMakeLists.txt` uses the `gen_selected_ops`, `generate_bindings_for_kernels`, and `gen_operators_lib` CMake functions to define an operator target. See [Kernel Library Selective Build](https://docs.pytorch.org/executorch/main/kernel-library-selective-build.html) for more information on selective build. | ||
|
||
```cmake | ||
gen_selected_ops( | ||
LIB_NAME | ||
"select_build_lib" | ||
OPS_SCHEMA_YAML | ||
"${_custom_ops_yaml}" | ||
ROOT_OPS | ||
"${EXECUTORCH_SELECT_OPS_LIST}" | ||
INCLUDE_ALL_OPS | ||
"${EXECUTORCH_SELECT_ALL_OPS}" | ||
OPS_FROM_MODEL | ||
"${EXECUTORCH_SELECT_OPS_MODEL}" | ||
DTYPE_SELECTIVE_BUILD | ||
"${EXECUTORCH_DTYPE_SELECTIVE_BUILD}" | ||
) | ||
|
||
generate_bindings_for_kernels( | ||
LIB_NAME | ||
"select_build_lib" | ||
FUNCTIONS_YAML | ||
${EXECUTORCH_ROOT}/kernels/portable/functions.yaml | ||
CUSTOM_OPS_YAML | ||
"${_custom_ops_yaml}" | ||
DTYPE_SELECTIVE_BUILD | ||
"${EXECUTORCH_DTYPE_SELECTIVE_BUILD}" | ||
) | ||
|
||
gen_operators_lib( | ||
LIB_NAME | ||
"select_build_lib" | ||
KERNEL_LIBS | ||
${_kernel_lib} | ||
DEPS | ||
executorch_core | ||
DTYPE_SELECTIVE_BUILD | ||
"${EXECUTORCH_DTYPE_SELECTIVE_BUILD}" | ||
) | ||
``` | ||
|
||
Check out `CMakeLists.txt` for demo of selective build APIs: | ||
1. `SELECT_ALL_OPS`: Select all ops from the dependency kernel libraries, register all of them into ExecuTorch runtime. | ||
2. `SELECT_OPS_LIST`: Only select operators from a list. | ||
3. `SELECT_OPS_YAML`: Only select operators from a yaml file. | ||
4. `SELECT_OPS_FROM_MODEL`: Only select operators from a from an exported model pte. | ||
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. | ||
To link against this target, the top-level binary target declares a dependency on `select_build_lib`, which is the library name defined by the above function invocations. To use outside of the CMake ecosystem, link against libselect_build_lib. | ||
|
||
Other configs: | ||
- `MAX_KERNEL_NUM=N`: Only allocate memory for N operators. | ||
See `test_selective_build.sh` for additional build examples. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
# | ||
# Simple CMake build system for selective build demo. | ||
# | ||
# ### Editing this file ### | ||
# | ||
# This file should be formatted with | ||
# ~~~ | ||
# cmake-format -i CMakeLists.txt | ||
# ~~~ | ||
# It should also be cmake-lint clean. | ||
# | ||
cmake_minimum_required(VERSION 3.19) | ||
project(selective_build_example) | ||
|
||
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..) | ||
set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch) | ||
|
||
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake) | ||
include(${EXECUTORCH_ROOT}/tools/cmake/Codegen.cmake) | ||
|
||
if(NOT PYTHON_EXECUTABLE) | ||
resolve_python_executable() | ||
endif() | ||
|
||
if(NOT CMAKE_CXX_STANDARD) | ||
set(CMAKE_CXX_STANDARD 17) | ||
# Can't set to 11 due to executor_runner.cpp make_unique | ||
endif() | ||
|
||
set(_common_compile_options -Wno-deprecated-declarations -fPIC | ||
-ffunction-sections -fdata-sections | ||
) | ||
|
||
add_subdirectory(${EXECUTORCH_ROOT} ${CMAKE_CURRENT_BINARY_DIR}/executorch) | ||
|
||
# ------------------------------ OPTIONS BEGIN ------------------------------- | ||
|
||
# The following options are defined by the core framework and are also used in | ||
# the generated kernel target. | ||
# | ||
# EXECUTORCH_SELECT_OPS_YAML EXECUTORCH_SELECT_OPS_LIST | ||
# EXECUTORCH_SELECT_OPS_MODEL EXECUTORCH_DTYPE_SELECTIVE_BUILD | ||
|
||
# ------------------------------- OPTIONS END -------------------------------- | ||
|
||
# | ||
# The `_<target>_srcs` lists are defined by executorch_load_build_variables. | ||
# | ||
executorch_load_build_variables() | ||
|
||
# For most use cases, we can configure the ExecuTorch kernel library build using | ||
# the EXECUTORCH_SELECT_OPS_* variables. This will reflect in the | ||
# executorch_kernels target, which includes the configured kernel libraries, | ||
# including selective build, where supported. | ||
|
||
list(TRANSFORM _executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/") | ||
|
||
# | ||
# selective_build_test: test binary to allow different operator libraries to | ||
# link to | ||
# | ||
add_executable(selective_build_test ${_executor_runner__srcs}) | ||
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") | ||
target_link_options_gc_sections(selective_build_test) | ||
endif() | ||
target_link_libraries( | ||
selective_build_test | ||
PRIVATE executorch_core extension_evalue_util extension_runner_util | ||
gflags::gflags executorch_kernels | ||
) | ||
target_compile_options(selective_build_test PUBLIC ${_common_compile_options}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
executorch in general requires 17 anyway, doesn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we can remove this, as it's a holdover from an older version of the example. I'll clean it up.