Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions examples/raspberry_pi/pico2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ set(MODEL_STAMP "${CMAKE_CURRENT_BINARY_DIR}/model_pte.stamp")

add_custom_command(
OUTPUT ${MODEL_STAMP}
COMMAND python3 ${EXECUTORCH_ROOT}/executorch/examples/rpi/pte_to_array.py
--model ${INPUT_MODEL} --file ${MODEL_PTE_C}
COMMAND
python3
${EXECUTORCH_ROOT}/executorch/examples/raspberry_pi/pico2/pte_to_array.py
--model ${INPUT_MODEL} --file ${MODEL_PTE_C}
COMMAND ${CMAKE_COMMAND} -E touch ${MODEL_STAMP}
DEPENDS ${INPUT_MODEL}
${EXECUTORCH_ROOT}/executorch/examples/rpi/pte_to_array.py
DEPENDS
${INPUT_MODEL}
${EXECUTORCH_ROOT}/executorch/examples/raspberry_pi/pico2/pte_to_array.py
COMMENT "Injecting PTE data from '${INPUT_MODEL}' into model_pte.c"
VERBATIM
)
Expand Down
139 changes: 115 additions & 24 deletions examples/raspberry_pi/pico2/README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,142 @@
# Overview
This document outlines the steps required to run a simple Add Module on the Pico2 microcontroller using executorch.

## (Pre-requisistes) Prepare the Environment for Arm
This document outlines the steps required to run a simple MNIST digit recognition neural network on the Pico2 microcontroller using ExecuTorch.

1. Setup executorch development environment, Also see <a href="https://docs.pytorch.org/executorch/main/tutorial-arm-ethos-u.html#software"/> for instructions on setting up the environment for Arm.
2. Make sure you have the toolchain configured correctly.
## Demo Model: Hand-crafted MNIST Classifier

The included `export_mlp_mnist.py` creates a demonstration model with hand-crafted weights (not production-trained). This tiny MLP recognizes digits 0, 1, 4, and 7 using manually designed feature detectors.
Note: This is a proof-of-concept. For production use, train your model on real MNIST data.

## Bring Your Own Model

This demo demonstrates ExecuTorch's ability to bring your own PyTorch model and deploy it to Pico2 with one simple script. The complete pipeline works from any PyTorch model to a runnable binary:

### Train your PyTorch model

Export using `torch.export()` and `to_edge()`
Build firmware with one command
Deploy directly to Pico2

#### Important Caveats:

- Memory constraints - Models must fit in 520KB SRAM
- Missing operators - Some ops may not be supported
- Selective builds - Include only operators your model uses

## Memory Constraints & Optimization

- Critical: Pico2 has limited memory:
- 520KB SRAM (on-chip static RAM)
- 4MB QSPI Flash (onboard storage)

### Always apply optimization techniques on large models that do not fit in Pico2 memory:

