|
1 | 1 | # Overview |
2 | | -This document outlines the steps required to run a simple Add Module on the Pico2 microcontroller using executorch. |
3 | 2 |
|
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. |
5 | 4 |
|
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. |
8 | 45 |
|
9 | 46 | ```bash |
10 | 47 | 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 |
12 | 49 | ``` |
13 | 50 |
|
14 | | -## Build Pico2 Firmware with Executorch |
| 51 | +## Build Pico2 Firmware with ExecuTorch |
| 52 | + |
| 53 | +This involves two steps: |
15 | 54 |
|
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 | +``` |
17 | 61 |
|
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: |
20 | 63 |
|
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 |
24 | 67 | ``` |
25 | 68 |
|
26 | 69 | ### Flash Firmware |
27 | 70 |
|
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. |
29 | 72 |
|
30 | 73 | ### Verify Execution |
31 | 74 |
|
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: |
35 | 76 | ```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! |
38 | 132 | ``` |
39 | 133 |
|
40 | 134 | ### Debugging via Serial Terminal |
41 | 135 |
|
42 | | -On macOS or Linux, open a serial terminal with: |
43 | | - |
| 136 | +On macOS/Linux: |
44 | 137 | ```bash |
45 | 138 | screen /dev/tty.usbmodem1101 115200 |
46 | 139 | ``` |
| 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! |
47 | 141 |
|
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! 🚀 |
0 commit comments