Quantization Training Process
This document only explains the operations required for performing quantization training in HAT. For the basic principles of quantization and its implementation in the training framework, please refer to the relevant documentation of horizon_plugin_pytorch.
In quantization training, the process of converting a floating-point model to a fixed-point model is as follows:
-
Model Registration: All modules in HAT adopt a registration mechanism. Only models registered in the corresponding registry can be used in the
configfile in the form ofdict(type={$class_name}, ...). -
qconfig Configuration: Before quantizing the model, we need to configure the quantization settings (
qconfig) for the model.horizon_plugin_pytorchprovides theQconfigSetterinterface to quickly configure the model'sqconfig. For details, refer to Qconfig Configuration.
Additionally, to enable the model to be converted into a quantized model, certain conditions must be met. For details, refer to the relevant documentation of horizon_plugin_pytorch.
Adding a Custom Model
Adding a Config File
Training
Simply specify the training stages in order when using the tools/train.py script. The corresponding solver will be automatically called to execute the training process based on the specified stage:
-
float: Standard floating-point training.
-
qat: Quantization-Aware Training (QAT). First, initialize a floating-point model, load the trained weights of the floating-point model, and then convert the model to a QAT model for training. The output of the training is a pseudo-quantized QAT model.
Resuming Training
You can resume interrupted training or fine-tune by configuring the resume_optimizer and resume_epoch_or_step fields in the {stage}_trainer section of the config. For example:
There are three scenarios for resuming training:
-
Full Recovery: This scenario is used to resume training after an unexpected interruption. It restores all states from the last checkpoint, including the optimizer, learning rate (LR), epoch, step, etc. In this case, you only need to configure the
resume_optimizerfield. -
Resume Optimizer for Fine-Tuning: This scenario only restores the states of the optimizer and LR, while the epoch and step will start from 0. It is used for fine-tuning specific tasks. In this case, you need to configure
resume_optimizerand setresume_epoch_or_step=False. -
Load Model Parameters Only: This scenario only loads the model parameters without restoring any other states (optimizer, epoch, step, LR). In this case, you only need to configure
LoadCheckpointin themodel_convert_pipelineand setresume_optimizer=Falseandresume_epoch_or_step=False.
