Sub topic 3
What quantisation does to a model
This chapter assumes you know that a model is a large set of numbers called parameters, and that each parameter takes some number of bits to store. It also assumes you are comfortable with bytes, bits, and GB. Number formats such as FP16 and 4-bit were covered in the previous chapter.
The working model
Quantisation stores each weight with fewer bits than the original. A model trained in 16-bit floating point (FP16 or BF16) can be rewritten at 8 bits or 4 bits per weight. The file shrinks in proportion, and so does the memory the weights occupy.
The cost is precision. A 16-bit weight can take about 65,000 distinct values. A 4-bit weight can take only 16. Quantisation maps each original value to the nearest of the few values available, so every weight is slightly wrong. The model still works because the errors are small and spread across billions of weights.
Blocks and scales
A single scale for a whole tensor would work badly. One large outlier weight would stretch the range, and most weights would collapse onto a few levels. Quantisation formats therefore work on small blocks, for example 32 weights at a time. Each block stores:
- the low-bit integers for its weights
- a scale factor that converts those integers back to real values
The scale is extra data, so the real cost is higher than the nominal bit count. A "4-bit" format with a 16-bit scale per 32 weights uses 4 + 16/32 = 4.5 bits per weight. This is why the code in the memory chapters uses about 4.85 bits for Q4KM, not 4.0.
def effective_bits(weight_bits, scale_bits, block_size):
return weight_bits + scale_bits / block_size
print(effective_bits(4, 16, 32)) # 4.5
print(effective_bits(8, 16, 32)) # 8.5Where the error comes from
Rounding error is the difference between the original weight and its restored value. Fewer bits means larger steps between levels, so larger error. Two things reduce the damage:
- Smaller blocks give each scale a narrower range to cover, at the cost of more scale data.
- Mixed schemes keep sensitive tensors at higher precision and compress the rest harder. Many 4-bit files do this, which is part of why they have more than 4.5 bits per weight.
Common errors
- Assuming file size equals bits divided by 8 times parameters. Scales and the higher-precision tensors add overhead. Use the effective bits per weight.
- Expecting speed to be unchanged. Smaller weights mean less data to read per token, which usually makes generation faster. The restore step adds a little compute.
- Quantising an already quantised file. Convert from the 16-bit original. Repeated rounding compounds error.
- Treating 8-bit as always necessary. Quality loss from 8-bit is usually very small. Whether a lower level is acceptable depends on the model and task, which a later chapter covers.
Exercise
A format stores 5-bit weights in blocks of 64, with one 16-bit scale per block. What is its effective bits per weight, and how many GiB do the weights of a 7-billion-parameter model need at that rate (ignoring other overhead)?
Show answer
Effective bits = 5 + 16/64 = 5.25 bits per weight. Memory = 7e9 × 5.25 / 8 bytes = 4.594e9 bytes, which is about 4.28 GiB (dividing by 2^30).