Bringing Google's TPU memory planning to microcontrollers.

The DeepGate compiler now uses Google's MiniMalloc to find optimal memory layouts in at most 1.3 ms, up to ~1,400× faster than our previous memory planner.

Memory planning is critical for AI on microcontrollers, where tight memory constraints can determine whether a model fits on a device or not. In our previous work on the DeepGate edge AI compiler, we showed that whole-graph memory planning could significantly reduce memory use compared with TensorFlow Lite for Microcontrollers (TFLM), the de facto runtime for deploying machine learning models on microcontrollers.

Up to now, our compiler has used integer linear programming (ILP) to find efficient memory layouts, but finding an optimal layout could take a considerable amount of time for some models. To accelerate memory planning without sacrificing optimality, we have adapted MiniMalloc, a search algorithm developed by Google for its TPU compilers, to solve memory allocation problems for AI models on microcontrollers. Across 24 models and three hardware targets, it found the optimal activation memory layout in at most 1.3 ms – up to ~1,400× faster than our ILP planner.

This makes optimal memory planning practical for every compilation. As embedded devices with more memory come to market, enabling larger and more complex AI models to run, efficient memory planning will become increasingly important. By removing memory planning as a computational bottleneck, we can accelerate the development of memory-efficient AI models and enable more capable AI within the memory constraints of embedded devices.

How edge AI compilers plan memory layout today

An AI model processes data through a sequence of layers, such as convolutions and linear operations. Each layer produces an output, called an activation, that must be stored in memory. Some activations must be preserved for use by later layers (for example, in the skip connections used by residual networks), while others can be overwritten sooner. This creates an interesting memory planning problem: how do we reuse memory as efficiently as possible while preserving activations that are still needed?

Most edge AI runtimes and compilers store these activations in a single block of memory, called the arena. The goal of memory planning is to minimise the size of this arena, reducing the model’s activation memory requirements.

Finding the smallest possible memory layout is an NP-hard packing problem, meaning that the time required to find an optimal solution can grow rapidly as models become larger and more complex. As a result, tools such as TFLM typically rely on greedy heuristics, which are fast but do not guarantee optimal layouts. Given the tight memory constraints of microcontrollers, optimal memory planning can enable models to run on devices with less memory or more capable models to run within the same memory budget.

To overcome the limitations of greedy memory planning, we previously implemented an ILP planner in the DeepGate edge AI compiler. It considers the entire model graph as a single optimisation problem, which we solve using HiGHS, an open-source solver. This significantly reduced memory usage compared with greedy planning, but at the cost of longer compilation times.

The problem

Our ILP planner had two practical limitations.

  1. ILP can be slow and its run time is hard to predict. Even on the small embedded ML models we compile for microcontrollers, memory planning took up to 410 ms. The run time can increase dramatically as planning problems become harder: prior work from Google on its TPU compilers found that an ILP planner failed to solve more than half of its harder test cases within one minute.
  2. In rare cases the solver may not find the true optimum. Memory planning is a discrete problem: activations must be placed at exact byte addresses. Numerical ILP solvers typically use floating-point calculations and tolerances rather than exact arithmetic. In our tests with HiGHS, this sometimes led the solver to report a layout as optimal when a slightly smaller one existed (false positives).

We therefore wanted a planner that could find the true optimal memory layout at close to the speed of a greedy planner.

Our solution: adapting Google’s MiniMalloc memory planner for microcontrollers

MiniMalloc is a memory planning algorithm designed to find optimal memory layouts quickly. Developed by Michael Moffitt at Google and published at ASPLOS 2023, it was evaluated on production models for the Pixel 6 phone, achieving a median speedup of 328× over an ILP solver. We adapted MiniMalloc for microcontrollers and made it the default memory planner in the DeepGate compiler.

MiniMalloc searches by placing activations in the arena one at a time, backtracking when they cannot all fit. The key to its speed is drastically reducing the number of possible placements it needs to explore without sacrificing optimality. Given enough search steps, it is guaranteed to find the smallest possible arena, and in all our tests, it finished within its step budget. It also works entirely with integers, avoiding the numerical precision issues associated with floating-point ILP solvers. We describe these techniques and our main adaptation for microcontrollers in Methods and technical details.

Results

We compiled 24 open-source models spanning computer vision, audio processing, anomaly detection and activity recognition for three microcontroller targets: Arm Cortex-M4, Silicon Labs EFR32MG24 and Espressif ESP32-S3. Across all 72 model–target combinations, we compared three memory planners within the DeepGate compiler – greedy, ILP and MiniMalloc.

Memory savings

MiniMalloc found the optimal memory layout for all 72 model–target combinations. In 52 cases, the greedy planner was already optimal, and all three planners produced the same arena size. The chart below shows the 12 cases where the greedy planner’s arena was at least 1.01× the size of the optimal one. Memory savings for the same model can vary across hardware targets due to differences in their kernels’ scratch buffer and memory alignment requirements.

