Skip to content

Add Arduino library support for ExecuTorch#20221

Open
psiddh wants to merge 2 commits into
pytorch:mainfrom
psiddh:arduino-library-support
Open

Add Arduino library support for ExecuTorch#20221
psiddh wants to merge 2 commits into
pytorch:mainfrom
psiddh:arduino-library-support

Conversation

@psiddh

@psiddh psiddh commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

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.

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.
Copilot AI review requested due to automatic review settings June 11, 2026 16:03
@pytorch-bot

pytorch-bot Bot commented Jun 11, 2026

Copy link
Copy Markdown

🔗 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 Failures

As of commit 2b37444 with merge base 1bf982a (image):

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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 11, 2026
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

- 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.sh to 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.

Comment on lines +9 to +12
void _Exit(int status) {
(void)status;
while (1) {}
}
Comment on lines +14 to +18
int fprintf(FILE* stream, const char* fmt, ...) {
(void)stream;
(void)fmt;
return 0;
}
Comment on lines +72 to +80
// 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};
}
Comment on lines +103 to +106
// Read output
EValue output;
method->get_outputs(&output, 1);
if (output.isTensor()) {
Comment on lines +15 to +16
#include "model.h"
#include "test_inputs.h"
Comment on lines +3 to +5
// 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,
Comment on lines +93 to +103
# 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
Comment on lines +165 to +167
2. **`cmake_macros.h` stub** — c10/torch headers expect a cmake-generated
file. The stub defines `C10_USING_CUSTOM_GENERATED_MACROS`.

Comment on lines +200 to +202
- **390+ source files** compile with zero errors
- **Flash**: 106 KB used (13% of 786 KB)
- **RAM**: 91 KB used (69% of 131 KB)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants