Quantized Awareness Training
After completing Calibration, if the current quantization results still cannot meet the accuracy requirements, the next step is to perform Quantization Aware Training (QAT).
Compared with pure floating-point model training, QAT usually makes the training process more challenging because of the additional quantization constraints introduced during training. The goal of the QAT toolkit is to reduce training difficulty while also lowering the engineering effort required for deploying quantized models.
The objective of this stage is to further optimize quantization error based on the existing quantization parameter initialization, thereby providing a higher-accuracy QAT model for subsequent model export, fixed-point conversion, and deployment validation.
BN Strategy and Training Mode
Before starting QAT, it is recommended to determine the BN handling strategy and the current training mode first. These settings directly affect the training behavior and also affect the accuracy and convergence patterns you will observe later.
FuseBN
FuseBN means that after Prepare and before QAT training, the BN weight and bias are absorbed into the Conv weight and bias, and BN is no longer updated independently during training. This absorption process itself is lossless, and it is also the default QAT training method.
Suitable when:
-
The model itself is suitable for BN fusion.
-
You want the training path and the final deployment path to stay as aligned as possible.
WithBN
WithBN means that Conv + BN stays unfused during QAT training, BN is trained together with the rest of the model, and fusion is performed only when the deployment model is generated after training.
Suitable when:
-
The floating-point training stage already uses
FreezeBNor a similar strategy. -
You want the QAT stage to align more closely with the floating-point training method.
Related Interface
qat_mode is used to control the current quantization training mode. If you need to switch BN strategy or training mode, it is recommended to decide based on the current model structure, floating-point training strategy, and the later deployment target, instead of applying a training habit directly.
The related interfaces are defined in horizon_plugin_pytorch.qat_mode. According to qat_mode.py, three modes are currently supported, and the default value is QATMode.FuseBN.
The most commonly used interfaces here are:
-
QATMode: used to specify the concrete quantization training mode. The available values areFuseBN,WithBN, andWithBNReverseFold. -
set_qat_mode: used to set the current quantization training mode, and it is usually recommended to call it beforeprepare. -
get_qat_mode: used to check which quantization training mode is currently active.
In practice, the usual choice is:
-
If there is no special requirement, start from the default
FuseBN. -
If float training already uses
FreezeBNor a similar strategy and you want to preserve the BN behavior during QAT, tryWithBNfirst. -
If you clearly plan to absorb BN gradually during QAT and want a smaller disturbance before and after absorption, then consider
WithBNReverseFold.
For example, if the floating-point training stage uses FreezeBN or a similar strategy and you want BN to remain involved during QAT, you can configure it as follows:
If you still need to decide which qat_mode to use, or want to continue tuning it together with learning rate, scale update strategy, and high-precision configuration, continue with the Precision Tuning Guide, especially Common Mixed Precision Tuning Workflow, Basic Tuning Methods, and Advanced Tuning Methods.
Quantization Parameter Update Strategy
Before training starts, whether the quantization parameters are fixed or continuously updated will also directly affect the later convergence behavior.
When Calibration accuracy is already good, fixing the feature map quantization parameters for QAT training can usually produce better results. When Calibration accuracy is poor, it is usually not recommended to directly fix the quantization parameters obtained from Calibration. In that case, it is better to go back and first resolve the quantization bottleneck in the Calibration stage.
If the quantization parameters from Calibration still do not meet the accuracy target and you want them to keep updating during QAT, the tool provides the following update methods.
Statistics-based Method
For performance reasons, it is recommended to use MinMaxObserver during the QAT stage to continuously collect statistics and update the quantization parameters. For details, refer to QConfig Details.
Learning-based Method (Not Recommended)
The tool also supports the LSQ algorithm (see Learned Step Size Quantization). The corresponding interface is _LearnableFakeQuantize.
For details, refer to Definition of FakeQuantize.
Workflow and Example
Although the Quantized Awareness Training tool does not strictly require you to provide a pretrained floating-point model at the beginning, in practice it is still recommended to start from a pretrained high-accuracy floating-point model, because this usually makes the training process much easier.
The training loop above is only the basic form. In real usage, QAT is usually not finished in a single round. Instead, you typically need to use staged training results and validation results to judge whether the current configuration is appropriate, and whether you still need to continue training or adjust the strategy.
Therefore, besides the training itself, two things should be done continuously:
-
Judge from the current results whether the learning rate, BN strategy, quantization parameter update method, or high-precision configuration still needs adjustment.
-
Do not only look at the QAT model accuracy. Continue validating the quantized model accuracy as well, so that you can confirm whether the current training result is truly beneficial for later deployment.
Due to the underlying limitations of the deployment platform, the accuracy of the QAT model cannot fully represent the final on-board accuracy. During QAT training, you must not focus only on the QAT model accuracy. You should also keep tracking the quantized model accuracy or other fixed-point-oriented validation results. Otherwise, accuracy drop may only become visible in later export, fixed-point conversion, or board deployment stages.
From the example above, compared with conventional floating-point training, QAT mainly adds two key steps:
-
prepare: transform the floating-point network and insert fake quantization nodes. -
Load the parameters from the
Calibrationmodel: by loading the pseudo-quantization parameters obtained duringCalibration, the model gets a better initialization.
For state_dict processing, you should not only pay attention to keys and values; _metadata also needs to be preserved. The following is an example of copying a state_dict:
Operator compatibility depends on the _version variable of torch.nn.Module, and _version is stored in state_dict._metadata. Make sure _metadata is preserved when saving or loading a state_dict; otherwise compatibility issues may occur.
At this point, the fake-quantized model construction and parameter initialization are finished. You can then continue with normal training iterations and parameter updates while continuously monitoring the quantized model accuracy.
To support segmented deployment or align with the floating-point training strategy, you may also need to freeze some parts of the model during training. The following is a common way to do that:
What to Monitor During Training
During QAT training, it is recommended to monitor at least the following metrics together.
QAT model accuracy
QAT model accuracy is mainly used to observe the overall convergence trend during training. It helps you judge whether the training is still moving in a better direction, but it should not be used alone to judge the final deployment accuracy.
quantized model accuracy
quantized model accuracy is closer to the actual result of the later deployment path, and is used to judge whether the current training result is truly helpful for later export, fixed-point conversion, and deployment.
If you only look at the QAT model accuracy and ignore the quantized model accuracy, it becomes easier to run into the situation where “training looks fine, but deployment accuracy drops” in later export or board verification stages.
Whether the loss is close to the final float training loss
QAT is usually a finetuning process on top of an already converged floating-point model, so whether the loss stays close to the final float training loss can help you judge whether quantization training has entered a normal convergence state.
-
If the loss is too large, it often means the model is hard to converge.
-
If the loss is too small, it may also introduce generalization issues.
Whether NAN / INF / long-term non-convergence appears
These phenomena usually indicate issues in the training pipeline itself, scale updates, quantization configuration, or initialization. They should be investigated as early as possible instead of waiting until the end of training.
