TactivergeLite

Lite / Independent Engineering Research / Week 23

Training versus inference: counting the cost

Training a speech model took a data centre. Running it fits on a chip in a light switch. What makes the two so different?

About 45 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Keyword spotting, gesture detection, predictive maintenance and visual wake-up all run small neural networks on microcontrollers. Whether a model fits is a calculation, not a guess: MACs for time and energy, bytes for memory.

Predict

A layer connects 128 inputs to 64 outputs. How many multiplications does one inference through it need (ignoring biases)?

  1. 192
  2. 8,192
  3. 128
  4. 64
Guess first, then reveal

Every output is a weighted sum of all 128 inputs: 64 × 128 = 8,192 multiply-accumulates. Dense layers grow with the product of their sizes.

Discover

The Nibble machine has no multiply instruction: your Level 4 program multiplies by repeated addition. Count how many instructions one 4-bit multiply takes, then estimate how many instructions a single 8,192-MAC layer would need on such a machine. That is why real chips have hardware multipliers and MAC units.

What the interactive shows (text version)

The Nibble machine task can be done on paper: multiplying x by y with repeated addition runs the loop y times, about 7 instructions each, so one 4-bit multiply costs tens of instructions. A layer of 8,192 MACs would need hundreds of thousands.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: Notice the difference between learning the weights from labelled data and simply using them to classify a new input.

A plain introduction to classifiers and neural networks, enough context to reason about what inference on a small device must compute.

“Machine Learning & Artificial Intelligence: Crash Course Computer Science #34” by CrashCourse (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

Understand

Two phases

Training searches for weights that minimise a loss over a dataset, using many passes of forward computation, gradient computation and updates: expensive, done once on big hardware. Inference runs one forward pass with fixed weights on a new input: much cheaper, done millions of times, often on the device itself for latency, privacy or battery reasons.

Counting MACs

A multiply-accumulate does y ← y + w·x. A dense layer with a inputs and b outputs costs a·b MACs. A convolution producing an H×W×C_out output with k×k kernels over C_in channels costs H·W·C_out·k²·C_in. Add the layers. Activations (ReLU) and pooling are usually small by comparison.

Memory: weights and activations

Weights must be stored somewhere, usually flash: one value per connection. Activations (the intermediate results) must fit in RAM while a layer runs: roughly the largest input plus output of any layer. A microcontroller with 1 MB flash and 256 KB RAM sets hard limits on both.

From MACs to milliseconds

Latency ≈ MACs ÷ (MACs per cycle × clock rate), plus memory stalls. A core doing 1 MAC per cycle at 100 MHz runs a 2 million-MAC model in about 20 ms. Vector (SIMD) instructions or a small accelerator can do 4–8 MACs per cycle at 8 bits.

Explain it another way

Intuitive

Training is writing a recipe book by cooking thousands of trial dishes. Inference is following one recipe once: much quicker, and you can do it in a tiny kitchen.

Technical

A forward pass computes y = f(Wx + b) layer by layer. Training adds backpropagation, roughly 2–3× the forward cost, plus storing activations for the backward pass and many epochs over the data.

Mathematical

Total MACs = Σ_layers (fan-in × outputs). For a 3×3 conv, 16 → 32 channels on a 32×32 map: 32·32·32·9·16 = 4,718,592 MACs. A 1×1 conv on the same map: 524,288, nine times fewer, which is why efficient networks use depthwise-separable convolutions.

Applied

A typical keyword-spotting model for microcontrollers has tens of thousands of weights and a few million MACs per second of audio, small enough to run continuously on a coin-cell-friendly chip.

Worked example 1

A network: 49×10 audio features flattened (490 inputs) → dense 64 → dense 12. MACs and weights?

MACs = 490×64 + 64×12 = 31,360 + 768 = 32,128. Weights equal MACs for dense layers: 32,128 (plus 76 biases). At float32: about 126 KB; at int8: about 31 KB.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

MACs

MACs

Practice 3. Which phase normally runs on a battery-powered sensor?

ms

KB

Apply it

Design (on paper) a classifier for 3-axis accelerometer gestures sampled at 50 Hz in 2 s windows. Choose layer sizes, count MACs and weights, and check it against a budget of 100 KB flash, 32 KB RAM and 10 ms on a 64 MHz core doing 1 MAC per cycle. State every assumption.

Reflect

If a device can't run your model in time, which would you change first: the model, the precision or the hardware? Why?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Wafers, lithography and yieldQuantisation: fewer bits, smaller models →