---
title: 自动量化
description: "本节介绍如何通过aclgrphCalibration接口对Graph进行自动量化。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/programug/graphdevg/atlasag_25_0042.html
sourcePath: /source/zh/canncommercial/900/programug/graphdevg/atlasag_25_0042.html
indexId: 4ec8b5c6f2dfd392c29d5a2d2e455f7bd69d8575f42e5ff53efac15547b502e070
---
# 自动量化

本节介绍如何通过aclgrphCalibration接口对Graph进行自动量化。

#### 功能介绍

通过调用aclgrphCalibration(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/ascendgraphapi/atlasgeapi_07_0122.html)接口对非量化的Graph进行自动量化。量化过程中会对模型中的某些结构做算子融合，融合后Graph结构得到优化，从而提升网络推理性能。具体支持的融合功能请参见支持的融合功能。支持量化的层及约束请参考支持量化的层及约束。

使用该特性时，需要单独安装AMCT（acl）软件包，该包的获取以及安装方法请参见《AMCT模型压缩工具(https://www.hiascend.com/document/detail/zh/canncommercial/900/devaids/amct/atlasamct_16_0001.html)》中的“准备环境>获取软件包、上传软件包、安装工具”章节。


#### 调用示例

本章节以TensorFlow框架网络模型量化为例进行说明。

1. 添加头文件。
  1 2 3 #include "amct/acl_graph_calibration.h" #include "amct/acl_calibration_configs.h" #include "amct/amct_error_code.h"

2. 通过解析或手动构造方式创建Graph对象。
  1 2 3 4 5 #include "parser/tensorflow_parser.h" ge::Graph graph("origin"); std::map<ge::AscendString, ge::AscendString> parseOptions; auto ret = ge::aclgrphParseTensorFlow("./path/tf_test.pb", parseOptions, graph);

3. 定义aclgrphCalibration接口所需配置参数。
  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"); // SOC_VERSION请配置为实际的AI处理器型号

  如果需要自行控制量化过程，可以使用CONFIG_FILE参数项传入控制量化过程的简易配置文件。配置文件的示例请参考量化简易配置文件。

  CONFIG_FILE参数的配置示例如下：

  1 quantizeConfigs[ge::AscendString(amct::aclCaliConfigs::CONFIG_FILE)]=ge::AscendString("./calibration.cfg");

4. 调用接口执行量化改图。
  1 2 3 4 5 ret = ge::aclgrphCalibration(graph, quantizeConfigs); if (ret != ge::GRAPH_SUCCESS) { return FAILED; } return SUCCESS;


编译上述程序的脚本时需要链接libamctacl.so，并添加链接时动态库的搜索路径${ASCEND_PATH}/compiler/lib64，其中ASCEND_PATH为CANN软件包安装路径，以root用户默认安装路径为例，/usr/local/Ascend/cann。
