Pidoku

Quantization Fundamentals

Module 10 Lesson 10.l 1h 30m Difficulty 3/5

Prerequisites 10.k

This lesson teaches the mechanism. Module 13 covers the production methods (GPTQ, AWQ, SmoothQuant, FP8, FP4), which one to choose, and the measured gains (13.b).


Before you start: words you need#

WordPlain meaning
QuantizeReplace each value with the nearest of a small set of levels, and store only which level it is.
DequantizeTurn the stored level back into an approximate real value.
ScaleThe step between levels. real value ≈ stored integer × scale.
Zero pointAn offset that shifts the levels so that a range like 0…10 can use all of them.
OutlierA value far larger than the rest. One outlier stretches the scale for everything that shares it.
GranularityHow many values share one scale: a whole tensor, one row, or a group of 32–128.
CalibrationRunning sample inputs through the model to measure value ranges before choosing scales.

1. What is it?#

Representing a tensor with fewer bits by mapping its range onto a small set of levels.

Original (FP16):   [-0.83,  0.12,  1.47, -1.92,  0.55, ...]
Quantized (INT8):  [  -55,     8,    97,  -127,    36, ...]  +  scale = 0.01512
Reconstructed:     [-0.832, 0.121, 1.467, -1.920, 0.544, ...]

You store the small integers (1 byte each instead of 2) and one or a few scales. The values you get back are approximations; the difference is the quantization error, and it is the price you pay.


2. Why does it exist?#

Because decode is memory-bandwidth-bound (2.e): every step reads every weight, so bytes moved set the speed. Halving the bytes roughly halves the time per token. Everything else about quantization is a consequence of pursuing that.

Secondary benefits: the model fits in less memory (more KV cache, cheaper GPUs), and on hardware with low-precision tensor cores, compute throughput increases too.


3. Simple analogy#

Rounding prices to the nearest dollar.

$4.37, $12.81, $0.99 → $4, $13, $1. You lose cents but the list is shorter to write and faster to add up.

The engineering questions are exactly the ones quantization asks:

  • What’s the range? If prices go from $0.99 to $10,000,000, rounding to dollars is fine for the big ones and destroys the small ones.
  • Should I use one rounding scheme for everything, or per category? (per-tensor vs per-channel)
  • What about the one item priced at $50 million? (outliers — the central problem)

4. Tiny example#

Symmetric INT8 quantization, worked completely:

Go
package main

import (
	"fmt"
	"math"
)

func main() {
	w := []float64{-0.83, 0.12, 1.47, -1.92, 0.55, 0.03, -0.41, 0.88}

	// 1. Find the scale
	var absmax float64
	for _, v := range w {
		absmax = math.Max(absmax, math.Abs(v)) // 1.92
	}
	scale := absmax / 127 // 0.01512
	fmt.Printf("scale: %.5f\n", scale)

	// 2. Quantize
	q := make([]int8, len(w))
	for i, v := range w {
		q[i] = int8(math.Round(v / scale))
	}
	fmt.Println("quantized:", q) // [-55 8 97 -127 36 2 -27 58]

	// 3. Dequantize, 4. Error
	var maxErr, maxRel float64
	fmt.Print("recovered:")
	for i, v := range w {
		wHat := float64(q[i]) * scale
		fmt.Printf(" %.4f", wHat) // -0.8315 0.1209 1.4665 -1.9200 0.5443 ...
		err := math.Abs(v - wHat)
		maxErr, maxRel = math.Max(maxErr, err), math.Max(maxRel, err/math.Abs(v))
	}
	fmt.Printf("\nmax error: %.4f (scale/2 = %.4f)\nrelative: %.3f\n", maxErr, scale/2, maxRel)
}

Maximum error is always scale/2 for round-to-nearest. That’s the key fact: absolute error is uniform, so relative error is terrible for small values and fine for large ones.

Now the outlier problem:

Go
w2 := []float64{-0.83, 0.12, 1.47, -1.92, 0.55, 0.03, -0.41, 45.0} // one outlier
scale2 := 45.0 / 127                                               // 0.354  ← 23x larger!
for _, v := range w2 {
	q := math.Round(v / scale2)
	fmt.Printf("%4.0f -> %6.3f\n", q, q*scale2)
}
// q:         -2   0   4      -5      2      0  -1      127
// recovered: -0.709 0 1.417 -1.772 0.709 0 -0.354 45.0

