Add Arduino library support for ExecuTorch#20221
Conversation
Add tooling to package the ExecuTorch runtime as an Arduino library, enabling PyTorch model inference on Arduino microcontrollers. The library vendors ET runtime sources, CMSIS-NN kernels, and portable ops into a self-contained package that compiles under the Arduino build system. Key components: - `build_arduino_library.sh` assembles the distributable library from repository sources (no vendored copies checked in) - `ExecuTorchArduino.h` configures the build environment for Arduino (fixes for std::variant, cmake_macros.h stub, build defines) - `platform_stubs.c` provides C library stubs for the LLEXT environment - Example sketches using the native ExecuTorch C++ API (no wrapper layer) - Zephyr board config for Arduino Uno Q (STM32U585, Cortex-M33) Validated on Arduino Uno Q with DS-CNN keyword spotting model (int8, CMSIS-NN): 390+ source files compile, 106 KB flash (13%), 91 KB RAM. Authored with assistance from Claude.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20221
Note: Links to docs will display an error until the docs builds have been completed. ❌ 2 New Failures, 15 Pending, 1 Unrelated Failure, 7 Unclassified FailuresAs of commit 2b37444 with merge base 1bf982a ( NEW FAILURES - The following jobs have failed:
UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:
FLAKY - The following job failed but was likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
- Add Meta copyright headers to all files - HelloExecuTorch: uses core ET runtime only (portable ops, no backend) - KeywordSpotting: uses Cortex-M backend with CMSIS-NN accelerated ops - Clarify the distinction in sketch comments
There was a problem hiding this comment.
Pull request overview
This PR adds tooling and example code to package ExecuTorch as an installable Arduino library, with support for building/running on Arduino-class microcontrollers (including a Zephyr-based Arduino Uno Q configuration).
Changes:
- Add
build_arduino_library.shto assemble a self-contained Arduino library by copying ExecuTorch sources + required third-party headers. - Introduce Arduino library metadata/header glue (
library.properties,ExecuTorchArduino.h) plus C-library stubs for the target environment. - Add Arduino example sketches and a Zephyr board config tuned for Uno Q.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| zephyr/samples/hello-executorch/boards/arduino_uno_q.conf | Adds Uno Q memory sizing defaults for ExecuTorch allocators/stack/heap. |
| examples/arduino/README.md | Documents the Arduino library packaging flow and usage. |
| examples/arduino/platform_stubs.c | Adds libc symbol stubs intended for Zephyr LLEXT-style environments. |
| examples/arduino/library.properties | Defines Arduino library metadata (name/version/architectures/includes). |
| examples/arduino/ExecuTorchArduino.h | Provides Arduino-specific compile configuration and exposes the native ExecuTorch C++ API. |
| examples/arduino/examples/KeywordSpotting/KeywordSpotting.ino | Adds a DS-CNN keyword spotting sketch using the native ExecuTorch API. |
| examples/arduino/examples/HelloExecuTorch/HelloExecuTorch.ino | Adds a minimal “runtime_init()” sanity-check sketch. |
| examples/arduino/build_arduino_library.sh | Adds the library assembly script (copies sources, generates stubs, applies patches). |
| examples/arduino/.gitignore | Ignores generated Arduino library output directory. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| void _Exit(int status) { | ||
| (void)status; | ||
| while (1) {} | ||
| } |
| int fprintf(FILE* stream, const char* fmt, ...) { | ||
| (void)stream; | ||
| (void)fmt; | ||
| return 0; | ||
| } |
| // Allocate planned buffers | ||
| size_t n_planned = meta->num_memory_planned_buffers(); | ||
| Span<uint8_t>* spans = static_cast<Span<uint8_t>*>( | ||
| method_allocator.allocate(n_planned * sizeof(Span<uint8_t>))); | ||
| for (size_t i = 0; i < n_planned; i++) { | ||
| size_t sz = static_cast<size_t>(meta->memory_planned_buffer_size(i).get()); | ||
| uint8_t* buf = static_cast<uint8_t*>(method_allocator.allocate(sz)); | ||
| spans[i] = {buf, sz}; | ||
| } |
| // Read output | ||
| EValue output; | ||
| method->get_outputs(&output, 1); | ||
| if (output.isTensor()) { |
| #include "model.h" | ||
| #include "test_inputs.h" |
| // Runs a quantized DS-CNN model (MLPerf Tiny KWS benchmark) on hardcoded | ||
| // MFCC test inputs and prints the detected keyword. The model classifies | ||
| // 12 keywords: silence, unknown, yes, no, up, down, left, right, on, off, |
| # Schema headers (generated — need a prior cmake build) | ||
| mkdir -p "$ET_SRC/schema" | ||
| cp "$ET_ROOT/schema/"*.h "$ET_SRC/schema/" 2>/dev/null || true | ||
| # Look for generated headers in common build dirs | ||
| for build_dir in "$ET_ROOT/cmake-out" "$ET_ROOT/cmake-out-mac" \ | ||
| "$ET_ROOT/outputs/build_uno_q"; do | ||
| if [ -d "$build_dir/schema/include/executorch/schema" ]; then | ||
| cp "$build_dir/schema/include/executorch/schema/"*.h "$ET_SRC/schema/" | ||
| break | ||
| fi | ||
| done |
| 2. **`cmake_macros.h` stub** — c10/torch headers expect a cmake-generated | ||
| file. The stub defines `C10_USING_CUSTOM_GENERATED_MACROS`. | ||
|
|
| - **390+ source files** compile with zero errors | ||
| - **Flash**: 106 KB used (13% of 786 KB) | ||
| - **RAM**: 91 KB used (69% of 131 KB) |
Add tooling to package the ExecuTorch runtime as an Arduino library, enabling PyTorch model inference on Arduino microcontrollers. The library vendors ET runtime sources, CMSIS-NN kernels, and portable ops into a self-contained package that compiles under the Arduino build system.
Key components:
build_arduino_library.shassembles the distributable library from repository sources (no vendored copies checked in)ExecuTorchArduino.hconfigures the build environment for Arduino (fixes for std::variant, cmake_macros.h stub, build defines)platform_stubs.cprovides C library stubs for the LLEXT environmentValidated on Arduino Uno Q with DS-CNN keyword spotting model (int8, CMSIS-NN): 390+ source files compile, 106 KB flash (13%), 91 KB RAM.
Authored with assistance from Claude.