Sub topic 13
How much quality you give up when you quantise
This chapter assumes you know what a parameter is, how bits per weight set file size, and how to read GGUF names such as Q4KM. It covers how to judge what quantisation costs you in output quality, and where that cost shows up first.
Quantisation is lossy compression
Quantisation stores each weight with fewer bits. The model's original weights are rounded to the nearest value the smaller format can represent, so the quantised model is a slightly different model. The difference is called quantisation error.
Quality loss is not a fixed percentage per bit removed. It depends on three things:
- The number of bits per weight. Fewer bits means coarser rounding.
- The scheme. K-quants and i-quants spend bits more carefully than the older, simpler formats.
- The model. Larger models usually tolerate rounding better than small ones.
The usual shape of the curve
Quality falls slowly at first and then quickly. Going from 16 bits to 8 bits is nearly free. Going from 8 to 5 or 6 bits costs very little. Below about 4 bits, the loss grows with each bit you remove.
The table gives typical bits per weight and a rough qualitative rating for common llama.cpp quantisation types. The bit counts are approximate and vary by model, because each scheme keeps some tensors at higher precision.
| Type | Approx. bits per weight | Typical quality versus 16-bit |
|---|---|---|
| F16 / BF16 | 16 | Reference |
| Q8_0 | 8.5 | Practically indistinguishable |
| Q6_K | 6.6 | Very close |
| Q5_K_M | 5.7 | Very close |
| Q4_K_M | 4.85 | Small, usually acceptable loss |
| Q3_K_M | 3.9 | Noticeable loss |
| Q2_K | 2.6 to 3.4 | Large loss, often unreliable |
Treat the last column as a guide, not a measurement. Your task decides whether "small" is small enough.
The values in the chart are illustrative. They show the shape of the curve and are not measured data.
How to measure the loss
Do not guess. Two measurements are standard.
Perplexity
Perplexity measures how surprised a model is by a reference text. Lower is better. You compare the quantised model with the 16-bit model on the same text. The quantity that matters is the increase over the 16-bit baseline, not the absolute number.
llama.cpp includes a tool for this:
llama-perplexity -m model-Q4_K_M.gguf -f wiki.test.raw -ngl 99Run it for each file you are considering, using the same text file and the same context size. Results from different texts or settings cannot be compared.
KL divergence
Perplexity can hide changes, because errors in different directions can cancel. KL divergence compares the full probability distribution the quantised model predicts against the 16-bit model's, token by token. It is a more sensitive measure of how far the quantised model has drifted. llama-perplexity can save the baseline logits and then report KL divergence for a quantised model against them.
Task benchmarks
Perplexity does not tell you whether the model still writes working code or follows your format. Test the behaviour you care about. A small set of 20 to 50 of your own prompts, run against each quantisation, often tells you more than a public score.
Where the loss shows up first
Average scores hide uneven damage. Lower-bit models tend to fail first on:
- Precise tasks such as code, arithmetic and structured output like JSON.
- Long multi-step reasoning, where small errors compound.
- Languages with less training data.
- Strict instruction following.
Casual chat, summarising and rewriting usually survive aggressive quantisation better. If your workload is the first kind, stay at Q5 or above. If it is the second kind, Q4KM is usually a safe default.
Why the quantisation schemes differ
Plain formats such as Q40 split weights into small blocks and store one scale per block. K-quants (the "K" in Q4K_M) use larger super-blocks with their own scales. The letter at the end (S, M, L) sets how many sensitive tensors, such as parts of the attention and feed-forward layers, stay at higher precision. M is a middle choice.
Importance-matrix quantisation (imatrix) goes further. You run calibration text through the model to find which weights matter most, then give those weights more precision. The i-quants (IQ2, IQ3, IQ4 families) use this and improve quality at very low bit counts, at some cost in speed on CPU. The calibration text should resemble your use. An imatrix built only from English prose can hurt code quality.
Bigger model, lower bits, or smaller model, higher bits
When memory is fixed, you can run a larger model at fewer bits or a smaller model at more bits. A common finding is that a larger model at about 4 bits beats a smaller model at 8 bits that occupies the same memory. The advantage fades below 3 bits, where the larger model's damage outweighs its extra capacity.
The calculation uses the weights formula from earlier in the tutorial:
def weights_gib(params_billion, bits_per_weight):
return params_billion * 1e9 * bits_per_weight / 8 / 2**30
# Two options that need similar memory for weights.
options = [
("8B at Q8_0", 8.0, 8.5),
("14B at Q4_K_M", 14.0, 4.85),
]
for name, params, bits in options:
print(f"{name}: {weights_gib(params, bits):.1f} GiB")The two options come out at 7.9 GiB and 7.9 GiB. In this case the 14B model at Q4KM is usually the better use of memory. Verify it on your own prompts, because model families differ.
Other things quantisation changes
- Speed. Smaller files read faster, so token generation usually speeds up as bits fall, because generation is limited by memory bandwidth. Some low-bit formats need extra decoding work, so very small types can be slower than their size suggests, mostly on CPU.
- KV cache. Weight quantisation does not shrink the KV cache. You can quantise the cache separately (for example to 8-bit), which saves memory at long context and has its own, usually small, quality cost.
- Pre-quantised models. Quantising an already quantised model compounds the error. Start from 16-bit weights when you can.
Failure modes to watch for
- Judging by file size alone. Two files with the same name from different uploaders can differ in imatrix data and tensor choices.
- Comparing perplexity across settings. Different text or context size makes the numbers meaningless.
- Trusting a single prompt. A model that answers one question well can still fail on others.
- Going to Q2 or Q3 to fit a bigger model. The extra parameters rarely repay the loss at that point. A smaller model at Q4 or Q5 is often more reliable.
- Ignoring the KV cache. Saving memory on weights while a long context overruns the budget forces a smaller context, which hurts quality in a different way.
A practical decision procedure
- Pick the largest model that fits your memory at Q4KM, with room for the KV cache and overhead.
- Test it on your own prompts.
- If it fails on precise tasks, move up to Q5KM or Q6_K, or drop to a smaller model at higher precision.
- If it passes with room to spare, try one step smaller to save memory or gain speed, and test again.
- Stop when quality is acceptable. More precision beyond that point costs memory and speed for no benefit you can see.
Exercise
You have about 8 GiB available for weights. Option A is an 8B model at Q80 (about 8.5 bits per weight). Option B is a 14B model at Q4K_M (about 4.85 bits per weight). Your workload is code generation. Which do you try first, why, and what would you check before settling?
Show answer
Compute the sizes: A is 8e9 × 8.5 / 8 / 2^30 ≈ 7.9 GiB and B is 14e9 × 4.85 / 8 / 2^30 ≈ 7.9 GiB, so they fit equally. Try B first, because a larger model at about 4 bits usually beats a smaller model at 8 bits for the same memory, and 4.85 bits is above the range where loss grows quickly. Before settling, run both on a set of your own code prompts and check whether B's output still compiles and passes tests. Because code is a precision-sensitive task, also try Q5KM if B shows errors. Remember that both options leave little room for the KV cache, so check the total budget with your intended context length.
Exercise
A colleague reports that the Q4KM file has a perplexity of 6.9 and says this proves it is worse than your Q5KM file, which scored 6.7 on a different text. What is wrong with the comparison?
Show answer
Perplexity is only comparable when both files are measured on the same text with the same context size and settings. Here the texts differ, so the numbers cannot be compared. Re-run both files on the same text, and compare each against the 16-bit baseline. Use KL divergence or your own task tests as well, since a difference of 0.2 may be small compared with what matters for your use.