← back

Implement INT8 Quantization

#294 · Deep Learning · Medium

⊣ Solve on deep-ml.com

Problem

Implement INT8 quantization for neural network weights. Convert floating-point weights to 8-bit integers using a scale and zero-point, and implement dequantization to recover approximate float values.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import numpy as np

def quantize_int8(weights: np.ndarray) -> dict:
    w_min = weights.min()
    w_max = weights.max()

    qmin = -128
    qmax = 127

    scale = (w_max - w_min) / (qmax - qmin)
    if scale == 0:
        scale = 1.0
    zero_point = int(np.round(qmin - w_min / scale))
    zero_point = np.clip(zero_point, qmin, qmax)

    quantized = np.round(weights / scale + zero_point).astype(np.int8)
    quantized = np.clip(quantized, qmin, qmax).astype(np.int8)

    return {"quantized": quantized, "scale": float(scale), "zero_point": int(zero_point)}

def dequantize_int8(quantized: np.ndarray, scale: float, zero_point: int) -> np.ndarray:
    return (quantized.astype(np.float32) - zero_point) * scale

Explanation

  1. Compute scale: maps the float range [w_min, w_max] to the int8 range [-128, 127]. scale = (w_max - w_min) / 255.
  2. Compute zero_point: the int8 value that corresponds to float 0, ensuring zero maps exactly.
  3. Quantize: round(weight / scale + zero_point) and clip to [-128, 127].
  4. Dequantize: (quantized - zero_point) * scale recovers approximate float values.
  5. Quantization reduces model size by ~4x (float32 to int8) and enables faster integer arithmetic on supported hardware.

Complexity

  • Time: O(n) where n is the number of weight elements
  • Space: O(n) for the quantized weights