Topic 31 of 62 · Generating and serving
Quantization: memory vs quality
MediumHow does quantization map weights or activations into lower bit-widths, and why doesn’t 4-bit weight quantization always make inference 4× faster?
1 · Start with the intuition
Quantization compresses continuous floating-point numbers (such as 16-bit BF16/FP16) into a small grid of low-bit integers or floats (such as INT8, FP8, or INT4) using a scale factor s (and an optional zero-point z). Storing weights in 4 bits instead of 16 bits cuts raw weight memory and weight HBM traffic by nearly 4×—which speeds up memory-bandwidth-bound decode at small batch sizes—but it does not speed up compute-bound prefill unless the activations are also quantized to a format supported by faster low-precision tensor cores (like FP8), and dequantization overhead plus KV cache traffic still remain.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
Calibrate scale (per-channel / group)
Choose a scale s (e.g., per output channel or per group of 64–128 weights) so outlier values do not crush the resolution of normal values.
Next, we will look at: Round & clip to integer grid. Move on when this operation makes sense.
Visualizing the behavior
Round-to-nearest on a grid of step s = 0.1
Derived- exact (x̂ = x)
- quantized
Raw weight storage for 7B parameters (decimal GB)
Derived3 · The governing equation
Affine quantization: q = clip( round(x / s) + z, q_min, q_max ) Dequantized value: x̂ = s · (q − z) Raw weight footprint: bytes ≈ P × (bits / 8) + scale/zero-point metadata
Notation: s > 0 is the scale factor, z is the integer zero-point (z = 0 for symmetric quantization), and [q_min, q_max] is the representable integer range (e.g., [−8, 7] for signed 4-bit).
Distinguish two families of quantization: (1) Weight-only quantization (e.g., W4A16 GPTQ or AWQ) keeps activations in BF16/FP16, loads 4-bit weights from HBM, and dequantizes them in registers/SRAM right before the BF16 matrix multiply. Because it reduces HBM bytes moved without reducing matmul FLOPs, it speeds up bandwidth-bound small-batch decode while leaving compute-bound prefill largely unchanged (or slightly slower due to dequantization instructions). (2) Weight-and-activation quantization (e.g., FP8 W8A8 on H100) quantizes both operands so the hardware can use 2× faster FP8 tensor cores, helping both prefill and decode.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Claiming “4-bit quantization gives a 4× speedup across all batch sizes and no quality drop on any task.” At large batches or during prefill, the GPU is compute-bound (so W4A16 gives little or no speedup), and coding, math, or long-context tasks are much more sensitive to quantization error than aggregate perplexity.
In practice · implementation & engineering rule of thumb
Match the quantization scheme to your bottleneck: use W4A16 (AWQ/GPTQ) when memory capacity or low-batch decode bandwidth binds; use FP8 (W8A8) on Hopper/Blackwell when high-batch prefill/decode compute binds.
# Symmetric per-group quantization (group_size=128, int4 range [-7, 7]): w_grouped = weight.reshape(-1, 128) scale = w_grouped.abs().amax(dim=-1, keepdim=True) / 7.0 q_int4 = torch.clamp(torch.round(w_grouped / scale), -7, 7).to(torch.int8) w_dequant = (q_int4.float() * scale).reshape_as(weight)
Go one level deeper
Follow-ups that often come after a correct first answer.
- Outliers and scale granularity
- If a tensor’s max |x| is 20 but most values are below 1, absmax INT8 gives s = 20/127 ≈ 0.157, and every value under about 0.08 rounds to zero. Per-channel or per-group scales limit the damage; LLM.int8() keeps the outlier feature dimensions in FP16.
- GPTQ vs AWQ
- Both are weight-only post-training methods that use a small calibration set. GPTQ quantizes column by column and uses approximate second-order information to push each rounding error onto the weights not yet quantized. AWQ finds salient channels from activation magnitudes and rescales them before quantizing to protect them.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWhy do activation outliers make W8A8 integer quantization harder than weight-only quantization?
Show follow-up reasoning
Model weights are fixed after training, so you can inspect them offline and group them into small blocks of 64–128 elements with dedicated scales. Activations change dynamically with every token and often develop systematic extreme outlier channels (10–100× larger than typical channels) in large Transformers; a single per-tensor activation scale crushes all non-outlier channels to near zero unless techniques like SmoothQuant (migrating scale difficulty from activations into weights) or higher-dynamic-range FP8 are used.