School/✦ Research Engineer/Week 23
Fitting a model into memory, time and energy
A smartwatch must recognise a gesture in 20 ms, using under 1 mJ, with 64 KB of RAM. Can your model do it? Work it out before you build it.
About 50 min · 3 goals · Checked by automated tests
1.Why should I care?
Embedded ML projects fail most often on budgets, not on accuracy. A few lines of arithmetic, done early, decide whether a design is possible and which knob to turn. This is the habit research engineers bring to any resource-constrained system.
- ◇ Build a budget table for an on-device model: flash, RAM, latency and energy per inference.
- ◇ Estimate energy per inference from MAC count and memory traffic, and explain why data movement often dominates.
- ◇ Choose between shrinking the model, lowering precision and changing hardware with evidence.
2.Predict
On a typical small chip, which costs more energy: one 8-bit MAC, or reading one byte from off-chip DRAM?
Guess first. Being wrong here is how the surprise works.
3.Understand
The budget table
Write one row per resource: flash (weights + code), RAM (peak activations + buffers + stack), latency (MACs ÷ MAC rate + overheads) and energy (MAC energy + memory energy + idle energy). Each has a limit from the product requirement. The design is feasible only if every row fits, with a margin.
Energy per inference
E ≈ MACs × e_MAC + bytes moved × e_byte + P_idle × time. Because e_byte can be tens to hundreds of times e_MAC (depending on which memory), a model with fewer weight reads can beat one with fewer MACs. Reusing data already in registers or SRAM is the key trick of efficient accelerators.
Which knob?
Too big for flash: quantise or prune. Too big for RAM: process in smaller tiles, or choose layers with smaller activations. Too slow: fewer MACs, SIMD instructions or an accelerator. Too much energy: reduce data movement, then MACs, then duty-cycle the device. Each change must be re-checked against accuracy on held-out data.
Explain it another way
Packing for a flight with a weight limit, a size limit and a time limit at security. Every item must pass all three, and the heavy boots matter more than the socks.
Roofline thinking: performance is limited either by compute (MACs per second) or by memory bandwidth (bytes per second × operations per byte). Arithmetic intensity, MACs per byte moved, tells you which.
Example: 2×10⁶ MACs at 1 pJ = 2 µJ; 200 KB of weights read from flash at 20 pJ/byte = 4 µJ; total 6 µJ. Quantising to 4 bits halves the weight bytes: 2 + 2 = 4 µJ, a 33% saving with no change in MACs.
Always-on audio wake-word detectors often run a tiny first-stage model continuously and wake a larger model (or the main processor) only when it fires: a cascade that keeps average energy low.
Worked example 1
Budget check: 32,128 MACs and 32 KB of int8 weights; 1 MAC/cycle at 48 MHz; 3 pJ/MAC; 25 pJ/byte from flash. Latency and energy?
Latency = 32,128/48×10⁶ ≈ 0.67 ms. Energy = 32,128×3 pJ + 32,768×25 pJ ≈ 0.096 µJ + 0.82 µJ ≈ 0.92 µJ: memory dominates by about 8×.
Common mix-ups
- Fewer MACs always means less energy.
✓ Memory traffic often costs more than arithmetic; a model with more MACs but more data reuse can use less energy. - If it fits in flash, it fits.
✓ RAM for activations, latency and energy are separate budgets that must each be met.
4.Discover
In the Code Lab, write a function that takes a list of layer sizes and returns MACs, weight bytes (at a chosen bit width), peak activation bytes and energy per inference for given pJ/MAC and pJ/byte. Test it on the network from the worked example, then search for the largest hidden layer that fits a 1 mJ, 64 KB RAM budget.
Can't use the interactive? Read a description instead
A budget table has four rows: flash (weights and code), RAM (peak activations), latency (MACs ÷ MAC rate) and energy (MACs × energy per MAC + bytes × energy per byte). The worked example needs 0.67 ms and 0.92 µJ, mostly from reading weights.
5.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
One inference performs 1000000 MACs at 0.2 pJ each and reads 50000 bytes from memory at 100 pJ per byte. What is the energy per inference in microjoules (µJ)?
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
One inference performs 50000 MACs at 3 pJ each and reads 10000 bytes from memory at 5 pJ per byte. What is the energy per inference in microjoules (µJ)?
Practice 3 · a fresh version every time
A network has two fully connected layers: 64 inputs → 32 hidden units → 4 outputs. How many multiply-accumulate operations (MACs) does one inference need (ignore biases and activations)?
Practice 4
Your model's weights fit in flash, but one layer's input and output activations together exceed RAM. What is the most direct fix?
Practice 5
A 200 mAh, 3 V coin cell stores about 2,160 J. If each inference costs 5 µJ and nothing else used energy, how many inferences is that? (Answer in millions.)
7.Apply it
Write a one-page feasibility study for an on-device classifier of your choice: requirement, budget table with every assumption and its source, the calculation, and a recommendation. Mark every number you have not measured as an estimate.
8.Reflect
Concepts: Embedded machine learning · Electrical power and energy budgets · Real-time computing on robots