Insights

TinyML on microcontrollers, budgets, runtimes and benchmarks

What fits in a few hundred kilobytes of SRAM, how TensorFlow Lite Micro and MCUNet manage memory, and what the MLPerf Tiny tasks measure, with the hardware each result was measured on.

A microcontroller can run a neural network if the model, its activations and the runtime all fit in on-chip memory. That condition usually decides feasibility before accuracy does. This article sets out the memory budget, explains how two published runtimes handle it, and summarizes the MLPerf Tiny benchmark tasks. Every result is given with the board and clock it was measured on, because microcontroller numbers do not transfer between parts.

Flash holds the weights, SRAM holds the activations

On a microcontroller, flash constrains model size (read-only) and SRAM constrains activation size (read and write) [1]. The MCUNet authors describe microcontrollers as bare-metal devices with no operating system and no DRAM [1]. They use the STM32F746, an Arm Cortex-M7 part with 320 kB of SRAM and 1 MB of flash, as their reference. Against it, ResNet-50 exceeds the storage limit by about 100x and MobileNetV2 exceeds the peak memory limit by 22x. Even int8 MobileNetV2 exceeds it by 5.3x, before counting runtime buffers [1].

The TensorFlow Lite Micro (TFLM) authors describe the range of targets this way. A large embedded system has a few megabytes of flash and at most a megabyte of SRAM. A small one has a few hundred kilobytes or less, split between ROM and RAM [2]. Many targets also lack dynamic memory management, virtual memory, a file system and floating-point hardware [2]. A runtime that assumes any of these will not port.

Two quick estimates are worth doing before any training. The numbers here are our own, not taken from a paper.

  • Weights. Parameter count times bytes per parameter. A 38.6K-parameter int8 model needs about 38.6 kB of weights. The MLPerf Tiny keyword-spotting model has 38.6K parameters, yet its TFLite file is 52.5 KB [3]. Budget for the file, not just the weights.
  • Peak activations. For a layer that reads one tensor and writes another, both must be live at once. An int8 feature map of 48 x 48 x 16 is 36,864 bytes. Input plus output is 73,728 bytes, close to a quarter of the STM32F746’s 320 kB, before stacks, I/O buffers and application code.

Peak memory is set by the single worst layer, not the average. MCUNet shows that MobileNetV2 cuts model size by 4.6x relative to ResNet-18 at similar ImageNet accuracy, but its peak activation size is 1.8x larger [1]. A smaller model can still be harder to fit.

TensorFlow Lite Micro: an interpreter with a fixed arena

TFLM runs models converted by the standard TensorFlow Lite toolchain and stored as FlatBuffer files [2]. Its design choices follow from the constraints above:

  • Narrow scope. The library assumes the model, input and output arrays are already in memory. It does not load files or read peripherals [2].
  • Selective linking. An “operator resolver” object lists the operators the application needs, so only those are linked into the binary [2].
  • One arena, allocated once. The application passes in a contiguous memory block. The interpreter takes everything it needs from that block during initialization and allocates nothing afterward, so long-running devices do not fragment a heap [2].
  • Two stacks in the arena. Allocations that live as long as the interpreter grow from one end. Shorter-lived allocations grow from the other. If the two meet, the arena is too small [2].
  • Memory planning. Intermediate tensors with non-overlapping lifetimes share space. The planner uses a first-fit-decreasing heuristic at run time. An offline-planned layout is also supported [2]. Dynamic shapes are not supported, because all sizes must be known at initialization [2].
  • Per-kernel vendor optimization. Chip vendors can replace individual operator kernels without writing a compiler. The paper’s optimized results use Arm CMSIS-NN and Cadence libraries [2].

The evaluation used two platforms [p206, Table 1]:

Platform Processor Clock Flash RAM
Sparkfun Edge (Ambiq Apollo3) Arm Cortex-M4 96 MHz 1 MB 0.38 MB
Tensilica HiFi Xtensa HiFi Mini DSP 10 MHz 1 MB 1 MB

On the Cortex-M4, the Visual Wake Words (VWW) person-detection model took 18,990.8K cycles with reference kernels and 4,857.7K cycles with CMSIS-NN, a speedup of more than 4x. On the HiFi Mini DSP, the optimized kernels gave 7.7x [p206, Table 2]. At 96 MHz, 4,857.7K cycles is about 51 ms per inference (our arithmetic). The interpreter’s own overhead was under 0.1% of cycles for VWW. For the much shorter Google Hotword model on the Cortex-M4 it was 3.3% with reference kernels and 4.1% with optimized kernels [p206, Table 2]. The lesson is that dispatch cost matters only when the model itself is tiny.

Memory use on the Sparkfun Edge was [p206, Table 3]:

Model Persistent Non-persistent Total
Two-layer convolutional reference 1.29 kB 7.75 kB 9.04 kB
Google Hotword 12.12 kB 680 bytes 12.80 kB
VWW 26.50 kB 55.30 kB 81.79 kB

The interpreter alone takes less than 2 KB [2].

MCUNet: designing the model and the runtime together