Large models will not fit. Keep your `.pte` files small!
- Quantization (INT8, INT4)
- Model pruning
- Operator fusion
- Selective builds (include only needed operators)
For more details , refer to the [ExecuTorch Quantization Optimization Guide](https://docs.pytorch.org/executorch/1.0/quantization-optimization.html), [Model Export & Lowering](https://docs.pytorch.org/executorch/1.0/using-executorch-export.html) and [Selective Build support](https://docs.pytorch.org/executorch/1.0/kernel-library-selective-build.html)

## (Prerequisites) Prepare the Environment for Arm

Setup executorch development environment. Also see instructions for setting up the environment for Arm.
Make sure you have the toolchain configured correctly. Refer to this [setup](https://docs.pytorch.org/executorch/1.0/backends-arm-ethos-u.html#development-requirements) for more details.

```bash
which arm-none-eabi-gcc
--> return something like executorch/examples/arm/ethos-u-scratch/arm-gnu-toolchain-13.3.rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc
# Should return: executorch/examples/arm/ethos-u-scratch/arm-gnu-toolchain-13.3.rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc
```

## Build Pico2 Firmware with Executorch
## Build Pico2 Firmware with ExecuTorch

This involves two steps:

This step involves two sub steps
### Generate your model:

```bash
cd examples/raspberry_pi/pico2
python export_mlp_mnist.py # Creates balanced_tiny_mlp_mnist.pte
```

1. Cross Compile Executorch for Arm Cortex M, Pico2 target
2. Build the firmware with the input model provided (If not provided, it will use the default_model.pte)
### Build firmware:

Use the following command to build the firmware:
``` bash
executorch/examples/rpi/build_firmware_pico.sh --model=<path_to_model.pte>
```bash
# In the dir examples/raspberry_pi/pico2
build_firmware_pico.sh --model=balanced_tiny_mlp_mnist.pte # This creates executorch_pico.uf2, a firmware image for Pico2
```

### Flash Firmware

Hold the BOOTSEL button on the Pico2 and connect it to your computer; it will mount as RPI-RP2. Copy the executorch_pico.uf2 file to this drive.
Hold the BOOTSEL button on Pico2 and connect to your computer. It mounts as `RPI-RP2`. Copy `executorch_pico.uf2` to this drive.

### Verify Execution

Check that the Pico2's LED blinks 10 times at 500 ms interval to confirm successful firmware execution.
The Pico2's LED should blink 10 times at 500 ms intervals, indicating successful firmware execution. If connected via serial, you should see:

The Pico2 LED blinks 10 times at 500ms intervals for successful execution. Via serial terminal, you'll see:
```bash
Method loaded [forward]
Output: 13.000000, 136.000000, 24.000000, 131.000000
...
...
PREDICTED: 4 (Expected: 4) ✅ CORRECT!

==================================================

=== Digit 7 ===
############################
############################
####
####
####
####
####
####
####
####
####
####
####
####
####
####
####
####
####
####
####
####
####
####
####
####
####
###

Input stats: 159 white pixels out of 784 total
Running neural network inference...
✅ Neural network results:
Digit 0: 370.000
Digit 1: 0.000
Digit 2: -3.000
Digit 3: -3.000
Digit 4: 860.000
Digit 5: -3.000
Digit 6: -3.000
Digit 7: 1640.000 ← PREDICTED
Digit 8: -3.000
Digit 9: -3.000

� PREDICTED: 7 (Expected: 7) ✅ CORRECT!

==================================================

🎉 All tests complete! PyTorch neural network works on Pico2!
```

### Debugging via Serial Terminal

On macOS or Linux, open a serial terminal with:

On macOS/Linux:
```bash
screen /dev/tty.usbmodem1101 115200
```
Replace `/dev/tty.usbmodem1101` with your device path. If LED blinks 10 times at 100ms intervals, check logs for errors, but if it blinks 10 times at 500ms intervals, it is successful!

Replace /dev/tty.usbmodem1101 with your device path. This terminal shows program logs and errors. If
the LED blinks 10 times at 100 ms intervals, your program hit an error state—check the logs here.

These steps complete running the simple model on Pico2 using ExecuTorch.
Result: A complete PyTorch → ExecuTorch → Pico2 demo neural network deployment! 🚀
6 changes: 3 additions & 3 deletions examples/raspberry_pi/pico2/build_firmware_pico.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
# build_firmware_pico.sh
# Simple script to cross-compile ExecuTorch and build Pico2 firmware with optional model input

set -e
set -euo pipefail

# Paths
ROOT_DIR="$(cd "$(dirname "$0")/../.." && pwd)" # examples/rpi/ -> root dir
PICO2_DIR="${ROOT_DIR}/examples/rpi/pico2"
ROOT_DIR="$(cd "$(dirname "$0")/../../.." && pwd)" # examples/raspberry_pi/ -> root dir
PICO2_DIR="${ROOT_DIR}/examples/raspberry_pi/pico2"
BUILD_DIR="${PICO2_DIR}/build"
EXECUTORCH_BUILD_DIR="${ROOT_DIR}/cmake-out"

Expand Down
Loading
Loading