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

#### 产品支持情况

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


注：标记“x”的产品，调用接口不会报错，但是获取不到性能收益。


#### 功能说明

KV-cache量化接口，根据用户传入模型、量化层信息与量化配置信息，生成每个层的详细量化配置。


#### 函数原型

```
create_quant_cali_config(config_file,model,quant_layers=None,config_defination=None)
```


#### 参数说明

| 参数名 | 输入/输出 | 说明 |
| --- | --- | --- |
| config\_file | 输入 | 含义：待生成的量化配置文件存放路径及名称，文件为JSON格式，包含每个KV Cache量化层的量化配置信息。如果存放路径下已经存在该文件，则调用该接口时会覆盖已有文件。 数据类型：string |
| model | 输入 | 含义：用户提供的待量化模型。 数据类型：torch.nn.Module |
| quant\_layers | 输入 | 含义：量化层信息，通过字典表示；如果传入了量化简易配置文件，则以配置文件为准。 KV\-cache量化示例如下： {'kv\_cache\_quant\_layers': ['MatMul\_1']} 默认值：None 数据类型：dict 使用约束：quant\_layers既可以在参数中指定，也可以在简易配置文件添加：当取值为None时，以参数传递为准；否则以简易配置文件为准。 |
| config\_defination | 输入 | 含义：量化简易配置文件。 基于quant\_calibration\_config\_pytorch.proto生成的简易配置文件quant.cfg，\*.proto文件所在路径为：AMCT安装目录/amct\_pytorch/proto/。\*.proto文件参数解释以及生成的quant.cfg简易量化配置文件样例请参见KV Cache量化简易配置文件。 默认值：None 数据类型：string |


#### 返回值说明

无


#### 调用示例

```
import amct_pytorch as amct
# 建立待量化的网络图结构
model = build_model()
model.load_state_dict(torch.load(state_dict_path))
input_data = tuple([torch.randn(input_shape)])

# 生成量化配置文件
amct.create_quant_cali_config(config_file="./configs/config.json",
                             model=model,
                             quant_layers=None,
                             config_defination="./configs/quant.cfg")
```

落盘文件说明：

生成JSON格式的量化配置文件（重新执行量化时，该接口输出的配置文件将会被覆盖），样例如下：

```
{
    "batch_num":1,
    "activation_offset":true,
    "matmul1":{
        "kv_data_quant_config":{
            "act_algo":"hfmg",
            "num_of_bins":4096,
            "quant_granularity":0
        }
    },
    "matmul2":{
        "kv_data_quant_config":{
            "act_algo":"hfmg",
            "num_of_bins":4096,
            "quant_granularity":0
        }
    },
    "matmul3":{
        "kv_data_quant_config":{
            "act_algo":"ifmr",
            "max_percentile":0.999999,
            "min_percentile":0.999999,
            "search_range":[
                0.7,
                1.3
            ],
            "search_step":0.01,
            "quant_granularity":0
        }
    }
}
```
