Source: Newsletter.maartengrootendorst.com Author: Maarten Grootendorst Date: July 22, 2024
Quantization compresses models from high precision (FP32) to low precision (INT8/INT4) for reduced memory and faster inference with minimal accuracy loss.
70B parameter model at FP32 = 280GB VRAM (impossible for most hardware)
| Type | Bits | Use case |
|---|---|---|
| FP32 | 32 | Full precision |
| FP16 | 16 | Half precision (standard) |
| BF16 | 16 | Better range than FP16 |
| INT8 | 8 | Common quantization target |
| INT4 | 4 | Aggressive compression |
- Range centered at zero
- Formula:
x_quant = round(x / s) - Scale:
s = max(|x|) / 127
- Range shifted from zero
- Includes zero-point offset
- Better for activations with asymmetry
Large outlier values reduce precision for most values:
- Clipping: Set dynamic range manually
- Calibration: Find optimal range (percentile, MSE, KL-divergence)
| Component | When known | Method |
|---|---|---|
| Weights | Static (before inference) | Per-layer calibration |
| Activations | Dynamic (during inference) | Dynamic or static quantization |
- Calculate scale/zeropoint per layer during inference
- More accurate, slightly slower
- Use calibration dataset beforehand
- Pre-compute scales
- Faster inference, less accurate
- Layer-by-layer processing
- Uses inverse-Hessian for error weighting
- Good for full GPU deployment
- CPU + GPU offloading
- Super/sub block quantization
- Many precision levels (Q2_K, Q4_K, Q5_K, Q8_0)
- Various optimizations
- 1-bit models (BitNet)
- Performance-focused
- "Fake" quantization during training
- Explores wide minima (lower quantization error)
- Better accuracy than PTQ
- Adds training cost
| Hardware | Recommended | Why |
|---|---|---|
| High-end GPU | GPTQ Q4/Q5 | Fast, good accuracy |
| Consumer GPU | GGUF Q4_K_M | CPU offload available |
| CPU only | GGUF Q5_K_M | Balance speed/quality |
| On-device | INT4 + QAT | Maximum compression |
70B params × 4 bytes (INT32) = 280 GB
70B params × 2 bytes (INT16) = 140 GB
70B params × 1 byte (INT8) = 70 GB
70B params × 0.5 byte (INT4) = 35 GB
- Mixture of Experts - Sparse params need quantization
- Mamba - Often quantized for deployment