Quantization là giảm độ chính xác của weights (và/hoặc activation) để tiết kiệm memory và tăng tốc inference, đánh đổi một chút chất lượng.
Data types phổ biến:
- FP32 (32-bit float) — training gốc, độ chính xác cao nhất, memory nhất.
- BF16 / FP16 (16-bit) — tiêu chuẩn serving hiện tại. Giảm 2x memory vs FP32, tốc độ ~2x trên GPU có Tensor Core (A100, H100). BF16 có range tương đương FP32 (ổn định hơn FP16, ít overflow).
- INT8 — 4x smaller than FP32. Giảm chất lượng nhỏ (~1-2% với method tốt), tốc độ inference nhanh (phần cứng GPU/CPU có INT8 kernel).
- INT4 / NF4 — 8x smaller. Mất chất lượng nhiều hơn (~3-5%) nhưng chấp nhận được với model lớn (≥7B).
- INT2, binary — thử nghiệm; chỉ model rất lớn mới chịu được.
Memory math LLaMA-70B: FP16 = 140GB; INT8 = 70GB (fit 1×A100 80GB); INT4 = 35GB (fit 1×RTX 3090 24GB với offloading).
Phương pháp quantize:
1. Post-Training Quantization (PTQ) — quantize model đã train xong. Nhanh, không cần lại dataset. Kỹ thuật:
- GPTQ — per-layer optimization với small calibration dataset. Phổ biến nhất cho INT4.
- AWQ (Activation-aware Weight Quantization) — bảo vệ weights quan trọng theo magnitude activation.
- GGUF (llama.cpp) — nhiều mức (Q2-Q8), dùng cho CPU và Apple Silicon.
2. Quantization-Aware Training (QAT) — simulate quantization khi training/fine-tune. Chất lượng cao nhất, tốn compute.
3. Mixed precision — giữ các layer nhạy cảm ở độ chính xác cao, layer khác quantize thấp.
Serving stack: bitsandbytes (Python, dễ dùng), vLLM + GPTQ/AWQ (production), llama.cpp (CPU/edge), TensorRT-LLM (NVIDIA tối ưu nhất).
Nguyên tắc: model càng lớn càng chịu được quantization sâu. Với model <3B, tránh INT4 vì chất lượng drop mạnh. Luôn evaluate lại sau quantize — benchmark chung có thể không phản ánh task của bạn.
Quantization reduces weight (and sometimes activation) precision to save memory and speed up inference, trading a bit of quality.
Common data types:
- FP32 (32-bit float) — original training precision, highest fidelity, most memory.
- BF16 / FP16 (16-bit) — current serving standard. 2x less memory than FP32, ~2x faster on Tensor Core GPUs (A100, H100). BF16 matches FP32 range (more stable, fewer overflows than FP16).
- INT8 — 4x smaller than FP32. Minor quality drop (~1–2% with good methods), fast inference (GPU/CPU INT8 kernels).
- INT4 / NF4 — 8x smaller. More quality loss (~3–5%) but acceptable for large models (≥7B).
- INT2, binary — experimental; only very large models survive.
Memory math LLaMA-70B: FP16 = 140GB; INT8 = 70GB (fits one A100 80GB); INT4 = 35GB (fits one RTX 3090 24GB with offloading).
Quantization methods:
1. Post-Training Quantization (PTQ) — quantize a trained model. Fast, no retraining. Techniques:
- GPTQ — per-layer optimization with a small calibration set. The most popular for INT4.
- AWQ (Activation-aware Weight Quantization) — protects important weights by activation magnitude.
- GGUF (llama.cpp) — many levels (Q2–Q8), for CPU and Apple Silicon.
2. Quantization-Aware Training (QAT) — simulate quantization during training/fine-tuning. Highest quality, compute-heavy.
3. Mixed precision — keep sensitive layers at higher precision, quantize the rest.
Serving stack: bitsandbytes (Python, easy), vLLM + GPTQ/AWQ (production), llama.cpp (CPU/edge), TensorRT-LLM (most NVIDIA-optimized).
Rule of thumb: bigger models tolerate deeper quantization. Below ~3B, avoid INT4 — quality drops hard. Always re-evaluate post-quantization — public benchmarks may not reflect your task.