|
1 | 1 | # 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 | 3 |
|
4 |
| -## How to run |
| 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. |
5 | 5 |
|
6 | 6 | Prerequisite: finish the [setting up wiki](https://pytorch.org/executorch/main/getting-started-setup).
|
7 | 7 |
|
8 |
| -Run: |
| 8 | +## Example 1 - Basic Flow |
9 | 9 |
|
10 |
| -```bash |
11 |
| -cd executorch |
12 |
| -bash examples/selective_build/test_selective_build.sh cmake |
| 10 | +This example showcases using CMake options to control which operators are included. This approach should be preferred when not using |
| 11 | +custom operators or additional kernel libraries beyond the standard kernels provided by ExecuTorch. |
| 12 | + |
| 13 | +The code under the basic/ directory builds a minimal model runner binary which links to a selective kernel target. To build the |
| 14 | +example with operators needed for the MobileNetV2 model, run the following commands: |
| 15 | +``` |
| 16 | +# From the executorch directory |
| 17 | +python -m examples.portable.scripts.export --model_name="mv2" # Create a PTE file for MobileNetV2 |
| 18 | +cd examples/selective_build/basic |
| 19 | +mkdir cmake-out && cd cmake-out |
| 20 | +cmake .. -DEXECUTORCH_SELECT_OPS_MODEL="../../mv2.pte" # Build with kernels needed for mv2.pte |
| 21 | +cmake --build . -j8 |
| 22 | +./selective_build_test --model_path="../../mv2.pte" # Run the model with the selective kernel library |
| 23 | +``` |
| 24 | + |
| 25 | +### CMake Options |
| 26 | + |
| 27 | +The example commands above show use of the EXECUTORCH_SELECT_OPS_MODEL option to select operators used in a PTE file, but there are |
| 28 | +several ways to provide the operator list. The options can be passed to CMake in the same way (during configuration) and are mutually |
| 29 | +exclusive, meaning that only one of these options should be chosen. |
| 30 | + |
| 31 | + * `EXECUTORCH_SELECT_OPS_MODEL`: Select operators used in a .PTE file. Takes a path to the file. |
| 32 | + * `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. |
| 33 | + * `EXECUTORCH_SELECT_OPS_LIST`: Provide a comma-separated list of operators to include. An example is included below. |
| 34 | + |
| 35 | +Example operator list specification (passed as a CLI arg to the CMake configure command): |
| 36 | +``` |
| 37 | +-DEXECUTORCH_SELECT_OPS_LIST="aten::convolution.out,\ |
| 38 | +aten::_native_batch_norm_legit_no_training.out,aten::hardtanh.out,aten::add.out,\ |
| 39 | +aten::mean.out,aten::view_copy.out,aten::permute_copy.out,aten::addmm.out,\ |
| 40 | +aten,aten::clone.out" |
| 41 | +``` |
| 42 | + |
| 43 | +#### DType-Selective Build |
| 44 | + |
| 45 | +To further reduce binary size, ExecuTorch can specialize the individual operators for only the dtypes (data types) used. For example, if |
| 46 | +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 |
| 47 | +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. |
| 48 | + |
| 49 | +### How it Works |
| 50 | + |
| 51 | +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. |
| 52 | + |
| 53 | +```cmake |
| 54 | +# basic/CMakeLists.txt |
| 55 | +target_link_libraries( |
| 56 | + selective_build_test |
| 57 | + PRIVATE executorch_core extension_evalue_util extension_runner_util |
| 58 | + gflags::gflags executorch_kernels |
| 59 | +) |
| 60 | +``` |
| 61 | + |
| 62 | +To use selective build in a user CMake project, take the following steps: |
| 63 | + * Reference the executorch framework via `add_subdirectory`. |
| 64 | + * Add `executorch_kernels` as a dependency (via target_link_libraries). |
| 65 | + * Set CMake options at build time or in user CMake code. |
| 66 | + |
| 67 | +To use the CMake-build framework libraries from outside of the CMake ecosystem, link against libexecutorch_selected_kernels. |
| 68 | + |
| 69 | +## Example 2 - Advanced Flow for Custom Ops and Kernel Libraries |
| 70 | + |
| 71 | +This example showcases defined a custom kernel target. This option can be used when defining custom operators or integrating with |
| 72 | +kernel libraries not part of the standard ExecuTorch build. |
| 73 | + |
| 74 | +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: |
| 75 | +``` |
| 76 | +# From the executorch directory |
| 77 | +python -m examples.portable.custom_ops.custom_ops_1 # Create a model PTE file |
| 78 | +cd examples/selective_build/basic |
| 79 | +mkdir cmake-out && cd cmake-out |
| 80 | +cmake .. -DEXECUTORCH_SELECT_OPS_MODEL="../../custom_ops_1.pte" -DEXECUTORCH_EXAMPLE_USE_CUSTOM_OPS=ON # Build with kernels needed for the model |
| 81 | +cmake --build . -j8 |
| 82 | +./selective_build_test --model_path="../../custom_ops_1.pte" # Run the model with the selective kernel library |
| 83 | +``` |
| 84 | + |
| 85 | +### CMake Options |
| 86 | + |
| 87 | +The CMake logic in `advanced/CMakeLists.txt` respects the CMake options described in the basic flow, as well as the following options: |
| 88 | + |
| 89 | + * `EXECUTORCH_EXAMPLE_USE_CUSTOM_OPS`: Build and link some simple custom operators. |
| 90 | + * `EXECUTORCH_EXAMPLE_SELECT_ALL_OPS`: Build a kernel target with all available operators. |
| 91 | + |
| 92 | +### How it Works |
| 93 | + |
| 94 | +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. |
| 95 | + |
| 96 | +```cmake |
| 97 | +gen_selected_ops( |
| 98 | + LIB_NAME |
| 99 | + "select_build_lib" |
| 100 | + OPS_SCHEMA_YAML |
| 101 | + "${_custom_ops_yaml}" |
| 102 | + ROOT_OPS |
| 103 | + "${EXECUTORCH_SELECT_OPS_LIST}" |
| 104 | + INCLUDE_ALL_OPS |
| 105 | + "${EXECUTORCH_SELECT_ALL_OPS}" |
| 106 | + OPS_FROM_MODEL |
| 107 | + "${EXECUTORCH_SELECT_OPS_MODEL}" |
| 108 | + DTYPE_SELECTIVE_BUILD |
| 109 | + "${EXECUTORCH_DTYPE_SELECTIVE_BUILD}" |
| 110 | +) |
| 111 | +
|
| 112 | +generate_bindings_for_kernels( |
| 113 | + LIB_NAME |
| 114 | + "select_build_lib" |
| 115 | + FUNCTIONS_YAML |
| 116 | + ${EXECUTORCH_ROOT}/kernels/portable/functions.yaml |
| 117 | + CUSTOM_OPS_YAML |
| 118 | + "${_custom_ops_yaml}" |
| 119 | + DTYPE_SELECTIVE_BUILD |
| 120 | + "${EXECUTORCH_DTYPE_SELECTIVE_BUILD}" |
| 121 | +) |
| 122 | +
|
| 123 | +gen_operators_lib( |
| 124 | + LIB_NAME |
| 125 | + "select_build_lib" |
| 126 | + KERNEL_LIBS |
| 127 | + ${_kernel_lib} |
| 128 | + DEPS |
| 129 | + executorch_core |
| 130 | + DTYPE_SELECTIVE_BUILD |
| 131 | + "${EXECUTORCH_DTYPE_SELECTIVE_BUILD}" |
| 132 | +) |
13 | 133 | ```
|
14 | 134 |
|
15 |
| -Check out `CMakeLists.txt` for demo of selective build APIs: |
16 |
| -1. `SELECT_ALL_OPS`: Select all ops from the dependency kernel libraries, register all of them into ExecuTorch runtime. |
17 |
| -2. `SELECT_OPS_LIST`: Only select operators from a list. |
18 |
| -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. |
| 135 | +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. |
21 | 136 |
|
22 |
| -Other configs: |
23 |
| -- `MAX_KERNEL_NUM=N`: Only allocate memory for N operators. |
| 137 | +See `test_selective_build.sh` for additional build examples. |
0 commit comments