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.
| BF16 (16 bits) | Q4_K_M (about 4.9 bits) | |
|---|---|---|
| 8B | 16. | 4. |
| 70B | 140 GB | 43. |
| 405B | 813 GB | 249. |
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.
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.
weights: 0.12, −0.40, 0.05, 0.31A tiny block.
scale = largest size ÷ 7 = 0.40 ÷ 7 = 0.0571So the largest weight lands exactly on −7.
stored: round(w ÷ scale) = 2, −7, 1, 5Four small whole numbers, plus the one scale.
read back: 0.114, −0.400, 0.057, 0.286Each is off by at most 0.024. Close enough for most purposes.
weights: 0.12, −0.40, 0.05, 2.00One weight is much bigger than the rest.
scale = 2.00 ÷ 7 = 0.286The outlier now sets a coarse scale for everyone.
stored: 0, −1, 0, 70.12 and 0.05 both round to zero.
read back: 0, −0.286, 0, 2.00The 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.
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
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.
| From | The idea | |
|---|---|---|
| LLM.int8() | University of Washington, Meta and Hugging Face, 2022 | Keep the few outlier dimensions in 16 bits and do the other 99.9 % in 8 bits. A 175B model ran without losing quality. |
| GPTQ | IST Austria and ETH Zürich, 2022 | Round 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. |
| SmoothQuant | MIT and NVIDIA, 2022 | Activations 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. |
| AWQ | MIT, 2023 | A 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 matrix | llama.cpp, 2024 | Run 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.
| Bits per weight | File size | Rule of thumb | |
|---|---|---|---|
| F16 | 16. | 14. | The original |
| Q8_0 | 8. | 7. | Practically the same as the original |
| Q6_K | 6. | 6. | Very close to the original |
| Q5_K_M | 5. | 5. | A safe choice when memory allows |
| Q4_K_M | 4. | 4. | The usual default |
| Q3_K_M | 4. | 3. | Noticeably worse, still usable |
| IQ2_XXS | 2. | 2. | Only for very large models |
| IQ1_S | 2. | 1. | An 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.
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.
| Without importance matrix | With importance matrix | |
|---|---|---|
| F16 | 6. | – |
| Q8_0 | 6. | – |
| Q6_K | 6. | – |
| Q4_K_M | 6. | 6. |
| Q3_K_M | 6. | 6. |
| Q2_K | 9. | 8. |
| IQ2_XXS | – | 14. |
| IQ1_S | – | 58. |
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?
-
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
# 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.pycomes with llama.cpp.--remotereads the model straight from Hugging Face.calibration.txtis 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.txtshort: 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.
-
LLM.int8(): 8-bit Matrix Multiplication for Transformers at Scale
The discovery of outlier features, and how to work around them. Figure 1 shows normal 8-bit rounding collapsing at 6.7B parameters.
-
GPTQ: Accurate Post-Training Quantization for Generative Pre-trained Transformers
The method that made 4-bit GPU models common.
-
SmoothQuant: Accurate and Efficient Post-Training Quantization for Large Language Models
A one-line idea, moving difficulty from activations to weights, explained with clear figures.
-
AWQ: Activation-aware Weight Quantization for LLM Compression and Acceleration
Why 1 % of weights matter most, and how to protect them without mixed formats.
-
The case for 4-bit precision: k-bit Inference Scaling Laws
More than 35,000 experiments on how to spend a fixed memory budget.
-
How Does Quantization Affect Multilingual LLMs?
Essential if you serve languages other than English: automatic tests miss most of the harm.
-
Scaling Laws for Precision
Why models trained on more data lose more from quantization.
-
The Era of 1-bit LLMs: All Large Language Models are in 1.58 Bits
BitNet b1.58: weights of −1, 0 and +1, trained from scratch.
-
k-quants (llama.cpp pull request 1684)
The description of every k-quant type, with bits per weight and early quality tables.
Check yourself
5 questions. Tap the answer you think is right.
0 of 5 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonChips: what AI runs on