How to use the Quantization scale Calculator tool
- Enter the lower end of the real range, zero or negative, in the Minimum value field.
- Enter the upper end, zero or positive, in the Maximum value field.
- Enter the integer width in the Bits field. Any whole number from 2 to 16 is allowed.
- Select Calculate, or select Example to use the range -1.5 to 2 at 8 bits.
- Read Scale, Zero point and Code range in the Result box, then select Copy.
Quantization scale and zero point formulas
Affine quantization relates a real value r to an integer code q by r = scale x (q - zero_point). The scale is the real distance between two neighboring codes. The zero point is the code that represents real 0.
This calculator uses the full unsigned code range, 0 to 2^bits - 1. It computes scale = (max - min) / (2^bits - 1) and zero_point = round(-min / scale). The Code range line shows the lowest and highest code, for example 0 to 255 at 8 bits. To quantize a value afterwards, compute round(r / scale) + zero_point and clamp it to that range.
Worked example: range -1.5 to 2 at 8 bits
The range is 3.5 wide and there are 255 steps, so the scale is 3.5 / 255, about 0.013725. Dividing 1.5 by the scale gives 109.29, which rounds to a zero point of 109.
Because the zero point had to be rounded, the representable range shifts slightly. Code 0 stands for (0 - 109) x scale = -1.4961 and code 255 stands for (255 - 109) x scale = 2.0039. The requested minimum of -1.5 still maps to code 0, with an error of about 0.004. This small nudge is the cost of representing zero exactly.
Minimum value: -1.5
Maximum value: 2
Bits: 8
Scale: 0.013725490196078431
Zero point: 109
Code range: 0 to 255Why the range must contain zero
The tool requires a minimum at or below zero, a maximum at or above zero, and a minimum smaller than the maximum. Otherwise it stops with the message Enter an increasing min/max range that contains zero. The rule comes from how quantized networks behave: zero padding and ReLU outputs produce exact zeros, and those must map to an integer code without error.
If your observed range is 0.5 to 2, enter 0 as the minimum. For a ReLU6 output, 0 to 6 at 8 bits gives a scale of 0.023529411764705882 and a zero point of 0. For a range that ends at zero, such as -3 to 0, the zero point is the top code, 255.
Converting to signed int8 and other details
A few details help when you move the numbers into a framework or firmware. The calculation itself is done locally in your browser.
- Signed codes: subtract 2^(bits - 1) from the zero point. For 8 bits, 109 becomes -19 on the -128 to 127 scale. The scale does not change.
- Symmetric ranges: -1 to 1 at 8 bits gives a zero point of 128, because 127.5 rounds up.
- Bits accepts whole numbers only. Minimum and Maximum must be finite numbers.
Frequently asked questions
What is the formula for scale and zero point?
Scale is (max - min) divided by the number of steps, 2^bits - 1. Zero point is -min divided by the scale, rounded to the nearest integer. With min -1 and max 3 at 8 bits, the scale is 0.01568627450980392 and the zero point is 64.
How do I get a zero point for int8 instead of uint8?
Subtract 128 from the unsigned zero point shown here. The code range then runs from -128 to 127 and the scale stays the same. A zero point of 64 for uint8 corresponds to -64 for int8.
What is the difference between symmetric and asymmetric quantization?
Symmetric quantization fixes the zero point at 0, which suits weights. Asymmetric, or affine, quantization adds a zero point so that a lopsided range such as 0 to 6 uses every code. This page computes the asymmetric parameters.
Does it quantize my actual values?
No. It turns a range into parameters and stops there. To see integer codes for a list of numbers under a symmetric scheme, use the INT8 quantization Calculator. For the affine case, apply round(r / scale) + zero point yourself.