MiniMalloc vs greedy arena size: up to 1.43× smaller
Greedy/MiniMalloc
BlazeFace · Cortex-M41.43×MiniMalloc 336.0 KiB, Greedy 480.0 KiB
BlazeFace · ESP32-S31.43×MiniMalloc 336.0 KiB, Greedy 480.0 KiB
BlazeFace · EFR32MG241.11×MiniMalloc 512.0 KiB, Greedy 570.6 KiB
MLPerf Tiny IC · Cortex-M41.20×MiniMalloc 56.6 KiB, Greedy 68.1 KiB
MLPerf Tiny VWW · Cortex-M41.18×MiniMalloc 54.0 KiB, Greedy 63.6 KiB
MLPerf Tiny VWW · ESP32-S31.16×MiniMalloc 54.1 KiB, Greedy 63.0 KiB
MicroNet VWW-4 · Cortex-M41.14×MiniMalloc 56.0 KiB, Greedy 64.0 KiB
MicroNet VWW-4 · ESP32-S31.14×MiniMalloc 56.1 KiB, Greedy 64.0 KiB
MiniResNetV2 · EFR32MG241.11×MiniMalloc 70.4 KiB, Greedy 78.0 KiB
YAMNet-256 · EFR32MG241.07×MiniMalloc 327.2 KiB, Greedy 351.2 KiB
Magic wand · EFR32MG241.07×MiniMalloc 14.0 KiB, Greedy 15.0 KiB
MLPerf Tiny IC large · EFR32MG241.02×MiniMalloc 197.0 KiB, Greedy 200.0 KiB
Arena size (KiB, log scale)DeepGate compiler benchmark · 12 model and target pairs where the greedy arena is at least 1.01× the optimal
Each row is one model and target, grouped by model. The grey bar spans the gap between the greedy and optimal arena, and the right column shows the memory saved.

Compile time

End-to-end compilation with the greedy planner takes between 3 ms and 23 ms. The chart below shows the additional compilation time over the greedy planner for the 20 cases where greedy planning was not optimal. MiniMalloc added a median of 0.2 ms and a maximum of 1.3 ms, compared with a median of 30 ms and a maximum of 410 ms for our ILP planner. Across these cases, MiniMalloc incurred 13× to approximately 1,400× less compilation overhead than our ILP planner.

MiniMalloc vs ILP compile time: up to ~1,400× faster
BlazeFace · ESP32-S3MiniMalloc 0.3 ms, ILP 409 ms
BlazeFace · Cortex-M4MiniMalloc 0.5 ms, ILP 210 ms
BlazeFace · EFR32MG24MiniMalloc 0.3 ms, ILP 164 ms
MicroNet VWW-4 · Cortex-M4MiniMalloc 0.8 ms, ILP 196 ms
MicroNet VWW-4 · ESP32-S3MiniMalloc 0.3 ms, ILP 48 ms
MLPerf Tiny VWW · Cortex-M4MiniMalloc 0.2 ms, ILP 50 ms
MLPerf Tiny VWW · ESP32-S3MiniMalloc 0.1 ms, ILP 6.4 ms
MobileNetV1-0.25 · EFR32MG24MiniMalloc 0.2 ms, ILP 36 ms
MobileNetV2-0.35 · EFR32MG24MiniMalloc 0.2 ms, ILP 35 ms
MobileNetV2-0.35 · Cortex-M4MiniMalloc 0.3 ms, ILP 32 ms
MobileNetV2-0.35 · ESP32-S3MiniMalloc 0.2 ms, ILP 22 ms
MLPerf Tiny IC · Cortex-M4MiniMalloc 0.1 ms, ILP 32 ms
MiniResNetV2 · EFR32MG24MiniMalloc 0.1 ms, ILP 27 ms
MLPerf Tiny IC large · EFR32MG24MiniMalloc 0.1 ms, ILP 24 ms
MicroNet-KWS-L · ESP32-S3MiniMalloc 1.3 ms, ILP 18 ms
Tiny Wav2letter · ESP32-S3MiniMalloc 0.1 ms, ILP 14 ms
YAMNet-256 · EFR32MG24MiniMalloc 0.1 ms, ILP 12 ms
DS-CNN-M · ESP32-S3MiniMalloc 0.1 ms, ILP 9.3 ms
DS-CNN-L · ESP32-S3MiniMalloc 0.4 ms, ILP 5.0 ms
Magic wand · EFR32MG24MiniMalloc 0.1 ms, ILP 4.6 ms
Extra compile time (ms, log scale)DeepGate compiler benchmark · 20 model and target pairs · median of at least 10 runs on an Apple M4
Each row is one model and target, grouped by model, with the models our ILP planner was slowest on first. Times are the extra compile time over the greedy planner. The grey bar spans the gap between the two methods.

Impact

