"""INT8 affine quantisation and an integer matrix multiply with int32 accumulation. q = clip(round(x / s) + z, -128, 127) x_hat = s * (q - z) Activations use an affine (asymmetric) mapping with a zero point z. Weights use the symmetric case z = 0, optionally with one scale per output channel. """ from __future__ import annotations import numpy as np QMIN, QMAX = -128, 127 def affine_qparams(x_min: float, x_max: float) -> tuple[float, int]: """Scale and zero point that map [x_min, x_max] onto [-128, 127] with 0.0 exact.""" x_min, x_max = min(float(x_min), 0.0), max(float(x_max), 0.0) if x_max == x_min: return 1.0, 0 scale = (x_max - x_min) / (QMAX - QMIN) zero_point = int(np.clip(round(QMIN - x_min / scale), QMIN, QMAX)) return scale, zero_point def symmetric_scale(w: np.ndarray, axis: int | None = None) -> np.ndarray | float: """Scale so that max|w| maps to 127 (zero point 0). axis=1 gives one scale per row.""" absmax = np.max(np.abs(w), axis=axis) scale = np.where(absmax > 0, absmax / QMAX, 1.0) return float(scale) if axis is None else scale def quantize(x: np.ndarray, scale: float | np.ndarray, zero_point: int = 0) -> np.ndarray: return np.clip(np.round(np.asarray(x) / scale) + zero_point, QMIN, QMAX).astype(np.int8) def dequantize(q: np.ndarray, scale: float | np.ndarray, zero_point: int = 0) -> np.ndarray: return scale * (q.astype(np.float64) - zero_point) def int8_matmul(qx: np.ndarray, zx: int, qw: np.ndarray) -> np.ndarray: """Integer part of y = W x: sum_k (qx[k] - zx) * qw[n, k], accumulated in int32. qx has shape (..., K), qw has shape (N, K). Each product fits in 16 bits (|qx - zx| <= 255, |qw| <= 127), so the int32 sum is exact for K < 66 000. """ if qx.shape[-1] >= 66_000: raise ValueError("K too large for exact int32 accumulation") xs = qx.astype(np.int32) - np.int32(zx) return xs @ qw.astype(np.int32).T def int8_linear(x: np.ndarray, w: np.ndarray, bias: np.ndarray | None, x_range: tuple[float, float], per_channel: bool = True) -> np.ndarray: """Quantised y = W x + b. Returns float output after one rescale per channel.""" sx, zx = affine_qparams(*x_range) sw = symmetric_scale(w, axis=1) if per_channel else symmetric_scale(w) qx = quantize(x, sx, zx) qw = quantize(w, np.reshape(sw, (-1, 1)) if per_channel else sw) acc = int8_matmul(qx, zx, qw) if bias is not None: # The bias is stored as int32 with scale sx * sw, so it adds straight into the accumulator. acc = acc + np.round(bias / (sx * sw)).astype(np.int32) return (sx * sw) * acc if __name__ == "__main__": s, z = affine_qparams(-1.0, 3.0) q = quantize(np.array([0.5]), s, z) print(f"range [-1, 3]: scale = {s:.6f}, zero point = {z}") print(f"x = 0.5 -> q = {int(q[0])} -> x_hat = {dequantize(q, s, z)[0]:.5f}") rng = np.random.default_rng(0) w = rng.normal(0.0, 0.2, (64, 128)) b = rng.normal(0.0, 0.1, 64) x = rng.uniform(-1.0, 3.0, (256, 128)) y_float = x @ w.T + b for pc in (False, True): y_q = int8_linear(x, w, b, x_range=(-1.0, 3.0), per_channel=pc) err = y_q - y_float rel = np.sqrt(np.mean(err**2)) / np.sqrt(np.mean(y_float**2)) print(f"per-channel={pc!s:5}: max |error| = {np.max(np.abs(err)):.4f}, relative RMS error = {rel:.2%}")