Quantization-Aware Training QAT
Quantization-aware training (QAT) is a family of methods that simulate quantization error during the training stage so that model weights actively adapt to low-bit representations. Compared with post-training quantization (PTQ), it can substantially recover the accuracy lost at INT8 and even lower bit widths; it is an important compromise between accuracy and cost in quantization deployment, and one of the standard practices for on-device deployment.
Core principles
PTQ clips and scales weights and activations directly after training is complete. The model has no chance to adapt to this error, so at low bit widths the cumulative effect becomes obvious and some sensitive layers lose accuracy heavily. QAT instead inserts fake quantization nodes into the network: in the forward pass these nodes scale, round and then rescale floating-point tensors according to the quantization formula, simulating the values produced by real quantization; at inference time the nodes are removed and replaced with integer operators.
The difficulty lies in backpropagation. Rounding is a step function whose derivative is almost everywhere zero, so the gradient stops there and parameters cannot be updated. The solution is the straight-through estimator (STE): in the backward pass the gradient of the quantization node is approximated as an identity mapping, that is, the gradient passes straight through, pretending the derivative of rounding is 1. This treatment is not mathematically rigorous, but it is extremely effective in engineering. For the problem that rounding error is underestimated, several improved STE variants distribute part of the gradient to neighbouring quantization grid points according to distance.
Key points
The pipeline is usually: load a pretrained floating-point model, insert fake quantization nodes and observers, then fine-tune on a small amount of data with a learning rate far below the original training and for few epochs. In practice not all parameters need updating; a common approach is to freeze most weights and train only scale and zero point, or fine-tune all parameters with a small learning rate. Activation quantization requires calibration data to determine the value range, and the closer the calibration data is to the real inference distribution the better; tail data from the training set is often more stable than random sampling.
On the engineering side, PyTorch provides FX Graph Mode Quantization, which performs graph-level rewriting through torch.ao.quantization, automatically inserts observers and fake quantization modules, supports static and dynamic quantization, and can export to a backend. Compared with the older Eager mode, FX mode can trace the whole computation graph and is better suited to models with complex structure. Watch the quantization support range of operators: unsupported operators fall back to floating point, forming a mixed-precision graph that in turn affects the speedup ratio.
The decision criterion is clear: only turn to QAT when the accuracy drop under PTQ exceeds an acceptable threshold (for example one to two points). The price is a marked increase in training cost, pipeline complexity and tuning expertise, plus the need to rerun full validation.
Common misconceptions
First, believing that QAT can reduce bit width without limit; below INT4, more complex schemes such as group quantization and mixed precision are usually required. Second, insufficient calibration data or distribution shift leads to a wrong estimate of the activation range, and the result can be worse than PTQ. Third, treating QAT as a universal patch, when in fact it works mainly in scenarios with small degradation.
Application scenarios
On-device inference, mobile and embedded deployment, lightweight serving of large models, vision model compression, real-time speech recognition, edge device inference, memory-constrained scenarios, automotive head units and surveillance devices.
In summary, PTQ is cheap but fragile and QAT is expensive but robust; the most reasonable order in a real project is to run PTQ first to get a baseline, find the layers most sensitive to the drop, and then decide whether QAT is worth the time and compute. The real conclusion for most teams is: at INT8, PTQ plus per-channel quantization is already sufficient, and QAT is genuinely needed only when pursuing extreme size or running on low-end hardware.