TactivergeLite

Lite / Independent Engineering Research / 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 · Full interactive version

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.

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?

  1. About 0.002
  2. About 0.04
  3. 0.5
  4. Zero
Guess first, then reveal

Scale = 0.5/127 ≈ 0.0039, and rounding is off by at most half a step: about 0.002. With 4 bits (−7…7) the step is 0.071 and the worst error 0.036, eighteen times larger.

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?

What the interactive shows (text version)

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.

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

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

Intuitive

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.

Technical

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.

Mathematical

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.

Applied

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
  1. Choose a scale s = max|w| / (2^(b−1) − 1), so the largest weight maps to the largest integer.

  2. Quantise: q = round(w / s), clamped to the b-bit range. Dequantise: ŵ = q·s.

  3. The rounding error e = ŵ − w lies in [−s/2, s/2]. If it is roughly uniform, its RMS is s/√12.

  4. Each extra bit halves s, so it halves the error: about 6 dB better signal-to-quantisation-noise per bit.

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

Practice

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

KB

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?

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.

Reflect

Where else in engineering do you trade precision for size or speed?

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

← Training versus inference: counting the costFitting a model into memory, time and energy →