文档
注册

create_quant_config

功能说明

根据图的结构找到所有可量化的层,自动生成量化配置文件,并将可量化层的量化配置信息写入文件。

函数原型

create_quant_config(config_file, model_file, skip_layers=None, batch_num=1, activation_offset=True, config_defination=None, updated_model=None)

参数说明

参数名

输入/返回值

含义

使用限制

config_file

输入

待生成的量化配置文件存放路径及名称。

如果存放路径下已经存在该文件,则调用该接口时会覆盖已有文件。

数据类型:string

model_file

输入

待量化的ONNX模型文件。

该模型必须是基于ONNX opset11生成的模型,并且能够在ONNX Runtime 1.5.2上完成推理。

数据类型:string

skip_layers

输入

可量化但不需要量化的层的层名。

默认值:None

数据类型:list,列表中元素类型为string

使用约束:如果使用简易配置文件作为入参,则该参数需要在简易配置文件中设置,此时输入参数中该参数配置不生效。

batch_num

输入

量化使用的batch数量,即使用多少个batch的数据生成量化因子。

数据类型:int

取值范围:大于0的整数

默认值:1

使用约束:

  • batch_num不宜过大,batch_num与batch_size的乘积为量化过程中使用的图片数量,过多的图片会占用较大的内存。
  • 如果使用简易配置文件作为入参,则该参数需要在简易配置文件中设置,此时输入参数中该参数配置不生效。

activation_offset

输入

数据量化是否带offset。

默认值:True

数据类型:bool

使用约束:如果使用简易配置文件作为入参,则该参数需要在简易配置文件中设置,此时输入参数中该参数配置不生效。

config_defination

输入

基于calibration_config_onnx.proto文件生成的简易量化配置文件quant.cfg,calibration_config_onnx.proto文件所在路径为:AMCT安装目录/amct_onnx/proto/calibration_config_onnx.proto。

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

默认值:None

数据类型:string

使用约束:当取值为None时,使用输入参数生成配置文件;否则,忽略输入的其他量化参数(skip_layers,batch_num,activation_offset),根据简易量化配置文件参数config_defination生成json格式的配置文件。

updated_model

输入

如果配置,会去更新模型里节点name,没有name的会按{op_type}_{index}格式生成唯一的name,对于已有的name重复的节点会通过增加数字后缀保证name唯一,然后按配置的文件路径保存更新后的ONNX模型。

默认值: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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{
    "version":1,
    "batch_num":2,
    "activation_offset":true,
    "joint_quant":false,
    "do_fusion":true,
    "skip_fusion_layers":[],
    "tensor_quantize":[
      {
         "layer_name": "maxpool_ld_default",
         "input_index":0,
         "activation_quant_params":{
             "num_bits":8,
             "max_percentile":0.999999,
             "min_percentile":0.999999,
             "search_range":[
                 0.7,
                1.3
             ],
             "search_step":0.01,
             "act_algo":"ifmr",
             "asymmetric":false
          }
       }
    },
    "layer_name1":{
        "quant_enable":true,
        "dmq_balancer_param":0.5,
        "activation_quant_params":{
            "num_bits":8,
            "max_percentile":0.999999,
            "min_percentile":0.999999,
            "search_range":[
                0.7,
                1.3
            ],
            "search_step":0.01,
            "act_algo":"ifmr",
            "asymmetric":false
        },
        "weight_quant_params":{
            "num_bits":8,
            "wts_algo":"arq_quantize",
            "channel_wise":true
        }
    },
    "layer_name2":{
        "quant_enable":true,
        "dmq_balancer_param":0.5,
        "activation_quant_params":{
            "num_bits":8,
            "max_percentile":0.999999,
            "min_percentile":0.999999,
            "search_range":[
                0.7,
                1.3
            ],
            "search_step":0.01,
            "act_algo":"ifmr",
            "asymmetric":false
        },
        "weight_quant_params":{
            "num_bits":8,
            "wts_algo":"arq_quantize",
            "channel_wise":false
        }
    }
}

调用示例

1
2
3
4
5
6
7
8
9
import amct_onnx as amct

model_file = "resnet101.onnx"
# 生成量化配置文件
amct.create_quant_config(config_file="./configs/config.json",
                         model_file=model_file
                         skip_layers=None,
                         batch_num=1,
                         activation_offset=True)
搜索结果
找到“0”个结果

当前产品无相关内容

未找到相关内容,请尝试其他搜索词