Skip to content

๐Ÿ”ข 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 โ€‹

FormatSign ยท Exp ยท MantMax valueNotes
FP321 ยท 8 ยท 23~3.4e38Matmul accumulators commonly output FP32
BF161 ยท 8 ยท 7~3.4e38FP32's range. Trains without hyperparameter tuning
FP161 ยท 5 ยท 1065,504Overflow โ†’ inf โ†’ NaN. A BF16-trained model can overflow in FP16
FP8 E4M31 ยท 4 ยท 3448No infinity (special codes reused for numbers; would be 240 otherwise). For weights & activations / inference
FP8 E5M21 ยท 5 ยท 257,344More range, for gradients
FP4 E2M11 ยท 2 ยท 16Needs 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.

ComparisonResult
NVIDIA illustration, MSE with power-of-2 scales vs E4M3 scales0.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 block20% โ†’ 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

  1. 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.
  2. Fewer bits โ‰  automatically faster math. Check the math rate for that format on that GPU.
  3. 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.)
  4. When you look at a 4-bit format, look at how it stores its scale first.
  5. 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.

<<>> with โ™ฅ๏ธ by S@Nchit