Skip to content

Commit a718415

Browse files
pytorchbotpsiddhGithub Executorch
authored
Summary: Pico2 demo of simple neural network (MNIST) (#15196)
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 5. ./build_firmware_pico.sh --model=balanced_tiny_mlp_mnist.pte Results: 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! Reviewers: Subscribers: Tasks: Tags: ### Summary [PLEASE REMOVE] See [CONTRIBUTING.md's Pull Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests) for ExecuTorch PR guidelines. [PLEASE REMOVE] If this PR closes an issue, please add a `Fixes #<issue-id>` line. [PLEASE REMOVE] If this PR introduces a fix or feature that should be the upcoming release notes, please add a "Release notes: <area>" label. For a list of available release notes labels, check out [CONTRIBUTING.md's Pull Requests](https://github.com/pytorch/executorch/blob/main/CONTRIBUTING.md#pull-requests). ### Test plan [PLEASE REMOVE] How did you test this PR? Please write down any manual commands you used and note down tests that you have written if applicable. Co-authored-by: Siddartha Pothapragada <[email protected]> Co-authored-by: Github Executorch <[email protected]>
1 parent 14e34d5 commit a718415

File tree

5 files changed

+609
-80
lines changed

5 files changed

+609
-80
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: 115 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,142 @@
11
# Overview
2-
This document outlines the steps required to run a simple Add Module on the Pico2 microcontroller using executorch.
32

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

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.
5+
## Demo Model: Hand-crafted MNIST Classifier
6+
7+
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.
8+
Note: This is a proof-of-concept. For production use, train your model on real MNIST data.
9+
10+
## Bring Your Own Model
11+
12+
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:
13+
14+
### Train your PyTorch model
15+
16+
Export using `torch.export()` and `to_edge()`
17+
Build firmware with one command
18+
Deploy directly to Pico2
19+
20+
#### Important Caveats:
21+
22+
- Memory constraints - Models must fit in 520KB SRAM
23+
- Missing operators - Some ops may not be supported
24+
- Selective builds - Include only operators your model uses
25+
26+
## Memory Constraints & Optimization
27+
28+
- Critical: Pico2 has limited memory:
29+
- 520KB SRAM (on-chip static RAM)
30+
- 4MB QSPI Flash (onboard storage)
31+
32+
### Always apply optimization techniques on large models that do not fit in Pico2 memory:
33+
34+
Large models will not fit. Keep your `.pte` files small!
35+
- Quantization (INT8, INT4)
36+
- Model pruning
37+
- Operator fusion
38+
- Selective builds (include only needed operators)
39+
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)
40+
41+
## (Prerequisites) Prepare the Environment for Arm
42+
43+
Setup executorch development environment. Also see instructions for setting up the environment for Arm.
44+
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.
845

946
```bash
1047
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
48+
# Should return: executorch/examples/arm/ethos-u-scratch/arm-gnu-toolchain-13.3.rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-gcc
1249
```
1350

14-
## Build Pico2 Firmware with Executorch
51+
## Build Pico2 Firmware with ExecuTorch
52+
53+
This involves two steps:
1554

16-
This step involves two sub steps
55+
### Generate your model:
56+
57+
```bash
58+
cd examples/raspberry_pi/pico2
59+
python export_mlp_mnist.py # Creates balanced_tiny_mlp_mnist.pte
60+
```
1761

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)
62+
### Build firmware:
2063

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

2669
### Flash Firmware
2770

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.
71+
Hold the BOOTSEL button on Pico2 and connect to your computer. It mounts as `RPI-RP2`. Copy `executorch_pico.uf2` to this drive.
2972

3073
### Verify Execution
3174

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-
75+
The Pico2 LED blinks 10 times at 500ms intervals for successful execution. Via serial terminal, you'll see:
3576
```bash
36-
Method loaded [forward]
37-
Output: 13.000000, 136.000000, 24.000000, 131.000000
77+
...
78+
...
79+
PREDICTED: 4 (Expected: 4) ✅ CORRECT!
80+
81+
==================================================
82+
83+
=== Digit 7 ===
84+
############################
85+
############################
86+
####
87+
####
88+
####
89+
####
90+
####
91+
####
92+
####
93+
####
94+
####
95+
####
96+
####
97+
####
98+
####
99+
####
100+
####
101+
####
102+
####
103+
####
104+
####
105+
####
106+
####
107+
####
108+
####
109+
####
110+
####
111+
###
112+
113+
Input stats: 159 white pixels out of 784 total
114+
Running neural network inference...
115+
✅ Neural network results:
116+
Digit 0: 370.000
117+
Digit 1: 0.000
118+
Digit 2: -3.000
119+
Digit 3: -3.000
120+
Digit 4: 860.000
121+
Digit 5: -3.000
122+
Digit 6: -3.000
123+
Digit 7: 1640.000 ← PREDICTED
124+
Digit 8: -3.000
125+
Digit 9: -3.000
126+
127+
� PREDICTED: 7 (Expected: 7) ✅ CORRECT!
128+
129+
==================================================
130+
131+
🎉 All tests complete! PyTorch neural network works on Pico2!
38132
```
39133

40134
### Debugging via Serial Terminal
41135

42-
On macOS or Linux, open a serial terminal with:
43-
136+
On macOS/Linux:
44137
```bash
45138
screen /dev/tty.usbmodem1101 115200
46139
```
140+
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!
47141

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.
142+
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)