Availit World How AI really works, in your language

Lesson 26 of 4622 minFacts checked October 20, 2026

Quantization: big models in small memory

Llama 3.1 8B is a 16 GB file. Saved as Q4_K_M, it is under 5 GB, writes more than twice as fast, and on most questions gives almost the same answers. That is quantization: storing each weight with fewer bits. It is why a laptop can now run models that needed a data centre a few years ago. Done carelessly, it is also why a model that seems fine in English can quietly get worse in Urdu or Bengali. This lesson shows how it works, how to read names like Q4_K_M and IQ2_XXS, and how to measure the damage.

In this lesson you will

  • Quantize a few numbers by hand with a scale, and work out the error
  • Explain why blocks and outliers decide how well quantization works
  • Read llama.cpp file names such as Q4_K_M and IQ2_XXS, and choose one
  • Tell the main methods apart: GPTQ, AWQ, SmoothQuant, importance matrices and QAT
  • Measure the damage properly, including in your own language
On this page
  1. Why fewer bits
  2. Quantizing by hand
  3. The outlier problem, and five answers to it
  4. Reading GGUF names
  5. Measuring the damage
  6. Quantize a model yourself
  7. Formats built into the chips
  8. Even fewer bits
  9. Remember
  10. Check yourself

Why fewer bits

Quantization brings two wins. The first is size: a model has to fit in memory before it can run at all. The second is speed: as the press-Enter lesson showed, every new token reads every weight, so a file a quarter of the size can be read about four times as fast.

Llama 3.1 sizes, before and after
BF16 (16 bits)Q4_K_M (about 4.9 bits)
8B16.1 GB4.9 GB
70B140 GB43.1 GB
405B813 GB249.1 GB

From the llama.cpp quantize guide. The 70B model goes from needing two H100s to fitting on one, and the 8B model fits on most laptops and on phones with plenty of memory.

Writing speed of Llama 3.1 8B at different precisions
  • F16 29.2 tokens per second
  • Q8_0 50.9 tokens per second
  • Q6_K 58.7 tokens per second
  • Q4_K_M 71.9 tokens per second
  • IQ3_XXS 74 tokens per second
  • IQ2_XXS 79.9 tokens per second

Measured by the llama.cpp project on one machine, for 128 generated tokens. Halving the bits nearly doubles the speed at first. Below about 4 bits the gains level off: the file keeps shrinking, but unpacking the cleverer formats takes work.

Quantizing by hand

The idea is the one from the bits-and-floats lesson: a group of weights shares one scale, and each weight is stored as a small whole number that, times the scale, comes close to the real value. With 4 bits and a sign we can use the whole numbers from −7 to 7.

Four weights, 4 bits
weights: 0.12, −0.40, 0.05, 0.31

A tiny block.

scale = largest size ÷ 7 = 0.40 ÷ 7 = 0.0571

So the largest weight lands exactly on −7.

stored: round(w ÷ scale) = 2, −7, 1, 5

Four small whole numbers, plus the one scale.

read back: 0.114, −0.400, 0.057, 0.286

Each is off by at most 0.024. Close enough for most purposes.

The same block with one outlier
weights: 0.12, −0.40, 0.05, 2.00

One weight is much bigger than the rest.

scale = 2.00 ÷ 7 = 0.286

The outlier now sets a coarse scale for everyone.

stored: 0, −1, 0, 7

0.12 and 0.05 both round to zero.

read back: 0, −0.286, 0, 2.00

The outlier is perfect; the small weights are wiped out.

That second example is the whole problem of quantization in one line. A single large number in a block ruins the precision of every small number sharing its scale. Everything that follows is a way to deal with it: smaller blocks, protecting the important weights, or moving the large numbers out of the way.

Try itOne outlier, three block sizes

These 32 weights include one outlier. With blocks of 32, it sets the scale for everyone. Switch to blocks of 8 and watch the error fall, then see what it costs: every block stores its own 16-bit scale. Then lower the bits to 2.

This interactive part needs JavaScript.

Quick check

A block’s largest weight is 0.7, and you use 4 bits (−7 to 7). What is stored for a weight of 0.33?

Show the answer

Answer: 3, read back as 0.3 The scale is 0.7 ÷ 7 = 0.1. Then 0.33 ÷ 0.1 = 3.3, which rounds to 3, read back as 0.3.

The outlier problem, and five answers to it

In 2022, Tim Dettmers and colleagues found that large models develop a few hidden dimensions with values up to 20 times larger than the rest. They appear in all layers once models reach about 6.7 billion parameters, and simple 8-bit rounding then falls apart. Since then, a series of methods has made 8-bit almost free and 4-bit routine.

Five ways to quantize well
FromThe idea
LLM.int8()University of Washington, Meta and Hugging Face, 2022Keep the few outlier dimensions in 16 bits and do the other 99.9 % in 8 bits. A 175B model ran without losing quality.
GPTQIST Austria and ETH Zürich, 2022Round the weights one column at a time, and nudge the columns not yet rounded to cancel the error. A 175B model at 3 or 4 bits in about four GPU hours.
SmoothQuantMIT and NVIDIA, 2022Activations have outliers too. Rescale them and fold the opposite scale into the weights, which gives the same result but is easy to round. 8-bit weights and activations, up to 1.56 times faster.
AWQMIT, 2023A small share of weights, about 1 %, matters most, and the activations show which. Scale those up before rounding so they keep their precision. Best Paper at MLSys 2024.
Importance matrixllama.cpp, 2024Run sample text through the model, record how much each weight matters, and round the important ones more carefully.

All five are post-training quantization (PTQ): they work on a finished model, using at most a little sample text, called calibration data.

The sixth answer is to not quantize after the fact at all. In quantization-aware training (QAT), the model keeps training for a while with the rounding switched on, and learns to cope with it. Google trained Gemma 3 this way for about 5,000 steps and cut the quality loss at Q4_0 by 54 %. Moonshot built QAT into the post-training of Kimi K2 Thinking, which was released in 4-bit (INT4) form: Moonshot calls it a lossless doubling of speed in low-latency mode.

Reading GGUF names

On Hugging Face, one model often comes in twenty GGUF files with names like Q4_K_M. Most of these formats were designed by one llama.cpp contributor, Iwan Kawrakow: k-quants in June 2023 and i-quants in January 2024. The names follow a pattern.

  • The number is the main number of bits per weight: Q8, Q6, Q4, Q2.
  • _0 and _1 are the original simple formats: blocks of 32 weights with one scale (_0), or a scale and a minimum (_1).
  • K means a k-quant: super-blocks of 256 weights, split into smaller blocks whose own scales are themselves stored in a few bits. Q4_K uses 8 blocks of 32 with 6-bit scales: 4.5 bits per weight.
  • _S, _M, _L (small, medium, large) say how many sensitive tensors get extra bits. Q4_K_M stores half of certain attention and feed-forward tensors in 6 bits and the rest in 4.
  • IQ means an i-quant: groups of weights are matched to the nearest pattern in a carefully chosen codebook (for IQ2_XXS, one of 256 patterns for each group of 8), an idea borrowed from the QuIP# paper. They give the best quality at 2 and 3 bits, especially with an importance matrix.
Common GGUF types, measured on Llama 3.1 8B
Bits per weightFile sizeRule of thumb
F1616.014.96 GiBThe original
Q8_08.57.95 GiBPractically the same as the original
Q6_K6.566.14 GiBVery close to the original
Q5_K_M5.705.33 GiBA safe choice when memory allows
Q4_K_M4.894.58 GiBThe usual default
Q3_K_M4.003.74 GiBNoticeably worse, still usable
IQ2_XXS2.382.23 GiBOnly for very large models
IQ1_S2.001.87 GiBAn experiment

Bits and sizes from the llama.cpp quantize guide. Even “1-bit” IQ1_S averages 2 bits, because the embeddings, the output layer and a few other tensors are kept at higher precision.

Measuring the damage

A quantized model rarely breaks outright. It gets slightly worse in ways that are easy to miss. Three measurements, all built into llama.cpp’s llama-perplexity tool, make the damage visible:

  • Perplexity: how surprised the model is by real text. Lower is better. It is useful for comparing versions of the same model, not different models.
  • KL divergence: how different the quantized model’s chances for the next token are from the original’s, averaged over every token. Zero means identical.
  • Same top token: how often both versions agree on the single most likely next token.
How often Llama 3 8B picks the same top token as the 16-bit original
  • Q8_0 97.7 %
  • Q6_K 96 %
  • Q4_K_M 91.9 %
  • Q2_K 71.1 %

From the llama.cpp perplexity guide, on English Wikipedia text. At Q4_K_M, 1 token in 12 has a different favourite. Most of those swaps are harmless, but in a long chain of reasoning, one bad swap can send the rest astray.

Perplexity of Llama 3 8B (lower is better)
Without importance matrixWith importance matrix
F166.23–
Q8_06.23–
Q6_K6.25–
Q4_K_M6.416.38
Q3_K_M6.896.73
Q2_K9.758.65
IQ2_XXS–14.09
IQ1_S–58.10

Same source. The importance matrix helps most where it is needed most, at the lowest bit counts.

Is each choice probably fine, or likely to cause visible problems?

0 of 6 sorted
  • Q8_0 for an Urdu translation app

    Answer: Probably fine

  • Q4_K_M of a 70B model for general chat

    Answer: Probably fine

  • Q2_K of an 8B model for maths homework

    Answer: Expect problems

  • IQ1_S of a 3B model for anything

    Answer: Expect problems

  • Q6_K of a coding model

    Answer: Probably fine

  • Q3_K_S of a small model, tested only in English, for Bengali users

    Answer: Expect problems

Quantize a model yourself

From Hugging Face to a measured Q4_K_M filebash
# 1. Convert the original weights to a 16-bit GGUF file
python convert_hf_to_gguf.py --remote Qwen/Qwen3-8B \
  --outtype bf16 --outfile qwen3-8b-bf16.gguf

# 2. Record which weights matter, using sample text
llama-imatrix -m qwen3-8b-bf16.gguf -f calibration.txt -o imatrix.gguf

# 3. Quantize, guided by the importance matrix
llama-quantize --imatrix imatrix.gguf \
  qwen3-8b-bf16.gguf qwen3-8b-Q4_K_M.gguf Q4_K_M

# 4. Save the original's predictions, then compare
llama-perplexity -m qwen3-8b-bf16.gguf -f test.txt --kl-divergence-base base.kld
llama-perplexity -m qwen3-8b-Q4_K_M.gguf --kl-divergence-base base.kld --kl-divergence
  • convert_hf_to_gguf.py comes with llama.cpp. --remote reads the model straight from Hugging Face.
  • calibration.txt is ordinary text. Put in text like the text you will use, in your language, so the importance matrix protects the weights your language needs.
  • Step 4 prints perplexity, KL divergence and the “same top token” rate. Keep test.txt short: the saved predictions can run to tens of gigabytes.
  • Hugging Face also has a free “GGUF-my-repo” page that runs steps 1 and 3 for you.

Formats built into the chips

Everything so far stores weights in few bits but still does the maths in 16 bits, after unpacking. Newer GPUs can do the maths itself in low precision, which is faster again.

Weights only (for example W4A16)

  • Weights stored in 4 bits, unpacked to 16 bits for the maths
  • Speeds up decode, which is limited by memory
  • Works on almost any hardware
  • GGUF files, GPTQ, AWQ

Weights and activations (W8A8, FP8, FP4)

  • The multiplications themselves run in 8 or 4 bits
  • Also speeds up prefill and large batches, which are limited by computing power
  • Needs chips with low-precision maths units: an H100 does 1,979 trillion FP8 operations per second, twice its BF16 rate
  • SmoothQuant, FP8, MXFP4, NVFP4
  • FP8: DeepSeek-V3 was trained and is served in FP8. To survive outliers, it gives every 1 × 128 tile of activations and every 128 × 128 block of weights its own scale.
  • MXFP4: 32 four-bit values share an 8-bit power-of-two scale (4.25 bits per weight). gpt-oss was released with its experts in this format.
  • NVFP4: NVIDIA’s format for its Blackwell chips. Blocks of only 16 values, each with an 8-bit floating-point scale, plus one scale for the whole tensor. NVIDIA reports about 1 % or less loss against FP8 on DeepSeek-R1, at about 3.5 times smaller than FP16.

