根据用户输入的模型、配置文件进行自动的校准过程,搜索得到一个满足目标精度的量化配置,输出可以在ONNX Runtime环境下做精度仿真的fake_quant模型,和可在昇腾AI处理器上做推理的deploy模型。
无。
accuracy_based_auto_calibration(model_file,model_evaluator,config_file,record_file,save_dir,strategy='BinarySearch',sensitivity='CosineSimilarity')
参数名 |
输入/返回值 |
含义 |
使用限制 |
---|---|---|---|
model_file |
输入 |
用户onnx模型文件,格式为.onnx。 |
数据类型:string |
model_evaluator |
输入 |
自动量化进行校准和评估精度的python实例。 |
数据类型:python实例 |
config_file |
输入 |
用户生成的量化配置文件。 |
数据类型:string |
record_file |
输入 |
存储量化因子的路径,如果该路径下已存在文件,则会被重写。 |
数据类型:string |
save_dir |
输入 |
模型存放路径。 该路径需要包含模型名前缀,例如./quantized_model/*model。 |
数据类型:string |
strategy |
输入 |
搜索满足精度要求的量化配置的策略,默认是二分法策略。 |
数据类型:string或python实例 默认值:BinarySearch |
sensitivity |
输入 |
评价每一层量化层对于量化敏感度的指标,默认是余弦相似度。 |
数据类型:string或python实例 默认值:CosineSimilarity |
无。
import amct_onnx as amct from amct_onnx.common.auto_calibration import AutoCalibrationEvaluatorBase # You need to implement the AutoCalibrationEvaluator's calibration(), evaluate() and metric_eval() funcs class AutoCalibrationEvaluator(AutoCalibrationEvaluatorBase): """ subclass of AutoCalibrationEvaluatorBase""" def __init__(self, target_loss, batch_num): super(AutoCalibrationEvaluator, self).__init__() self.target_loss = target_loss self.batch_num = batch_num def calibration(self, model_file): """ implement the calibration function of AutoCalibrationEvaluatorBase calibration() need to finish the calibration inference procedure so the inference batch num need to >= the batch_num pass to create_quant_config """ onnx_forward(onnx_model=model_file, batch_size=32, iterations=self.batch_num) def evaluate(self, model_file): """ implement the evaluate function of AutoCalibrationEvaluatorBase params: model_file in .onnx return: the accuracy of input model on the eval dataset, or other metric which can describe the 'accuracy' of model """ top1, top5 = onnx_forward(onnx_model=model_file, batch_size=32, iterations=5) return top1 def metric_eval(self, original_metric, new_metric): """ implement the metric_eval function of AutoCalibrationEvaluatorBase params: original_metric: the returned accuracy of evaluate() on non quantized model new_metric: the returned accuracy of evaluate() on fake quant model return: [0]: whether the accuracy loss between non quantized model and fake quant model can satisfy the requirement [1]: the accuracy loss between non quantized model and fake quant model """ loss = original_metric - new_metric if loss * 100 < self.target_loss: return True, loss return False, loss ... config_json_file = os.path.join(TMP, 'config.json') skip_layers = [] batch_num = 1 amct.create_quant_config( config_file=config_json_file, model_file=model_file, skip_layers=skip_layers, batch_num=batch_num, activation_offset=True, config_defination=None) # 1. step1 create quant config json file scale_offset_record_file = os.path.join(TMP, 'scale_offset_record.txt') result_path = os.path.join(PATH, 'results/mobilenet_v2') # 2. step2 construct the instance of AutoCalibrationEvaluator evaluator = AutoCalibrationEvaluator(target_loss=0.5, batch_num=batch_num) # 3. step3 using the accuracy_based_auto_calibration to quantized the model amct.accuracy_based_auto_calibration( model_file=model_file, model_evaluator=evaluator, config_file=config_json_file, record_file=scale_offset_record_file, save_dir=result_path, strategy='BinarySearch', sensitivity='CosineSimilarity' )