20.1.2 Calibration and kernel execution
This part of Quantization: Math to Code develops the mechanisms below. Work the numerical examples or trace the code before checking the answer.
Concepts from other sections (6)
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.
- rounding error: the error from floating-point rounding, about in a difference quotient. Read more: 7.1 Derivatives and Finite Differences in Mathematics
- Variance: the expected squared distance from the mean; its square root is the standard deviation. Read more: 13.1 Random variables, expectation, variance, and normal draws by Box-Muller in Mathematics
- granular noise: the rounding error of a uniform quantizer, with variance . Read more: 20.1.4 Choosing and validating quantization
- 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
- W4A16 / W8A8: notation for weight and activation bit-widths, here 4-bit weights with 16-bit activations and 8-bit weights with 8-bit activations. Read more: 20.1.4 Choosing and validating quantization
- Dot product: ; it measures how much two vectors point the same way, and it is zero exactly when they are perpendicular. Read more: Chapter 10: Calculus 3 in Mathematics
Learning objectives
- Explain how calibration and dequantization kernels affect quality and speed.
Distortion, clipping, and calibration
Section titled “Distortion, clipping, and calibration”Granularity decides how many ranges you have. Calibration decides what each range should be. For a uniform quantizer with step , the granular noise (the rounding error) is uniform on , so its variance is . If you clip at threshold , the step is , and the total distortion has two parts:
A smaller shrinks the granular noise but raises the clipping error. Calibration picks , and there are three common strategies. Min-max sets : there is no clipping, but a single outlier blows up for everything else. Percentile sets to, say, the 99.9th percentile, deliberately clipping the tail. MSE-optimal calibration runs a grid or golden-section search over to minimize the measured , which is what AWQ and GPTQ calibration and compressed-tensors do.
Stated abstractly, this is a rate-distortion problem: at a fixed rate , choose the quantizer that minimizes expected distortion for the source distribution.
Where the speed comes from: dequant-fused GEMM
Section titled “Where the speed comes from: dequant-fused GEMM”All of this saves memory, but memory saved is not time saved unless the kernel exploits it. The pattern for weight-only INT4 (W4A16) has three steps. First, load the 4-bit weights from HBM, using a quarter of the bandwidth FP16 would need. Second, dequantize in registers or shared memory to FP16 just before the tensor-core matmul. Third, accumulate in FP32.
This is what Marlin (W4A16 on Ampere and Ada) and Machete (Hopper) do in vLLM, and what CUTLASS’s mixed-input GEMM provides. For W8A8 the matmul itself runs in INT8 (mma.sync or dp4a), and you dequantize in the epilogue: . The dp4a dot product in code/symmetric_quant.cu shows this.
Warning: A quantizer is only as good as its kernel. INT4 with a slow Python dequantization loop is slower than FP16. Always check that a fused kernel (Marlin, Machete, CUTLASS, tinygemm) exists for your format on your GPU, and profile the dequant-plus-GEMM path, not just the quantizer.
Summary
Section titled “Summary”Explain how calibration and dequantization kernels affect quality and speed. State the assumptions before applying the result to a different workload.
Review questions
Section titled “Review questions”-
Can a smaller checkpoint execute more slowly?
Answer
Yes. Missing fused kernels, unpacking overhead, shape mismatch, and device support can erase the bandwidth saving.