Quantization Theory & Fundamentals
Mathematical foundations of neural network quantization: uniform and non-uniform quantization, the rate-distortion tradeoff, calibration methods, per-tensor vs per-channel vs per-group scaling, and error analysis.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Uniform Quantization
- Symmetric vs Asymmetric
- Granularity: Per-Tensor, Per-Channel, Per-Group
- Calibration Methods
- Quantization Error Bounds
- Non-Uniform Quantization
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive uniform quantization and compute reconstruction error.
- Compare symmetric vs asymmetric quantization tradeoffs.
- Explain per-group quantization and its overhead calculation.
- Derive optimal calibration ranges (MinMax, percentile, MSE-optimal).
- Analyze quantization error propagation through neural network layers.
Notation
- — bit-width
- — scale factor
- — zero-point
- — group size
- — quantization step size
Core Intuition
Quantization maps continuous-valued weights/activations to a discrete set of levels (e.g., 256 levels for INT8, 16 for INT4). Fewer bits = less memory = faster inference (less data to load). The mathematical challenge: minimize the information lost by this discretization while maintaining model accuracy.
Interactive: Weight Quantization
MSE
7.52e-6
Max Error
0.0046
Compression
4.0×
Uniform Quantization
Quantize (float → int):
Dequantize (int → float):
Parameters:
Quantization error: .
Symmetric vs Asymmetric
Symmetric (zero-point ):
Range is symmetric around zero. Simpler computation (no zero-point offset).
Asymmetric (non-zero ): Uses full quantization range for asymmetric distributions. Better precision when data isn't centered at zero (e.g., ReLU activations are always non-negative).
Rule of thumb: Symmetric for weights (roughly zero-centered). Asymmetric for activations (often non-negative after ReLU).
Granularity: Per-Tensor, Per-Channel, Per-Group
Per-tensor: One for entire weight matrix. Coarsest; highest error.
Per-channel (per-output-channel): One per row of the weight matrix. Standard for INT8.
Per-group: One per group of consecutive weights:
For INT4 with : overhead = .
Tradeoff: Finer granularity → better accuracy, more metadata overhead, more complex kernel.
Calibration Methods
Finding optimal for quantization range:
MinMax: Use actual min/max values. Simple but sensitive to outliers.
Percentile: Use -th and -th percentile (e.g., ). Clips outliers.
MSE-optimal: Find range that minimizes :
Cross-entropy optimal: Minimize KL divergence between original and quantized output distributions.
Quantization Error Bounds
Single-layer error: For , quantization error in output:
For uniform quantization: .
Multi-layer accumulation: Errors compound multiplicatively through layers. Total error grows roughly as for well-conditioned networks.
Non-Uniform Quantization
Idea: Place quantization levels non-uniformly, with more levels where the weight distribution is dense.
k-means quantization: Find optimal levels by minimizing:
Log-scale quantization: Levels spaced logarithmically. Good for distributions with many small values and few large ones (common for weights).
NormalFloat (NF4): Quantization levels chosen for the normal distribution . Used in QLoRA.
Common Pitfalls
Pitfall 1. Using MinMax calibration with outlier-heavy activations. A single outlier 100x larger than typical values wastes most of the quantization range on empty space.
Pitfall 2. Quantizing small models (under 3B) to INT4. Larger models are more robust to quantization error because they have more redundancy.
Pitfall 3. Ignoring activation quantization. Even with INT4 weights, if activations remain in FP16, the memory bandwidth improvement is limited to weight loading (not activation communication).
Summary
- Uniform quantization: Linear mapping with scale and zero-point.
- Granularity determines accuracy-overhead tradeoff: per-group () is standard for INT4.
- Calibration (MinMax, percentile, MSE) determines quantization range.
- Error scales as per layer; compounds across layers.
- Non-uniform (NF4, k-means) adapts to weight distribution for better precision.
Exercises
Exercise 1. Compute the quantization step size for INT4 symmetric quantization of weights in range .
Exercise 2. Derive the storage overhead of per-group quantization with FP16 scales for and .
Exercise 3. For a Gaussian distribution : compute the optimal percentile clipping for INT8 that minimizes MSE.
Exercise 4. Show that per-channel quantization is equivalent to per-group with number of input features.
Exercise 5. Compute the NF4 quantization levels for (16 levels placed at quantiles).