Weight quantization for inference
Map weights to int8/int4 with per-group scales and explain why this speeds up memory-bound decoding. Explain why naive quantization fails: a few activation dimensions take values far larger than the rest (outliers), and they wreck a shared scale.
Builds on: Floating point: fp32, fp16, bf16, int8, Prefill vs decode: compute- vs memory-bound
What the animation says
Decode is a memory race
When a model generates one token at a time, it reads every weight once and does very little math with it. So speed is set by memory: bandwidth divided by the bytes of weights. Seven billion weights in bf16, two bytes each, is fourteen gigabytes per token. At one terabyte per second, that's about fourteen milliseconds, roughly seventy tokens a second. Now store each weight in four bits instead of sixteen. What happens to tokens per second?
Small integers plus a scale
Close to four times: about three and a half gigabytes, near three hundred tokens a second, ideally. Here's how. Split each weight matrix into groups of, say, a hundred twenty-eight weights. Each group stores one bf16 scale, and each weight as a small integer. Picture a ruler with fifteen marks, stretched to fit the group's largest weight. At decode, the kernel rescales the integers and runs the usual matmul. The extra math is nearly free, since it was waiting on memory anyway. Storage: four bits, plus a sixteen-bit scale shared by the group. Just over four bits per weight.
One group, number by number
Take one int4 group: point one two, minus point three, point zero five, point two one. The scale is the largest magnitude, point three, divided by seven: about point zero four three. Divide each weight by the scale and round to the nearest integer: three, minus seven, one, five. Multiply back by the scale. Every weight lands within half a step, about point zero two. Now swap minus point three for an outlier: three point zero. What happens to the other three weights?
The largest value sets the step
With three point zero in the group, the scale jumps to three over seven, about point four three. Point one two, point zero five, point two one: each is under half a step, so each rounds to zero. Three weights wiped out, because the largest value sets the step for everything sharing it. That's why groups are small: the damage stays inside one group of a hundred twenty-eight, not a whole row.
Activation outliers
Weights are fairly tame. Activations are not. In trained models, a few hidden dimensions, the same ones at every token, run fifty to a hundred times larger than the rest. Fast int8 matmuls quantize activations too, usually with one scale per token row. Here's one token's activations, quantized to int8 with one shared scale. Then dimension seven is set to sixty. Error on the ordinary values jumps from under one percent to about twenty-seven percent. Tap the line that lets one value do this.
Fixes and the takeaway
That scale line takes the max over all four thousand values, so sixty sets a step near point five. Ordinary values, about half that size, round to zero or plus or minus one. The fix: keep those few outlier dimensions in bf16, or give them their own scale. So: store weights as int8 or int4, with one scale per small group. Decode reads two to four times fewer bytes, and speed rises almost as much. Each value's error is at most half a step, and the largest value sharing a scale sets that step.
Small integers plus a scale
Symmetric absmax per-group quantization
One group, then one outlier
Activation outliers wreck a shared scale
import numpy as np
x = np.random.default_rng(0).normal(0, 0.5, 4096) # one token's activations
def q8(v): # int8, ONE shared scale
s = np.abs(v).max() / 127
return np.clip(np.round(v / s), -127, 127) * s
y = x.copy(); y[7] = 60.0 # one outlier dimension
keep = np.arange(4096) != 7 # judge the ordinary dims
for v in (x, y):
e = q8(v) - v
print(np.linalg.norm(e[keep]) / np.linalg.norm(v[keep]))
# ~0.008 without the outlier, ~0.27 with it
What to remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses