Quantization of models
How a model is shrunk by storing each weight in fewer bits. Four weights are taken from 32-bit floats to 8-bit integers and back, with the size saved and the accuracy lost counted exactly.
In 30 seconds
A neural network is billions of numbers called weights, normally stored with 32 bits each. Quantization stores them with fewer bits, such as 8 or 4, by rounding every weight to the nearest step on a coarse ruler.
The model becomes several times smaller and usually faster, because there is less data to move and small integers are cheap to compute with.
The cost is rounding error in every weight. With 8 bits it is usually negligible; with very few bits the model’s answers get noticeably worse. That trade is what lets large models run on laptops and phones.
Quantization of models, step by step
This is the simplest scheme: one scale per layer, symmetric around zero. Real tools often use a scale per row or block and may add an offset. Sizes count the weights only, with 1 GB = 1 billion bytes.
- Step 1: A model is a pile of numbers. A neural network’s knowledge sits in its weights: plain numbers, billions of them. By default each is stored as a 32-bit floating-point number, which takes 4 bytes. For a model with 7 billion weights that comes to 28 GB, all held in memory while the model runs. That is more than most laptops have, let alone a phone.
- Step 2: Pick a scale. Quantization stores each weight with fewer bits. Take four weights from one layer. With 8 bits we can use the whole numbers from −127 to 127, so stretch that ruler over the weights: the largest, 1.27 in size, lands on 127. One step is then worth 1.27 ÷ 127 = 0.01. That number is the scale, stored once for the whole layer.
- Step 3: Round to whole numbers. Divide each weight by the scale and round to the nearest whole number. 0.4237 becomes 42.37, which rounds to 42; 0.0561 becomes 5.61, which rounds up to 6. Every result lies between −127 and 127, so it fits in a single byte. The digits after the rounding are thrown away for good: this is where information is lost.
- Step 4: Multiply back to use them. To use a stored weight, multiply the integer by the scale again. 42 gives back 0.42, not the original 0.4237: an error of 0.0037. Rounding moves a value by at most half a step, so no weight is ever off by more than 0.005. The model now computes with weights that are all slightly wrong, each by a tiny amount.
- Step 5: Four times smaller, or more. One byte instead of four cuts the 7-billion-weight model from 28 GB to 7 GB. Four bits per weight, a common choice for running language models locally, halves it again to 3.5 GB. This is the main reason quantized models can run on laptops and phones: the whole model has to fit in the device’s memory before anything else matters.
- Step 6: Faster as well. Running a model is mostly multiplying weights by inputs, and much of the time goes on fetching the weights from memory. A quarter of the bytes means a quarter of the traffic. Processors also handle small integers in bulk: one 256-bit register holds 8 floats but 32 eight-bit integers. Real gains depend on chip and software.
- Step 7: The price: accuracy. Fewer bits mean fewer levels to round to. Eight bits give 255 levels, and our weight lands 0.0037 from the truth. Four bits give only 15 levels, 0.1814 apart, and the same weight becomes 0.3629, off by 0.0608. Such errors blur the model’s answers. Typically 8 bits cost little quality, 4 bits somewhat more, fewer still a lot.
Try it yourself
Learn this first
Spotted a mistake on this page?