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 as weight, containing the quantized valuesscale_factors: 1D float array of shape (out_channels,) with one scale factor per channeldequantized_weights: Float array of same shape as weight, obtained by converting the quantized integers back to floating point using the scale factorsRequirements:
[-(2^(bits-1)), 2^(bits-1) - 1]2^(bits-1) - 11.0 to avoid division by zero