๐ข Chapter 7 โ Quantization & Numerics โ
Episode 28 ยท lever pulled: move fewer bytes
1. Every value FP4 (E2M1) can store โ
- 16 codes, 15 distinct values: ยฑ{0, 0.5, 1, 1.5, 2, 3, 4, 6} (with +0 and โ0)
- Step sizes: 0.5 (0 โ 2), 1 (2 โ 4), 2 (4 โ 6)
- Max = 6, only 12ร the smallest non-zero value. BF16's max is ~3.4 ร 10ยณโธ.
So how do BF16 weights fit in 16 codes? Scale factors: each small block of values gets its own scale.
2. Anatomy of a float โ
| Format | Sign ยท Exp ยท Mant | Max value | Notes |
|---|---|---|---|
| FP32 | 1 ยท 8 ยท 23 | ~3.4e38 | Matmul accumulators commonly output FP32 |
| BF16 | 1 ยท 8 ยท 7 | ~3.4e38 | FP32's range. Trains without hyperparameter tuning |
| FP16 | 1 ยท 5 ยท 10 | 65,504 | Overflow โ inf โ NaN. A BF16-trained model can overflow in FP16 |
| FP8 E4M3 | 1 ยท 4 ยท 3 | 448 | No infinity (special codes reused for numbers; would be 240 otherwise). For weights & activations / inference |
| FP8 E5M2 | 1 ยท 5 ยท 2 | 57,344 | More range, for gradients |
| FP4 E2M1 | 1 ยท 2 ยท 1 | 6 | Needs per-block scales |
FP8 recipe: scale each tensor so its max magnitude โ the FP8 max, then clamp overflow. Casting with no scale caused significant accuracy loss. One scale per tensor works for FP8 (E4M3 spans ~18 doublings) but not for FP4 (~3.5 doublings): after scaling, anything below 0.25 (half of the smallest step) rounds to zero.
3. ๐งฎ NVFP4 worked example (one block) โ
Block = [10, 20, 30, 40] (a 4-value toy; real NVFP4 blocks have 16)
Values near the top of a block take the biggest absolute rounding errors, because that's where the step is 2 wide.
4. NVFP4 vs MXFP4 โ
Why power-of-two scales hurt โ
Block max โ 3.1. The ideal scale is just over 0.5. With powers of two, 0.5 would push the top value past 6, so you round up to 1. The top value then reaches only ~3, codes 4 and 6 go unused, and you lose almost a whole doubling out of ~3.5.
| Comparison | Result |
|---|---|
| NVIDIA illustration, MSE with power-of-2 scales vs E4M3 scales | 0.72 vs 0.08 (~9ร lower) |
| Meta analysis (Llama-3.1-8B-Instruct, Qwen3-8B): finer E4M3 scale | +3โ4 dB QSNR |
| Same: halving the block 32 โ 16 | +~1 dB QSNR |
| Same: activations rounded to zero, 32 โ 16 block | 20% โ 13% |
The scale's precision matters more than the block size. The largest value in each NVFP4 block comes back at close to FP8 precision.
5. โ Before you pick a format โ
Checklist
- Fewer bits = fewer bytes, so decode (memory-bound) speeds up. For a batch-1 8B model on an H100: FP16 โ FP8 gives exactly 2ร (209 โ 418 tok/s). See Chapter 2.
- Fewer bits โ automatically faster math. Check the math rate for that format on that GPU.
- Accuracy loss at 4 bits depends on the model, so measure on yours. (Remember the attention sinks' massive activations, ~2000 vs a median of 0.2.)
- When you look at a 4-bit format, look at how it stores its scale first.
- The ridge point moves with precision: H100 FP16 = 295 โ FP8 โ 590.
The 16 codes never had to cover the whole model's range. The scales carry the range; the codes only say where a value sits inside its block.
