Skip to content

Commit 1f8d6a8

Browse files
author
Github Executorch
committed
Summary: Pico2 demo of neural network (MNIST)
Test Plan: 1. Setup Conda 2. examples/arm/setup.sh --i-agree-to-the-contained-eula; source examples/ arm/ethos-u-scratch/setup_path.sh 3. cd examples/raspberry_pi/pico2 4. python export_mlp_mnist.py; ./build_firmware_pico.sh --model=balanced_tiny_mlp_mnist.pte Reviewers: Subscribers: Tasks: Tags:
1 parent 1f114f1 commit 1f8d6a8

File tree

5 files changed

+603
-86
lines changed

5 files changed

+603
-86
lines changed

examples/raspberry_pi/pico2/CMakeLists.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,14 @@ set(MODEL_STAMP "${CMAKE_CURRENT_BINARY_DIR}/model_pte.stamp")
6464

6565
add_custom_command(
6666
OUTPUT ${MODEL_STAMP}
67-
COMMAND python3 ${EXECUTORCH_ROOT}/executorch/examples/rpi/pte_to_array.py
68-
--model ${INPUT_MODEL} --file ${MODEL_PTE_C}
67+
COMMAND
68+
python3
69+
${EXECUTORCH_ROOT}/executorch/examples/raspberry_pi/pico2/pte_to_array.py
70+
--model ${INPUT_MODEL} --file ${MODEL_PTE_C}
6971
COMMAND ${CMAKE_COMMAND} -E touch ${MODEL_STAMP}
70-
DEPENDS ${INPUT_MODEL}
71-
${EXECUTORCH_ROOT}/executorch/examples/rpi/pte_to_array.py
72+
DEPENDS
73+
${INPUT_MODEL}
74+
${EXECUTORCH_ROOT}/executorch/examples/raspberry_pi/pico2/pte_to_array.py
7275
COMMENT "Injecting PTE data from '${INPUT_MODEL}' into model_pte.c"
7376
VERBATIM
7477
)
Lines changed: 107 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,128 @@
11
# Overview
2-
This document outlines the steps required to run a simple Add Module on the Pico2 microcontroller using executorch.
3-
4-
## (Pre-requisistes) Prepare the Environment for Arm
5-
6-
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.
7-
2. Make sure you have the toolchain configured correctly.
2+
This document outlines the steps required to run a simple MNIST digit recognition neural network on the Pico2 microcontroller using ExecuTorch.
3+
4+
## Demo Model: Hand-crafted MNIST Classifier
5+
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.
6+
Note: This is a proof-of-concept. For production use, train your model on real MNIST data.
7+
8+
## Bring Your Own Model
9+
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:
10+
11+
### Train your PyTorch model
12+
Export using `torch.export()` and `to_edge()`
13+
Build firmware with one command
14+
Deploy directly to Pico2
15+
16+
#### Important Caveats:
17+
- Memory constraints - Models must fit in 520KB SRAM
18+
- Missing operators - Some ops may not be supported
19+
- Selective builds - Include only operators your model uses
20+
21+
## Memory Constraints & Optimization
22+
- Critical: Pico2 has limited memory:
23+
- 520KB SRAM (on-chip static RAM)
24+
- 4MB QSPI Flash (onboard storage)
25+
26+
### Always apply optimization techniques on large models that do not fit in Pico2 memory:
27+
Large models will not fit. Keep your `.pte` files small!
28+
- Quantization (INT8, INT4)
29+
- Model pruning
30+
- Operator fusion
31+
- Selective builds (include only needed operators)
32+
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)
33+
34+
## (Prerequisites) Prepare the Environment for Arm
35+
Setup executorch development environment. Also see instructions for setting up the environment for Arm.
36+
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.
837

938
```bash
1039
which arm-none-eabi-gcc
11-
--> 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
40+
# Should return: executorch/examples/arm/ethos-u-scratch/arm-gnu-toolchain-13.3.rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc
1241
```
1342

