Sub topic 3
What quantisation does to a model
This chapter assumes you know what a parameter is, that weights are stored as numbers with a fixed number of bits, and that you can do arithmetic with bits, bytes and GiB. The previous chapter covered number formats from FP32 down to 4-bit. Here you will see how a model's weights are actually compressed into those smaller formats, what is lost, and what the common errors are.
The core idea
Quantisation replaces each high-precision weight with a number from a much smaller set of values. A 16-bit float can take about 65,000 distinct values. A 4-bit integer can take 16. To make the small set useful, the weights are stored as small integers plus a scale, a higher-precision number that maps the integers back to a realistic range.
The reconstruction step is called dequantisation:
weight ≈ scale × integerThe stored file holds the integers and the scales. At run time the inference engine converts them back to approximate weights, usually on the fly inside the matrix multiplication, so the full 16-bit model is never rebuilt in memory.
Quantisation is lossy. The reconstructed weight is close to the original but not equal to it. The difference is the quantisation error. The whole design of a quantisation format is about keeping that error small while using few bits.
A worked example
Take six weights and quantise them to signed 4-bit integers, which range from -8 to 7. A simple method, called absmax quantisation, picks the scale so the largest absolute weight maps to the edge of the range.
def quantise_block(weights, bits=4):
qmax = 2 ** (bits - 1) - 1 # 7 for 4-bit signed
scale = max(abs(w) for w in weights) / qmax
ints = [round(w / scale) for w in weights]
return scale, ints
def dequantise_block(scale, ints):
return [scale * q for q in ints]
weights = [0.12, -0.50, 0.33, 0.02, -0.07, 0.41]
scale, ints = quantise_block(weights)
restored = dequantise_block(scale, ints)
print(f"scale = {scale:.4f}")
print("integers:", ints)
for w, r in zip(weights, restored):
print(f"{w:+.3f} -> {r:+.3f} error {abs(w - r):.3f}")The largest weight, -0.50, becomes -7 and is restored almost exactly. The small weight 0.02 rounds to 0 and comes back as 0.000. Every weight in the block is rounded to the nearest of the available steps, and the step size is the scale. Larger scale means coarser steps and larger error.
Why blocks matter
The example used one scale for the whole list. Real formats use one scale per small block of weights, commonly 32 weights in the simpler llama.cpp formats, or 256 weights grouped into super-blocks in the K-quant formats.
The reason is outliers. A single large weight forces a large scale, which makes the steps coarse for every other weight that shares it. Smaller blocks confine the damage from an outlier to its own block.
Blocks have a cost. Each scale takes bits too, and those bits are spread over the weights in the block. For a block of 32 weights with one 16-bit scale, the scale adds 16 / 32 = 0.5 bits per weight. A "4-bit" format therefore uses about 4.5 bits per weight. This is why a file labelled Q4 is larger than parameters × 4 / 8.
| Format idea | Bits per integer | Block size | Scale cost per weight | Effective bits per weight |
|---|---|---|---|---|
| 4-bit, 16-bit scale | 4 | 32 | 16 / 32 = 0.5 | about 4.5 |
| 8-bit, 16-bit scale | 8 | 32 | 16 / 32 = 0.5 | about 8.5 |
| 4-bit, 16-bit scale and minimum | 4 | 32 | 32 / 32 = 1.0 | about 5.0 |
The last row shows an asymmetric scheme, which stores a minimum value as well as a scale. It fits blocks whose weights are not centred on zero, at the price of one more number per block.
What gets quantised
Not every number in a model is treated the same way.
- Weight matrices in the attention and feed-forward layers hold nearly all the parameters. These are quantised.
- Normalisation parameters are few and sensitive, so they are usually kept at higher precision.
- Embeddings and the output layer are large and affect quality strongly. Many quantisation recipes keep them at a higher bit width than the rest.
- Activations, the values flowing through the model during a forward pass, are normally left in 16-bit or 32-bit floating point in common local setups.
- The KV cache is a separate store, covered in a later chapter. Its precision is set independently of the weights.
The practical result is that a "4-bit" model is a mixture. Some tensors are stored at 4 bits, some at 6 or 8, and a few at 16 or 32. The effective bits per weight for the whole file is the average, which is why the figure of about 4.85 bits per weight used for Q4KM in the spine's memory example is higher than 4.5.
Two ways to quantise
Post-training quantisation takes a finished model and compresses it without further training. It is fast and cheap, and it is what nearly all downloadable local model files use.
Quantisation-aware training trains or fine-tunes the model while simulating the rounding, so the model learns to tolerate it. It can keep more quality at very low bit widths but needs training resources, so it is done by the model's publisher, not by the person running it locally.
Within post-training quantisation there is one more distinction.
- Round-to-nearest quantisation, as in the example above, treats every weight equally.
- Calibration-based methods, such as importance-matrix approaches, run sample text through the model first. They measure which weights matter most for the output and spend precision on those. The same bit budget then gives lower error on typical text.
A calibration-based file can beat a plain file of the same size, but it is tuned to its calibration text. If that text is mostly English prose, quality on code or other languages may improve less.
Measuring the error
You can check how much a format hurts by measuring reconstruction error on a block, though for real models you would compare output quality, which a later chapter covers. This code compares bit widths on random data.
import random
def quantise_block(weights, bits):
qmax = 2 ** (bits - 1) - 1
scale = max(abs(w) for w in weights) / qmax
return scale, [round(w / scale) for w in weights]
def mean_abs_error(weights, bits):
scale, ints = quantise_block(weights, bits)
restored = [scale * q for q in ints]
return sum(abs(w - r) for w, r in zip(weights, restored)) / len(weights)
random.seed(0)
weights = [random.gauss(0, 0.05) for _ in range(32)]
for bits in (8, 6, 4, 3, 2):
print(f"{bits}-bit: mean abs error {mean_abs_error(weights, bits):.5f}")Each bit removed roughly doubles the step size, so the error roughly doubles. Going from 8 bits to 4 multiplies it by about 16. That is large in relative terms, yet neural networks tolerate it surprisingly well because they carry a lot of redundancy and the errors are spread across millions of weights rather than concentrated in a few.
Common errors
- Assuming file size is parameters × bits ÷ 8 exactly. Scales, mixed-precision tensors and metadata make real files larger. Use the effective bits per weight of the specific format.
- Believing quantisation speeds up computation directly. It shrinks memory use, and for token generation that also reduces the data read per token, which often helps speed. The arithmetic itself still involves a dequantisation step, so the gain depends on the hardware and engine.
- Quantising an already quantised model again. Errors compound. Always start from the 16-bit original when you make a file.
- Judging a format from one prompt. Differences between 8-bit and 5-bit are usually too small to see in a short chat. Use a measured comparison.
- Assuming all 4-bit files are equal. Two files with the same nominal bit width can differ in block size, mixed precision and calibration, so their quality and size differ.
Summary of the working model
- Quantisation stores weights as small integers with a per-block scale, then dequantises during computation.
- The loss is rounding error, which grows as bits fall and as outliers inflate the scale.
- Blocks limit outlier damage but add scale overhead, so effective bits per weight exceed the nominal figure.
- Different tensors get different precision, and calibration can direct precision to where it matters.
- Naming schemes such as Q4KM encode these choices, which the next chapter decodes.
Exercise
A block of 32 weights is stored as 4-bit integers with one 16-bit scale and one 16-bit minimum. How many effective bits per weight does it use, and how many GiB would a 7-billion-parameter model need if every weight used this format?
Show answer
Overhead is (16 + 16) / 32 = 1.0 bit per weight, so the effective size is 4 + 1 = 5 bits per weight. Memory is 7e9 × 5 / 8 bytes = 4.375e9 bytes, which is 4.375e9 / 2^30, about 4.07 GiB.
Exercise
In the absmax example, one weight in a block is 10 times larger than all the others. Why does this hurt the accuracy of the other weights, and what design choice reduces the problem?
Show answer
The scale is set by the largest weight, so it becomes about 10 times larger and every integer step covers a much wider range of values. The small weights then round to a few integer values, often 0 or ±1, and lose most of their detail. Using smaller blocks, so the outlier shares a scale with fewer weights, limits the damage to that block.
Exercise
Why can a 4-bit model file be larger than parameters × 4 / 8 bytes, and why is that not a mistake in the file?
Show answer
The file also stores per-block scales (and sometimes minimums), and some tensors, such as embeddings and the output layer, are kept at higher precision. Together these raise the average to more than 4 bits per weight, so the file is correctly larger than the naive figure.