Look at what happened. 0.12 became 0.0. 0.03 became 0.0. 0.55 became 0.709. One outlier destroyed the precision of every other value. Outlier handling is the central problem of quantization, and every named method (LLM.int8, SmoothQuant, AWQ, GPTQ) is a different answer to it.


5. Technical explanation#

Symmetric vs asymmetric#

Symmetric:   x ≈ q · s                       zero maps to zero
             s = absmax / (2^(b-1) - 1)
             simpler kernels, wastes half the range for one-sided distributions

Asymmetric:  x ≈ (q - z) · s                 z = zero-point offset
             s = (max - min) / (2^b - 1)
             z = round(-min / s)
             better for one-sided data (e.g. post-ReLU activations)
             extra add in the kernel

Weights are roughly zero-centered, so symmetric is standard for weights. Activations after ReLU/SiLU can be one-sided, so asymmetric is sometimes used there.

Granularity — the most important knob#

Per-tensor:    one scale for the whole matrix           smallest overhead, worst accuracy
Per-channel:   one scale per output row                 standard for weights
Per-group:     one scale per group of 32/64/128 values  standard for INT4
Per-token:     one scale per token (activations)        standard for dynamic activation quant

The overhead:

INT4 weights with group size 128, FP16 scales:
  4 bits per weight + 16 bits per 128 weights = 4 + 0.125 = 4.125 bits/weight
  → effective 4.125/16 = 25.8% of FP16.  The scales cost ~3%.

Finer granularity → better accuracy → slightly more memory and slightly more kernel work. Group size 128 is the usual default for INT4 because it captures most of the accuracy benefit at small cost. The FP4 formats go further and build the group into the hardware: blocks of 32 (MXFP4) or 16 (NVFP4) values, each with its own scale applied inside the tensor core (10.k).

Weight-only vs weight-and-activation#

WEIGHT-ONLY (W4A16, W8A16):
    store weights in INT4/INT8
    dequantize to FP16 in the kernel, compute in FP16
    ✓ big memory/bandwidth saving → helps DECODE a lot
    ✗ no compute saving → helps PREFILL almost none
    ✓ easy: no activation calibration needed
    → GPTQ, AWQ

WEIGHT AND ACTIVATION (W8A8, FP8):
    both quantized; compute in INT8/FP8 tensor cores
    ✓ memory saving AND ~2x compute
    ✗ harder: activations have per-input outliers
    ✗ needs calibration
    → SmoothQuant, FP8, and NVFP4 / MXFP4 (W4A4) on Blackwell

Choose by phase: for decode-heavy workloads, weight-only INT4 is excellent. For prefill-heavy ones (RAG, long documents) you need W8A8 (FP8) or W4A4 (FP4) to get faster arithmetic too. 13.b turns this into a decision guide.

Static vs dynamic activation quantization#

Static:  scales computed offline from a calibration set. Fastest; risky if
         production data has a different distribution.
Dynamic: scales computed per batch at runtime (a max-reduction over the tensor).
         Costs a pass over the data; robust. Standard for per-token quantization.

Where the error comes from#

For a matmul Y = X W:

Ŷ = X̂ Ŵ = (X + εx)(W + εw) ≈ XW + X·εw + εx·W

error ≈ ||X||·||εw|| + ||εx||·||W||

Two consequences:

  1. Error is proportional to the magnitude of the other operand. Quantizing a weight column that multiplies large activations hurts more. This is AWQ’s entire insight — protect the weight channels that see large activations.
  2. Errors accumulate through layers, and through generated tokens.

What to quantize and what to leave alone#

Quantize aggressively:  FFN weights (65-70% of parameters)
Quantize carefully:     attention projections
Usually leave in FP16:  embeddings, LM head, norm weights, biases
Quantize separately:    KV cache (13.h) — different tradeoffs

The LM head is often left alone: it’s a single large matrix whose errors directly perturb the sampling distribution.


6. Under the hood#

A W4A16 GEMM kernel:

for each tile:
    load INT4 weights from HBM         ← 4x less traffic than FP16
    load FP16 scales for the groups
    UNPACK: two 4-bit values per byte → separate values
    DEQUANTIZE: w_fp16 = (int4_val - zero) * scale
    load FP16 activations
    tensor core MMA in FP16
    accumulate FP32

