Quantization
Quantization stores the model's numbers with fewer bits, like FP8 or INT4 instead of BF16. It saves memory and memory reading, so decode gets faster and more chats fit on a GPU. But it adds small rounding errors, and the quality loss is often uneven between tasks and languages. Choose the format by hardware and workload, and prove quality with evals split by group.
Author: bezzad
Contents
The problem: half the cost, the same answers?
GPU rent is the biggest line in our shop assistant’s bill. A teammate opens a Pull Request that changes one line: the 8B model now runs with INT4 weights. “The weights are 4 times smaller. I tried 20 prompts and the answers look the same.”
Two weeks later, support sees more broken return forms, and some Persian customers get strange answers. On average, the model was fine. In some groups, it was not.
What quantization is
A model is billions of numbers. Normally each is stored in BF16: 16 bits, 2 bytes. Quantization stores them with fewer bits.
- Rounding. With 4 bits there are only 16 possible values. Each weight is rounded to the nearest one.
- Scales. To keep the rounding small, numbers are split into small groups, and each group gets its own scale factor. That is why INT4 is “about 4 to 5 GB”, not exactly 4.
- Smart methods. Methods like GPTQ and AWQ choose the rounding so that the most important weights lose the least. They need a small set of example text (calibration data).
- The result is a small error in every layer. Usually small enough. Sometimes not, as we will see.
What can be quantized
| What | Common formats | Main win | Notes |
|---|---|---|---|
| Weights only | INT4 (AWQ, GPTQ), INT8 | Less memory, faster decode at small batches | The math still runs in 16 bits inside the kernel. |
| Weights and activations | FP8, INT8 | Also faster math, so prefill and big batches gain too | Needs hardware support, for example FP8 on recent NVIDIA GPUs. |
| KV cache | FP8, sometimes lower | About twice as many tokens in flight with FP8 | Often a big win for long chats. Check quality too. |
Check what your GPU and your serving engine support. A format that has no fast kernel on your hardware can be slower than BF16.
Where the speed comes from
Quantization is not “4 times smaller, so 4 times faster”. The win depends on where the GPU is limited.
- Decode at small batches is memory-bound. The GPU reads all weights in every step. Half the bytes means close to half the reading time. Weight-only INT4 shines here.
- Prefill and big batches are compute-bound. Fewer bytes to read do not help much. Only formats that also make the math faster, like FP8 on supporting hardware, help here.
- Freed memory is the hidden win. Smaller weights leave more room for the KV cache. More tokens in flight means bigger batches, so more users per GPU. This is often where the cost saving really comes from.
- Kernels matter. The same format can be fast in one engine and slow in another. Measure on your setup.
Quality: check every group
The loss from quantization is often small on average, but it is not spread evenly.
Areas that often feel it first:
- Exact formats, like JSON with the right field values.
- Math, code and reasoning, where one small error changes the whole result.
- Long contexts, where small errors add up over many tokens.
- Some non-English languages. Research suggests some languages can lose more than English. Test each language you serve.
- Smaller models. A small model often loses more from the same format than a big one.
So the eval must be split by group. A simple check:
def compare(base, quant, max_drop=1.0):
ok = True
for group, score in base.items():
drop = score - quant[group]
if drop > max_drop:
ok = False
print(f"{group:<18} {score:5.1f} {quant[group]:5.1f} drop {drop:4.1f}")
return ok
base = {"english_chat": 92, "persian_chat": 90, "json_valid": 99}
quant = {"english_chat": 92, "persian_chat": 86, "json_valid": 95}
print("ship" if compare(base, quant) else "do not ship") # do not ship
A safe way to roll it out
- Build the eval set first, from real traffic: each language, each task type, short and long chats.
- Start with the gentlest format your hardware supports, often FP8. Go lower only if you need to.
- Run the eval on both versions, and set a clear limit for the drop in each group.
- Load test both at your latency targets, and compare cost per million tokens. The saving must be real.
- Release to a small share of traffic, and watch quality signals: thumbs down, broken forms, support reopen rate.
- Keep the BF16 version ready to switch back.
Common mistakes
| Mistake | Result | Right way |
|---|---|---|
| “I tried 20 prompts, it looks the same.” | Hidden drops in some groups. | An eval set split by language and task. |
| Looking only at the average score | One group gets much worse unseen. | A limit for every group. |
| Expecting 4 times faster from 4 times smaller | Disappointment at big batches. | Know if you are memory-bound or compute-bound, and measure. |
| A format with no fast kernel on your GPU | Slower than before. | Check hardware and engine support. |
| Not using the freed memory | Little cost saving. | Let the engine use it for more KV cache and bigger batches. |
Summary in six lines
- Quantization stores numbers with fewer bits: FP8, INT8, INT4.
- It saves memory and memory reading, so decode gets faster and more chats fit.
- Weight-only formats help most at small batches; FP8 math also helps prefill and big batches.
- Quantizing the KV cache can almost double the tokens in flight.
- Quality loss is uneven: check each language and task on its own.
- Prove it with evals and a load test, then release step by step.