Shrinking a model to fit: quantization from scratch
The six-week core sprint taught me how a language model works, end to end. But I still couldn’t run a
capable one on my own laptop — a 7B model is ~28 GB in float32, more memory than most machines have.
This week I built the thing that fixes that: quantization. Store each weight as an 8-bit (or 4-bit)
integer plus a shared scale instead of a 32-bit float, and the model shrinks 4–8× with little or no
loss. It is, concretely, the single biggest reason local LLMs exist at all.
What I built
int8 and int4 weight quantization from scratch, applied to GPT-2:
| precision | memory | vs fp32 | greedy output |
|---|---|---|---|
| float32 | 494 MB | 1.0× | reference |
| int8 | 124 MB | 4.0× | identical |
| int4 | 70 MB | 7.1× | drifts, still coherent |
The whole method is three lines per group of weights: find the largest magnitude, divide so the group
fits a small integer range, round. Store the integers and one float scale; reconstruct by multiplying
back. The only craft is in the grouping — one scale per column for int8, one per 64-weight block for
int4 — because finer groups mean smaller rounding errors.
The concepts that made it work
- Precision is mostly waste. 32 bits per weight is wild overkill. The model’s behaviour lives in
the rough shape of the weights, not their 7th decimal place — which is why you can throw most of the
bits away and barely notice. - Scale + round + clip, per group.
q = round(w / scale),scale = max(|group|)/qmax. The clip
matters: one outlier left unclamped blows past the integer range and corrupts the block. - Grouping is the accuracy dial. int8 per-column keeps error tiny; int4’s 16 levels are coarse, so
you quantize in small blocks to stop a few large weights from dominating a scale. - Don’t quantize the small stuff. Biases, layernorms and positions stay float32 — negligible memory,
precision-sensitive. Spend your bit budget on the big matrices.
The thing that bit me
I had it backwards. I expected that cutting weights to a quarter of their bits would visibly degrade
the output, and that I’d spend the week chasing accuracy loss. Instead int8 produced output that was
character-for-character identical to float32 — a 4× smaller model with zero behavioural change. The
surprise wasn’t that quantization works; it’s how much precision was pure dead weight. int4 is where it
finally bites: the output wanders onto a different (still grammatical) path, and you can read the lost
precision in the text — “the first theorem is that the number of possible values…” becomes “…the
statistical theory of computation.” That made the tradeoff visceral in a way a perplexity number never
would: I’m literally watching half a byte per weight change the model’s mind.
Tradeoffs & the road not taken
- Weights only, not activations. I quantize the stored weights; the matmuls still run in float. True
integer inference quantizes activations too — more speed, more care. - Round-to-nearest, not calibration. Real quantizers (GPTQ, AWQ) use a little data to decide which
weights to round which way, squeezing more quality out of int4. Mine is the honest baseline they beat. - Dequantize-then-run, to measure. I reconstruct floats and reuse the Week-1 forward, because the
point was to measure error and memory — not to write fused int4 kernels (that’s llama.cpp’s job).
Where this sits in the system
This is the keystone of the “run it locally” track. The core sprint made the model transparent; this
removes the one thing keeping a real one off my laptop. A 7B at int4 is ~4 GB — it fits in RAM, even on
a phone. And the format I just hand-rolled (block-wise integer weights + per-block scales) is exactly
what llama.cpp’s GGUF files are. Next I stop simulating: download an actual quantized 7B and drive it
with llama.cpp / Ollama / MLX on Apple Silicon — and recognise every stage, because I built them all
first. Same spine: one system, a layer at a time.
Part of a series building a modern AI stack from scratch, told visually. (Last time: tensor parallelism.)