The most widely used approach is uniform quantization, which maps values onto an evenly spaced grid. A tensor (such as a weight matrix) is converted to integers by the following steps.
- Choose the range of values [α,β] to represent with integers (the clipping range). This step is called calibration. The simplest approach uses the minimum and maximum of the values as they are.
- From the bit width b, determine the scale s, which is the spacing of the grid.
- Determine the zero point z (an integer), which says which integer the real number 0 maps to.
- Divide each value by s, round, add z, and clip at the ends so it fits in the integer range (quantization).
- For computation, subtract z from the integer and multiply by s to get a real number close to the original (dequantization).
The scale is the size obtained by dividing the range into 2b−1 intervals (Eq. (4) in the survey by Gholami et al.).
s=2b−1β−α
Quantization and dequantization can be written as follows (Eqs. (4) and (6) in the white paper by Nagel et al.).
xint=clamp(⌊sx⌉+z; 0, 2b−1),x^=s(xint−z)
Here ⌊⋅⌉ is rounding to the nearest integer, and clamp(⋅;a,c) sets values smaller than a to a and values larger than c to c. This form is called asymmetric quantization (affine quantization). Thanks to the zero point, the real 0 maps exactly to an integer, so zero padding and the zeros output by ReLU incur no error.
Fixing the zero point at 0 gives symmetric quantization. With signed integers, it becomes the following (Eqs. (8a) and (8c) in the white paper).
xint=clamp(⌊sx⌉; −2b−1, 2b−1−1),x^=sxint
A common choice is to set s=max∣x∣/(2b−1−1) from the maximum absolute value, which maps to [−127,127] at 8 bits. This is also called absmax quantization.
When both the weights W and the activations x are quantized, a matrix-vector product can be computed as a sum of integer products multiplied by the scales just once (Eq. (3) in the white paper, for symmetric quantization).
y^n=swsxm∑Wn,mintxmint
Since the sum contains only integers, it can be computed by integer multiply-accumulate units. However, adding many products quickly exceeds 8 bits, so the accumulator is 32 bits. The result is requantized to 8 bits before being passed to the next layer.
"W8A8" means 8-bit weights and 8-bit activations, and "W4A16" means 4-bit weights and 16-bit activations. Weight-only quantization is suited to reducing memory, while quantizing both weights and activations is suited to speeding up computation with integer arithmetic units.