已合并
feat(mul): 稀疏广播非连续 Mul 回退转连续 + l0op::Mul #3316
feat(mul): 稀疏广播非连续 Mul 回退转连续 + l0op::Mul #3316
已合并
qianzehong创建于 6月12日
qianzehong成员
6月12日

描述

非连续 Mul kernel 在「外层广播 + 内层稀疏 gather」的输入上严重劣化(典型来自 RoPE cos/sin 表的 [1, N, 1] 视图,stride 含 +1 对齐 padding,最内连续块仅 1 个元素,且被外层广播重复读 K 次)。

基于 Ascend950 上板基准(lm eager 网络的 mul 非连续 case,npu.Event 设备侧计时,三轮稳定):

输入 非连续 Mul 转连续 + Mul 倍数
[1,4800,1] strides [312000,65,1] 49.3 us 11.1 us 4.4x
[1,688,1] strides [22704,33,1] 16.2 us 9.5 us 1.7x

而转置大张量([80,381,640] 等 0.36~0.52)、大张量带 padding 切片、纯 stride-0 广播等场景非连续 kernel 仍更优,因此只在命中退化模式时回退。

改动

aclnnMulGetWorkspaceSize 中新增 MulPreferContiguous(self, other) 判定:任一输入同时满足

  1. 广播倍数 MulBroadcastFactor >= 8(被重复读 K 次)
  2. payload MulViewNumel >= 256
  3. 最内连续块 MulInnerContiguousRun < 32(无可向量化连续块)

时,把两个非连续门(isSupportNonContiguous 与 normal 分支的 IsMulSupportNonContiguous 条件)置假,走原有的 l0op::Contiguous + l0op::Mul 路径。阈值均为具名 constexpr,便于回归微调。

关联的Issue

#1965

测试

  • 对日志中提取的 23 个非连续 Mul layout 在 Ascend950 上实跑:非连续 mul 与「转连续+mul」数值零不一致,该判据精确命中上述 2 个回退 case,其余 21 个保持非连续,零误判
  • 950 性能复测:89418 49.9us→11.1us(4.51x)、89424 16.3us→9.5us(1.70x)。
  • CANN 9.0.0 aarch64 真工具链 -fsyntax-only -Wall -Wextra 编译零错误零告警。

文档更新

无需更新文档

类型标签

  • Bug修复
  • 新特性
  • 性能优化
  • 文档更新
  • 其他,请描述:
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 qianzehong 的贡献)
Qqianzehong成员
6月12日 创建了 pull request,commit 863c6240
CANN-robotCANN-robot成员
6月12日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
6月12日 评论:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here
You can get sig-info at here


PR Approval Progress

Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
math/mul 周奇龙, 宋凯 (2/2) 周奇龙, 宋凯 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

qianzehong, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
qianzehong成员
6月13日 评论:

950 上板端到端验证(自定义编译 aclnnMul 实跑)

build.sh --pkg --soc=ascend950 --ops=mul 编出含本改动的自定义 vendor 包(libcust_opapi.so 导出 aclnnMulGetWorkspaceSize),--extract 到独立路径并经 ASCEND_CUSTOM_OPP_PATH 让 torch_npu 的 torch.mul 解析到改动后的 aclnnMul未改系统内置 opp)。开 INFO 日志跑 2 个命中 case + 1 个对照 case:

Case 输入 MulPreferContiguous Mul tiling 正确性
89418(命中) self[381,4800,1] × other[1,4800,1] s[312000,65,1] off64 ✅ 触发 → 转连续 inputAllContiguous 0→1 correct=True
89424(命中) self[381,688,1] × other[1,688,1] s[22704,33,1] off32 ✅ 触发 → 转连续 inputAllContiguous 0→1 correct=True
89399(对照) self[381,32,1] s[4128,129,1] off128 × other[1,32,1] s[1056,33,1] ❌ 不触发(payload=32 太小) inputAllContiguous:0(保持非连续) correct=True

关键日志(节选):

>>>BEGIN 89418_FLAG
[INFO] aclnn_mul.cpp:523 [MulPreferContiguous] aclnnMul_5: sparse-broadcast operand detected, route to Contiguous + Mul instead of non-contiguous kernel.
[INFO] aclnnMul_5_Contiguous ... x: [381, 4800, 1]
[INFO] aclnnMul_5_Contiguous ... x: [1, 4800, 1] strides[312000,65,1] off64
[INFO] [OPS_MATH_CUSTOM][Mul] DoTiling coreNum:56 inputAllContiguous:1
>>>END 89418_FLAG correct=True

>>>BEGIN 89399_keep
[INFO] [OPS_MATH_CUSTOM][Mul] DoTiling coreNum:56 inputAllContiguous:0   ← 对照组保持非连续,未误触发
>>>END 89399_keep correct=True

OPS_MATH_CUSTOM tiling tag + aclnn_mul.cpp:523 行号确认跑的是本 PR 代码。结论:两个非连续 Mul kernel 劣化 case 正确改走「转连续 + l0op::Mul」,对照组保持非连续不误触发,全部数值正确——与离线基准(89418 4.4x / 89424 1.7x)一致。

设备:Ascend950PR,CANN 9.1.0,torch 2.8 + torch_npu 2.8.0.post4。

likedislike
qianzehong成员
6月13日 评论:

compile

likedislike
此处折叠了161条消息 查看更多
CANN-robotCANN-robot成员
6月15日 添加了label:approved
sunday成员
6月16日 评论:

/lgtm
/approve

likedislike
CANN-robotCANN-robot成员
6月16日 添加了label:lgtm
CANN-robotCANN-robot成员
6月16日 关闭了关联的issue
CANN-robotCANN-robot成员
6月16日 合入了pull request