当前位置: 首页 > news >正文

CANN/AMCT基于精度自动校准API

accuracy_based_auto_calibration

【免费下载链接】amctAMCT是CANN提供的昇腾AI处理器亲和的模型压缩工具仓。项目地址: https://gitcode.com/cann/amct

产品支持情况

产品是否支持
Ascend 950PR/Ascend 950DT
Atlas A3 训练系列产品/Atlas A3 推理系列产品
Atlas A2 训练系列产品/Atlas A2 推理系列产品

功能说明

根据用户输入的模型、配置文件进行自动的校准过程,搜索得到一个满足目标精度的量化配置,输出可以在ONNX Runtime环境下做精度仿真的fake_quant模型,和可在AI处理器上做推理的deploy模型。

函数原型

accuracy_based_auto_calibration(model,model_evaluator,config_file,record_file,save_dir,input_data,input_names,output_names,dynamic_axes,strategy='BinarySearch',sensitivity='CosineSimilarity')

参数说明

参数名

输入/输出

说明

model

输入

含义:用户的torch model。

数据类型:torch.nn.Module

model_evaluator

输入

含义:自动量化进行校准和评估精度的Python实例。

数据类型:Python实例

config_file

输入

含义:用户生成的量化配置文件。

数据类型:string

record_file

输入

含义:存储量化因子的路径,如果该路径下已存在文件,则会被重写。

数据类型:string

save_dir

输入

含义:模型存放路径。该路径需要包含模型名前缀,例如./quantized_model/*model

数据类型:string

input_data

输入

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

数据类型:tuple

input_names

输入

含义:模型的输入的名称,用于modfied_onnx_file中显示。

默认值:None

数据类型:list(string)

output_names

输入

含义:模型的输出的名称,用于modfied_onnx_file中显示。

默认值:None

数据类型:list(string)

dynamic_axes

输入

含义:对模型输入输出动态轴的指定,例如对于输入inputs(NCHW),N、H、W为不确定大小,输出outputs(NL),N为不确定大小,则{"inputs": [0,2,3], "outputs": [0]}。

默认值:None

数据类型:dict<string, dict<python:int, string>> or dict<string, list(int)>

strategy

输入

含义:搜索满足精度要求的量化配置的策略,默认是二分法策略。

数据类型:string或Python实例

默认值:BinarySearch

sensitivity

输入

含义:评价每一层量化层对于量化敏感度的指标,默认是余弦相似度。

数据类型:string或Python实例

默认值:CosineSimilarity

返回值说明

调用示例

import amct_pytorch as amct from amct_pytorch.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): """ 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 """ model_forward(model=model, batch_size=32, iterations=self.batch_num) def evaluate(self, model): """ implement the evaluate function of AutoCalibrationEvaluatorBase params: model in torch.nn.module return: the accuracy of input model on the eval dataset, or other metric which can describe the 'accuracy' of model """ top1, _ = model_forward(model=model, batch_size=32, iterations=5) if torch.cuda.is_available(): torch.cuda.empty_cache() 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 ... # 1. step1 create quant config json file config_json_file = os.path.join(TMP, 'config.json') skip_layers = [] batch_num = 2 amct.create_quant_config( config_json_file, model, input_data, skip_layers, batch_num ) # 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 record_file = os.path.join(TMP, 'scale_offset_record.txt') result_path = os.path.join(PATH, 'result/mobilenet_v2') amct.accuracy_based_auto_calibration( model=model, model_evaluator=evaluator, config_file=config_json_file, record_file=record_file, save_dir=result_path, input_data=input_data, input_names=['input'], output_names=['output'], dynamic_axes={ 'input': {0: 'batch_size'}, 'output': {0: 'batch_size'} }, strategy='BinarySearch', sensitivity='CosineSimilarity' )

落盘文件说明:

  • 精度仿真模型文件:ONNX格式的模型文件,模型名中包含fake_quant,可以在ONNX Runtime环境进行精度仿真。
  • 部署模型文件:ONNX格式的模型文件,模型名中包含deploy,经过ATC转换工具转换后可部署到AI处理器。
  • 量化因子记录文件:在接口中的record_file中写入量化因子。
  • 敏感度信息文件:该文件记录了待量化层对于量化的敏感度信息,根据该信息进行量化回退层的选择。
  • 自动量化回退历史记录文件:记录的回退层的信息。

【免费下载链接】amctAMCT是CANN提供的昇腾AI处理器亲和的模型压缩工具仓。项目地址: https://gitcode.com/cann/amct

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

http://www.jsqmd.com/news/786984/

相关文章:

  • CANN/shmem原理与架构详解
  • Godot游戏开发实战:从节点系统到高级架构的模块化教程指南
  • 基于PHP 8.4+与原生JS的现代电商引擎eMarket架构解析与实战
  • Slipbot:基于AI Agent的自动化个人知识库管理框架
  • CANN驱动获取设备CPU频率信息
  • 基因数据交易模拟平台:用金融市场模型探索基因组学动态分析
  • CANN/pto-isa P2P指令详解
  • 对比自行维护API中转与使用Taotoken在稳定性上的体感差异
  • 机器学习求解偏微分方程:算子学习与物理信息神经网络全解析
  • AI成本管理利器tokencost:精准计算与监控LLM应用开销
  • Dokploy MCP:基于Docker Compose与MCP协议的轻量级自托管部署平台
  • 小红书自动化发布技术解析:从浏览器模拟到风控对抗
  • GPU加速向量搜索:cuvs库原理、实战与性能调优指南
  • Agent Skills:让 AI 编码像高级工程师一样工作(37,222 Stars)
  • 从原型到生产:构建企业级LangChain应用的核心挑战与实战指南
  • 2026年质量好的番禺旧房翻新改造装修公司/番禺一站式整装装修公司哪家正规 - 品牌宣传支持者
  • 手机相机分辨率
  • 节点与边:LangGraph 中智能体通信的底层机制
  • 开源AI智能体框架安全定制指南:非侵入式补丁与工程化实践
  • 射频测试技术演进与RP-6100系列产品解析
  • mdflow:将Markdown文件转化为可执行AI代理的自动化工具
  • 分治策略与SVD低秩压缩在地震模拟中的应用
  • Riskified在2026年Ascend大会上发布新一代AI套件,为商户赋予前所未有的电商风险可视性和管控能力
  • 哔哩下载姬DownKyi终极指南:3分钟掌握B站视频无损下载的完整教程
  • ARM架构缓存层次与计时器寄存器深度解析
  • 基于大语言模型的LaTeX到HTML智能转换:提升学术文档可访问性
  • 构建结构化技能知识库:从Git管理到团队协作的实践指南
  • 选择诚信通托管服务,这五家机构值得重点关注 - 品牌策略师
  • ClawKernel:基于WebSocket的OpenClaw网关实时监控与管理平台
  • 终极指南:3步破解微信设备限制,实现手机平板双登录