14-
## Build Pico2 Firmware with Executorch
15-
16-
This step involves two sub steps
43+
## Build Pico2 Firmware with ExecuTorch
44+
This involves two steps:
1745

18-
1. Cross Compile Executorch for Arm Cortex M, Pico2 target
19-
2. Build the firmware with the input model provided (If not provided, it will use the default_model.pte)
46+
### Generate your model:
47+
```bash
48+
cd examples/raspberry_pi/pico2
49+
python export_mlp_mnist.py # Creates balanced_tiny_mlp_mnist.pte
50+
```
2051

21-
Use the following command to build the firmware:
22-
``` bash
23-
executorch/examples/rpi/build_firmware_pico.sh --model=<path_to_model.pte>
52+
### Build firmware:
53+
```bash
54+
# In the dir examples/raspberry_pi/pico2
55+
build_firmware_pico.sh --model=balanced_tiny_mlp_mnist.pte # This creates executorch_pico.uf2, a firmware image for Pico2
2456
```
2557

2658
### Flash Firmware
27-
28-
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.
59+
Hold the BOOTSEL button on Pico2 and connect to your computer. It mounts as `RPI-RP2`. Copy `executorch_pico.uf2` to this drive.
2960

3061
### Verify Execution
31-
32-
Check that the Pico2's LED blinks 10 times at 500 ms interval to confirm successful firmware execution.
33-
The Pico2's LED should blink 10 times at 500 ms intervals, indicating successful firmware execution. If connected via serial, you should see:
34-
62+
The Pico2 LED blinks 10 times at 500ms intervals for successful execution. Via serial terminal, you'll see:
3563
```bash
36-
Method loaded [forward]
37-
Output: 13.000000, 136.000000, 24.000000, 131.000000
64+
...
65+
...
66+
PREDICTED: 4 (Expected: 4) ✅ CORRECT!
67+
68+
==================================================
69+
70+
=== Digit 7 ===
71+
############################
72+
############################
73+
####
74+
####
75+
####
76+
####
77+
####
78+
####
79+
####
80+
####
81+
####
82+
####
83+
####
84+
####
85+
####
86+
####
87+
####
88+
####
89+
####
90+
####
91+
####
92+
####
93+
####
94+
####
95+
####
96+
####
97+
####
98+
###
99+
100+
Input stats: 159 white pixels out of 784 total
101+
Running neural network inference...
102+
✅ Neural network results:
103+
Digit 0: 370.000
104+
Digit 1: 0.000
105+
Digit 2: -3.000
106+
Digit 3: -3.000
107+
Digit 4: 860.000
108+
Digit 5: -3.000
109+
Digit 6: -3.000
110+
Digit 7: 1640.000 ← PREDICTED
111+
Digit 8: -3.000
112+
Digit 9: -3.000
113+
114+
� PREDICTED: 7 (Expected: 7) ✅ CORRECT!
115+
116+
==================================================
117+
118+
🎉 All tests complete! PyTorch neural network works on Pico2!
38119
```
39120

40121
### Debugging via Serial Terminal
41-
42-
On macOS or Linux, open a serial terminal with:
43-
122+
On macOS/Linux:
44123
```bash
45124
screen /dev/tty.usbmodem1101 115200
46125
```
126+
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!
47127

48-
Replace /dev/tty.usbmodem1101 with your device path. This terminal shows program logs and errors. If
49-
the LED blinks 10 times at 100 ms intervals, your program hit an error state—check the logs here.
50-
51-
These steps complete running the simple model on Pico2 using ExecuTorch.
128+
Result: A complete PyTorch → ExecuTorch → Pico2 demo neural network deployment! 🚀

examples/raspberry_pi/pico2/build_firmware_pico.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
# build_firmware_pico.sh
1010
# Simple script to cross-compile ExecuTorch and build Pico2 firmware with optional model input
1111

12-
set -e
12+
set -euo pipefail
1313

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

0 commit comments

Comments
 (0)