School/✦ Research Engineer/Week 23
Quantisation: fewer bits, smaller models
Store every weight in 8 bits instead of 32 and the model is four times smaller. How much accuracy do you lose? Sometimes almost none.
About 45 min · 3 goals · Checked by automated tests
1.Why should I care?
int8 quantisation is the standard way to fit networks onto phones and microcontrollers: 4× less memory, cheaper integer MACs and lower energy per operation. Knowing how it works, and how to measure what it costs, is core embedded-ML engineering.
- ◇ Quantise real values to b-bit integers with a scale factor and recover them (dequantise).
- ◇ Relate bit width to rounding error, memory footprint and the cost of arithmetic.
- ◇ Measure, rather than assume, the accuracy cost of quantisation on held-out data.
2.Discover
Sweep the bits from 16 down to 2 and read the accuracy curve. Find the smallest bit width that stays within 1 percentage point of float. Then set 1,000,000 parameters and find the smallest width that fits the 256 KB budget. Do the two answers agree?
Loading the interactive…
Can't use the interactive? Read a description instead
The explorer plots accuracy of a toy classifier against bit width: 90.25% in float and at 8, 12 and 16 bits, 90% at 6 bits, about 89% at 3 and 4 bits, and about 74% at 2 bits. Footprint = parameters × bits ÷ 8; one million int8 weights take about 977 KB, so they do not fit a 256 KB budget.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Weights lie in [−2, 2] and are quantised to symmetric int8 with qmax = 127. What is the scale (step size)?
4.Predict
Weights range from −0.5 to +0.5 and are stored as symmetric int8 (−127…127). What is the largest rounding error on any weight?
Guess first. Being wrong here is how the surprise works.
5.Understand
Scale and zero point
A quantised tensor stores small integers plus one scale (and sometimes a zero point for asymmetric ranges, useful for ReLU outputs that are never negative). Per-channel scales, one per output channel, often keep accuracy much better than one scale for the whole layer.
Why accuracy often survives
A trained network is robust to small perturbations of its weights; int8 errors are tiny compared with the spread of the weights. Below about 4 bits, errors become comparable to the weights themselves and decisions start to flip. Outliers matter too: one huge weight stretches the scale and wastes resolution on everyone else.
Measure on held-out data
Compare float and quantised accuracy on the same held-out test set, report both with their uncertainty, and look at which inputs changed class. Calibration (choosing scales from representative data) and quantisation-aware training can recover accuracy at low bit widths. The explorer's toy classifier shows the trend only.
Explain it another way
Measuring a room in whole centimetres instead of micrometres: the furniture still fits, and the notebook is much shorter. Measure in whole metres and things stop fitting.
Post-training quantisation maps float32 weights (and activations, with calibration) to int8. Kernels use int8 × int8 → int32 accumulation and requantise outputs. Hardware SIMD instructions run several int8 MACs per cycle.
For weights with max|w| = 1: b = 8 gives s = 1/127 and RMS error ≈ 0.0023; b = 4 gives s = 1/7 and RMS error ≈ 0.041. Footprint = N·b/8 bytes.
Mobile and microcontroller ML frameworks routinely ship int8 models; many microcontroller cores include DSP instructions that perform two or four 8- or 16-bit MACs in one cycle.
Derivation: Symmetric uniform quantisation and its error
Choose a scale s = max|w| / (2^(b−1) − 1), so the largest weight maps to the largest integer.
Quantise: q = round(w / s), clamped to the b-bit range. Dequantise: ŵ = q·s.
The rounding error e = ŵ − w lies in [−s/2, s/2]. If it is roughly uniform, its RMS is s/√12.
Each extra bit halves s, so it halves the error: about 6 dB better signal-to-quantisation-noise per bit.
Integer MACs: Σ wᵢxᵢ ≈ s_w s_x Σ q_w q_x. The sum runs in integers (with a 32-bit accumulator) and one rescale happens at the end.
Worked example 1
Quantise w = 0.31 with max|w| = 0.8 to int8.
s = 0.8/127 = 0.0063. q = round(0.31/0.0063) = round(49.2) = 49. ŵ = 49 × 0.0063 = 0.3087. Error −0.0013, within ±s/2 = ±0.0031.
Common mix-ups
- Quantisation always destroys accuracy.
✓ At 8 bits the loss is often small; it must be measured, and grows quickly below about 4 bits. - Quantising to 8 bits makes inference exactly 4× faster.
✓ Memory shrinks 4×; speed depends on hardware support for int8 MACs and on memory traffic.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
A neural network has 50000 weights stored at 4 bits each. How many kilobytes (1 KB = 1024 bytes) do the weights occupy?
Practice 3
With scale 0.02, what integer does w = −0.537 quantise to (round to nearest)?
Practice 4
One weight in a layer is 50× larger than all the others. What happens with a single per-layer int8 scale?
Practice 5
Which evaluation of an int8 model is fair?
7.Apply it
Quantise a small set of filter coefficients (for example, the FIR or complementary filter you used in Term 2) to 16, 8 and 4 bits in the Code Lab. Measure the output error on the same test signal for each and decide which width you would ship, against an error requirement you write down first.
8.Reflect
Concepts: Embedded machine learning · Units, scaling and fixed-point arithmetic · Measurement error and uncertainty