---
title: quantize_model_ascend
description: "| 产品 | 是否支持 |"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/devaids/amct/atlasamct_16_0330.html
sourcePath: /source/zh/canncommercial/900/devaids/amct/atlasamct_16_0330.html
indexId: 2cf2a7f0b5dff0c975c9bc240d6a491a07f90a9915a6d32e20277b34c3d55b7765
---
# quantize_model_ascend

#### 产品支持情况

| 产品 | 是否支持 |
| --- | --- |
| Atlas 350 加速卡 | x |
| Atlas A3 训练系列产品 / Atlas A3 推理系列产品 | x |
| Atlas A2 训练系列产品 / Atlas A2 推理系列产品 | x |
| Atlas 200I/500 A2 推理产品 | x |
| Atlas 推理系列产品 | x |
| Atlas 训练系列产品 | √ |


#### 功能说明

训练后量化接口，将输入的待量化的图结构按照给定的量化配置文件进行量化处理，在传入的图结构中插入量化相关的算子，生成量化因子记录文件record_file，并返回量化处理新增的算子列表。


#### 函数原型

```
calibration_graph, calibration_outputs=quantize_model_ascend(graph, outputs, config_file, record_file)
```


#### 参数说明

| 参数名 | 输入/输出 | 说明 |
| --- | --- | --- |
| graph | 输入 | 含义：用户传入的待量化模型的tf.Graph图。 数据类型：tf.Graph 使用约束：graph必须为推理图，图中不能包含训练模式的算子，例如FusedBatchNormV3算子的is\_training必须为False。图中必须加载了训练好的权重。 |
| outputs | 输入 | 含义：graph中输出算子的列表。 数据类型：list，列表中元素类型为string |
| config\_file | 输入 | 含义：用户生成的量化配置文件，用于指定模型tf.Graph图中量化层的配置情况。 数据类型：string |
| record\_file | 输入 | 含义：量化因子记录文件路径及名称。 数据类型：string |


#### 返回值说明

- calibration_graph：工具修改后的图，插入了量化算子，类型为tf.Graph。
- calibration_outputs：calibration_graph中量化的层名信息列表，类型为list，列表中元素类型为string。
  quantize_model_ascend对图做了BN融合，如果网络模型的outputs包含BN层，且该BN层也做融合，那么网络的输出节点会发生变化。例如，Conv+BN（或Conv+BiasAdd+BN）融合后为Conv+BiasAdd，BN等价的输出节点为BiasAdd节点。


#### 调用示例

```
import amct_tensorflow as amct
# 建立待量化的网络结构
network = build_network()

# 插入量化API
calibration_graph, calibration_outputs = amct.quantize_model_ascend(
      graph=tf.get_default_graph(),
      config_file="./configs/config.json",
      record_file="./record_scale_offset.txt")
```
