A model file called Q4_K_M is about a third the size of the 16-bit original. Nothing was deleted: every one of the billions of weights is still there. So what happened to them?
Each weight was rounded. Instead of storing 0.0236 exactly, the file stores a small whole number, say 7, and a scale shared with 31 neighbours. When the model runs, the two are multiplied back together and you get something close to 0.0236. That rounding is quantisation, and this page lets you watch it happen to real formats, one weight at a time.
By the end you will be able to see why blocks of 32 exist, why one unusually large weight can ruin the rounding for its neighbours, why Q4_K_M costs 4.89 bits per weight rather than 4, and how small rounding errors add up as they pass through a network.
Before this: Will it run on my machine? shows why smaller weights fit in less memory and run faster. This page is about what the shrinking costs.
Just choosing a file? Jump to reading a GGUF filename.
Round a block of weights
0.0088459. The block’s largest value is 0.204, so d = max ÷ −8 = −0.0255 and 1/d = −39.216. x × (1/d) = −0.347; + 8.5 = 8.153; keep the whole part: q = 8 (a 4-bit code, 0 to 15). The file stores d as f16, −0.025497, and dequantising gives (q − 8) × d. Back out: 0, error −0.0088.Sixteen levels and a scale
Take q4_0, one of the oldest formats in llama.cpp. It cuts the weights into blocks of 32. In each block it finds the value with the largest magnitude, keeping its sign, and makes that value the scale’s anchor:
Each weight becomes a 4-bit code q between 0 and 15, and the block stores one 16-bit scale d. In the default block the largest value is 0.2040, so d = 0.2040 ÷ −8 = −0.0255. Value #10 gives x ÷ d = −0.347; add 8.5 to get 8.153, drop the fraction and you get q = 8, which dequantises to (8 − 8) × d = 0. The readout under the figure walks through the same steps for whichever value you pick.
Two details are worth noticing. First, the largest value always lands exactly on a level, at code 0, so the biggest weight in each block is stored almost perfectly. Second, the 16 levels run from −8 to +7 steps, so values of the opposite sign can only reach seven steps; a value as large as the max but with the other sign gets clipped.
q8_0 is the 8-bit cousin: the scale is the largest magnitude divided by 127, and each weight is rounded to the nearest whole number of steps, from −127 to 127. The f16 and bf16 formats have no scale at all; each weight is simply rounded to the nearest 16-bit floating-point number, and bf16 keeps fewer significant bits than f16 in exchange for a much wider range.
The scale is not free. A block of 32 weights at 4 bits is 128 bits, plus 16 for its scale:
That is why q4_0 is 4.5 bits per weight and q8_0 is 8.5.
Why blocks of 32
Every block gets a scale fitted to its own largest value, so smaller blocks fit the weights more tightly. But every block also pays 16 bits for that scale. Halve the block size and you double the overhead.
Smaller blocks: less error, more scales
Bits per weight = 4 + 16 ÷ 32 = 4.50 (this is q4_0)
The curves show the trade-off for 4,096 weights. At 4 bits, blocks of 32 cost 4.50 bits per weight and reach 17.7 dB. Blocks of 8 reach 22.7 dB, but cost 6.00 bits, and a third of those bits are scales.
The SNR here is the signal-to-noise ratio, a single number for “how close”:
Every 10 dB means the error carries ten times less energy. Each extra bit per value adds about 6 dB, which you can see in the gaps between the curves.
One big value spoils the scale
Trained weights are mostly small, but not all of them. A tiny fraction are far larger than their neighbours, and they matter: Yu and colleagues found single weights in open models whose removal destroys the model’s ability to generate text (The Super Weight in Large Language Models, 2024). In the activations that flow between layers, Dettmers and colleagues found whole feature dimensions with systematically large values (LLM.int8(), 2022). Absmax rounding is defenceless against them: the largest value sets the step, so everyone else in its group shares a coarse grid.
Where one outlier does its damage
Ordinary weights rounded to zero: 171, 79, 47 of 254, top to bottom.
With one scale for all 256 weights, the two outliers make the step so wide that 171 of the 254 ordinary weights round to zero, and their SNR is 7.3 dB. With a scale per block of 32, the damage stays in the two rows that contain an outlier: 79 round to zero, and the SNR is 13.6 dB, at the same 4 bits per value.
The third row borrows the idea of keeping outliers in higher precision. Values above eight times the median size are pulled out and stored separately in f16, along with their position. The remaining weights get tight scales again: 20.4 dB, for 5.00 bits per weight instead of 4.50. LLM.int8() does this for whole activation dimensions, multiplying them in 16-bit and more than 99.9% of the values in 8-bit; here it is done per weight, in storage.
Scales of scales
Look back at the main figure and press Scales of scales. That is q4_K, the building block of Q4_K_M. It groups 256 weights into a super-block of eight sub-blocks of 32. Each sub-block gets its own scale and its own minimum, so the 16 levels can shift to cover the block’s range instead of being centred on zero. Those eight scales and eight minimums are themselves quantised to 6 bits each, and stored with two 16-bit scales for the whole super-block: scales of scales.
The cost per weight is the same 4.5 bits as q4_0: 32 bits for the two super-block scales, 96 bits for sixteen 6-bit numbers, and 1,024 bits of codes, over 256 weights. The fit is better, because llama.cpp searches 21 candidate scales per sub-block and keeps the one with the least weighted error. On the same 256 weights, the ordinary weights reach 17.1 dB with q4_K against 13.6 dB with q4_0.
Q4_K_M is not one format but a recipe. Most tensors use q4_K, but llama.cpp gives two sensitive kinds of tensor (the attention value projection and the feed-forward output) q6_K in about half the layers, 16 of 32 in Llama 3.1 8B, and stores the output layer as q6_K too. That mix is where the extra bits come from: 4.89 bits per weight on average, not 4.5.
From rounding error to a worse model
A weight’s error does not stay put. Every layer multiplies its input by a matrix of rounded weights, so the output of each layer carries the error of every weight in it, and the next layer inherits that and adds its own.
Rounding error, layer after layer
In this toy network, the output after one layer is about as accurate as the weights themselves: random errors in a matrix-vector product do not cancel out, but they do not grow either. Then each layer adds fresh error. With q4_0, the weights sit at 17.8 dB, and after six layers the output is down to 10.0 dB. Six layers’ worth of independent errors is six times the error energy, about 8 dB. In this toy, 32 layers would lose about 15 dB.
SNR in a toy network is not the same as quality in a language model. The usual proxy is perplexity: how surprised the model is by real text, where lower is better. llama.cpp publishes the increase for each type, measured on one model, Llama 3 8B:
| Type | File size | Perplexity increase |
|---|---|---|
Q8_0 |
7.96 GiB | +0.0026 |
Q6_K |
6.14 GiB | +0.0217 |
Q5_K_M |
5.33 GiB | +0.0569 |
Q4_K_M |
4.58 GiB | +0.1754 |
Q4_0 |
4.34 GiB | +0.4685 |
Q3_K_M |
3.74 GiB | +0.6569 |
Q2_K |
2.96 GiB | +3.5199 |
Two things stand out. Q4_K_M is only 6% bigger than Q4_0 but has well under half the perplexity increase, which is the payoff of scales of scales and the mixed recipe. And the damage is not linear: below about 4 bits, it grows quickly. Treat the table as a rough guide. It is one model, one text and one measure, and perplexity can hide failures on particular tasks.
What this means for local AI
Reading a GGUF filename
llama.cpp’s model files (GGUF) carry the quantisation type in their name. A short key, from llama.cpp’s own quantiser:
Q4_K,Q5_K. K-quants with 4-bit or 5-bit codes: the super-blocks of 256 from scales of scales.Q4_0andQ8_0are the older blocks of 32 with one scale each._S,_M,_L: small, medium, large. The base type is the same; the suffix sets how many sensitive tensors are bumped to more bits.Q4_K_Sgives the attention value and feed-forward output tensorsq5_Kin the first few layers;Q4_K_Mgives themq6_Kin about half the layers. On Llama 3.1 8B that is 4.67 against 4.89 bits per weight, andQ5_K_SagainstQ5_K_Mis 5.57 against 5.70.IQ4_XS. An i-quant: 4.25 bits per weight, with its 16 levels on a non-linear grid rather than evenly spaced. The whole Llama 3.1 8B file comes to 4.46 bits per weight and 4.17 GiB, smaller thanQ4_K_Sat 4.36 GiB.- “imatrix”. The file was quantised with an importance matrix (see below). llama.cpp records the matrix’s file and dataset in the GGUF metadata (
quantize.imatrix.file,quantize.imatrix.dataset), so you can check the claim rather than trust the name.
Choosing
Q4_K_M,Q5_K_MorQ8_0? On the table above,Q8_0is indistinguishable from the original for most uses, but about 1.7 times the size ofQ4_K_M.Q5_K_Mcosts 16% more thanQ4_K_Mand cuts its perplexity increase by about two thirds.Q4_K_Mis the usual default because it is where the curve starts to bend. If a model only fits atQ4_K_M, use it; if you have room,Q5_K_Mis cheap insurance. Will it run on my machine? does the memory and speed arithmetic for your hardware.- Why “the same 7B” files differ in size. The name is a recipe, not a bit count. Two
Q4_K_Mfiles of similar models differ because the models have different parameter counts, different tensor shapes (a larger vocabulary means bigger input and output tables) and a different mix of sensitive tensors that the recipe bumps to more bits. - Importance matrices. Many files, especially the IQ types and those labelled imatrix, are quantised with an importance matrix: llama.cpp first runs the model on some sample text and records how large each input to each matrix tends to be. When it then chooses scales, it weights the rounding error by that importance, so the weights that meet large inputs are rounded more carefully. The bits per weight stay the same; the error is moved to where it matters less. The result depends on the sample text, so it can favour some kinds of input over others.
- Small models tend to feel it more. It is often reported that larger models tolerate aggressive quantisation better than small ones. This is a tendency, not a rule, so if quality matters, compare two levels on your own prompts.
What we simplified
- The weights are synthetic. They are heavy-tailed random numbers with planted outliers, not a real model’s tensors. Real weight distributions differ by layer and by model.
- The formats follow llama.cpp; the recipe is not built.
q8_0,q4_0andq4_Kfollow llama.cpp’s reference code, including fp16 scales and rounding rules.Q4_K_Mis described, not built, and we ignore the importance matrix that llama.cpp can apply to the same search. - Other methods are left out. GPTQ uses calibration data to correct rounding error as it goes; AWQ uses it to protect the most important channels. Both are common outside llama.cpp, and some runtimes also quantise activations. The Modular LLM inference handbook and Maarten Grootendorst’s visual guide cover these. Within llama.cpp, some i-quants (IQ2, IQ3) use lattice-based codebooks, and IQ4_NL uses a non-linear grid of 16 levels.
- Our outlier scheme is per value. It borrows the idea from LLM.int8(), which does this for whole activation dimensions during the multiplication; we keep individual weights in f16 to show it in storage.
- The network is a toy. Six square matrices with normalisation between them: no attention, no nonlinearity and no residual connections. It shows how errors accumulate, not how much a real model degrades.
- Perplexity is quoted, not computed. The table is llama.cpp’s own measurement on Llama 3 8B.
Next: How RAG finds the right passage looks at what a model can read beyond its weights.
Inspired by, and worth your time
- llama.cpp quantisation (llama-quantize README and ggml-quants.c) by ggml-org and llama.cpp contributors
- LLM quantization, LLM Inference Handbook by Modular
- A Visual Guide to Quantization by Maarten Grootendorst