Quantization & Model Compression
Every modern AI deployment faces an inescapable hardware reality: memory bandwidth and VRAM capacity scale much slower than compute FLOPS. A 70-billion parameter model stored in 16-bit floating point requires 140 GB of VRAM for weights alone—demanding two 80GB datacenter GPUs and costing tens of thousands of dollars per year.
Quantization is the mathematical discipline of mapping continuous high-precision real numbers into compact, discrete, low-bit representations (FP8, INT8, INT4):
This chapter details the mathematical formulas, hardware execution mechanics, and outlier protection strategies behind modern quantization.
Imagine you have a high-resolution color photograph with 16 million colors. If you need to send that photo over a slow radio connection, sending all 16 million color values takes forever.
Instead, you create a 256-color palette (INT8) or a 16-color palette (INT4). Every pixel in the image is assigned the nearest color number in your palette. To the human eye, the compressed image looks 99% identical to the original, but the file size is 4x to 8x smaller and sends in a fraction of the time.
In neural networks, Quantization does the exact same thing to model weights: it replaces 16-bit numbers with 8-bit or 4-bit palette indexes.
1. The Numerical Precision Hierarchy
Explore the bit layout and hardware trade-offs of modern precision formats:
8-bit signed integer [-128, 127]q = round(x / Scale)2. Uniform Quantization Mathematics
The objective of uniform quantization is to map real floating-point values into signed integers .
The Quantization Equation
Where:
- represents rounding to the nearest integer.
- is the Scale Factor, representing the step size between consecutive discrete integers.
- is the Zero-Point, an integer offset ensuring that real zero () maps exactly to a representable integer without rounding error (crucial for zero-padding in convolutions and attention masks).
De-quantization
To perform subsequent operations or decode logits, integers are mapped back to real values:
3. Post-Training Quantization (PTQ) vs Quantization-Aware Training (QAT)
Quantization can be applied at two distinct lifecycle stages:
Straight-Through Estimator (STE) in QAT
Because the rounding operator has a derivative of zero almost everywhere (), standard backpropagation cannot backpropagate gradients through quantization.
QAT introduces the Straight-Through Estimator (STE):
- During the forward pass: Use the true discrete quantized value .
- During the backward pass: Treat the rounding operation as an identity function (), allowing gradients to flow back into floating-point master weights!
4. The Outlier Problem & 4-bit Quantization (AWQ & GPTQ)
When quantizing large language models below 8 bits (e.g. 4-bit), naive rounding causes severe perplexity degradation.
Researchers discovered why: in transformers with parameters, activation outliers emerge in specific hidden dimensions. While these outlier channels make up less than 1% of all activations, they carry over 70% of the model's semantic reasoning capability!
AWQ: Activation-aware Weight Quantization
Instead of quantizing all weights uniformly, AWQ (Lin et al., MIT HAN Lab):
- Observes activation magnitudes across calibration samples.
- Identifies the top 1% salient weight channels corresponding to high-magnitude activation spikes.
- Scales salient channels up before quantizing, protecting them from rounding noise:
- Achieves 8x VRAM compression with virtually indistinguishable perplexity from FP16!
5. Python Simulation: Symmetric INT8 Quantizer
Run this live in-browser Python implementation to verify symmetric INT8 quantization, scale calculation, and error metrics:
- Datacenter GPUs (H100 / Blackwell): Use FP8 (E4M3) for native Tensor Core hardware throughput without calibration overhead.
- Serving 70B+ Models on Single GPUs: Use INT4 (AWQ) to fit 70B models into 36 GB VRAM with near-zero perplexity loss.
- Ultra-Low-Power Microcontrollers (TinyML): Use Symmetric INT8 (Per-Tensor or Per-Channel) for hardware DSP acceleration with zero floating-point unit (FPU) requirement.
See how quantized models execute on bare-metal hardware with zero dynamic memory allocation in Edge AI & TinyML Architecture.