CANN/cann-bench 分组矩阵乘量化融合算子评测
GroupedMatmulSwigluQuant 算子 API 描述【免费下载链接】cann-bench评测AI在处理CANN领域代码任务的能力涵盖算子生成、算子优化等领域支撑模型选型、训练效果评估统一量化评估标准识别Agent能力短板构建CANN领域评测平台推动AI能力在CANN领域的持续演进。项目地址: https://gitcode.com/cann/cann-bench1. 算子简介分组矩阵乘法GroupedMatmul / GMM、反量化、SwiGLU 激活与再量化的融合算子将四个步骤合并为一次 Kernel 调用以减少中间数据搬运。语义对齐torch_npu.npu_grouped_matmul_swiglu_quant_v2的默认配置。主要应用场景大语言模型中 MoEMixture of Experts结构的 FFN 层前半段GateUp 投影 SwiGLU 量化量化推理流水线中 GMM → 激活 → 再量化的整体融合对 token 分组路由后每组 token 使用独立专家权重的高性能推理算子特征难度等级L4FusedComposite多输入、双输出涉及 GMM、per-token / per-channel 反量化、SwiGLU、再量化激活与权重均为 int8x 已完成 per-token 量化输出为 int8 float32 scale2. 算子定义数学公式$$ \begin{aligned} \text{对每个专家 } g \in [0, E),\ \text{根据 } group_list\ (\text{cumsum}) \text{ 取属于该组的 token 行 } rows_g: \ mm_g x[rows_g] \cdot weight[g] \ deq_g mm_g \odot x_scale[rows_g] \odot weight_scale[g] \ \text{合并所有组得到 } deq \in \mathbb{R}^{M \times N} \ left, right \text{split}(deq,\ \text{last_dim}/2) \ act \text{SiLU}(left) \odot right \ y,\ y_scale \text{PerTokenQuant}(act) \end{aligned} $$步骤拆解分组矩阵乘法GMMgroup_list采用 cumsum 语义将x的M行划分到E个专家第g组的 token 与weight[g]做矩阵乘。反量化Dequant左 per-token 右 per-channeldeq[i, j] mm[i, j] * x_scale[i] * weight_scale[g(i), j]。SwiGLU沿最后一维对半拆分为left, right计算SiLU(left) * right输出宽度减半为N/2。再量化Quantper-tokeny_scale[i] max_j|act[i, j]| / 127y[i, j] clamp(round(act[i, j] / y_scale[i]), -128, 127)。3. 接口规范算子原型cann_bench.grouped_matmul_swiglu_quant( Tensor x, Tensor weight, Tensor weight_scale, Tensor x_scale, Tensor group_list, ) - (Tensor y, Tensor y_scale)输入参数参数类型Shape描述xTensor (int8)[M, K]激活矩阵GMM 左矩阵已完成 per-token 量化允许非连续weightTensor (int8)[E, K, N]专家权重GMM 右矩阵weight_scaleTensor (float32)[E, N]权重 per-channel 反量化因子x_scaleTensor (float32)[M]激活 per-token 反量化因子group_listTensor (int32)[E]每个专家的 token 累计和cumsum输出参数Shapedtype描述y[M, N/2]int8SwiGLU 后的 per-token int8 量化结果y_scale[M]float32输出 per-token 反量化因子数据类型输入 dtype输出 dtypex: int8weight: int8*_scale: float32group_list: int32y: int8y_scale: float32规则与约束x的K维必须与weight的K维一致。weight的最后一维N必须为偶数以便 SwiGLU 对半拆分。group_list为 cumsum 序列长度为E最终累计值不得超过M。输出y会被截断到[-128, 127]。4. 精度要求采用生态算子精度标准进行验证。误差指标平均相对误差MERE采样点中相对误差平均值$$ \text{MERE} \text{avg}(\frac{\text{abs}(actual - golden)}{\text{abs}(golden)\text{1e-7}}) $$最大相对误差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 from typing import Tuple def grouped_matmul_swiglu_quant( x: torch.Tensor, weight: torch.Tensor, weight_scale: torch.Tensor, x_scale: torch.Tensor, group_list: torch.Tensor, ) - Tuple[torch.Tensor, torch.Tensor]: M, K x.shape E, _, N weight.shape N_out N // 2 ends group_list.to(torch.int64).tolist() starts [0] ends[:-1] dequant torch.empty((M, N), dtypetorch.float32, devicex.device) x_scale_f x_scale.float() for g in range(E): s, e starts[g], ends[g] if s e: continue mm torch.matmul(x[s:e].float(), weight[g].float()) xs x_scale_f[s:e].unsqueeze(1) ws weight_scale[g].float().unsqueeze(0) dequant[s:e] mm * xs * ws left, right dequant[..., :N_out], dequant[..., N_out:] act torch.nn.functional.silu(left) * right eps torch.finfo(torch.float32).tiny y_scale act.abs().amax(dim-1).clamp_min(eps) / 127.0 y torch.clamp(torch.round(act / y_scale.unsqueeze(1)), -128, 127).to(torch.int8) return y, y_scale.to(torch.float32)6. 额外信息算子调用示例import torch import cann_bench M, K, N, E 64, 256, 512, 4 x torch.randint(-128, 127, (M, K), dtypetorch.int8, devicenpu) weight torch.randint(-128, 127, (E, K, N), dtypetorch.int8, devicenpu) weight_scale torch.rand(E, N, dtypetorch.float32, devicenpu) * 0.01 x_scale torch.rand(M, dtypetorch.float32, devicenpu) * 0.01 # cumsum 语义四组累计 16/32/48/64 group_list torch.tensor([16, 32, 48, 64], dtypetorch.int32, devicenpu) y, y_scale cann_bench.grouped_matmul_swiglu_quant( x, weight, weight_scale, x_scale, group_list, )参考文档torch_npu.npu_grouped_matmul_swiglu_quant_v2https://www.hiascend.com/document/detail/zh/Pytorch/730/apiref/torchnpuCustomsapi/docs/context/torch_npu-npu_grouped_matmul_swiglu_quant_v2.md【免费下载链接】cann-bench评测AI在处理CANN领域代码任务的能力涵盖算子生成、算子优化等领域支撑模型选型、训练效果评估统一量化评估标准识别Agent能力短板构建CANN领域评测平台推动AI能力在CANN领域的持续演进。项目地址: https://gitcode.com/cann/cann-bench创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2598218.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!