Sub topic 9
Quantization: trading precision for memory
This chapter assumes you have read the previous chapter on estimating memory. You should know that weight memory is parameter count times bytes per weight, and that a prompt, a token, and a context are different things.
What quantization does
Quantization stores a model's weights with fewer bits. A weight is one number inside the model. At 16-bit precision, each weight takes 2 bytes. At 4-bit precision, each takes half a byte. The model has the same number of weights, but each one is stored more coarsely, so it can represent fewer distinct values.
This is why quantization matters for model choice. A model that does not fit on your GPU at 16-bit may fit at 8-bit or 4-bit. You give up some precision and get back memory.
The memory arithmetic
The function from the previous chapter already covers this. Halving the bits halves the weight memory.
def weight_memory_gb(params_billion: float, bits_per_weight: int) -> float:
"""Memory for the weights alone, in GB (1 GB = 1e9 bytes)."""
return params_billion * bits_per_weight / 8
for bits in (16, 8, 4):
print(f"7B model at {bits}-bit: {weight_memory_gb(7, bits):.1f} GB")
# 7B model at 16-bit: 14.0 GB
# 7B model at 8-bit: 7.0 GB
# 7B model at 4-bit: 3.5 GBThese numbers cover the weights only. You still need room for the KV cache (the stored attention state for the tokens in the context) and for runtime overhead. A 4-bit 7B model needs more than 3.5 GB in practice.
What you lose
Fewer bits means rounding error in every weight. The effects usually look like this:
| Precision | Weight memory (7B) | Typical quality effect |
|---|---|---|
| 16-bit | 14 GB | Reference quality |
| 8-bit | 7 GB | Usually very close to the reference |
| 4-bit | 3.5 GB | Small loss on many tasks, larger on some |
The "typical" column is a rule of thumb. Loss varies by model, by quantization method, and by task. Tasks that need exact reasoning, precise formatting, or rare facts tend to degrade first. Quantization can also change speed. Moving fewer bytes often makes generation faster, but some formats add decoding work, so measure it.
A useful comparison
A quantized larger model is often better than a full-precision smaller model that uses the same memory. For example, a 13B model at 4-bit needs about 6.5 GB for weights, while a 7B model at 8-bit needs 7 GB. Which one wins on your task is something to test, not assume.
Common mistakes
- Ignoring the KV cache and overhead when checking whether a model fits.
- Trusting a leaderboard score for the full-precision model and applying it to the 4-bit version.
- Comparing quantized models with different methods as if they were the same.
Exercise
A 70B model at 16-bit needs about how much memory for weights alone? What about at 4-bit? Why is the 4-bit figure still not the amount of GPU memory you should plan for?
Show answer
At 16-bit: 70 16 / 8 = 140 GB. At 4-bit: 70 4 / 8 = 35 GB. You also need memory for the KV cache, which grows with context length and the number of concurrent requests, and for runtime overhead, so the real requirement is higher than 35 GB.