quantize_preprocess

功能说明

量化数据均衡预处理接口,将输入的待量化的图结构按照给定的量化配置文件进行量化处理,在传入的图结构中插入均衡量化相关的算子,生成均衡量化因子记录文件record_file,返回修改后的torch.nn.module校准模型。

函数原型

calibration_model = quantize_preprocess(config_file, record_file, model, input_data)

参数说明

参数名

输入/返回值

含义

使用限制

config_file

输入

用户生成的量化配置文件,用于指定模型network中量化层的配置情况。

数据类型:string

record_file

输入

均衡量化因子记录文件路径及名称。

数据类型:string

model

输入

待量化的模型,已加载权重。

数据类型:torch.nn.module

input_data

输入

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

数据类型:tuple

calibration_model

返回值

修改后的torch.nn.module校准模型。

默认值:None

数据类型:torch.nn.module

返回值说明

返回修改后的torch.nn.module校准模型。

函数输出

无。

调用示例

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

tensor_balance_factor_record_file = os.path.join(TMP, 'tensor_balance_factor_record.txt')
modified_model = os.path.join(TMP, 'modified_model.onnx')
# 插入量化API
calibration_model = amct.quantize_preprocess(config_json_file,
                                             tensor_balance_factor_record_file,
                                             model,
                                             input_data)