Lesson 2: How Quantization Actually Works
Lesson 2: How Quantization Actually Works
A 4-bit number can only hold 16 distinct values (2โด). If you rounded every weight to one of 16 values, the model would collapse โ weights vary across huge ranges. The trick is grouped quantization with scaling:
- Split the weight matrix into blocks (typically 32-128 weights each).
- Record one scale per block โ a normal float that captures that block's magnitude (min/max or similar).
- Store each weight in the block as a small integer relative to the scale (e.g. 0-15 for 4-bit).
- At inference, each value is reconstructed as
scale ร inton the fly.
The scale is the hidden cost: a 4-bit quant is really ~4.85 bits per weight once you count the scales and metadata. That number โ total bits divided by parameter count โ is bits per weight (bpw), and you'll see it in filenames like 3.69bpw.
Why K-quants say "K" and why they're better
The old Q4_0/Q4_1 scheme used one simple scale per block. K-quants (the K in Q4_K_M) use k-means-style codebooks and mixed precision: attention and output layers get more bits, feed-forward layers get fewer. Since attention weights do the heavy lifting in reasoning, this buys quality at almost no size cost. The second letter is the size tier: S (small โ fewer tensors upgraded), M (medium โ the default), L/XL (large โ more tensors at higher precision).
Weights vs activations: W4A16
Weights are static โ you can spend hours calibrating them. Activations (the data flowing through the network at inference time) are dynamic and full of wild outliers, so they're much harder to quantize. Most local formats are W4A16: 4-bit weights, 16-bit activations. When you see W8A8 or W4A8, both weights and activations are quantized โ that's what the big serving engines (vLLM, TensorRT) do for throughput.
Not all errors are equal โ calibration is the 2024-2026 revolution
Roughly 1% of weights are "outliers" that dominate the model's behavior. Blind rounding destroys them; calibrated quantization runs a sample of text through the model, measures which weights matter, and protects them:
- imatrix โ an importance matrix from calibration data, used when quantizing GGUFs.
- AWQ โ activation-aware: scales the important channels before rounding.
- GPTQ โ error-correction pass that nudges remaining weights to compensate for rounded ones.
- QAT โ the model itself is fine-tuned while simulating quantization, so it learns to be robust to it.
The deep lesson
Quantization error compounds with model size and task difficulty. A well-calibrated 4-bit quant of a 27B model is often smarter than an 8-bit quant of a 7B model โ bigger base + decent compression beats smaller base + lossless. That's the entire game of local LLMs.
🧠 Knowledge Check
1. In grouped quantization, what does the scale do?
2. The 'K' in Q4_K_M refers to:
3. W4A16 means: