Automatic Quantization
AMCT (ACL) quantization is classified into automatic quantization and manual quantization.
- Automatic quantization (recommended): The aclgrphCalibration API is used to automatically insert quantization operators. During quantization, operator fusion is performed on some structures in the model.
- Manual quantization: You need to manually modify the model and insert quantization operators. For details, see Manual Quantization.
Function Description
You can call aclgrphCalibration to automatically quantize a non-quantized graph. During quantization, operator fusion is performed on some structures in the model. After the fusion, the graph structure is optimized, thereby improving the network inference performance. For details about the supported fusion functions, see Supported Fusion Features. For details about the layers that support quantization and their constraints, see Quantifiable Layers and Constraints.
To use this feature, you need to install the AMCT (ACL) software package separately. For details about how to install the package, see AMCT (ACL).
Calling Example
This section uses the TensorFlow network model quantization as an example.
- Add the header files.
1 2 3
#include "amct/acl_graph_calibration.h" #include "amct/acl_calibration_configs.h" #include "amct/amct_error_code.h"
- Create a graph object manually or by parsing an existing graph.
- Define the parameters required by the aclgrphCalibration API.
1 2 3 4
std::map<ge::AscendString, ge::AscendString> quantizeConfigs = {}; quantizeConfigs[ge::AscendString(amct::aclCaliConfigs::INPUT_DATA_DIR)] = ge::AscendString("./path/cali_data.bin"); quantizeConfigs[ge::AscendString(amct::aclCaliConfigs::INPUT_SHAPE)] = ge::AscendString("input:16,224,224,3"); quantizeConfigs[ge::AscendString(amct::aclCaliConfigs::SOC_VERSION)] = ge::AscendString("SOC_VERSION"); // Set SOC_VERSION to the actual AI processor model.
If you need to control the quantization process, you can use the CONFIG_FILE parameter to pass in a simplified configuration file for quantization control. For the example of a configuration file, see Simplified Quantization Configuration File.
The following is a configuration example of the CONFIG_FILE parameter:
1quantizeConfigs[ge::AscendString(amct::aclCaliConfigs::CONFIG_FILE)]=ge::AscendString("./calibration.cfg");
- Perform graph modification for quantization.
1 2 3 4 5
ret = ge::aclgrphCalibration(graph, quantizeConfigs); if (ret != ge::GRAPH_SUCCESS) { return FAILED; } return SUCCESS;
When compiling the script of the preceding program, you need to link libamctacl.so and add the search path of the dynamic library ${ASCEND_PATH}/compiler/lib64. ASCEND_PATH indicates the installation path of the CANN software package. The default installation path for the user root, /usr/local/Ascend/cann, is used as an example.