Skip to content
mentoor

Topic 4 of 6 · 5 min read

Measuring quality loss after quantizing

Goal: Compare a full-precision and a 4-bit model on a small task set and decide whether the loss is acceptable.

Never trust a quant by feel: measure it against the full-precision model with a statistic and with your own tasks, then decide from the numbers.

Check yourself · 1 of 3

What does a rise in perplexity from the 16-bit model to its quant mean?

Choose one answer

Why it matters

A quantized model usually sounds fine. The damage shows in subtle places: a wrong digit, a broken JSON bracket, a function that almost works. A few chat messages will not reveal this, and a published "no loss" claim may not apply to your prompts.

Three levels of measurement

  1. Perplexity. It measures how surprised the model is by a text sample. Lower is better. Compute it for the 16-bit model and the quant on the same text and compare. A small rise (a percent or two) is normal at Q4. Perplexity is quick but blunt: it can hide failures that matter.
  2. KL divergence. It compares the full probability distribution of the quant with the baseline at each token. It captures drift that perplexity misses, and a high maximum or a rising tail warns about rare large errors. llama.cpp's perplexity tool can save baseline logits and report KL divergence against them.
  3. Task evals. Run your own prompts and score the answers. This is the measurement that answers your question: does it still do my job?

A small task eval

# 1) baseline logits from the 16-bit model
llama-perplexity -m model-f16.gguf -f wiki.test.raw --kl-divergence-base base.kld

# 2) compare a quant against that baseline
llama-perplexity -m model-Q4_K_M.gguf -f wiki.test.raw \
  --kl-divergence-base base.kld --kl-divergence

For tasks, collect 30 to 100 real prompts with a checkable answer: extract these fields, fix this test, answer from this document. Run each model with the same sampling settings, temperature 0, then score by a script or a fixed rubric.

Reading the result

model        wikitext ppl   task pass rate (50 prompts)
F16          6.10           41/50
Q8_0         6.11           41/50
Q5_K_M       6.14           40/50
Q4_K_M       6.25           39/50
Q3_K_M       6.62           34/50

Q4_K_M loses 2 of 50 tasks. Q3_K_M loses 7. Whether 39/50 is acceptable depends on the job, and that is your decision.

What goes wrong

  • Comparing perplexity across different models or tokenizers; it only works for the same model on the same text.
  • Using too few prompts, so one lucky answer flips the result. Fix the sample size and the seed.
  • Testing only easy prompts. Include the hardest 20 percent: long context, exact formats, arithmetic.
  • Changing the sampling settings between runs and blaming the quant.

Examples

A pass-rate table

Run 50 prompts through F16 and Q4_K_M at temperature 0. If the pass rate drops from 41 to 39, check which two failed: if both are format errors, a stricter prompt or Q5 may fix them.

Setting an acceptance rule

Write the rule before the run: accept a quant if it loses no more than 2 tasks of 50 and no failure is a safety or data-loss case. Then the result is a decision, not an argument.

Key points

  • Measure a quant against the full-precision model on the same text and settings.
  • Perplexity is fast but blunt; KL divergence against baseline logits catches drift it hides.
  • Task evals on your own prompts decide whether a loss matters for your work.
  • Use temperature 0 and a fixed prompt set so the comparison is repeatable.
  • Include hard cases such as exact formats, long context and arithmetic, where low-bit damage shows first.

Common mistakes

  • Judging by a few chat replies.
  • Comparing perplexity numbers between different models.
  • Running too few prompts to see a difference beyond noise.

Check yourself · 2 of 3

Why add KL divergence to a perplexity comparison?

Choose one answer

Check yourself · 3 of 3

You compare F16 and Q4_K_M on 50 prompts. Which setup gives a fair result?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic