Quantization for Transformer Inference
Reducing precision for faster inference: INT8/INT4 weight quantization, activation quantization, GPTQ, AWQ, SmoothQuant, per-channel vs per-group calibration, and the theory of quantization error bounds.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Uniform Quantization Theory
- Weight-Only Quantization (W4A16)
- GPTQ: Optimal Brain Quantization
- AWQ: Activation-Aware Weight Quantization
- SmoothQuant (W8A8)
- Per-Channel vs Per-Group Calibration
- Error Analysis and Bounds
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive uniform quantization and compute the reconstruction error.
- Explain why weight-only quantization is effective for memory-bound inference.
- Derive the GPTQ algorithm from optimal brain surgery.
- Explain how AWQ exploits activation magnitudes to protect salient weights.
- Prove how SmoothQuant migrates quantization difficulty from activations to weights.
Notation
- — scale factor
- — zero-point
- — bit-width (4 or 8)
- — Hessian of the layer-wise loss
- — group size for per-group quantization
Core Intuition
A 70B model in FP16 requires 140 GB — too large for most GPUs. Quantization represents the same weights with fewer bits (INT4 = 4 bits per weight → 35 GB). For memory-bound inference, this directly translates to speedup: less data to load from memory means faster generation. The challenge: minimize the accuracy loss from reduced precision.
Interactive: Weight Quantization
MSE
7.52e-6
Max Error
0.0046
Compression
4.0×
Uniform Quantization Theory
Forward quantization:
Dequantization:
Scale and zero-point from range :
Symmetric quantization (zero-point = 0): .
Quantization error for a single weight:
Weight-Only Quantization (W4A16)
Strategy: Quantize weights to INT4; keep activations in FP16.
Why it works for inference: Generation is memory-bound → loading weights is the bottleneck. INT4 weights are 4x smaller → 4x less memory bandwidth → ~4x speedup.
Computation: Dequantize weights on-the-fly during matrix multiply:
Modern GPU kernels fuse dequantization with GEMM — negligible overhead.
GPTQ: Optimal Brain Quantization
Problem: Quantize each weight to minimize the output error of the layer:
Algorithm (column-by-column):
- For column : quantize weight , compute error .
- Compensate: Distribute the error to remaining unquantized columns:
where is the Hessian of the layer loss.
Intuition: After quantizing one weight with error, adjust remaining weights to compensate, weighted by their sensitivity (Hessian).
Result: Near-lossless INT4 quantization for models up to 175B parameters.
AWQ: Activation-Aware Weight Quantization
Key observation: Not all weights are equally important. Weights that correspond to large activation channels have more impact on the output.
Salient weight identification: For each weight column , compute the average activation magnitude:
Strategy: Protect important weights by per-channel scaling before quantization:
Choose larger for salient channels → reduces their quantization error at the expense of less important channels.
Optimal scaling: (balances activation and weight quantization ranges).
SmoothQuant (W8A8)
Problem: Activations have outlier channels (values 100x larger than average), making activation quantization difficult. Weights are well-behaved.
Solution: Migrate the quantization difficulty from activations to weights:
where balances the ranges.
Effect: Smoothed activations have smaller outliers (easier to quantize). Scaled weights absorb the outlier magnitude.
Result: Enables INT8 quantization for both weights AND activations (W8A8) → can use INT8 tensor cores for 2x compute speedup.
Per-Channel vs Per-Group Calibration
Per-tensor: One for the entire weight matrix. Coarse; high error.
Per-channel: One per output channel. Better accuracy; standard for INT8.
Per-group: One per group of weights (e.g., ). Fine-grained; standard for INT4.
For : overhead = additional storage.
Tradeoff: Smaller → better accuracy, more overhead. is the standard for INT4; for INT3.
Error Analysis and Bounds
Layer-wise error: The output error from quantizing layer :
Error accumulation across layers: Errors compound:
Empirical observation: INT8 quantization typically adds perplexity increase. INT4 with GPTQ/AWQ: perplexity increase for models .
Smaller models are harder to quantize: The relative error is larger when there are fewer weights to distribute the error across.
Common Pitfalls
Pitfall 1. Quantizing small models () to INT4. The quality degradation is substantial — INT8 is safer for small models.
Pitfall 2. Ignoring calibration data quality. GPTQ and AWQ require representative calibration data. Using random data or a mismatched domain leads to poor quantization.
Pitfall 3. Assuming W4A16 provides 4x compute speedup. It provides 4x memory bandwidth reduction (faster weight loading) but activations are still FP16, so tensor core utilization doesn't improve. W8A8 (SmoothQuant) provides actual compute speedup via INT8 tensor cores.
Summary
- W4A16 (GPTQ, AWQ): 4x memory reduction → 4x bandwidth speedup. Near-lossless for large models.
- W8A8 (SmoothQuant): 2x memory + 2x compute speedup via INT8 tensor cores.
- GPTQ: Hessian-aware error compensation; optimal per-column quantization.
- AWQ: Protect salient weights via activation-aware scaling.
- SmoothQuant: Migrate quantization difficulty from activations to weights.
- Per-group quantization () is standard for INT4 quality.
Exercises
Exercise 1. Compute the memory size of LLaMA-70B in FP16, INT8, and INT4 (with per-group overhead).
Exercise 2. Derive the optimal scale factor that minimizes MSE for symmetric quantization of a uniform distribution .
Exercise 3. Explain why GPTQ processes columns in order and compensates remaining columns, rather than quantizing all independently.
Exercise 4. For SmoothQuant with : compute the smoothing factor given and .
Exercise 5. Compute the effective bits-per-weight for INT4 quantization with group size 128, accounting for the FP16 scale and zero-point overhead.