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.
- 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.
- 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.
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.
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 case | Models |
|---|---|
| MLPerf Tiny | Visual wake words, image classification (standard and large), keyword spotting, anomaly detection, streaming wake word |
| Person detection | TFLM person detection, MicroNet VWW-4, MCUNet VWW |
| Image classification | MCUNet-in2, MobileNetV1-0.25, MobileNetV2-0.35, MobileNetV2-1.0, EfficientNet-Lite0 |
| Object detection | BlazeFace, MCUNet person detector |
| Keyword spotting | TFLM micro_speech, DS-CNN-M, DS-CNN-L, MicroNet-KWS-L, LSTM micro_speech |
| Gesture and activity | Magic wand, WISDM activity recognition |
| Anomaly detection | MicroNet-AD-L |
| Audio events | YAMNet-256, MiniResNetV2 |
| Noise suppression | RNNoise, DTLN |
| Speech recognition | Tiny 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.
| Target | DeepGate Kernels | Largest alignment |
|---|---|---|
| Arm Cortex-M4 | Arm DSP-extension kernels | 4 bytes |
| Silicon Labs EFR32MG24 | MVP accelerator kernels, with DSP kernels as a fallback | 4 bytes |
| Espressif ESP32-S3 | PIE SIMD kernels | 16 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
- DeepGate, the DeepGate edge AI compiler.
- M. D. Moffitt, MiniMalloc: A Lightweight Memory Allocator for Hardware-Accelerated Machine Learning, ASPLOS 2023. The source code is at github.com/google/minimalloc.
- K. He, X. Zhang, S. Ren and J. Sun, Deep Residual Learning for Image Recognition, CVPR 2016.
- M. Maas, U. Beaugnon, A. Chauhan and B. Ilbeyi, TelaMalloc: Efficient On-Chip Memory Allocation for Production Machine Learning Accelerators, ASPLOS 2023.
- TensorFlow Lite for Microcontrollers: the greedy memory planner and its use as the default in micro_allocator.h.
- Apache TVM v0.14.0: the Unified Static Memory Planner and its greedy algorithms.
- HiGHS: the project site and the list of options and defaults.