MCUNet pairs a neural architecture search (TinyNAS) with an inference engine (TinyEngine) and optimizes both against the same memory budget [1]. The authors argue that interpreter-based runtimes can spend up to 65% of peak memory on their own structures and add 22% latency [1]. That measures something different from TFLM’s dispatch overhead above, which counts cycles only. Both figures should be checked on the actual target.

TinyEngine generates code ahead of time. It schedules memory using the whole network rather than layer by layer, and it updates depth-wise convolutions in place [1]. Against TF-Lite Micro and CMSIS-NN, the authors report 3.4x lower peak memory and 1.7x to 3.3x faster inference. In-place depth-wise convolution alone cut measured memory by 1.6x [1].

Results, with the hardware stated in the paper (latency normalized to the STM32F746 at 216 MHz) [1]:

  • ImageNet, int8, STM32F746 (320 kB SRAM, 1 MB flash). A scaled MobileNetV2 on CMSIS-NN reaches 35.2% top-1. The same model family on TinyEngine reaches 47.4%. Full MCUNet reaches 61.8% [p208, Table 2].
  • ImageNet, 4-bit with 25 epochs of quantization-aware fine-tuning. 62.0% on STM32F412 (Cortex-M4, 256 kB SRAM, 1 MB flash), 63.5% on STM32F746, 65.9% on STM32F765 (512 kB, 1 MB) and 70.7% on STM32H743 (512 kB, 2 MB) [p208, Table 4].
  • Visual Wake Words. Compared with scaled MobileNetV2 and ProxylessNAS models on TF-Lite Micro, MCUNet is 2.4x to 3.4x faster with 3.7x smaller peak memory at higher accuracy [1].
  • Speech Commands. 2.8x faster with 4.1x smaller peak memory [1].

The 70.7% figure needs both the larger H743 and 4-bit weights and activations. On the 320 kB part with int8, the number is 61.8%.

MLPerf Tiny: four tasks with fixed quality targets

MLPerf Tiny measures accuracy, latency and energy on four tasks [3]. Each task fixes a dataset, a reference model and a minimum quality [p207, Table 1]:

Task Dataset (input) Reference model (TFLite size) Quality target
Keyword spotting Speech Commands (49x10) DS-CNN (52.5 KB) 90% top-1
Visual wake words VWW (96x96) MobileNetV1 (325 KB) 80% top-1
Image classification CIFAR-10 (32x32) ResNet (96 KB) 85% top-1
Anomaly detection ToyADMOS (5x128) FC autoencoder (270 KB) 0.85 AUC

The targets sit below the reference accuracies to leave room for quantization and platform differences. The VWW model reaches about 86% against an 80% target. The image classifier reaches 86.5% on the 200-image test subset against 85%. The quantized keyword model reaches 91.6% on the full test set against 90%. The anomaly detector’s AUC falls from 0.88 in FP32 to 0.86 after quantization, against a threshold of 0.85 [3].

Other rules that matter when reading results:

  • The reference implementations run TFLM on a NUCLEO-L4R5ZI board [3].
  • Measurement covers model inference only. Pre- and post-processing are excluded, and keyword spotting excludes feature extraction [3].
  • The closed division keeps the reference model, dataset and quality target. It allows post-training quantization with the provided calibration data but no retraining. The open division allows changes to the model, training scripts and dataset, need not meet the accuracy threshold, and must document how it deviates from the reference [3].
  • Energy is measured with an external energy monitor and reported per inference [3].

Because feature extraction is excluded, a fast benchmark result does not guarantee a fast product. The audio front end still runs on the same core.

A practical sequence

  1. Write down the budget: flash, SRAM, latency, energy per inference and duty cycle.
  2. Estimate weight bytes and the worst layer’s activation bytes before choosing an architecture.
  3. Start from the MLPerf Tiny reference model closest to the task, so there is a published baseline to compare against.
  4. Quantize to int8. Our article on quantization for deployment covers the choices.
  5. Measure on the board: cycles, arena high-water mark and energy, including preprocessing.
  6. Split evaluation data by speaker, device or session. The Speech Commands dataset puts each speaker in only one of the training, validation and test subsets [3]. Our evaluation pitfalls checklist explains why.

If the budget cannot be met on a microcontroller, a small FPGA or an accelerator may fit better. See neural network inference on FPGAs.

Where Takumetra can help

We can size a model against a specific microcontroller’s flash, SRAM and energy budget. We can also port and profile it under TFLM or a code-generating runtime, and measure it on the target board. See our AI/ML engineering services.

References

  1. J. Lin, W.-M. Chen, Y. Lin, J. Cohn, C. Gan, S. Han, “MCUNet: Tiny Deep Learning on IoT Devices”, Advances in Neural Information Processing Systems 33 (NeurIPS 2020), 2020. arxiv.org/abs/2007.10319
  2. R. David, J. Duke, A. Jain, et al., “TensorFlow Lite Micro: Embedded Machine Learning on TinyML Systems”, Proc. 4th MLSys Conference, 2021. arxiv.org/abs/2010.08678
  3. C. Banbury, V. Janapa Reddi, P. Torelli, et al., “MLPerf Tiny Benchmark”, arXiv:2106.07597, 2021. arxiv.org/abs/2106.07597

Copies of the cited sources are held on file. DOIs were checked against CrossRef.

Related articles