混合量化需要在量化功能开启状态才可以使用,需要保证编译脚本中存在以下设置开启量化功能。
compile_options = nncase.CompileOptions()
#compile_options.xxx = xxx
compiler = nncase.Compiler(compile_options)
ptq_options = nncase.PTQTensorOptions()
#ptq_options.xxx = xxx
...
compiler.use_ptq(ptq_options)ptq_options.quant_scheme = ""
ptq_options.quant_scheme_strict_mode = False
ptq_options.export_quant_scheme = False
ptq_options.export_weight_range_by_channel = False- quant_scheme:导入量化参数配置文件的路径
- quant_scheme_strict_mode:是否严格按照quant_scheme执行量化
- export_quant_scheme:是否导出量化参数配置文件
- export_weight_range_by_channel:是否导出
bychannel形式的weights量化参数,为了保证量化效果,该参数建议设置为True
由于量化参数配置文件输入编译中间信息,需要用户打开 dump_ir功能。
compile_options.dump_ir = Trueptq_options.quant_scheme = ""
ptq_options.quant_scheme_strict_mode = False
ptq_options.export_quant_scheme = True
ptq_options.export_weight_range_by_channel = True生成的量化参数配置文件与 kmodel同目录
量化参数配置文件内格式如下:
{
"Version": "1.0",
"Model": null,
"Outputs": [
{
"Name": "input",
"DataType": "u8",
"DataRange": [
{
"Min": 0.0,
"Max": 0.99999493,
"IsFull": false
}
],
"DataRangeMode": "by_tensor"
},
{
"Name": "Const_90",
"DataType": "i16",
"DataRange": [
{
"Min": -0.82569844,
"Max": 1.2145407,
"IsFull": false
},
{
"Min": -3.459431,
"Max": 2.9377983,
"IsFull": false
},
//...
],
"DataRangeMode": "by_channel"
},
{
"Name": "MobilenetV2/Conv/Relu6",
"DataType": "f16",
"DataRange": [
{
"Min": 0.0,
"Max": 6.0,
"IsFull": false
}
],
"DataRangeMode": "by_tensor"
},
// ...
]
}可以自行修改每层的 Min ,Max,DataType来调整量化的效果,其中 DataType 支持设置为 u8 ,i8, i16, f16, f32
需要在编译脚本中设置量化参数配置文件
ptq_options.quant_scheme = "./QuantScheme.json" # path to your 'QuantScheme.json'
ptq_options.quant_scheme_strict_mode = False # Whether to strictly follow quant_scheme for quantification
ptq_options.export_quant_scheme = False
ptq_options.export_weight_range_by_channel = False # whatever在导入量化参数配置文件后不需要额外设置校正集
ptq_options.set_tensor_data([])