Neural Mastery
← Practice
Practice: INT8 Weight Quantization From Scratchmedium

Practice: INT8 Weight Quantization From Scratch

medium ML Systems & MLOps 15–20 min

NumPy allowed · plain Python earns 10 bonus points

New to this topic? MLOps Overview
Study this concept first →

Implement symmetric absolute maximum INT8 quantization to compress neural network weights with minimal accuracy degradation.

Task

Implement quantize_int8(W). Compute scale = max(|W|) / 127.0. Round W / scale to nearest integer clipped to [-127, 127]. Return [quantized_W, scale].

Function Signature

quantize_int8(W: list[list[float]]) -> tuple[list[list[int]], float]

Examples

Example 1: Symmetric extrema matrix
Input: W = [[-127, 0], [63.5, 127]]
Output: [[[-127, 0], [64, 127]], 1]
Example 2: Uniform small weights
Input: W = [[0.5, -0.5], [0.25, 0.1]]
Output: [[[127, -127], [64, 25]], 0.003937]

Constraints

  • W is a 2D matrix of floats
  • Quantized values in integer range [-127, 127]
  • Symmetric zero-point at 0
Python3Saved ✓
import numpy as np

def quantize_int8(W: list[list[float]]) -> tuple[list[list[int]], float]:
"""Compute symmetric INT8 weight quantization.
scale = max(abs(W)) / 127.0 (or 1.0 if zero).
Return (quantized_matrix_as_int, scale_float).
"""
# Your code here
pass

Case 1: Symmetric extrema matrix
Input: W = [[-127, 0], [63.5, 127]]
Expected: [[[-127, 0], [64, 127]], 1]
Case 2: Uniform small weights
Input: W = [[0.5, -0.5], [0.25, 0.1]]
Expected: [[[127, -127], [64, 25]], 0.003937]