Numerics Explained
← /learn · 08

AWQ and NF4

Scale the input channels that matter before rounding; build a 4-bit code book from the normal distribution's quantiles. Then quantise a real (tiny) transformer.

Check yourself ↓ three questions and what to remember, at the end

Loading the animation…

Concept

INT4's sixteen levels are evenly spaced. Trained weights are not evenly spread: they cluster near zero in a roughly normal bell. NF4 (NormalFloat, from QLoRA) spends its sixteen codes where the weights are, so each code is used about equally often. QLoRA's recipe: take quantiles of the standard normal distribution, scale them to [−1,1][-1, 1], and quantise each block of 64 weights by dividing it by its absolute maximum and picking the nearest code.

The animation rebuilds bitsandbytes' table, the one QLoRA ships, from scratch: eight positive quantiles at probabilities evenly spaced from δ=0.9677083\delta = 0.9677083 down to 0.50.5, seven negative ones, and an exact zero (so the type is asymmetric: zero gets a code of its own, and padding or pruned weights quantise exactly). Divided by the largest quantile, 1.848, the values match bitsandbytes' table to 1.08 × 10⁻⁷ (its table is built in 32-bit floats). bitsandbytes documents the offset as the outermost quantile boundary, about 1.85 standard deviations, chosen empirically.

Does it help? On 4,096 approximately normal values in blocks of 64, NF4's mean squared error is 0.008666 against 0.01172 for INT4 absmax with the same blocks, 1.352 times smaller (SQNR 20.7 dB against 19.4 dB).

Loading the animation…

Concept

AWQ (activation-aware weight quantisation, Lin et al.) starts from an observation: a small fraction of weights matter far more than the rest, and they are found by looking at the activations, not the weights. Keeping just 1% of the weight channels in FP16, chosen by activation size, recovers most of the accuracy that plain 4-bit rounding loses. Mixed precision is awkward for kernels, so AWQ gets the same effect with a scale instead: multiply the weights of input channel ii by si>1s_i > 1 before rounding and divide its activations by sis_i. The scaled weights see a relatively finer grid, so the important channels are rounded more accurately.

The scale is si=∣xi∣‾αs_i = \overline{|x_i|}^{\alpha}, and AWQ searches α∈[0,1]\alpha \in [0, 1] on calibration data. The animation runs that search over the layer of chapter 7, in eighths: the grey bars are each channel's mean activation, the orange bars the scale it gets. At INT3 in groups of 8, no scaling (α=0\alpha = 0) gives an output error of 0.02271; the best eighth, α=\alpha = 0.375, gives 0.01833, 1.239 times smaller. This layer's channels differ by less than an LLM's, so the gain is smaller than the paper's.

Loading the animation…

Concept

Now a real network. The animation above runs the Transformer Decoder Explainer's tiny transformer (two blocks, 16-wide, two heads, a 64-character vocabulary) in your browser, twice: once as is, once with the weights of every block's six linear layers quantised. By default it runs the model as the Training site leaves it, trained on Alice's Adventures in Wonderland, so its predictions are real guesses at the next letter. It predicts the next character at all 8 positions of 16 prompts cut from the part of the book it never trained on, and compares.

Over the 128 predictions, with the token embedding (and the tied output head) kept at full precision:

Weightsbits per weightlogit drifttop prediction unchanged
INT8 per channel90.001917128 of 128
FP8 E4M3 per channel90.00739125
INT4 per channel50.03321121
INT4 groups of 860.03349127
INT4 blocks of 644.250.05249114
NF4 blocks of 644.250.03551120
MXFP4 blocks of 324.250.06938114
INT3 per channel40.1095118
GPTQ INT3 per channel40.05569121
INT3 groups of 850.07499117

Three lessons survive the model's smallness. Each bit removed roughly doubles the drift or more. With the same blocks of 64, NF4's grid beats INT4's, because these weights are close to normal. And GPTQ, calibrated on 16 other windows of the book, recovers part of what plain INT3 rounding loses. Pick a format and a prompt above and watch where predictions flip: they flip where the reference's top two candidates were close, and the reference's median gap between them is 0.7313 logits.

Switch the widget's weights to random init to see the model before training, on four 32-character prompts: every format drifts more there (INT4 per channel 0.09727 against 0.03321, INT3 per channel 0.2304 against 0.1095), and its outputs are gibberish, so a flipped prediction means little. The trained model is the one whose predictions are worth keeping.

Code

NF4's quantiles, cut from src/lib/num/model.ts. The normal CDF is summed as a power series and inverted by bisection, so nothing depends on a library's inverse-CDF approximation:

const pos = Array.from({ length: 8 }, (_, i) => {
  const p = NF4_OFFSET + ((0.5 - NF4_OFFSET) * i) / 8;
  return { p, z: normPpf(p) };
});
const neg = Array.from({ length: 7 }, (_, i) => {
  const p = NF4_OFFSET + ((0.5 - NF4_OFFSET) * i) / 7;
  return { p: 1 - p, z: -normPpf(p) };
});

AWQ's scales for one α=k/8\alpha = k/8, normalised so the largest and smallest multiply to one:

let s = meanAbs.map((m) => powDyadic(Math.max(m, 1e-8), k8));
const smax = Math.max(...s);
const smin = Math.min(...s);
const norm = Math.sqrt(smax * smin);
s = s.map((v) => v / norm);

Check yourself

Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.

Q1On roughly normal values in blocks of 64, how many times smaller is NF4's mean squared error than INT4 absmax's?
Reveal the answer

Answer: 1.352 (counted right within 10%)

1.352 times: NF4 puts its sixteen codes at normal quantiles, where the weights are.

Does NF4 help? →

Commit to a number, then reveal.

Q2Which weights does AWQ protect?
Reveal the answer

Answer: The weights of the channels with the largest activations

Those whose input channels have large activations: scaling them up before rounding gives them a relatively finer grid. On this layer at INT3 the best scale cuts the error 1.239 times.

AWQ →

Pick one, then reveal.

Q3The trained tiny transformer at INT3 per channel: how many of its 128 top predictions does GPTQ win back over plain rounding?
Reveal the answer

Answer: 3 (exact)

3: GPTQ keeps 121 unchanged, plain INT3 118.

A real network →

Commit to a number, then reveal.