Skip to content
Tactiverge

School/✦ Research Engineer/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

1.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.

  • ◇ Distinguish training from inference and explain why only inference must run on the device.
  • ◇ Count multiply-accumulate operations (MACs) for dense and convolutional layers.
  • ◇ Estimate a model's latency from MACs and a device's MAC rate, and list its memory needs (weights and activations).

2.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.

Can't use the interactive? Read a description instead

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.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A convolution layer takes a 8×8 image with 8 channels and produces a 8×8 output with 32 channels using 5×5 kernels. How many MACs does it need?

MACs

4.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.

Open BUILD 008: Create a Virtual Computer →

5.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

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.

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

  • Running a model costs as much as training it.
    ✓ One inference is a single forward pass; training is many passes plus gradients over a whole dataset.
  • Only the number of weights matters.
    ✓ A convolution reuses few weights many times: MACs, not weights, set its time; activations set its RAM.

6.Watch

→ 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.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A network has two fully connected layers: 784 inputs → 16 hidden units → 2 outputs. How many multiply-accumulate operations (MACs) does one inference need (ignore biases and activations)?

MACs

Practice 3

Which phase normally runs on a battery-powered sensor?

Practice 4

A model needs 3,000,000 MACs. The core does 2 MACs per cycle at 80 MHz. Ignoring memory stalls, what is the latency in ms?

ms

Practice 5 · a fresh version every time

A neural network has 600000 weights stored at 16 bits each. How many kilobytes (1 KB = 1024 bytes) do the weights occupy?

KB

8.Predict

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

Guess first. Being wrong here is how the surprise works.

9.Reflect

Concepts: Embedded machine learning · Thresholding and simple classification · Algorithms, searching and complexity