Skip to content

Commit 27aa628

Browse files
psiddhclaudeCopilot
authored
Pico2: Document selective build options and auto-detect ops from model (#18640)
A community user hit "Operator missing" (error 20) on bare-metal RISC-V because the selective build guidance was unclear (see #18573). - Document the three selective build approaches in README, recommending EXECUTORCH_SELECT_OPS_MODEL as the default - Update build_firmware_pico.sh to auto-pass SELECT_OPS_MODEL when a model file is provided - Note that the baremetal pattern is portable to other architectures --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent c39a0ff commit 27aa628

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

examples/raspberry_pi/pico2/README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ This demo demonstrates ExecuTorch's ability to bring your own PyTorch model and
1515
- Build firmware with one command and pass the model file (.pte) as an argument
1616
- Deploy directly to Pico2
1717

18+
### Adapting to Other Baremetal Architectures
19+
20+
While this example targets the Pico2 board, the same pattern — embedding the `.pte` model as a C array, using `BufferDataLoader`, and statically allocating memory — can be adapted to other baremetal targets (e.g., RISC-V) by providing your own CMake toolchain file. The key requirement is a correct selective build (see below) so all operators your model needs are included.
21+
1822
### Important Caveats
1923

2024
- Memory constraints - Models must fit in 520KB SRAM (Pico2)
21-
- Missing operators - Some ops may not be supported
22-
- Selective builds - Include only operators your model uses if you want to reduce binary size
25+
- Missing operators - If you get "Operator missing" (error 20) at runtime, your build is missing operators that the model needs. Use `EXECUTORCH_SELECT_OPS_MODEL` (see below) to auto-detect the required operators from your `.pte` file.
26+
- Selective builds - Include only operators your model uses to reduce binary size
2327

2428
## Memory Constraints & Optimization
2529

@@ -36,10 +40,30 @@ Large models will not fit. Keep your `.pte` files small!
3640
- Operator fusion
3741
- Selective builds (include only needed operators)
3842

39-
For more details , refer to the following guides:
43+
### Selective Build: Choosing the Right Operators
44+
45+
When cross-compiling ExecuTorch for baremetal targets, you need to register the operators your model uses. There are three approaches:
46+
47+
1. **`EXECUTORCH_SELECT_OPS_MODEL` (recommended)** — Point to your `.pte` file and the build system auto-detects all required operators:
48+
```bash
49+
cmake ... -DEXECUTORCH_SELECT_OPS_MODEL=/path/to/model.pte
50+
```
51+
This is the most reliable approach because it reads the exact operators from the serialized model, including any operators introduced by compiler passes or edge IR lowering that may not be obvious from the original PyTorch model.
52+
53+
2. **`EXECUTORCH_SELECT_OPS_LIST`** — Manually specify operators by name:
54+
```bash
55+
cmake ... -DEXECUTORCH_SELECT_OPS_LIST="aten::addmm.out,aten::relu.out,..."
56+
```
57+
This requires you to know the exact operator names (including `.out` suffixes). If you miss any, you'll get "Operator missing" (error 20) at runtime.
58+
59+
3. **All portable operators (no selective build)** — Omit any `EXECUTORCH_SELECT_OPS_*` options when configuring CMake. This registers all portable operators, which is simple but produces larger binaries, an important consideration on memory-constrained targets.
60+
61+
The `build_firmware_pico.sh` script uses `EXECUTORCH_SELECT_OPS_MODEL` by default when a model file is provided.
62+
63+
For more details, refer to the following guides:
4064

4165
- [ExecuTorch Quantization Optimization Guide](https://docs.pytorch.org/executorch/1.0/quantization-optimization.html)
42-
- [Model Export & Lowering](https://docs.pytorch.org/executorch/1.0/using-executorch-export.html) and
66+
- [Model Export & Lowering](https://docs.pytorch.org/executorch/1.0/using-executorch-export.html)
4367
- [Selective Build support](https://docs.pytorch.org/executorch/1.0/kernel-library-selective-build.html)
4468

4569
## (Prerequisites) Prepare the Environment for Arm

examples/raspberry_pi/pico2/build_firmware_pico.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ fi
5858
# Step 1: Cross compile ExecuTorch from root dir
5959
echo "Cross compiling ExecuTorch baremetal ARM..."
6060

61+
# Resolve the model path for selective build. Using EXECUTORCH_SELECT_OPS_MODEL
62+
# auto-detects the exact operators the model needs from the .pte file, avoiding
63+
# "Operator missing" errors at runtime.
64+
SELECT_OPS_FLAGS=""
65+
if [ -n "$MODEL_INPUT" ] && [ -f "${PICO2_DIR}/${MODEL_INPUT}" ]; then
66+
MODEL_ABS_PATH="$(cd "${PICO2_DIR}" && realpath "${MODEL_INPUT}")"
67+
SELECT_OPS_FLAGS="-DEXECUTORCH_SELECT_OPS_MODEL=${MODEL_ABS_PATH}"
68+
echo "Using selective build from model: ${MODEL_ABS_PATH}"
69+
fi
70+
6171
cmake -B "${EXECUTORCH_BUILD_DIR}" \
6272
-DCMAKE_TOOLCHAIN_FILE="${ROOT_DIR}/examples/arm/ethos-u-setup/arm-none-eabi-gcc.cmake" \
6373
-DTARGET_CPU=cortex-m0plus \
@@ -69,6 +79,7 @@ cmake -B "${EXECUTORCH_BUILD_DIR}" \
6979
-DEXECUTORCH_SELECT_ALL_OPS=OFF \
7080
-DEXECUTORCH_BUILD_EXECUTOR_RUNNER=OFF \
7181
-DCMAKE_INSTALL_PREFIX="${EXECUTORCH_BUILD_DIR}" \
82+
${SELECT_OPS_FLAGS} \
7283
"${ROOT_DIR}"
7384

7485
cmake --build "${EXECUTORCH_BUILD_DIR}" --target install -j$(nproc)

0 commit comments

Comments
 (0)