The unpack+dequantize is extra work, done in registers. It costs FLOPs — but decode has FLOPs to spare (2.e), so trading compute for bandwidth is exactly the right trade. In prefill, where you’re compute-bound, that same extra work is a pure cost, which is the mechanistic reason W4A16 doesn’t help prefill.


7. Performance implications#

The mechanism above predicts the shape of every measurement (13.b has the numbers):

  • Decode speed-up ≈ the byte reduction, minus a little for scales and unpacking: close to 2x for 8-bit and 2.5–3.5x for 4-bit weights.
  • Prefill speed-up ≈ the arithmetic speed-up: about 2x for FP8 W8A8, about 1x for weight-only schemes, sometimes slightly below 1x because the unpacking is pure overhead when you are compute-bound. Some engines use different kernels for prefill and decode for this reason.
  • Smaller groups cost a few percent of the memory saving and buy back accuracy.

8. Production implications#

  • Quantize offline, ship the quantized checkpoint. Never quantize at startup (3.g). For many 2026 open models the authors publish FP8 or FP4 checkpoints; start there.
  • Evaluate on your own task, with long generations. Public benchmark deltas do not transfer.
  • Record the exact quantization config in your registry: method, bits, group size, symmetric/asymmetric, which layers excluded, calibration dataset.
  • Prefer FP8 on Hopper/Blackwell if quality permits — it’s simpler than INT8 (no zero points, wide dynamic range) and gets compute benefits.
  • Watch the prefill regression. If your workload is RAG-shaped, W4A16 may make things worse.
  • A/B in production. Quality regressions from quantization are exactly the kind that offline evals miss.

9. Common mistakes#

  1. Assuming quantization is free. It costs quality. Measure it.
  2. Using per-tensor scales for INT4. Accuracy collapses. Use groups.
  3. Quantizing the embeddings and LM head with everything else. A large quality cost for little memory saving.
  4. Expecting a prefill speed-up from weight-only quantization. It does not work that way.
  5. Evaluating with perplexity only. Perplexity barely notices the failures users do (reasoning, instruction following, long-form coherence).
  6. Calibrating on the wrong data. Calibrating on Wikipedia and serving code gives worse results than calibrating on code.
  7. Forgetting that KV-cache quantization is a separate decision with its own trade-offs (13.h).

10. Hands-on exercise#

A. Implement it. Write symmetric INT8 quantize/dequantize with per-tensor, per-channel, and per-group (128) granularity. For a real weight matrix from a model, measure mean and max relative error for each. Plot error vs group size.

B. The outlier experiment. Take a real activation tensor from a model (hook a forward pass). Plot its distribution. Find the outliers. Quantize with and without clipping the top 0.1% and compare error on the non-outlier values.

C. End-to-end quality. Quantize a small model to INT8 and INT4 (use llm-compressor or GPTQModel; AutoAWQ is archived and its methods moved into llm-compressor). Measure perplexity on a held-out set AND generate 20 long responses, comparing them qualitatively to FP16.

D. Phase asymmetry. Measure prefill and decode throughput for FP16 and INT4 weight-only on the same model. Confirm decode improves and prefill doesn’t. Explain using arithmetic intensity.

E. Scale overhead. Compute the exact bits-per-weight for INT4 with group sizes 32, 64, 128, and per-tensor. Plot bits/weight vs quality error from exercise A.


11. Interview questions#

  1. Explain symmetric INT8 quantization, including how the scale is computed.
  2. What is the outlier problem and why does it make activation quantization harder than weight quantization?
  3. Compare per-tensor, per-channel, and per-group quantization.
  4. Why does W4A16 speed up decode but not prefill? Give the mechanism.
  5. What is the effective bits-per-weight for INT4 with group size 128?
  6. Which parts of a model would you leave unquantized, and why?
  7. How would you validate a quantized model before deploying it?

12. Further reading#

  • [ESTABLISHED] Dettmers et al., “LLM.int8()” (2022) — the outlier discovery
  • [ESTABLISHED] Frantar et al., “GPTQ” (2022)
  • [ESTABLISHED] Lin et al., “AWQ” (2023)
  • [ESTABLISHED] Xiao et al., “SmoothQuant” (2022)
  • [REFERENCE] llm-compressor (vLLM’s quantization toolkit), GPTQModel, bitsandbytes documentation

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in