Qconfig Configuration
Definition of QConfig
qconfig (Quantization Configuration) refers to the set of key parameters for quantization, which determines the quantization method of a deep learning model. Before preparing QAT/Calibration models, the qconfig must be set for the model.
For detailed principles of qconfig, readers can refer to the section Building QConfig.
Configuring qconfig Using QconfigSetter (Recommended)
In large models or complex networks, manually setting QConfig for each layer is very tedious.
QconfigSetter, combined with templates, can automatically distribute configurations based on the computation graph.
QconfigSetter relies on the computation graph (Graph Mode) generated during the prepare stage and applies rules sequentially according to the template list.
The core structure of QconfigSetter is as follows:
Setting reference_qconfig
We recommend using get_qconfig to quickly configure reference_qconfig. An example is:
qconfig Configuration Templates (Templates)
Templates are applied in the order they are listed, with later-defined templates overriding earlier ones (or specific names overriding global settings). For more detailed explanations, refer to the Building QConfig section.
Common templates include:
-
ModuleNameTemplate(General): Setsdtypebased on operator names or prefixes.
"":dtyperepresents the global default configuration. Longer names (more specific) have higher priority. Supports setting fixed thresholds. -
ConvDtypeTemplate: Specifically used for batch configuration of theinputandweighttypes for convolution layers. -
MatmulDtypeTemplate: Specifically used for batch configuration of theinputtypes for matrix multiplication (Linear/MatMul). -
SensitivityTemplate: Automatically sets the Top-N sensitive layers to high precision based on sensitivity analysis results. -
LoadFromFileTemplate: Loads aqconfig.ptfile to reproduce previous quantization configurations. In this case,enable_optimizemust be set toFalse, otherwise the correctness of the configuration results cannot be guaranteed, and there may be CPU operators during deployment.
An example of a commonly used template configuration is:
Configuring Custom qconfig
Although qconfig templates can cover most model qconfig configuration needs, QconfigSetter also provides an interface to configure qconfig for individual operators in the model.
First, users can directly configure a qconfig. A qconfig object can set the input, weight, and output keywords, which represent the quantization configuration for the operator's input, weight, and output, respectively.
When preparing the model, these configurations determine whether to insert FakeQuantize or FakeCast nodes at the corresponding positions. None means no nodes will be inserted.
FakeQuantize is a pseudo-quantization node that performs quantization and dequantization operations on the input. Inserting pseudo-quantization simulates the quantization error during the forward pass of the floating-point model. It is mainly used for quantizing fixed-point operators, such as qint8 and qint16.
FakeCast is a pseudo-conversion node that converts the input to float32. If the data type is float16, it also simulates truncation errors caused by converting to float16. This node is mainly used to mark operators that require floating-point computation.
An example of a Qconfig definition is:
This qconfig defines the weight type as per-channel symmetric quantization, with the type qint8 and the observer as MinMaxObserver.
With the qconfig, users can use custom_qconfig_mapping to specify qconfig for individual operators in the model:
This allows users to have complete control over the qconfig of the model.
