Post-Training Quantization with Per-Channel Scale Factors
Problem statement
Post-training quantization (PTQ) is a technique for compressing neural network weights from floating-point to lower-precision integer representations, reducing memory usage and accelerating inference. Per-channel quantization computes a separate scale factor for each output channel (row) of a weight matrix, preserving more accuracy than a single global scale.
Implement the function per_channel_quantize(weight, bits) that performs symmetric per-channel quantization on a 2D weight matrix:
Inputs:
weight: A 2D numpy array of shape(out_channels, in_features)containing floating-point weightsbits: An integer specifying the target bit-width (default 8)
The function should return a tuple of three numpy arrays:
quantized_weights: Integer array of same shape asweight, containing the quantized valuesscale_factors: 1D float array of shape(out_channels,)with one scale factor per channeldequantized_weights: Float array of same shape asweight, obtained by converting the quantized integers back to floating point using the scale factors
Requirements:
- Use symmetric quantization where the integer range is
[-(2^(bits-1)), 2^(bits-1) - 1] - The scale factor for each channel should map the channel's maximum absolute weight value to the maximum positive representable integer
2^(bits-1) - 1 - If a channel has all zero weights, use a scale factor of
1.0to avoid division by zero - Quantized values must be clipped to the valid integer range
- Standard rounding rules apply (numpy default rounding)
Example
Input:
weight = np.array([[1.0, -2.0, 3.0], [-4.0, 5.0, -6.0]]), bits = 8
Output:
quantized = [[42, -85, 127], [-85, 106, -127]], scale = [0.023622, 0.047244], dequantized = [[0.9921, -2.0079, 3.0], [-4.0157, 5.0079, -6.0]]
Reasoning: For 8-bit quantization, qmax = 127. Channel 0 has max|w| = 3.0, so scale = 3.0/127 = 0.023622. Channel 1 has max|w| = 6.0, so scale = 6.0/127 = 0.047244. Channel 0: round([1.0/0.023622, -2.0/0.023622, 3.0/0.023622]) = round([42.33, -84.67, 127.0]) = [42, -85, 127]. Dequantize by multiplying back: [420.023622, -850.023622, 127*0.023622] = [0.9921, -2.0079, 3.0]. The quantization error is small but nonzero for values that don't map exactly to integer grid points.
Show hint
What single number per row would make that row's largest weight land exactly on the top integer code?
Relevant roles: ML Engineer, MLOps Engineer, Deep Learning Engineer, LLM Engineer and AI Engineer.
Related free problems
- Multi-Head AttentionMedium
- Masked Self-AttentionMedium