Symmetric INT8 Quantization
Problem statement
Implement symmetric INT8 quantization of a list of floats, the scheme commonly used to shrink model weights and speed up inference.
- Scale: map the largest absolute value to 127: $s = \max_i |x_i| / 127$. If every value is zero, use $s = 1.0$.
- Quantize: $q_i = \text{clip}(\text{round}(x_i / s), -127, 127)$ as integers (zero point is 0, so the range is symmetric).
- Dequantize: $\hat{x}_i = q_i \cdot s$.
Write int8_quantize(x) that returns a dictionary:
'quantized': list of ints,'scale': the scale rounded to 6 decimal places,'dequantized': list of reconstructed floats, each rounded to 4 decimal places.
Example
Input:
x = [1.0, -1.0, 0.5, -0.5]
Output:
{'quantized': [127, -127, 64, -64], 'scale': 0.007874, 'dequantized': [1.0, -1.0, 0.5039, -0.5039]}
Reasoning: 1) Find abs_max = max(|1.0|, |-1.0|, |0.5|, |-0.5|) = 1.0
- Calculate scale = 1.0 / 127 = 0.007874
- Quantize each value: 1.0/0.007874 = 127, -1.0/0.007874 = -127, 0.5/0.007874 = 63.5 -> round to 64, -0.5/0.007874 = -63.5 -> round to -64
- Dequantize: 127 0.007874 = 1.0, -127 0.007874 = -1.0, 64 0.007874 = 0.5039, -64 0.007874 = -0.5039
Note the quantization error for 0.5 and -0.5 due to the discrete nature of INT8 representation.
Show hint
Which single statistic of the input decides the step size of a symmetric integer grid?
Relevant roles: ML Engineer, LLM Engineer, Deep Learning Engineer, MLOps Engineer and AI Engineer.
Related free problems
- Multi-Head AttentionMedium
- Masked Self-AttentionMedium