INT8 Weight Quantization

medium
Unit 15 · Inference I0/1 to milestone

Storing model weights as 8-bit integers instead of 32-bit floats makes models 4× smaller and much faster to serve. The simplest scheme is symmetric per-tensor quantization:

scale=max⁡∣w∣127,q=clip(round(wscale),−127,127)\text{scale} = \frac{\max |w|}{127}, \qquad q = \text{clip}\left(\text{round}\left(\frac{w}{\text{scale}}\right), -127, 127\right)

Implement:

  • quantize(w), returning (q, scale), where q is a NumPy array with dtype np.int8. If every weight is zero, use scale = 1.0.
  • dequantize(q, scale), returning the float approximation q * scale.

Use np.round.

Example

q, s = quantize(np.array([0.5, -1.27, 0.0, 1.0]))
# q = array([50, -127, 0, 100], dtype=int8), s = 0.01
#inference#quantization#numpy3 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
INT8 Weight Quantization · TechGradify