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

CANN权重量化批量矩阵乘法算子描述

WeightQuantBatchMatmul 算子 API 描述

【免费下载链接】cann-bench评测AI在处理CANN领域代码任务的能力,涵盖算子生成、算子优化等领域,支撑模型选型、训练效果评估,统一量化评估标准,识别Agent能力短板,构建CANN领域评测平台,推动AI能力在CANN领域的持续演进。项目地址: https://gitcode.com/cann/cann-bench

1. 算子简介

权重量化批量矩阵乘法算子,支持权重的反量化计算。

主要应用场景

  • 大语言模型推理中的权重量化加速
  • 低精度(INT8/INT4)量化模型的矩阵乘法计算
  • 模型压缩与部署场景中的量化矩阵运算

算子特征

  • 难度等级:L3(Contraction)
  • 多输入(x、weight、antiquantScale、可选 antiquantOffset、可选 bias)单输出
  • weight 为量化权重(INT8/INT4),通过反量化参数转换为浮点后参与矩阵乘法

2. 算子定义

数学公式

$$ y = x \times \text{ANTIQUANT}(weight) + bias $$

其中反量化公式:

$$ \text{ANTIQUANT}(weight) = (weight + \text{antiquantOffset}) \times \text{antiquantScale} $$

计算步骤

  1. 对量化权重矩阵进行反量化(ANTIQUANT)操作
  2. 执行矩阵乘法 $x \times \text{ANTIQUANT}(weight)$
  3. 加上偏置 bias(可选)

3. 接口规范

算子原型

cann_bench.weight_quant_batch_matmul(Tensor x, Tensor weight, Tensor antiquantScale, Tensor antiquantOffset, Tensor bias) -> Tensor y

输入参数说明

参数类型默认值描述
xTensor必选左输入矩阵,shape 为 [M, K],dtype 为 float16/bfloat16
weightTensor必选右输入矩阵(量化权重),shape 为 [K, N],dtype 为 int8/int4
antiquantScaleTensor必选反量化scale参数,shape 为 [N] 或 [1, N],dtype 与 x 相同
antiquantOffsetTensorNone反量化offset参数(可选),shape 与 antiquantScale 相同
biasTensorNone偏置张量(可选),shape 为 [N] 或 [1, N]

输出

参数Shapedtype描述
y[M, N]与 x 相同输出张量

数据类型

x dtypeweight dtypeantiquantScale dtype输出 dtype
float16int8/int4float16float16
bfloat16int8/int4bfloat16bfloat16

规则与约束

  • x 的 shape 为 [M, K],weight 的 shape 为 [K, N],矩阵乘法要求 K 维度相等
  • antiquantScale 的 shape 为 [N] 或 [1, N],对应 weight 的 N 维度
  • antiquantOffset(可选)shape 与 antiquantScale 相同
  • bias(可选)shape 为 [N] 或 [1, N]
  • antiquantScale/antiquantOffset/bias 的 dtype 与 x 相同

4. 精度要求

采用生态算子精度标准进行验证。

误差指标

  1. 平均相对误差(MERE):采样点中相对误差平均值

    $$ \text{MERE} = \text{avg}(\frac{\text{abs}(actual - golden)}{\text{abs}(golden)+\text{1e-7}}) $$

  2. 最大相对误差(MARE):采样点中相对误差最大值

    $$ \text{MARE} = \max(\frac{\text{abs}(actual - golden)}{\text{abs}(golden)+\text{1e-7}}) $$

通过标准

数据类型FLOAT16BFLOAT16FLOAT32HiFLOAT32FLOAT8 E4M3FLOAT8 E5M2
通过阈值(Threshold)2^-102^-72^-132^-112^-32^-2

当平均相对误差 MERE < Threshold,最大相对误差 MARE < 10 * Threshold 时判定为通过。

5. 标准 Golden 代码

