PigeonColumba compressa

Lesson 7 of 10

Fake quantization with group scales

To store a weight in b bits with a symmetric scheme, pick a scale s and keep the integer q = round(w / s), clipped to [-qmax, qmax] where qmax = 2^(b-1) - 1. The stored model holds q and s; using it means computing q * s.

Fake quantization returns q * s in float directly, so you can measure the accuracy of the quantized model without any integer kernels. That's what harness/quant.py does to YOLO and lab/nn.py:fake_quantize does here.

The scale is shared by a group of consecutive weights: s = max|w in group| / qmax. Smaller groups, more scales, less error.

Implement fake_quantize(w, bits, group) for bits in 2..8, where group=None means one scale for the whole tensor. Group along the flattened tensor; the last group may be shorter.

Your code

Where this is used