下载
中文
注册

create_quant_cali_config

功能说明

根据用户传入模型、量化层信息与量化配置信息,生成每个层的详细量化配置,目前主要涉及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

quant_calibration_config_pytorch.proto文件所在路径为:AMCT安装目录/amct_pytorch/proto/。

proto文件参数解释以及生成的quant.cfg简易量化配置文件样例请参见KV Cache量化简易配置文件

默认值:None

数据类型:string

返回值说明

无。

函数输出

输出一个json格式的量化配置文件(重新执行量化时,该接口输出的配置文件将会被覆盖),样例如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
    "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
        }
    }
}

调用示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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")