INT4 has only 16 representable levels (-7..7 signed, using symmetric quantization), so a single scale per row ([02-per-channel-weight-quantization]'s approach) is often too coarse: any local variation in magnitude within a row gets quantized very lossily. Implement group-wise ("group quantization") INT4 weight quantization: split each row of a weight matrix into fixed-size contiguous groups along the input dimension, and quantize each group with its own independent scale — the scheme used by popular INT4 weight-only quantization methods (GPTQ, AWQ) in production LLM serving.
scale[o, g] = max(|W[o, g*group_size:(g+1)*group_size]|) / 7
Q[o, i] = clip(round(W[o, i] / scale[o, i // group_size]), -7, 7)
in_features must be divisible by group_size.group_size input features (per row) gets its own scale, computed the same way as symmetric quantization restricted to that group.[-7, 7].(out_features, n_groups) scale matrix, and the dequantized reconstruction.Reshape each row's in_features values into (n_groups, group_size) to compute all group scales with one np.max(np.abs(...), axis=-1) call instead of a nested Python loop. Assume in_features already divides evenly by group_size — no ragged final group in this simplified scheme.
Click "Run Tests" to test your implementation