MLOps · Models in the real world

Shrink Ray

A brain stores every wire strength as a long, precise number: 32 bits each. Phones, watches and web pages want models that are small and fast. The shrink ray rounds every number onto a short ruler with only a few marks. Fewer marks = fewer bits = tinier model. But round too hard and the brain gets confused.

First I'm training a small brain (648 wires and 49 extra numbers) to tell the two spiral arms apart. Watch the map on the left fill in. Then point the shrink ray at it!

Training steps: 0

Full-size brain (32-bit)

Shrunk brain

arm Aarm Bdot the brain gets wrong

Shading is the brain's guess for every spot. When the shrunk map gets blotchy and dots turn red-ringed, rounding has broken the brain.

The wires and the ruler

Challenges

Pocket size: get the brain under 1 KB (1024 bytes) while still at least 90% accurate.
Find where it breaks: shrink to 2 bits with one ruler for the whole layer and watch accuracy crash.
Rescue 3-bit: at 3 bits (whole-layer ruler, centred), use QAT to win back at least 3 accuracy points.
Fit the ruler: find a bit setting where "each row" gets clearly more accurate than "the whole layer".

What just happened?

How it works

  1. Train a 2 → 24 → 24 → 1 brain on the spiral using normal 32-bit numbers.
  2. For each layer (or each row) find the biggest wire. Space the ruler marks evenly so the end marks reach it.
  3. Store each wire as just the number of its nearest mark (a small whole number), plus one scale number per ruler.
  4. To use the brain, multiply the mark numbers back by the scale and run it as usual.
  5. QAT: train a bit more, rounding in the forward pass but nudging the hidden exact numbers as if rounding weren't there.
For grown-ups: what's really going on

This is uniform affine quantisation of a 697-parameter MLP (tanh hidden layers, sigmoid output, Adam, full batch, 300 training points, 600 test points). Symmetric: s = max|w| / (2b−1 − 1), q = clamp(round(w / s), −(2b−1−1), 2b−1−1), ŵ = s·q. Asymmetric: s = (max − min) / (2b − 1), zero-point z = round(−min / s), ŵ = s·(q − z).

  • PTQ vs QAT. Post-training quantisation just rounds a trained model. Quantisation-aware training fine-tunes with fake-quantised weights in the forward pass and the straight-through estimator (∂ŵ/∂w ≈ 1) in the backward pass, on float "shadow" weights. Here: 150 Adam steps at lr 0.005.
  • Granularity. Per-channel scales (one per output row) fit each row's range and lower the rounding error; they're standard for conv/linear weights in int8 toolchains. Activations here use a fixed [−1, 1] range (tanh), so no calibration is needed; real PTQ calibrates activation ranges on sample data.
  • Size counts packed b-bit weights, 32-bit biases (int8 schemes usually keep int32 biases) and one fp32 scale (+1 byte zero-point) per group. "16-bit" here is int16, not fp16/bf16. Chip time is a made-up cost model, only for the shape of the trade-off.
  • In practice. ONNX Runtime and PyTorch export int8 models (dynamic or static quantisation, QDQ graphs). The Live AI Lab downloads q8 ONNX models for exactly this reason, and Mood Racer showed that the 8-bit version's scores differ slightly from full precision. LLMs go further: 4-bit weight-only methods like GPTQ and AWQ, and formats like NF4.
  • Measured in Node (seed 3/5, 1500 steps): float ≈ 99.7%; 8-bit any scheme ≈ 99.7%; 3-bit per-tensor symmetric ≈ 92% → about 99% after QAT; 2-bit per-tensor ≈ 28–33% (worse than guessing: the boundary flips), per-channel 2-bit 80–99%.

Reading: Jacob et al., "Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference" (2018, arXiv:1712.05877).