PythiloLesson 6 of 8 · progress kept in this browser

Practice in Lab: Train through per-channel fake quantization and saturation →

Train through per-channel fake quantization and saturation

Quantization-aware training simulates rounding during training so the model can adapt to a finite grid. Rounding has almost-everywhere zero derivative; a declared straight-through rule supplies the training surrogate. Saturation still needs a policy: this exercise passes gradients inside the representable interval and stops them outside it. Per-channel scales prevent one large output row from determining every row’s resolution. The result remains floating point and is not evidence of compressed storage, integer execution or faster inference.

Your task

Implement symmetric signed per-output-channel fake quantization of floating [out,in] weights with qmax=2**(bits-1)-1 and qmin=-qmax, bits in 2..8. Infer detached scales=max(abs(channel))/qmax with floor 1e-8, or accept explicit finite positive detached [out] scales. Round ties to even, simulate quantize/dequantize in floating point and use straight-through rounding with zero gradient outside the clipping interval. Return a floating tensor; this is QAT simulation, not a packed int8 kernel or a speed benchmark.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.