20.1 Quantization: Math to Code
Decoding a large language model is limited by memory bandwidth: every generated token requires reading all of the model’s weights, and a growing KV cache, out of GPU memory. Quantization attacks that bottleneck directly. Store each weight in 4 or 8 bits instead of 16, and there are fewer bytes to move, so decode gets faster and more memory is left for the KV cache. The compute is usually still done in FP16 or BF16 after dequantization (weight-only quantization, written “WxA16”), or in INT8 or FP8 when activations are quantized too (“WxAx”).
Concepts from other sections (8)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- KV cache: the stored keys and values of all processed tokens, per layer, that let each new token attend without recomputation. Read more: Chapter 20: Inference Engines and GPU Deployment
- fp16: a 16-bit format with 5 exponent bits and 10 mantissa bits; finer precision but a maximum of 65504 and a smallest subnormal of . Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
- BF16: a 16-bit float with float32’s sign and exponent and 7 mantissa bits; the top half of a rounded float32. Read more: 6.7 Safetensors for every dtype, atomic checkpoints, and the token stream
- symmetric quantization: affine quantization with , used for roughly zero-mean weights to avoid GEMM cross-terms. Read more: 20.1.4 Choosing and validating quantization
- Hessian: the symmetric matrix of second partial derivatives, whose eigenvalues classify a critical point. Read more: 10.3 Calculus 3 problem set: vectors, gradients, the chain rule, Hessians, multiple integrals, Lagrange in Mathematics
- dequant-fused GEMM: a kernel that dequantizes low-bit weights in registers or shared memory immediately before the matrix multiply. Read more: 20.1.4 Choosing and validating quantization
- GGUF: llama.cpp’s single-file quantized model container, readable across llama.cpp, candle, mistral.rs, and Zig bindings. Read more: 20.2 Inference Frameworks: The Cross-Language Landscape
- K-quants / i-quants: llama.cpp’s weight quantization schemes for GGUF files; k-quants mix precision per block, and i-quants choose precision guided by an importance matrix measured on sample data. Read more: Chapter 26: Edge and Realtime Inference
This section develops quantization math first, then the code that implements it. Every quantizer you will meet is “just” a way of mapping high-precision numbers to a few bits while controlling the distortion that mapping introduces, and the methods differ entirely in which math they use to pick the mapping. We start with the ten-line core, affine and symmetric quantization, then work through granularity, distortion, and the kernels that turn fewer bits into real speed, before walking the history of methods from LLM.int8() to TurboQuant and closing with practical guidance. Examples are in C, CUDA, and Rust (Burn), targeting the 2025/2026 inference stack.
This section builds on the chapter opening, LLM Systems & Inference, which explains why decode is bandwidth-bound and why the KV cache dominates memory. The math draws on Linear Algebra (Hessians, Cholesky factors, orthogonal and Hadamard matrices) and Information Theory (rate-distortion) from Mathematics, and the code assumes basic C and CUDA. Standalone, compilable examples live in code/.
Learning objectives
- Derive and implement affine and symmetric quantization, and explain how granularity trades metadata for accuracy.
- Write down the distortion of a uniform quantizer and explain how calibration chooses the clipping threshold.
- Explain why a quantizer only pays off with a dequant-fused GEMM kernel.
- For each major method (GPTQ, AWQ, SmoothQuant, NF4, GGUF k-quants, FP8, QuIP#, TurboQuant), state the distribution it assumes, the error it minimizes, and the extra state it stores.
- Choose a quantizer for a given goal and hardware, including KV-cache quantization.
In this chapter
Section titled “In this chapter”- Quantization error and granularity: Derive quantization error for a stated scale and grouping rule.
- Calibration and kernel execution: Explain how calibration and dequantization kernels affect quality and speed.
- Weight and KV quantization methods: Distinguish weight compression from cache compression and their error sources.
- Choosing and validating quantization: Choose a supported format and validate it on the deployment workload.