import torch """ WeightQuantBatchMatmul 算子 Torch Golden 参考实现 权重量化批量矩阵乘法算子 公式: y = x @ ANTIQUANT(weight) + bias ANTIQUANT(weight) = (weight + antiquantOffset) * antiquantScale """ def weight_quant_batch_matmul( x: torch.Tensor, weight: torch.Tensor, antiquantScale: torch.Tensor, antiquantOffset: torch.Tensor = None, bias: torch.Tensor = None ) -> torch.Tensor: """ 权重量化批量矩阵乘法算子 公式: y = x @ ANTIQUANT(weight) + bias ANTIQUANT(weight) = (weight + antiquantOffset) * antiquantScale Args: x: 左输入矩阵,shape 为 [M, K],dtype 为 float16/bfloat16 weight: 右输入矩阵(量化权重),shape 为 [K, N],dtype 为 int8/int4 antiquantScale: 反量化scale参数,shape 为 [N] 或 [1, N] antiquantOffset: 反量化offset参数(可选),shape 与 antiquantScale 相同 bias: 偏置张量(可选),shape 为 [N] 或 [1, N] Returns: 输出张量,shape 为 [M, N],dtype 与 x 相同 """ # 反量化 weight: (weight + antiquantOffset) * antiquantScale weight_float = weight.float() # [K, N] scale_float = antiquantScale.float() # [N] 或 [1, N] if antiquantOffset is not None: offset_float = antiquantOffset.float() weight_dequant = (weight_float + offset_float) * scale_float else: weight_dequant = weight_float * scale_float # 矩阵乘法: [M, K] @ [K, N] = [M, N] x_float = x.float() y_float = torch.matmul(x_float, weight_dequant) # 加偏置(可选) if bias is not None: bias_float = bias.float() y_float = y_float + bias_float y = y_float.to(x.dtype) return y

6. 额外信息

算子调用示例

import torch import cann_bench # x: [M, K] = [16, 32] x = torch.randn(16, 32, dtype=torch.float16, device="npu") # weight: [K, N] = [32, 64] weight = torch.randint(-128, 127, (32, 64), dtype=torch.int8, device="npu") # antiquantScale: [N] = [64] antiquantScale = torch.randn(64, dtype=torch.float16, device="npu") # antiquantOffset: [N] = [64] (可选) antiquantOffset = torch.randn(64, dtype=torch.float16, device="npu") # bias: [N] = [64] (可选) bias = torch.randn(64, dtype=torch.float16, device="npu") y = cann_bench.weight_quant_batch_matmul(x, weight, antiquantScale, antiquantOffset, bias) # y shape: [M, N] = [16, 64]

【免费下载链接】cann-bench评测AI在处理CANN领域代码任务的能力,涵盖算子生成、算子优化等领域,支撑模型选型、训练效果评估,统一量化评估标准,识别Agent能力短板,构建CANN领域评测平台,推动AI能力在CANN领域的持续演进。项目地址: https://gitcode.com/cann/cann-bench

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

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

相关文章:

  • 大连福邸加装饰设计:中山正规的家装装修公司推荐几家 - LYL仔仔
  • 在自动化工作流中集成Taotoken实现多模型智能调度
  • CANN/cann-recipes-infer Qwen3-MoE模型NPU推理
  • CANN算子测试竞赛作品
  • cann/cann-recipes-infer DeepSeek-R1推理优化
  • AI赋能胶质瘤病理诊断:从深度学习技术路径到临床应用解析
  • 调节效应不只是‘分组回归’:用真实商业案例讲透它在AB测试与产品策略中的应用
  • 内容创作场景下如何用Taotoken灵活调用最适合的文案生成模型
  • 别再死记硬背了!用这5个真实项目场景,彻底搞懂ROS节点、话题与服务
  • CANN/ops-blas ACLBLASLt接口文档
  • HarmonyOS 6 ArkUI 粒子动画(Particle)使用文档
  • 35岁转行AI,社恐杨老师教你如何在大模型时代找到自己的坐标(收藏版)
  • 从入门到精通:彻底讲懂Agent的Skill,不做“炫技式浪费”
  • GraphGen:从科学文本自动构建知识图谱的实战指南
  • 2026年德州沥青加温设备、沥青储存罐与筑路设备源头厂家完全采购指南 - 企业名录优选推荐
  • 语音AI测试:构建科学评估体系与工程实践
  • OnmyojiAutoScript:阴阳师手游智能自动化脚本终极指南
  • 如何搭建个人游戏云:Sunshine串流服务器完全指南
  • 别再只写TodoList了!用HTML+CSS+JS做个王者荣耀抽奖Demo,放进你的前端作品集
  • CANN/ops-nn Hardswish反向传播API
  • CANN/pypto gt大于比较运算
  • 华为CANN/ops-math反射填充3D梯度算子
  • 从PSPICE到Cadence 17.2:一个硬件工程师的EDA工具升级心路与避坑实录
  • HarmonyOS 6 ArkUI 粒子动画(Particle)干扰场特性使用文档
  • 从入门到进阶:大模型学习的正确打开方式
  • AI智能体竞技场:零代码可视化多智能体系统实战
  • 动态域名解析工具diny:基于Cloudflare API的轻量级DDNS解决方案
  • 日常开发小记录
  • AirPodsDesktop深度解析:打破生态壁垒的Windows音频革命
  • VS Code 又官宣了一个 Agent 新玩具!有点东西!