TinyML on microcontrollers

Running models on microcontrollers with kilobytes of memory: the tensor arena, supported operators and memory limits.

Advanced11 min read

Educational draft content that needs expert review before it is used as an academic reference. Specifications, versions and licences change; check the official source. No experimental result is shown here unless it was actually run.

Learning objectives

  • Explain the tensor arena
  • Estimate whether a model fits a microcontroller’s memory
  • List the steps to deploy on an MCU

Microcontrollers (MCUs) typically have hundreds of kilobytes of SRAM, a little flash and no full operating system. Frameworks such as TensorFlow Lite for Microcontrollers (LiteRT for Microcontrollers) avoid dynamic allocation: they reserve a static array, the tensor arena, to hold intermediate activations.

The arena size is set by the peak concurrent activation use, not the sum of all layers, so it can change with the architecture. Weights are embedded as a constant array in flash, and only the operators the model needs are linked (the op resolver) to keep the binary small.

Practical rules: quantize to int8, pick a small architecture, and measure memory and time on the real hardware rather than estimating. Libraries such as CMSIS-NN speed up kernels on Arm Cortex-M processors.

Terminology (with Arabic equivalents)

Microcontroller (MCU)متحكم دقيق
SRAMذاكرة وصول عشوائي ساكنة
Flashذاكرة فلاش
Tensor arenaمنطقة الموتّرات
Op resolverمحلّل المشغّلات
Cortex-Mمعالجات Arm كورتكس-إم

Worked example

An int8 model with 20 thousand parameters occupies about 20 KB of flash (one byte per parameter), but working memory is determined by its intermediate activations. If the largest activation is 32 KB, reserve an arena no smaller than it with a margin, then verify on the device. (Illustrative figures.)

Code snippet

PythonRough estimate (illustrative figures)
def fits(weights_params, bits, peak_activation_kb, flash_kb, sram_kb, overhead_kb=16):
    flash_need = weights_params * bits / 8 / 1024
    sram_need = peak_activation_kb + overhead_kb
    return flash_need <= flash_kb and sram_need <= sram_kb, flash_need, sram_need

# illustrative numbers only
print(fits(20_000, 8, 32, 512, 256))

An illustrative snippet that was not executed in the lab environment; check library versions in their official documentation before use.

Check yourself

  1. What is the tensor arena?

    • A weight file on disk
    • A static array that holds intermediate activations
    • A language compiler
    • A layer in the network
    Answer and explanation

    A static array that holds intermediate activations — It is a memory region reserved in advance for activations.

  2. What usually determines the arena size?

    • The number of images
    • The peak of simultaneous activations
    • The model name
    • The interface language
    Answer and explanation

    The peak of simultaneous activations — The peak, not the sum of the layers.

  3. Why do we measure on the actual hardware?

    • The theoretical estimate may differ from reality
    • Because measuring is useless
    • To avoid quantization
    • To increase latency
    Answer and explanation

    The theoretical estimate may differ from reality — Real memory and time depend on the implementation and the hardware.

References

Suggested reading (links not verified yet)

  • Warden & Situnayake (2019). TinyML. O’Reilly Media Not verified
  • TensorFlow Lite for Microcontrollers documentation Not verified

Image and audio processing in the experiments runs inside your browser. Your images and audio are never uploaded and conversations are not stored. Privacy