Quantization
On this page
A ruler marked only in whole millimetres still measures a table well. Every reading is moved to the nearest mark, and is wrong by half a millimetre at most. Quantization does this to the numbers inside a model fitted to data: each one is moved to the nearest mark on a coarse scale, so that it can be stored in 8 bits and not 32.
See it
The weights of a model lie between −1.016 and 1.016, and each is stored in 32 bits. You store each one in 8 bits. What happens to the values?
A dark point is the exact weight. The gold bar is the level it is moved to. Where the point sits off its bar, the gap between them is the error. The figure uses the same number of levels on each side of zero, which is 127 at 8 bits. The size is for a real small model of 12,195 weights, which takes 48,780 bytes at 32 bits. Tools for microcontrollers use 8 bits. The lower settings are here to show why they do not use fewer.
The idea
A model holds thousands of weights, the numbers found when it was trained. A computer stores each as a 32 bit number with a decimal point, called float32, which takes 4 bytes. Quantization replaces each with an 8 bit whole number, called int8, which takes 1 byte and can name 256 levels, from −128 to 127.
Two numbers connect the two forms. The scale is the step between two levels, chosen so that the levels cover the range of the weights. The zero point is the whole number that stands for 0.0. It is 0 in every example on this page.
float value = (int8 value − zero point) × scale
the first line stores a weight, the second reads it back
The rounding moves each weight by at most half a step. The weights become 4 times smaller (a whole model file somewhat less, because its description and scales do not shrink), and arithmetic on whole numbers is 2 to 4 times faster on a small processor. The errors do add up through the model, so its accuracy falls a little and must be measured again. The lesson on quantization for microcontrollers gives an example of a fall from 95.5% to 93.2%.
One worked number
A weight has the value 0.219. The scale is 0.008 and the zero point is 0. What whole number is stored, what value is read back, and how large is the error?
Show the worked solution, 3 steps
- Divide by the scale and round
0.219 ÷ 0.008 = 27.375, which rounds to 27. This whole number is what the chip stores, in 1 byte.
- Read it back
27 × 0.008 = 0.216.
- Find the error
0.219 − 0.216 = 0.003, less than half a step (0.004). It is the first weight in the figure, which shows the same three values.
The mistake to avoid
A second trap: the largest value sets the scale. One extreme weight stretches the range, the step grows, and every other weight is stored more coarsely.
Check yourself
A weight has the value 0.42. The scale is 0.05 and the zero point is 0. What is stored, and what value is read back?
A model has 200,000 weights stored as float32. How much memory do the weights take before and after quantization to int8?
A model is right 95.5% of the time as float32. It is quantized to int8. What should you expect?
Where you meet it
- A vibration monitor on a motorIts model fits in the flash of a small chip only after quantization.
- A sensor that runs on a batteryThe quantized model finishes sooner, so the processor sleeps for more of the time.
- Every digital measurementA converter does the same to a voltage: it reports the nearest of a fixed number of levels, as in ADC sampling.
- Quantization stores each weight as an 8 bit whole number in place of a 32 bit number: 1 byte in place of 4.
- Stored value = round(weight ÷ scale) + zero point. The error is at most half a step.
- The weights are 4 times smaller, and the model runs 2 to 4 times faster on a small processor.
- The accuracy falls a little. Measure it again after quantization.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.