Running a neural network on a microcontroller is possible only when the entire inference pipeline is optimized around limited resources.
Typical constraints include:
- small SRAM,
- limited Flash,
- modest CPU frequency,
- low power budget.
Optimization should target:
- accuracy,
- memory,
- latency,
- energy
at the same time.
Fundamentals and Technology Overview
Start With the Right Model
The easiest model to optimize is the one that was designed for embedded use from the beginning.
Prefer:
- compact CNNs,
- depthwise separable convolutions,
- small feature vectors.
Do not begin with a large cloud model unless there is a clear compression path.
Establish a Baseline
Before optimization, measure:
- accuracy,
- model size,
- peak SRAM,
- inference latency,
- energy.
Without a baseline, optimization gains are impossible to verify.
Quantize to INT8
INT8 is usually the first major optimization.
Benefits include:
- smaller weights,
- smaller activations,
- faster integer kernels,
- lower bandwidth.
PTQ First
Try post-training quantization first because it is simple and fast.
Use representative calibration data.
Use QAT When Needed
If PTQ causes too much accuracy loss, use quantization-aware training.
Use Optimized Kernels
Generic C kernels are rarely optimal.
For Cortex-M, CMSIS-NN provides optimized neural-network kernels.
Arm's edge AI ecosystem combines:
- Cortex-M,
- CMSIS-NN,
- Ethos-U NPUs.
Use DSP and Vector Extensions
Modern Cortex-M processors may include:
- DSP instructions,
- Helium/MVE vector extensions.
These accelerate:
- convolution,
- feature extraction,
- matrix math.
Use an NPU When Available
If the MCU includes an NPU, map as much of the network to it as possible.
ST's STM32N6 guidance notes that CPU fallback can be dramatically slower than NPU execution.
Check Operator Coverage
Before hardware benchmarking, analyze:
- which operators run on accelerator,
- which fall back to CPU.
A single unsupported operation can become the bottleneck.
Simplify the Graph
Replace unsupported or expensive operators with accelerator-friendly alternatives.
Operator Fusion
Combine sequences such as:
convolution + bias + activation
where supported.
This reduces intermediate memory traffic.
Performance, Memory, and Data Processing
Reduce Input Size
Image resolution strongly affects compute.
Reducing:
- 224 × 224
to:
- 160 × 160
can significantly reduce operations, depending on architecture.
Crop Region of Interest
If only part of the sensor data matters, process only that region.
Feature Extraction
For audio and vibration, classical DSP features may reduce network complexity.
Examples:
- FFT,
- MFCC,
- RMS.
A smaller model can then work on compact features.
Reduce Channels
Network width directly affects:
- MAC count,
- activation memory.
Use architecture search or manual tuning to reduce unnecessary channels.
Reduce Layer Count
Deeper is not always better for constrained applications.
Pruning
Pruning removes less-important weights or structures.
Structured pruning is usually more useful for embedded hardware because it can reduce real compute.
Memory Planning
Peak SRAM often determines whether the model fits.
Measure:
- tensor arena,
- activation peaks,
- input buffer,
- stack,
- RTOS memory.
Reuse Buffers
Buffers whose lifetimes do not overlap can share the same memory.
Good runtimes perform tensor-memory planning automatically.
Avoid Dynamic Allocation
Static memory allocation improves:
- determinism,
- reliability.
Internal vs External Memory
Keep frequently accessed tensors in fast internal SRAM where possible.
External memory increases:
- latency,
- power.
Weight Streaming
For larger models, weights may be streamed from external Flash.
This saves SRAM but increases bandwidth pressure.
Processing Resources and Performance Tradeoffs
DMA
Use DMA to move:
- ADC samples,
- camera data,
- audio buffers.
This keeps the CPU available for inference.
Double Buffering
While one buffer is processed, another can be filled.
This is especially useful for continuous:
- audio,
- vibration,
- camera streams.
Cache Management
On MCUs with cache and DMA, maintain coherency correctly.
Incorrect cache handling can cause both:
- wrong results,
- hidden performance penalties.
Avoid Excessive Copies
Every memory copy consumes:
- CPU time,
- energy.
Design clear buffer ownership.
Optimize Preprocessing
Inference may be fast while preprocessing dominates.
Profile:
- normalization,
- resizing,
- FFT,
- color conversion.
Fixed-Point DSP
For preprocessing, fixed-point algorithms may save energy compared with floating point on suitable processors.
Compiler Optimization
Use appropriate compiler options such as:
- optimization for speed,
- link-time optimization
when validated for the product.
Remove Unused Operators
Inference runtimes may allow selective operator registration.
This can reduce:
- firmware size.
Minimize Runtime Overhead
Use embedded-focused runtimes rather than large general-purpose frameworks.
RTOS Scheduling
Place inference in a task with appropriate priority.
Do not allow AI to block:
- motor control,
- safety,
- communication deadlines.
Interrupt Strategy
Keep ISRs short.
Do not run heavy neural inference directly inside an interrupt handler.
Implementation, Testing, and Optimization
Measure End-to-End Latency
Include:
- sensor acquisition,
- preprocessing,
- inference,
- postprocessing.
Energy per Inference
Measure:
joules per useful decision
not only average current.
Duty Cycling
If inference is needed only periodically:
- sleep,
- wake,
- sample,
- infer,
- return to sleep.
This can reduce average energy dramatically.
Event-Driven Inference
Use a low-power threshold or sensor interrupt to trigger AI only when needed.
Benchmark on Real Hardware
ST Edge AI Core and similar vendor tools can estimate:
- model compatibility,
- memory footprint,
- inference time.
But final validation should run on the real target.
Accuracy Validation
After every major optimization, verify:
- task accuracy,
- false positive rate,
- false negative rate.
A faster model that fails the application metric is not optimized.
Common Mistake: Optimizing Only the Neural Network
The complete system may be dominated by preprocessing or communication.
Common Mistake: Ignoring SRAM
Model file size does not equal runtime memory.
Common Mistake: Using the Largest Clock
Higher frequency may improve latency but hurt energy and thermal behavior.
Common Mistake: No Performance Margin
Leave room for:
- firmware growth,
- new features,
- model updates.
Optimization Workflow
- Measure baseline.
- Quantize to INT8.
- Use optimized kernels.
- Analyze operator compatibility.
- Reduce input dimensions.
- Simplify architecture.
- Optimize tensor memory.
- Use DMA and double buffering.
- Profile preprocessing.
- Benchmark accuracy, latency and energy.
Conclusion
Microcontroller neural-network optimization is a system-level process.
The largest gains usually come from:
- choosing a smaller model,
- INT8 quantization,
- optimized kernels,
- efficient memory planning,
- reducing data movement.
The most important principle is:
optimize the complete sensor-to-decision pipeline rather than only the neural-network kernel.
A well-designed compact model on a modest MCU can outperform a poorly mapped larger model on much more expensive hardware.