Even fewer bits

How far can it go? Microsoft’s BitNet b1.58 (2024) trains models whose every weight is −1, 0 or +1. Three choices take log₂ 3 ≈ 1.58 bits, hence the name. Multiplying by −1, 0 or 1 is just subtracting, skipping or adding, so the maths becomes very cheap.

The catch is that it only works if the model is trained this way from the start: squeezing an ordinary model to 1.58 bits after training ruins it, as the IQ1_S results above show. In 2025, Microsoft released BitNet b1.58 2B4T, a 2-billion-parameter model trained on 4 trillion tokens, and reported results on par with ordinary models of the same size. Whether this holds for the largest models is still an open question.

Remember

  • Quantization stores weights in fewer bits: smaller files, and faster decode because fewer bytes are read per token.
  • A block of weights shares a scale. One outlier makes the scale coarse and wipes out the small weights, so smaller blocks and outlier handling matter.
  • LLM.int8(), GPTQ, SmoothQuant, AWQ and the importance matrix quantize finished models well; QAT trains the model to cope.
  • GGUF names: the number is the bits, K means k-quant super-blocks, _S/_M/_L the mix, IQ codebook i-quants. Q4_K_M is the usual default.
  • Measure with perplexity, KL divergence and “same top token”. Damage is often worse in non-Latin scripts and in maths, so test in your language.
  • Chips now compute in FP8 and FP4 (MXFP4, NVFP4); BitNet trains models with weights of only −1, 0 and +1.

Words to know

Quantization
Storing numbers with fewer bits, accepting a small rounding error.
Scale
The number a block of stored whole numbers is multiplied by to get back close to the real weights.
Outlier
A value far bigger than its neighbours, which makes rounding the others much less precise.
PTQ
Post-training quantization: quantizing a finished model.
QAT
Quantization-aware training: training with the rounding switched on, so the model learns to cope.
Calibration data
A little sample text used to see which weights and activations matter most.
Importance matrix
llama.cpp’s record of how much each weight matters, used to round the important ones more carefully.
GGUF
The file format llama.cpp uses for models, holding the weights in many possible quantized types.
k-quant
A llama.cpp format with super-blocks of 256 weights whose block scales are themselves quantized.
KL divergence
A measure of how different two sets of next-token chances are. Zero means identical.
W4A16
4-bit weights with 16-bit activations: the weights are unpacked before the maths.
FP8, MXFP4, NVFP4
Low-precision number formats that modern GPUs can compute with directly.

Go to the source

This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.

Check yourself

5 questions. Tap the answer you think is right.

0 of 5 answered

Why does a quantized model write faster, not just take less space?

Show the answer

Answer: Decode reads every weight for every token, so fewer bytes per weight means more tokens per second Decode is limited by memory speed. Fewer bytes to read means each step finishes sooner.

What does one large outlier in a block do?

Show the answer

Answer: It makes the shared scale coarse, so the small weights in the block lose their precision The scale is set by the largest value. With a coarse scale, small values round to zero or nearly so.

What does the K in Q4_K_M mean?

Show the answer

Answer: A k-quant: super-blocks of 256 weights whose block scales are themselves quantized K-quants were added to llama.cpp in 2023. The M says how many sensitive tensors get extra bits.

You have 24 GB. Which is usually better: a 30B model at 4 bits, or a 14B model at 8 bits?

Show the answer

Answer: The 30B model at 4 bits For a fixed memory budget, 4-bit with more parameters is almost always the better trade.

A 4-bit model scores almost the same as the original on English benchmarks. Is it safe for Bengali users?

Show the answer

Answer: Not necessarily: non-Latin scripts are often hurt more, and automatic tests underestimate the harm Cohere’s 2024 study found the biggest losses in non-Latin scripts, and human judges saw far larger drops than benchmarks did.

Lesson complete

Answer every question above to complete this lesson.

Your progress is saved on this device only.

Next lessonChips: what AI runs on
Previous lesson All lessons

Choose your language