MobileNetV2 needs 1,372 kB of working memory. A tiny chip has 256 kB. MCUNet cuts the need to 172 kB, 8 times less, by running the first layers one patch at a time.
Learn why deep learning does not fit on a microcontroller, how to count the memory it needs, and how TinyNAS and patch-based inference make it fit.
Pick a chip. Switch from whole layers to patches, then drag how many early blocks run patch by patch. Watch the tallest bar fall under the chip line. Then we build, piece by piece, the memory count, the search, the patches and the three tiny applications.
You need the idea of a convolution layer (a small filter slides over an image) and a little Python for the code. We build the rest from zero.
Based on Lecture 10 of MIT 6.5940 TinyML and Efficient Deep Learning Computing (Fall 2026), taught by Song Han and the MIT HAN Lab. The lecture slides are on the course page. Every idea is re-taught here in our own words and devices. It follows Lecture 9, Knowledge Distillation and leads into Lecture 11, TinyEngine.
Bars from MobileNetV2's layer sizes at 224 by 224, int8, 1 kB = 1,024 bytes: its 1,372 kB peak and its 172 kB result. The 256 kB and 320 kB chips are the lecture's boards, and 512 kB is for contrast. A patch bar is the block's memory divided by the patch count, a simplified view that skips the held output. The lecture shows about 10 percent more MACs for the full patch stage. Fewer blocks scale that down in a straight line, which is our guess.
Chapter 0
See why billions of tiny chips want deep learning, and why a model that fits a phone cannot fit them
A farmer clips a two dollar sensor board to a water pump. It runs on a coin battery for a year. Its job is to hear the pump rattle and send one message. Can that board run a neural network?
Cloud AI sends your data to GPUs or TPUs in a data center. Mobile AI runs the model on your phone. Tiny AI runs it on IoT devices and microcontrollers.
An IoT device (Internet of Things) is a small gadget with a sensor and a network link. A microcontroller (MCU) is a whole computer on one cheap chip, often with no operating system. The lecture's reasons to go tiny:
Two kinds of memory matter. Memory is the desk: fast working space. On a chip it is SRAM. Storage is the bookshelf: slow and large, and it keeps its contents with the power off. On a chip it is Flash.
| Tier | Memory (desk) | Storage (bookshelf) |
|---|---|---|
| Cloud AI | 32 GB | terabytes to petabytes |
| Mobile AI | 4 GB | 256 GB |
| Tiny AI | 320 kB | 1 MB |
Divide to see the gap. 32 GB is 32,000,000 kB, and 32,000,000 / 320 = 100,000. A phone gives 4,000,000 / 320 = 12,500, which the lecture rounds to 13,000 times.
Each bar is the storage of one tier, on a log scale. The vertical line is the size of the model's weights. A bar that reaches the line can hold the model.
Tier sizes from the lecture; cloud storage is drawn as 1 TB. GPT-3 and BERT sizes come from the lecture's growth chart, the others are the usual published counts. Weight size is parameters times bits / 8, weights only.
MobileNetV2 in int8 has 3.4 MB of weights. A phone holds that easily. The tiny chip's 1 MB of Flash is 3.4 times too small, and MobileNetV2 was built to be small. The lecture's rule: reduce both the weights and the activations. Chapter 1 defines activations.
Mobile AI fights two limits: latency and energy. Tiny AI adds a third, memory, and all three bind at once. A fast, frugal model still fails if it does not fit.
Chapter 1
Add up the stored model and the working memory of every layer, and find the one layer that sets the limit
Your tiny image model has 5 kB of weights. You load it on a chip with 256 kB of SRAM, and the program crashes. The weights are not the problem.
Weights are the numbers in the convolution kernels, the small filters that slide over the image. Training sets them, and they never change at run time, like a recipe. Activations are the grids that flow between layers, new for every input, like dough. Weights sit in Flash. Activations sit in SRAM.
The lecture's count skips temporary buffers and code size. Call the bytes of the input activation of layer l inl and of its output outl. Each is height × width × channels × bytes per number.
Peak SRAM equals the max over l of in-l plus out-l.
For each layer l, add in-l and out-l. Keep the largest sum. That is the peak SRAM.
Below, layer 3 has in-3 = 221,184 B and out-3 = 55,296 B. The sum, 276,480 B, is the largest, so the peak is about 276 kB: over a 256 kB chip.
Flash is static: the chip must hold the whole model. Call the number of weights in layer l paramsl and the bytes per weight b.
Flash equals the sum over l of params-l times b.
Count the weights params-l of every layer, add them, and multiply by b.
The four layers below have 432, 1,536, 864 and 2,304 weights. With b = 1, Flash = 5,136 B, about 5 kB. The peak SRAM is 54 times larger.
Take a 96 × 96 input with 3 colour channels in int8, then four layers. Here 1 kB is 1,000 bytes.
pythondef memory(layers, bytes_per=1): # layers: [(in_shape, out_shape, n_weights), ...] size = lambda s: s[0] * s[1] * s[2] * bytes_per # H x W x C numbers sram = [size(i) + size(o) for i, o, _ in layers] # per layer: input + output flash = sum(w for _, _, w in layers) * bytes_per # static: every weight return flash, max(sram), sram.index(max(sram)) # Flash, peak SRAM, worst layer
Seven bars, one per layer: input activation (light) plus output (dark). The dashed line is the chip's SRAM, and a bar over it turns red.
A toy network made for this lesson: a 3×3 stride 2 stem, then expand, depthwise and project with expansion 6, int8.
Width 0.50 halves the channels, and the network fits. Doubling the input size quadruples the activations, so resolution is the strongest knob.
| Model | Peak SRAM over a 320 kB chip |
|---|---|
| ResNet-50 | 23 times |
| MobileNetV2 | 22 times |
| MobileNetV2, int8 | 5 times |
At about 70 percent ImageNet accuracy, MobileNetV2-0.75 has 4.6 times fewer parameters than ResNet-18, but only 1.8 times less peak activation. MCUNet cuts both: 6.1 and 3.4 times.
Symptom: the model file is small, and the board still resets. Measure input plus output per layer: layer 3 needs 276 kB of a 256 kB chip. Fix: shrink the activations (a smaller input, narrower early layers), or change how the layers run.
Chapter 2
Compare two one-sided fixes with the MCUNet loop, where the network and the runtime shape each other
You must furnish a small room. You can buy a smaller sofa, or rearrange the room so the old sofa fits. Often neither works alone. The lecture calls choosing both together system-algorithm co-design: the sofa is the network, and the room is the software that runs it.
Every network needs a library, the code that runs each layer on the chip. On a 256 kB chip they can take a third of the memory:
Fix (a) is neural architecture search (NAS): a program searches for a network on top of a fixed library. Fix (b) tunes the library for a fixed network.
| Approach | Fixed | Changed | Example | Limit |
|---|---|---|---|---|
| (a) Search the model | Library | Network | ProxylessNAS, MnasNet | Can only shrink the network until it squeezes in |
| (b) Tune the library | Network | Library | TVM, a compiler | Cannot shrink an activation that is too large |
| (c) MCUNet | Nothing | Both | TinyNAS with TinyEngine | Each side is shaped by the other |
TinyNAS designs the network for the chip's memory. TinyEngine compiles and runs it, and optimizes its code for that network. If the engine saves memory, the search can pick a wider network, and the design improves in turns. This lecture covers TinyNAS. Lecture 11 covers TinyEngine.
Call the model's peak SRAM from Chapter 1 peakmodel. Call the buffers and runtime data that the library adds overheadlibrary, and the chip's SRAM SRAMchip.
Peak-model plus overhead-library is at most SRAM-chip.
Add overhead-library to peak-model. If the sum is at most SRAM-chip, the model runs.
With made-up numbers on a 256 kB chip: a generic library takes 100 kB, so peak-model can be 156 kB. A lean library takes 30 kB and leaves 226 kB, 70 kB more for the model.
The tall bar is the chip. The yellow part is the library. The blue part is the model's peak. Pick an approach, then drag the sliders that approach can change.
Illustrative toy numbers, not from the lecture: libraries of 20 to 100 kB, a fixed 300 kB model for (b), and accuracy 45 + 17 log10(peak / 20) percent.
In (a) the library stays at 100 kB, so the model stops near 155 kB and 60 percent. In (b) the model stays at 300 kB, and even a 20 kB library makes 320 kB: nothing fits. In (c), 20 kB plus 235 kB reaches about 63.
Symptom: your count says 240 kB on a 256 kB chip, but the board runs out of memory. Measure the library's buffers: 40 kB, so the true need is 280 kB, 24 kB over. Fix: put the overhead into the search's memory limit, or make the engine leaner. TinyNAS and TinyEngine do both.
Chapter 3
Pick the input size and width that give the best models under your memory limit, before you search at all
With one hour to buy shoes, the shop you pick matters more than the shoe. In NAS the shop is the search space, the set of networks the search may look at. Its quality largely decides the quality of the model found.
Reuse a mobile space such as MnasNet, and even its smallest network cannot fit: a phone has 4 GB, a tiny chip 320 kB. The lecture builds a space for the chip instead.
It scales a MobileNet-like space with two numbers. Resolution R is the side of the input image in pixels. Width multiplier W scales the channels of every layer. Each pair (R, W) is one sub-search space:
Kernel sizes, expansion ratios and blocks per stage stay open for the search in Chapter 4.
Training every network would take years. The lecture's proxy: more FLOPs (here, multiply-adds) means more capacity, so higher accuracy is likely. Call one sampled network n, and the networks of a space that fit both chip limits fit(R, W). Among them, take p80, the FLOPs value that 80 percent stay at or under.
Score of R, W equals p-eighty of FLOPs of n, over every n in fit of R, W.
Sample networks n from the space. Drop any that break SRAM or Flash. Sort the rest by FLOPs of n and read the value 80 percent of the way up.
With made-up numbers, 10 networks fit: 12, 15, 18, 21, 24, 27, 31, 33, 38 and 52 M FLOPs. Eight of ten are at or below 33, so the score is 33 M.
pythondef space_score(nets, sram_kb, flash_kb): # nets: sampled networks of one (R, W) space fit = sorted(n.flops for n in nets if n.peak_sram <= sram_kb * 1000 and n.flash <= flash_kb * 1000) if len(fit) < 15: return 0 # too few fit: a bad space return fit[max(0, int(len(fit) * 0.8 + 0.999) - 1)] # p80
Top: one cell per space, shaded by the p80 of 300 sampled networks, in M FLOPs. A cross means too few fit. Green outlines the best. Bottom: the curves of your tapped space (teal) and the best (green).
A toy made for this lesson: six stages, depth 1 to 4, expansion 3, 4 or 6, kernel 3, 5 or 7, int8, one fixed random seed. The lecture's curves use far more networks.
Slide the SRAM down: the best resolution drops, because big images make big activations. Slide the Flash up: the best width grows, because wide layers carry more weights. On the lecture's grid, more Flash favours more channels and a smaller resolution. More SRAM favours a larger resolution.
Symptom: the search ends with a mediocre model, or none fits. Measure p80. In the lecture, w0.3-r160 reaches 32.3 M FLOPs, and its best model 74.2 percent. w0.5-r144 reaches 50.3 M and 78.7 percent. Fix: score the spaces with no training, then search the best one.
Chapter 4
Train one super network, sample sub-networks, reject any that break the memory limit, and keep the rest
A tailor cuts every suit from one bolt of cloth. TinyNAS trains one large network once, then cuts many smaller ones out of it. The lecture calls this stage resource-constrained model specialization, done by one-shot NAS with weight sharing.
The super network is the biggest network in the space. Every candidate is a sub-network nested inside it, reusing part of its weights:
Training samples sub-networks at random and fine-tunes them together into the same weights. Lecture 8 covers this as Once-for-All.
Memory follows from shapes, so the chip limits become a hard rule. For each sample, compute peak SRAM and Flash with Chapter 1's count, and reject any that break a limit. Rank the survivors by accuracy with the borrowed weights. That score is an estimate, but it ranks candidates well enough.
One block, by hand: 16 channels at 48×48, expanded 6 times, a 5×5 depthwise filter with stride 2, then a projection to 24 channels.
With expansion 3 the block has 48 channels. Its sums are 147,456, 138,240 and 41,472 B, so its peak is 147,456 B and it survives.
pythondef specialize(supernet, sram, flash, val_set, n=1000): best = None for _ in range(n): net = supernet.sample() # kernel, expansion, depth per stage if net.peak_sram() > sram or net.flash() > flash: continue # reject from shapes alone, no run acc = evaluate(net, val_set) # uses the shared weights if best is None or acc > best[0]: best = (acc, net) return best
Top: every dot is a sub-network, placed by peak SRAM (across) and Flash (up), both on a log scale. Bottom: the memory of each block of the network you picked. The dashed line is the chip's SRAM.
The toy family of Chapter 3, not the lecture's space. FLOPs stand in for accuracy, since nothing is trained here.
At width 0.5 and size 144 on the 256 kB chip, about a quarter of the samples fit. A larger input slides the dots right. A wider network moves them up.
TinyNAS designs networks with more uniform peak memory per block. In the first two stages, MobileNetV2's tallest block is 2.2 times its average, and TinyNAS brings that to 1.6 times. No block spikes, so a larger model fits.
On Visual Wake Words (is a person in the picture?), MCUNet beats MobileNetV2 and ProxylessNAS in accuracy. It runs 2.4 and 3.4 times faster and needs 3.7 times less peak SRAM. One baseline runs out of memory on the 256 kB chip.
Symptom: the most accurate network, once exported, does not run. Measure: its widest block peaks at 276,480 B, 20,480 B over a 256,000 B chip. Fix: filter every candidate from shapes first, then rank only the survivors.
Chapter 5
Plot the memory of every block and see that one early block is 8 times too large while the tail is fine
A car park must hold the noon rush, not the quiet hours. Working memory is the same: only the peak counts. MCUNetV2, the second half of the lecture, asks where the noon hour of MobileNetV2 is.
A block here is one inverted residual block: three layers that expand the channels, filter them and project them back. MobileNetV2 has a first convolution (block 0) and 17 blocks after it.
The lecture's chart: block 2 needs 1,372 kB, its neighbours 500 to 600 kB, and every block from 5 on under 200 kB. On a 256 kB chip the first five cannot fit and the rest can. The lecture calls this the imbalanced memory distribution of CNNs: cut the first stage, and you cut the whole peak.
Call the grid's height and width H and W, the input channels Cin, and the channels after the expand step Cexp. Each stride 2 step cuts H × W to a quarter, while the channels grow only 1.5 to 2 times.
Mem is about H times W, times C-in plus C-exp.
Count the grid positions, H times W. Multiply by C-in plus C-exp. At one byte per number, that is mem.
Block 2 of MobileNetV2: H = W = 112, C-in = 16, C-exp = 96. So 12,544 × 112 = 1,404,928 B. The lecture counts 1 kB as 1,024 B, so mem = 1,372 kB.
| Block | Grid | Channels | Input plus output | Memory |
|---|---|---|---|---|
| 2 | 112 × 112 | 16 to 96 | 200,704 + 1,204,224 = 1,404,928 B | 1,372 kB |
| 3 | 56 × 56 | 24 to 144 | 75,264 + 451,584 = 526,848 B | 515 kB |
| 5 | 28 × 28 | 32 to 192 | 25,088 + 150,528 = 175,616 B | 172 kB |
| 8 | 14 × 14 | 64 to 384 | 12,544 + 75,264 = 87,808 B | 86 kB |
Each row has a grid 4 times smaller and channels 1.5 to 2 times larger, so memory falls 2 to 3 times per row. The tail is not the enemy.
Each bar is one block. Bars over the dashed chip line turn red. The outlined bar is the block you chose with the block slider.
From MobileNetV2's published layer sizes, int8, 1 kB = 1,024 bytes. At 224 it gives the lecture's 1,372 kB peak and the 172 kB plateau at blocks 5 to 7. Accuracy is not modelled.
Drop the input to 96 × 96 and the whole network fits. That is not free: a smaller picture holds less detail, and accuracy falls. Chapter 8 returns to this cost.
Symptom: you halve the channels and quantize to int8, and the model still does not fit. Measure block 2: 112×112×(8 + 48) = 702,464 B, about 686 kB, still 2.7 times the chip. Fix: aim at the block that sets the peak, and change how that stage runs.
Chapter 6
Cut the image into tiles, push one tile through several layers, and watch the working memory fall
To copy a giant poster on a letter-size copier, you cut it into tiles and copy one at a time. MCUNetV2's patch-based inference does this for the first stage only. It cuts the input into patches (tiles) and runs each one through several layers before the next.
Per-layer inference computes all of layer 1, keeps its full output, then computes layer 2. While layer 2 runs, a full input and a full output sit in SRAM together.
Per-patch inference swaps the loops. One tile goes through conv 1 and conv 2, and its small result goes into an output buffer. The big tensors in between never exist. The stage output must stay whole, since later layers need all of it. The lecture notes it is much smaller than the earlier layers.
Two layers on a 96×96×3 int8 input. Conv 1 is 3×3, stride 1, 3 to 16 channels. Conv 2 is 3×3, stride 2, 16 to 24 channels.
Call the bytes of the whole stage output outstage. For one tile, call the input and output of layer l tile-inl and tile-outl.
Peak-patch equals out-stage plus the max over l of tile-in-l plus tile-out-l.
Run Chapter 1's count on one tile, not the whole image. Then add out-stage, which stays in SRAM the whole time.
From the steps above: out-stage = 55,296 B. The worst tile layer is conv 2, 17,424 + 6,144 = 23,568 B. So peak-patch = 78,864 B, about 79 kB, 2.6 times less than 203 kB.
pythondef patch_stage(x, layers, n, halo): # x: (H, W, C) input; layers: functions on a tile H = x.shape[0]; s = H // n # each patch covers s x s input pixels out = None # the one big buffer we must hold for i in range(n): for j in range(n): r0, r1 = max(0, i*s - halo), min(H, (i+1)*s + halo) # tile plus its halo c0, c1 = max(0, j*s - halo), min(H, (j+1)*s + halo) t = x[r0:r1, c0:c1] for f in layers: t = f(t) # tile through every layer of the stage out = write_tile(out, t, i, j, n) # crop the halo away, place the result return out # same tensor as per-layer inference
Left: the input in patches. Purple runs now, and yellow is the border it borrows. Right: peak memory, whole against per patch.
The toy two-layer stage above, exact for inner patches. Border patches need slightly less halo. The real MCUNetV2 stage has several blocks.
At 1 × 1 nothing changes: 203 kB. At 2 × 2 the peak is 108 kB, at 3 × 3 it is 79 kB, and at 6 × 6 it is 61 kB. The bars flatten toward the 55 kB output floor.
For MobileNetV2, patching the first stage cuts the peak from 1,372 kB to 172 kB, 8 times less. On an STM32F746, with TinyEngine as the baseline, peak SRAM in kB:
| Model | Per layer | 2 × 2 patches | 3 × 3 patches | Reduction |
|---|---|---|---|---|
| MobileNetV2 (w0.5, r144) | 315 | 113 | 64 | 4.9 times |
| MobileNetV2-RD (w0.5, r144) | 300 | 94 | 51 | 5.9 times |
| FBNet-A (w0.45, r144) | 310 | 132 | 76 | 4.1 times |
| MCUNet (w1.0, r144) | 234 | 85 | 56 | 4.2 times |
"RD" means redistributed (Chapter 7).
Symptom: you add patches, and the peak stops falling while the run slows. Measure: in the toy, 4 × 4 to 6 × 6 lowers the peak only from 68.8 to 61.5 kB. Extra MACs rise from 2.8 to 4.3 percent. Fix: use the fewest patches that fit the chip.
Chapter 7
Measure how tile overlap grows with the receptive field, and shrink it by moving depth later
A convolution at a patch's edge needs pixels from the next patch. That borrowed border is the halo. Both neighbours compute it, so they repeat that work.
The receptive field is the area of the input that one output value depends on, like the circle of a torch beam. The lecture's rule: the overlap grows as the receptive field grows.
Walk backward from the output. For one layer, call the width of the output tile tout, the stride s and the kernel size k. The input tile width tin is:
T-in equals t-out minus one, times s, plus k.
Step back t-out minus one strides of size s, then add one kernel width k. Repeat for each layer, back to the input.
The lecture's pair, conv 3×3 stride 1 then conv 3×3 stride 2, with 12 outputs per side. Conv 2: (12 − 1) × 2 + 3 = 25. Conv 1: (25 − 1) × 1 + 3 = 27. A perfect split needs 24, so the halo is 3 pixels.
So conv 1 computes 25 × 25 = 625 outputs per patch, where a perfect split needs 24 × 24 = 576: 8.5 percent extra work. Each added layer widens the halo, and the early layers pay most.
pythondef halo_overhead(kernels, strides, chans, F, n): # chans has one more entry than kernels L = len(kernels); t = [0] * (L + 1); f = [0] * (L + 1) t[L], f[L] = F // n, F # patch tile and full map at the stage output for l in range(L, 0, -1): # walk backward to the input t[l-1] = (t[l] - 1) * strides[l-1] + kernels[l-1] f[l-1] = f[l] * strides[l-1] macs = lambda l, size: size**2 * kernels[l-1]**2 * chans[l-1] * chans[l] patched = n*n * sum(macs(l, t[l]) for l in range(1, L+1)) exact = sum(macs(l, f[l]) for l in range(1, L+1)) return patched / exact - 1 # extra work as a fraction
Left: one patch's input, drawn to scale. Purple is the tile's own area, yellow is the halo it borrows. Right: extra work in each layer of the stage, as a percent over a perfect split.
A toy stage made for this lesson with plain convolutions. Real MobileNet blocks use cheap depthwise layers, so the lecture's overhead is about 10 percent. Redistribute makes the first kernel 1×1.
With 3×3 kernels, 4 layers and 2 × 2 patches, the extra work is about 22 percent. With 7×7 kernels it is about 79 percent. Back at 3×3, turn on redistribute: the overhead falls to 15 percent.
The lecture's fix is network redistribution: move receptive field out of the patch stage and into the later per-layer stage. That stage runs on small grids with no tiles, so extra receptive field there costs no halo.
MobileNetV2 does 301 M MACs, and 330 M when run per patch, 10 percent more. MobileNetV2-RD is back to 301 M, with the same performance on classification and detection. Its first patch block drops to a 1×1 kernel, and later blocks gain receptive field.
Symptom: memory fits, but each prediction is much slower. Measure: in the toy, 5 layers of 5×5 kernels on a 3 × 3 grid do about 117 percent extra work. Fix: keep the patch stage shallow with small kernels, and move receptive field later.
Chapter 8
Read the results on three tasks, then read what the winning architecture does in each of its stages
MCUNet is TinyNAS plus TinyEngine. MCUNetV2 adds patch-based inference, redistribution, and a joint search over two groups of knobs:
The two shape each other. So the search samples a network with its patch plan, computes peak SRAM from shapes, rejects pairs over the limit, and keeps the most accurate.
Each panel fixes the chip and changes only the method. ImageNet and VOC use int8. Visual Wake Words (VWW) asks if a person is in the picture. mAP (mean average precision) scores detection, and higher is better.
| Task and chip | Baselines | MCUNet | MCUNetV2 |
|---|---|---|---|
| VWW, peak SRAM at 90 percent accuracy (lower is better) | none shown | 119 kB | 30 kB, 4 times less: fits a 32 kB chip |
| ImageNet top-1, STM32F412 (256 kB) | MobileNetV2 0.35: 49.0; ProxylessNAS 0.3: 56.2 | 60.3 | 64.9 (+4.6) |
| ImageNet top-1, STM32F746 (320 kB) | none shown | 68.5 | 71.8 (+3.3) |
| Pascal VOC mAP, STM32F746 (320 kB) | MobileNetV2 with CMSIS: 31.6 | 51.4 | 68.3 (+16.9) |
Detection gains most because it needs a larger input, and patches let the network read one in the same memory. Classification suffers less from a small picture, so its gains are smaller.
Pick a task. Each bar is one method on that chip. The purple bar is MCUNetV2. Compare bars inside one panel only, since each panel has a different chip or metric.
Figures from the lecture's result slides. The ImageNet bars start at 40 percent and the VOC bars at 20 mAP, as in the lecture's charts, so read the numbers, not the lengths.
The lecture opens a network that the joint search found for VWW. It runs per patch on grids of 160, 80, 40 and 20 positions per side, then per layer on 10 and 5. MB4_5×5 means expansion 4 and a 5×5 kernel.
| Where | What the search chose | Why |
|---|---|---|
| Patch stage | Small kernels | Small kernels keep the halo small, so tiles overlap less (Chapter 7) |
| Middle stage | Small expansion ratios | The expand step is where memory spikes, so a small ratio lowers the peak (Chapter 5) |
| Later stage | Large expansion ratios | The grids are small there, so a big ratio is cheap in memory and adds accuracy |
| Input | A larger resolution for VWW | VWW is resolution sensitive. MCUNet used 128×128 |
Nobody wrote these rules into the search. The memory limit and the halo cost taught them.
Symptom: someone says MCUNet beats MCUNetV2, 68.5 against 64.9. Measure: 68.5 is on the 320 kB chip, and 64.9 on the 256 kB chip, where MCUNet scores 60.3. Fix: compare inside one panel, with the same chip and the same metric.
Chapter 9
Wake a big model with a tiny one, detect at higher resolution, and spot a keyword in sound
A video doorbell watches an empty hallway all day. A large face recognition model would drain its battery. So a tiny model watches first and wakes the large one when a person appears.
With int4 quantization (4 bits per number), MCUNet's ImageNet top-1 accuracy rises with chip memory:
| Chip | SRAM / Flash | Top-1 |
|---|---|---|
| STM32F412 | 256 kB / 1 MB | 62.0 |
| STM32F746 | 320 kB / 1 MB | 63.5 |
| STM32F765 | 512 kB / 1 MB | 65.9 |
| STM32H743 | 512 kB / 2 MB | 70.7, the first above 70 percent on a commercial MCU |
| MobileNetV2 with CMSIS-NN, for contrast | 512 kB / 2 MB | 53.8 |
VWW is the vision version of "Hey Siri". The tiny model asks each camera frame one question: is a person here? Only then does the much larger face recognition model wake.
Detection is more sensitive to input resolution, because it makes a denser prediction. On the lecture's plot, VOC accuracy falls faster than ImageNet accuracy as the input shrinks to 160. Patches fit a larger input in the same memory, so face and person detection run on small boards.
Keyword spotting, the audio guard that hears "Hey Siri", runs in three steps. First, cut the sound into overlapping frames. Call the clip length L, the frame length l and the stride s, the gap between frame starts. The number of frames T is:
T equals L minus small l, over s, plus one.
Take one frame length l from the clip length L. Count how many strides s fit in the rest. Add one for the first frame.
A 1,000 ms clip with 40 ms frames and a 20 ms stride: T = (1000 − 40) / 20 + 1 = 49 frames.
Second, turn each frame into spectral coefficients: the energy in each band of pitch, a step designed by hand. With 10 bands the grid is 49 × 10 = 490 numbers. Third, a network gives each word a probability, such as 0.91 for "Yes".
Top: a made-up spoken word, with your frame in purple. Bottom: the feature grid, one column per frame, one row per pitch band.
A synthetic sound made for this lesson: two gliding tones from 0.30 s to 0.70 s, read by a Goertzel filter at 10 centres. Real spotters use MFCC features with the same frame logic.
A Google study the lecture cites: speech grids have strong links in time and pitch, which a dense network ignores. A CNN slides one filter over the grid, so it also handles speech that shifts with speaking style. It wins at matched multiplies. On Speech Commands, MCUNet's hardware-aware search gives 2 percent higher accuracy, 2.8 times faster inference and 4.1 times less peak memory.
Symptom: keyword spotting works, but the battery drains. Measure: with a 5 ms stride, T = (1000 − 40) / 5 + 1 = 193 frames, 3.9 times the work of a 20 ms stride. Fix: use a stride near half the frame length.
Chapter 10
Train a small autoencoder on normal data only, then flag any input it rebuilds badly
A factory fan's bearing wears, and its hum changes. There are no labels, only hours of healthy hum. Teach the chip what normal looks like and flag the rest. That is anomaly detection, used in surveillance, health care and fraud prevention.
An autoencoder is a network that predicts its own input through a narrow middle. The encoder squeezes the input into a short code vector. The decoder rebuilds the input from it, like an artist who draws a face from five words. The lecture lists three properties:
In a tiny world every normal window has four equal samples, so one code number is enough. The direction is v = (1, 1, 1, 1) / 2. Encoding is a dot product, and decoding multiplies the code by v.
Train on normal data only. Call the live window x, with D samples xi, and its rebuild x′i. The reconstruction error e is:
E equals the square root of: the sum over i of x-i minus x-prime-i, squared, over D.
Subtract each x-prime-i from x-i and square it. Average over the D samples and take the root. If e passes the threshold, flag an anomaly.
Normal: squares 0.01, 0.01, 0, 0, so e = √(0.02 / 4) = 0.07. Odd: squares 1.89, 1.27, 0.77, 1.27, so e = √(5.19 / 4) = 1.14. With a made-up threshold of 0.3, the first passes and the second raises the alarm.
pythondef train(normal, k): # normal: (N, D) windows of healthy data C = normal.T @ normal / len(normal) # (D, D) covariance _, vecs = np.linalg.eigh(C) return vecs[:, -k:] # (D, k): the k directions that matter def score(x, V): # x: (D,) one live window code = V.T @ x # encoder: (k,) numbers xhat = V @ code # decoder: (D,) rebuilt window return np.sqrt(np.mean((x - xhat) ** 2)) # reconstruction error alarm = score(window, V) > threshold # one number against one number
Top: a window (blue) and its rebuild (purple). Bottom: errors of 100 healthy (blue) and 100 faulty (warm) windows against your threshold. Red bars are mistakes.
Synthetic data: 200 healthy windows of 24 samples, 3 cycles, random size and phase, noise 0.05. Faults: 4.5 cycles, three spikes, or an added 6 cycle wave of 0.35 or 0.12. The model is a linear autoencoder, which equals PCA.
At code size 2 and threshold 0.08 with the faint hum, no healthy window alarms and 97 of 100 hums are caught. A healthy window has two degrees of freedom, size and phase.
The lecture's fan runs on an Arduino Nano 33 BLE Sense: 256 kB SRAM, 1 MB Flash, a Cortex-M4 at 64 MHz. A 24 to 2 to 24 autoencoder has 24×2 + 2 + 2×24 + 24 = 122 weights, 122 bytes in int8. K-means and Gaussian mixture models are other common choices.
Symptom: the detector stays quiet when the fan is faulty. Measure: at code size 12 and threshold 0.08, only 4 of 100 faint hums are caught, because the big code rebuilds the fault too. Fix: keep the code small, and set the threshold just above the largest healthy error.
Chapter 11
Keep the whole lecture on one page, then take the memory count with you
On a tiny chip, the working memory of the worst layer sets the limit. MCUNet designs the network and the engine together. TinyNAS scores spaces by p80, then searches with shared weights and a memory filter. MCUNetV2 runs the first blocks patch by patch, and redistribution keeps the halo cheap.
| Number | What it is | Where |
|---|---|---|
| 32 GB, 4 GB, 320 kB | Memory of cloud, phone and tiny chip. 100,000 and 13,000 times smaller | 0 |
| 256 kB, 320 kB | SRAM of the Arduino Nano 33 BLE Sense and the STM32 F746ZG, each with 1 MB Flash | 1 |
| 23x, 22x, 5x | Peak SRAM over a 320 kB chip: ResNet-50, MobileNetV2, MobileNetV2 int8 | 1 |
| 4.6x and 1.8x; 6.1x and 3.4x | Smaller parameters and peak activation: MobileNetV2-0.75, then MCUNet, against ResNet-18 | 1 |
| 32.3 M and 50.3 M FLOPs | p80 of a bad space (w0.3-r160) and a good space (w0.5-r144). Best accuracy 74.2 and 78.7 percent | 3 |
| 2.2x to 1.6x | Tallest block over the average, first two stages: MobileNetV2 to TinyNAS | 4 |
| 1,372 kB to 172 kB | MobileNetV2 peak SRAM, per layer to per patch. 8 times less | 5, 6 |
| 4.1x to 5.9x | Peak SRAM reduction from patches on four models, STM32F746 | 6 |
| 301 M, 330 M, 301 M MACs | MobileNetV2, with patches, then redistributed | 7 |
| 119 kB to 30 kB | VWW peak SRAM at 90 percent accuracy, MCUNet to MCUNetV2 | 8 |
| 64.9, 71.8, 68.3 | MCUNetV2: ImageNet on 256 kB, ImageNet on 320 kB, VOC mAP on 320 kB | 8 |
| 70.7 percent | First ImageNet top-1 above 70 percent on a commercial MCU (int4) | 9 |
| T = (L − l) / s + 1 | Frames in a keyword spotting clip | 9 |
| Term | Meaning | Ch |
|---|---|---|
| SRAM, Flash | The working memory (desk) and the storage (bookshelf) of a chip | 0, 1 |
| Peak SRAM | The largest input plus output activation over all layers | 1 |
| Co-design | Shape the network and the runtime together | 2 |
| Search space, p80 | The set of networks searched, scored by the 80th percentile FLOPs of the fitting ones | 3 |
| Super network, weight sharing | One large network whose weights every sub-network reuses | 4 |
| Imbalanced memory | The early blocks use far more SRAM than the later ones | 5 |
| Patch-based inference | Run the first stage tile by tile, then hold only the stage output | 6 |
| Halo, receptive field | The border a tile borrows, set by how far one output looks | 7 |
| Redistribution | Move receptive field from the patch stage to the later stage | 7 |
| Autoencoder, code vector | A network that rebuilds its input through a narrow middle | 10 |
The lecture's formulas fit in a few lines of Python. Shapes are in the comments.
pythonimport numpy as np def memory(layers, b=1): # layers: [(in_shape, out_shape, n_weights)] sz = lambda s: s[0] * s[1] * s[2] * b sram = [sz(i) + sz(o) for i, o, _ in layers] return sum(w for _, _, w in layers) * b, max(sram) # Flash, peak SRAM def space_score(nets, sram, flash): # p80 FLOPs among networks that fit fit = sorted(n.flops for n in nets if n.peak <= sram and n.flash <= flash) return fit[max(0, int(len(fit) * 0.8 + 0.999) - 1)] if len(fit) >= 15 else 0 def tile_in(t_out, k, s): # input tile for an output tile: (t - 1) * s + k return (t_out - 1) * s + k def patch_peak(out_bytes, tile_in_bytes, tile_out_bytes): return out_bytes + tile_in_bytes + tile_out_bytes # held output + one tile's working memory def n_frames(L, l, s): # keyword spotting: frames in a clip return (L - l) // s + 1 def anomaly(x, V, threshold): # V: (D, k) trained on healthy data only xhat = V @ (V.T @ x) # encode (k,), decode (D,) return np.sqrt(np.mean((x - xhat) ** 2)) > threshold
Run these on the worked examples. Chapter 1 gives 276,480 B for layer 3. Chapter 3 gives p80 = 33 M. Chapter 6 gives 78,864 B for 3 × 3 patches. Chapter 7 gives a 27-wide input tile. Chapter 9 gives 49 frames.
Earlier lectures made models small: pruning, quantization, architecture search and distillation. This lecture adds the missing question: where does the working memory go? The next lecture, TinyEngine, covers the runtime that makes the plan real. Later lectures return to the same chips for training on the device.
This lesson follows Lecture 10 of MIT 6.5940, TinyML and Efficient Deep Learning Computing (Fall 2026), taught by Song Han and the MIT HAN Lab. Read the lecture slides on the course page. The papers the lecture cites include MCUNet, MCUNetV2, ProxylessNAS, MnasNet, TVM, Once-for-All, Visual Wake Words, Hello Edge and the Google study on small-footprint keyword spotting. All explanations, devices and examples here are our own. Toy numbers are labelled as such, and the lecture's numbers are cited from its slides.
Now press Teach and explain, from memory, how to count the peak SRAM of a network, why patches lower it, and what the halo costs. Then go back to the instrument at the top and fit MobileNetV2 on the 256 kB chip.