昇腾社区首页
中文
注册

create_quant_retrain_config

产品支持情况

产品

是否支持

Atlas A2 训练系列产品/Atlas 800I A2 推理产品

  • INT8量化:√
  • INT4量化:x

Atlas 200I/500 A2 推理产品

  • INT8量化:√
  • INT4量化:x

Atlas 推理系列产品

  • INT8量化:√
  • INT4量化:√

Atlas 训练系列产品

  • INT8量化:√
  • INT4量化:x

注:上述INT4量化,标记“x”的产品,调用接口不会报错,但是获取不到性能收益。

功能说明

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

函数原型

1
create_quant_retrain_config(config_file, model, input_data, config_defination=None)

参数说明

参数名

输入/输出

说明

config_file

输入

含义:待生成的量化感知训练配置文件存放路径及名称。如果存放路径下已经存在该文件,则调用该接口时会覆盖已有文件。

数据类型:string

model

输入

含义:待进行量化感知训练的模型,已加载权重。

数据类型:torch.nn.module

input_data

输入

含义:模型的输入数据。一个torch.tensor会被等价为tuple(torch.tensor)。

数据类型:tuple

config_defination

输入

含义:简易配置文件。

基于retrain_config_pytorch.proto文件生成的简易配置文件quant.cfg,*.proto文件所在路径为:AMCT安装目录/amct_pytorch/proto/。*.proto文件参数解释以及生成的quant.cfg简易量化配置文件样例请参见量化感知训练简易配置文件

默认值:None。

数据类型:string

返回值说明

调用示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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_retrain_config(config_file="./configs/config.json",
                            model=model,
                            input_data=input_data)

落盘文件说明:生成json格式的量化感知训练配置文件,样例如下(重新执行量化感知训练时,该接口输出的配置文件将会被覆盖)。

  • INT8量化配置文件
     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
    {
        "version":1,
        "batch_num":1,
        "conv1":{
            "retrain_enable":true,
            "retrain_data_config":{
                "algo":"ulq_quantize",
                "dst_type":"INT8"
            },
            "retrain_weight_config":{
                "algo":"arq_retrain",
                "channel_wise":true,
                "dst_type":"INT8"
            }
        },
        "layer1.0.conv1":{
            "retrain_enable":true,
            "retrain_data_config":{
                "algo":"ulq_quantize",
                "dst_type":"INT8"
            },
            "retrain_weight_config":{
                "algo":"arq_retrain",
                "channel_wise":true,
                "dst_type":"INT8"
            }
        },
        "fc":{
            "retrain_enable":true,
            "retrain_data_config":{
                "algo":"ulq_quantize",
                "dst_type":"INT8"
            },
            "retrain_weight_config":{
                "algo":"arq_retrain",
                "channel_wise":false,
                "dst_type":"INT8"
            }
        }
    ...
    }
    
  • INT4量化配置文件
     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
    {
        "version":1,
        "batch_num":2,
        "conv1":{
            "retrain_enable":true,
            "retrain_data_config":{
                "algo":"ulq_quantize",
                "dst_type":"INT8"
            },
            "retrain_weight_config":{
                "algo":"arq_retrain",
                "channel_wise":true,
                "dst_type":"INT8"
            }
        },
        "layer1.0.conv1":{
            "retrain_enable":true,
            "retrain_data_config":{
                "algo":"ulq_quantize",
                "dst_type":"INT4"
            },
            "retrain_weight_config":{
                "algo":"arq_retrain",
                "channel_wise":true,
                "dst_type":"INT4"
            }
        },
        "fc":{
            "retrain_enable":true,
            "retrain_data_config":{
                "algo":"ulq_quantize",
                "dst_type":"INT4"
            },
            "retrain_weight_config":{
                "algo":"arq_retrain",
                "channel_wise":true,
                "dst_type":"INT4"
            }
        }
    }