Skip to main content
Quantization Deep Dive

Quantization Deep Dive

Why Quantization?

Think of quantization like compressing a high-resolution photograph into a JPEG: you are trading some fidelity for a dramatically smaller file that loads faster. In neural networks, we replace 32-bit floating-point weights with lower-precision integers (8-bit, 4-bit, or even fewer). The model gets smaller, inference gets faster, and — if done carefully — accuracy barely moves. This is not a theoretical trick: virtually every model you interact with on your phone (keyboard prediction, face unlock, voice assistant) runs quantized. The math is simple but the gains are dramatic:
Quantization is not free lunch. Models with very narrow weight distributions (e.g., small transformer layers) quantize well, but models with outlier weights (common in large language models) can degrade significantly. Always measure accuracy after quantization — do not assume the table above applies to your specific model.

Quantization Fundamentals

At its core, quantization is a mapping problem: given a continuous range of floating-point values, map them to a smaller set of discrete integer values. The art is choosing this mapping so that the values the model cares about most (the ones that actually affect predictions) are represented accurately, while values in less-important ranges can tolerate more error. Think of it like a piano: FP32 is a piano with thousands of keys spanning the full audible range. INT8 is a piano with only 256 keys. Quantization decides which 256 pitches to include. A good quantization scheme puts more keys where the music actually plays (near the weight distribution’s center) and fewer keys in the rarely-used extremes.

Number Representation

Granularity Levels


Post-Training Quantization (PTQ)

PTQ is the “quick and easy” path to quantization: take a fully-trained FP32 model and convert it to lower precision without any additional training. The appeal is obvious — no retraining means no GPU hours, no hyperparameter tuning, and you can quantize models you do not even have the training data for. The trade-off is that PTQ typically loses more accuracy than QAT (Quantization-Aware Training), especially at very low bit widths (4-bit).
For most models, start with dynamic quantization (quantize weights, compute activation scales at runtime). It requires zero calibration data and provides 2-4x speedup on CPU inference with typically under 1% accuracy loss. Only move to static quantization if dynamic does not meet your latency requirements, since static quantization requires collecting calibration data but provides additional speedup by pre-computing activation scales.

Quantization-Aware Training (QAT)


Advanced Quantization Methods

The methods above (PTQ, QAT) work well for models up to a few hundred million parameters. But large language models (LLMs) with billions of parameters present unique challenges: they have outlier activations that break standard quantization, retraining them (QAT) costs millions of dollars, and they need to run on consumer hardware. The following methods were specifically designed for this regime.
For quantizing LLMs in practice, the landscape in 2024-2025 has converged: use GPTQ or AWQ for weight-only quantization (good for inference), and use bitsandbytes for training (QLoRA). The choice between GPTQ and AWQ is mostly about toolchain preference — accuracy is similar. For models under 7B parameters, INT8 is usually sufficient. For 13B+ models, INT4 with group quantization (group_size=128) gives the best size-accuracy trade-off.

GPTQ (LLM Quantization)


Mixed-Precision Quantization


Hardware-Aware Quantization


Exercises

Implement and compare:
  • Min-max calibration
  • Percentile calibration
  • Entropy calibration
Measure accuracy vs. calibration set size.
Implement 4-bit quantization with:
  • Group size of 128
  • Separate scale per group
  • Compare to per-tensor quantization
Implement automated mixed-precision search:
  • Use sensitivity analysis
  • Optimize for target model size
  • Meet accuracy constraint

What’s Next?

Knowledge Distillation

Transfer knowledge to smaller models

Continual Learning

Learn new tasks without forgetting