Linear algebra for LLMsPart 9: Numbers on real hardware
Quantization
7 min
Scale, round, scale back
Symmetric INT8 quantization: pick one scale so the largest value lands on , store the rounded integers plus the scale, and multiply back when needed.
Write
INT8 by hand
The largest magnitude, 1.27, sets . Every value is stored as a whole number of hundredths; 0.031 becomes 3, so it comes back as 0.03. Rounding never moves a value by more than .
Write
INT4: only 15 levels
With levels the steps are 18× coarser: 0.42 comes back as 0.363, and 0.031 vanishes to 0. INT4 halves the memory again, at a real cost in accuracy unless it’s done carefully.
One outlier ruins the scale
The 10 forces a big step, so the small values get one to three levels each: 0.1 comes back as 0.079, a 21% error. LLM activations and some weights do have such outliers.
Write
Per-channel and per-group scales
Give each output channel, or each group of 128 consecutive weights, its own scale. An outlier then only coarsens its own group. A 16-bit scale per 128 four-bit weights adds an eighth of a bit per weight.
Scales factor out of the matmul
With one scale per output row, the dot product can run on the integers and be rescaled once at the end. That’s how quantized kernels avoid converting every weight back to floating point first.
✏️ Your turn
Quantize to INT4
Use . Write , and .
Work it out on paper, then go to the next card to check.
Answer
and . Both errors are under .
📌 Recap
Quantization in one card
- , , ; error .
- INT4 has 15 levels: much coarser than INT8’s 255.
- Outliers coarsen a shared scale; per-channel or per-group scales contain them.
- Per-row scales factor out of the dot product.
Write