llm-inference-notes / articles /quantization-for-serving.md
inference-engineering-academy's picture
Upload folder using huggingface_hub
cc5fceb verified
|
Raw History Blame Contribute Delete
3.71 kB
# Quantization for serving: FP8, INT8, INT4 tradeoffs
Quantization stores numbers in fewer bits. For serving, it does two things: it shrinks memory (more room for KV cache, fewer GPUs) and it cuts the bytes read per decode step (faster tokens when memory-bound). The question is always what you are quantizing and what it costs in quality.
## Three things you can quantize
1. **Weights only** (W4A16, W8A16): weights stored in 4 or 8 bits, dequantized to 16-bit for the matmul. Saves memory and bandwidth; compute stays 16-bit. Examples: AWQ, GPTQ.
2. **Weights and activations** (W8A8 in FP8 or INT8): matmuls run in 8-bit on tensor cores. Saves memory, bandwidth, and compute. Needs hardware support (FP8 tensor cores arrived with Hopper/Ada).
3. **KV cache** (FP8 KV): halves KV memory, so more concurrent tokens fit.
## The memory math (Llama 3.1 70B, 70.6B params)
| Format | Bytes/param | Weights |
|---|---|---|
| BF16 | 2 | 141 GB |
| FP8 / INT8 | 1 | 71 GB |
| INT4 | 0.5 (+ scales) | ~35 GB + a few % for scales |
So BF16 needs at least two 80 GB GPUs (and really four for useful KV space), FP8 just fits on one with little KV room, and INT4 fits on one with plenty of room.
## Speed: what to expect
At small batch sizes decode is bandwidth-bound, so halving weight bytes can approach ~2x faster decode in the ideal case, and 4-bit can approach ~4x vs 16-bit. Real gains are lower because of dequantization overhead, KV-cache reads, and kernels that are not perfectly efficient.
At large batch sizes decode becomes more compute-bound. Weight-only INT4 then helps less, and can even be slower than FP8 because the dequantize step adds work. W8A8 FP8 keeps helping because the matmuls themselves get faster on FP8 tensor cores.
Rule of thumb:
- **Low concurrency, latency-sensitive, or memory-starved:** weight-only INT4 (AWQ/GPTQ).
- **High throughput on Hopper or newer:** FP8 W8A8.
- **Older GPUs without FP8 (A100):** INT8 W8A8 (SmoothQuant-style) or INT4 weight-only.
## FP8 vs INT8
Both use 8 bits. INT8 is a uniform grid; FP8 (E4M3 for inference) has an exponent, giving more resolution near zero and a wider dynamic range. LLM activations have outliers, so FP8 usually needs less calibration effort than INT8 for the same quality. Many teams report near-baseline quality for FP8 on large models, but always verify on your own evals.
## Quality: how to check
- Run task evals you care about (your own test set first, then standard benchmarks such as MMLU or GSM8K).
- Check long-context and reasoning tasks separately; errors from quantized KV cache can accumulate over long sequences.
- Compare outputs on real prompts side by side; perplexity alone can hide regressions.
- Smaller models usually lose more quality from 4-bit than large ones.
## KV-cache quantization
For Llama 3.1 70B, KV per token is 2 x 80 layers x 8 KV heads x 128 x 2 bytes = 320 KiB in FP16, and 160 KiB in FP8. On a deployment with 40 GiB free for KV, that is about 131,000 tokens in FP16 vs about 262,000 in FP8, which roughly doubles how many long conversations you can serve at once.
## Practical checklist
1. Prefer a published, pre-quantized checkpoint from the model vendor or a trusted source.
2. Confirm your engine and GPU support the format with fast kernels (otherwise it may be slower than BF16).
3. Benchmark at your real concurrency, not just batch 1.
4. Run evals before and after; set a quality budget in advance.
5. Quantize the KV cache separately and test long contexts.
---
*If you want to go deeper, the free Foundations course at [Inference Engineering Academy](https://www.inference-engineering.xyz/) covers these topics with browser labs. Disclosure: written by the academy team.*