The DeepGate compiler now gets optimal memory plans at close to the cost of a greedy planner, so exact planning can run on every compile.

  • Faster hardware profiling. Optimal planning is now cheap enough to run on every compile, which is of particular relevance in our MCU farm, where we compile and test models on real target boards.
  • More memory for the application. Every byte saved in the arena is RAM that the rest of the firmware can use, or room for a larger model. The largest saving in our tests was on BlazeFace (Cortex-M4 and ESP32-S3): the arena shrank from 480 KiB with the greedy planner to 336 KiB, a 1.43× reduction that frees 144 KiB of RAM.
  • Faster design-space searches. Searching for the best model means compiling many candidates, for example in DeepGate’s neural architecture search and agentic model search. A fast, exact planner lets each candidate be judged by its true memory use. This matters most when an agent compiles and tests many variants in one session.
  • Simpler, repeatable builds. Our MiniMalloc implementation is plain Rust code with no solver dependency. Our ILP planner depended on a C++ solver library, which added several minutes to a clean build, and our multi-threaded runs did not always return the same layout twice.

Methods and technical details

How MiniMalloc narrows the search

MiniMalloc reduces the number of placements it explores in five ways:

  • It never tries the same layout twice. Placing activations in a different order can give the same layout, so MiniMalloc only allows one order: each activation starts at or above the previous one, and ties go in a fixed order.
  • It only tries gap-free layouts. Each activation sits either at the bottom of the arena or directly on top of an activation it overlaps in time. Any valid layout can be compacted downwards into this form without using more memory, so no solution is lost.
  • It skips choices that cannot be better. If another activation would fit entirely below the current candidate without moving anything, the search places that activation first.
  • It stops early on dead ends. After each placement, it checks that the remaining activations still fit under the limit at every point in time.
  • It splits the problem. Parts of the model whose activations are never alive at the same time are solved separately. A dead end in one part then does not make the search retry choices in another.

What we added for microcontrollers

The paper assumes that an activation can start at any byte. On microcontrollers, activations must start at aligned addresses; the ESP32-S3’s vector instructions, for example, need 16-byte alignment. Alignment adds padding between activations, which needs to be accounted for during planning.

We extended the lower bound and the search pruning to count this padding. On four of our pairs, the optimal arena is a few bytes above the plain sum of the live activation sizes, only because of padding. Before this change the search ran out of steps on one of them, and now it finds the optimal solutions in under 2 ms.

Models

We used 29 open-source TensorFlow Lite models, all quantised to 8-bit integers. They come from the MLPerf Tiny benchmark, the TensorFlow Lite for Microcontrollers examples, the Arm ML-Zoo, the MCUNet project and the Apache-2.0 part of the ST model zoo. All are available under the Apache-2.0 or MIT licence.

Use caseModels
MLPerf TinyVisual wake words, image classification (standard and large), keyword spotting, anomaly detection, streaming wake word
Person detectionTFLM person detection, MicroNet VWW-4, MCUNet VWW
Image classificationMCUNet-in2, MobileNetV1-0.25, MobileNetV2-0.35, MobileNetV2-1.0, EfficientNet-Lite0
Object detectionBlazeFace, MCUNet person detector
Keyword spottingTFLM micro_speech, DS-CNN-M, DS-CNN-L, MicroNet-KWS-L, LSTM micro_speech
Gesture and activityMagic wand, WISDM activity recognition
Anomaly detectionMicroNet-AD-L
Audio eventsYAMNet-256, MiniResNetV2
Noise suppressionRNNoise, DTLN
Speech recognitionTiny Wav2letter

Five of the models do not compile with the DeepGate compiler yet, so the results cover 24 models.

Targets

Each model was compiled for three targets, which gives 72 model and target pairs.

TargetDeepGate KernelsLargest alignment
Arm Cortex-M4Arm DSP-extension kernels4 bytes
Silicon Labs EFR32MG24MVP accelerator kernels, with DSP kernels as a fallback4 bytes
Espressif ESP32-S3PIE SIMD kernels16 bytes

Measurement

For each pair, we ran the DeepGate compiler’s memory planning step with the greedy planner, our implementation of MiniMalloc, and our ILP planner. All three receive the same activations, with MiniMalloc and our ILP planner both using the greedy layout as a starting point. We report only the wall-clock time added to the compilation process for each approach, using the median time from at least 10 runs.

The machine was an Apple M4 with 16 GB of memory, running a release build of the DeepGate compiler. MiniMalloc used a budget of 500,000 search steps per memory limit tried. Our ILP planner ran single-threaded with the smallest floating-point tolerance it accepts (to minimise false positives), and a 60-second time limit.

Checking the results

Every layout was checked for overlapping activations and correct alignment. We also checked every arena against a lower bound. At the busiest point in the model, several activations are alive at once and each needs its own memory, so no layout can be smaller than their total size plus the alignment padding between them. An arena that reaches this floor cannot be beaten. All arenas reached it, so each one is provably optimal.

Sources