Quantized Training Process Introduction
This document only describes the operations needed to perform quantization training in HAT. For the basic principles of quantization and its implementation in the training framework, refer to the documentation of horizon_plugin_pytorch .
In quantization training, the conversion process from a floating-point model to a fixed-point model is as follows:
Most of these steps are already integrated in the HAT training pipeline, and you only needs to pay attention to implementing the fuse_model method to complete the model fusion when adding a custom model and the set_qconfig method to configure the quantization method. The following points need to be noted when writing the models.
-
HAT will only call the
fuse_modelmethod of the outermost module, so the implementation offuse_modelis responsible for the fuse of all submodules. -
Preference should be given to the base modules provided in
hat.models.base_modules, which has already implemented thefuse_modelmethod to reduce the effort and development difficulties. -
Model registration, all the modules in HAT use the registration mechanism, only when the defined model is registered in the corresponding registration item, can the model be used in the config file as
dict(type={$class_name}, ...). -
The
set_qconfigmethod needs to be implemented in the outermost module. If there is a special layer in a submodule that needs a separate QConfig setting, theset_qconfigmethod needs to be implemented in that submodule as well, details of which can be found in the Writing Specifications of set_qconfig and Customization of qconfig sections.
In addition, to make the model transferable to a quantized model, some conditions need to be met, as described in the documentation for horizon_plugin_pytorch.
Add Custom Models
Add the Config File
Training
You only need to simply specify the training phases in order when using the tools/train.py script, and the corresponding solver will be called automatically according to the training phase to execute the training process.
-
float: normal floating-point training.
-
qat: QAT training (Quantized Awareness Training), this stage first initializes a floating-point model, loads the trained floating-point model weights, and then converts this floating-point model into a QAT model for training.
-
int_infer: Fixed-point transformation prediction, this stage first initializes a floating-point model, converts the floating-point model into a QAT model and loads the trained QAT model weights, and then converts the QAT model into a fixed-point model. The converted fixed-point model cannot be trained and can only perform validation to obtain the final fixed-point model accuracy.
Resume Training
Unexpectedly interrupted training can be resumed by configuring the resume_optimizer and resume_epoch_or_step fields in {stage}_trainer of config, or by resuming only the optimizer for fine-tuning. For example:
Training recovery has three scenarios:
-
Full Recovery: This scenario is to resume the training that was unexpectedly interrupted, and will restore all the states of the previous checkpoint, including optimizer, LR, epoch, step, and so on. In this scenario, you only need to configure the
resume_optimizerfield. -
Resume Optimizer for Fine-tune: This scenario will only restore the state of optimizer and LR, with epoch and step reset to 0 for the fine-tuning of certain tasks. In this scenario, you need to configure both
resume_optimizerandresume_epoch_or_step=False. -
Load Model Parameters Only: This scenario loads only model parameters and does not restore any other state (optimizeizer, epoch, step, or LR). In this scenario, you need to configure
LoadCheckpointinmodel_convert_pipeline,resume_optimizer=False, andresume_epoch_or_step=False.
