All courses

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

▶ Watch this lesson Start the whole course

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.

Can we read fewer bytes per token?

At batch 1, each new decode token reads every weight once for very little compute, so speed ≈ memory bandwidth ÷ bytes of weights. A model with 7B weights in bf16 is 14 GB per token. At 1 TB/s that is about 14 ms, or ~70 tokens/s. Storing each weight in 4 bits instead of 16 would read 4× fewer bytes. Can we do that without breaking the model, and what goes wrong if we do it naively?

Small integers plus a scale

Split each weight matrix into small groups, e.g. 128 consecutive weights. For each group, store one bf16 scale and each weight as a small integer. Think of a ruler with 255 marks (int8) or 15 marks (int4), stretched to fit the group's largest-magnitude weight. During decode the kernel reads the integers, multiplies them by the scale (dequantizes) and runs the usual bf16 matmul. Bytes drop 2–4×. The extra arithmetic is nearly free because decode was waiting on memory anyway.

Symmetric absmax per-group quantization

For a group $g$ of $G$ weights: $$s=\frac{\max_{i\in g}|w_i|}{q_{\max}},\quad q_i=\mathrm{round}(w_i/s),\quad \hat w_i=s\,q_i$$ - $q_{\max}=127$ for int8, $7$ for int4. - $q_i$ is the stored integer; $\hat w_i$ is what the matmul uses. - Error per weight: $|w_i-\hat w_i|\le s/2$, which is set by the group's largest weight. - Storage is $b+16/G$ bits per weight, where $b$ is the integer width (8 or 4) and 16 is the bf16 scale shared by the group. For int4 with $G=128$ that is $4.125$ bits. - Groups run along $d_{in}$ of $W$.

One group, then one outlier

int4, group $[0.12,-0.30,0.05,0.21]$. - $s=0.30/7\approx0.043$. - $w/s=[2.8,-7,1.17,4.9]$, which rounds to $q=[3,-7,1,5]$. - $\hat w=[0.129,-0.300,0.043,0.214]$; every error is below $s/2\approx0.021$. Now replace $-0.30$ with $3.0$: - $s=3/7\approx0.43$. - $w/s=[0.28,7,0.12,0.49]$, so $q=[0,7,0,0]$. Three weights are wiped to 0. Small groups confine that damage to $G$ weights.

Activation outliers wreck a shared scale

Weights are fairly tame; activations are not. In trained LLMs a few hidden dimensions, the same few at every token, reach ~50–100× the typical size. Fast int8×int8 matmuls require quantizing activations too, usually with one scale per token row. A single outlier then sets that scale, and ordinary values round to 0 or ±1. Fixes: keep the outlier dimensions in bf16, or give them their own 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

Store weights as int8/int4 plus one scale per small group. This cuts the bytes read per decode token 2–4×, so tokens/s rises almost in proportion. Each value's error is at most half a step, and the step is set by the largest value sharing the scale. Outliers, especially the few huge activation dimensions, therefore need their own scale or higher precision.

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.