已合并
triton_experimental: softmax aclnn routing + npu_expand recursion fix #44889
triton_experimental: softmax aclnn routing + npu_expand recursion fix #44889
已合并
huyuchao创建于 20 天前
huyuchao成员
20 天前

关联 issue: #4384

【合入来源】

如有社区issue,请关联issue链接 > 请勿携带内部流程信息(需求链接、问题单、内部issue等)

triton_experimental 后端两类问题:①softmax 族 Triton 融合在 Ascend 上性能劣化(带掩码注意力 softmax 融合物化 [B,H,S,S] 掩码矩阵、损失路径 log_softmax 拖带 gather 拆解、宽行 softmax
退化为单核串行扫描);②npu_expand 降低链在 accuracy 流程无限递归(RecursionError)。

【修改方案】

涉及组件:torch_npu/_inductor/triton_experimental/(Python 层,lowering.py / config.py / __init__.py),与 Inductor 降低注册表(torch._inductor.lowering 的 lowerings/decompositions 表)交互,无 C++
改动。

1. softmax 三级路由(lowering.py::_register_softmax_aclnn_fallback_activate 挂载)

上游 softmax 分解为三段式 amax→exp→÷sum(两次归约真依赖),调度上 ≤1024 行宽走持久归约(行驻留、可邻域融合),更宽行本应走 split_reductions 跨核拆分,但 TE 已显式关闭 split(历史 OOB 规避)→
宽行退化为每行单 program 串行扫描。msprof(910B2)实测宽行(30k/50k)Triton softmax 比 aclnnSoftmax 慢 2.5–2.8x。据此分三类路由:

  1. aten._safe_softmax(掩码复合):分解为 aten._softmax。两种旧路径均劣化——Triton 融合会把掩码物化进 kernel(TrOCR 实测 10.3 ms/iter 额外设备时间);直接 fallback 则 eager 复合实现运行冗余 -inf
    守卫链 IsNegInf→SWhere→All(4.6 ms/iter,输入已被 masked_fill 预掩码)。分解后交行宽路由接管。
  2. log_softmax 族:整体 aclnn 回退(损失路径 Triton log_softmax 慢于 aclnnLogSoftmax,且融合拖带 Gather_AsStrided 拆解 +2.9 ms/iter)。
  3. 纯 softmax 行宽路由softmax_aclnn_max_fuse_numel=256):行宽 ≤256 保留 Triton 融合(邻域融合正收益,BertForMaskedLM 实证),>256 回退 aclnnSoftmax。行宽取 sympy 静态求值(size_hint
    兜底,异常时保守走 Triton)。

2. npu_expand 递归修复(RecursionError)

模块体先 from torch._inductor.lowering import expand as _orig_expand,随后 monkeypatch lowering.expand = npu_expand。模块二次执行时 from-import 绑定到已 patch 的 npu_expand_orig_expand 自别名 →
npu_expand:385 无限自递归(accuracy 流程先跑 eager 改变加载时序触发;perf 流程及缓存命中不触发,长期掩盖)。修复双保险:

  1. 原始上游句柄首次执行时暂存到永不被重载的上游模块属性 _npu_upstream_expand_saved_orig_expand 恒取暂存;
  2. tail-bcast 物化分支(Pointwise.create+realize 拖拽惰性输入求值可绕回 broadcast 链)加 threadlocal 重入哨兵,重入即返回上游结果。

3. 新增配置开关(config.pysafe_softmax_aclnn_fallback / log_softmax_aclnn_fallback(默认 True)、softmax_aclnn_max_fuse_numel(默认 256,0 = 关闭路由恒走 Triton),均可独立关闭用于 A/B 回归。

【资料变更】

不涉及

【接口变更】

不涉及跨代码仓/客户面接口。仓内 triton_experimental.config 新增 3 个内部调优开关(见修改方案 3),仅影响本后端内部降低策略。

【功能验证】

测试场景:Ascend 910B2 + torch 2.13 + torch_npu(triton_experimental) + triton-ascend 3.2.2,torchbench/huggingface benchmark runner(--backend inductor --npu-backend triton --inference --disable-aclgraph),全部在清空缓存后全新编译降低条件下执行(排除缓存命中假阳性)。

测试方法

  1. 性能:--performance,30 迭代中位数(eager vs compile);
  2. 精度:--accuracy(tolerance 1e-4),并确认无 RecursionError;
  3. 设备侧归因:torch_npu.profiler(msprof 集成)逐 kernel 设备时间对账,4.64 ms/iter 残差 100% 定位到 -inf 守卫链;
  4. 尺寸效应:softmax 行宽 64→50265 扫描确立路由阈值。

结果

模型 修改前 修改后 加速比 精度
TrOCRForCausalLM 0.881x(劣化) compile 71.24→61.59 ms/iter 1.037x pass_accuracy
BertForMaskedLM 1.279x compile 40.08→35.88 ms/iter 1.401x pass_accuracy
fastNLP_Bert 1.081x compile 20.59→13.78 ms/iter 1.464x pass_accuracy

回归:resnet18 1.66x / drq 1.23x / yolov3 持平(无 softmax 路径模型不受影响);修改前 accuracy 流程 RecursionError 必现的 4 个 torchbench 模型(resnet18/drq/fastNLP_Bert/yolov3)修复后全部正常完成且 Rec=0。

UT:本 PR 未新增 UT,看护方式为端到端 benchmark + 精度流(上述);后续可在 pytorch/test/_inductor/ 补 softmax 路由的降级断言用例(区分 _safe_softmax 分解、log_softmax 回退、宽行路由三分支)。

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 huyuchao 的贡献)
Hhuyuchao成员
20 天前 创建了 pull request,commit 848d1ba5
atomgit-bot
atomgit-bot
20 天前 评论:

变更摘要

本 PR 针对 torch_npu/_inductor/triton_experimental/ 后端的两类问题进行修复与优化:一是为 softmax 族算子新增 aclnn 回退路由(lowering.py::_register_softmax_aclnn_fallback),解决 Ascend 上带掩码 softmax 融合物化 [B,H,S,S] 掩码、loss 路径 log_softmax 拖带 gather 拆解、宽行 softmax 退化为单核串行扫描三类性能劣化;二是修复 npu_expand 在 accuracy 流程中的无限自递归(RecursionError)。改动仅涉及 Python 层 lowering.py / config.py / __init__.py,无 C++ 改动,新增 3 个可独立开关的内部调优配置项。

主要改动

  • softmax 三级路由(lowering.py::_register_softmax_aclnn_fallback:将掩码复合算子 aten._safe_softmax 分解为 aten._softmax(跳过 eager 复合实现的冗余 -inf 守卫链,交行宽路由接管);log_softmax 族整体回退 aclnn(make_fallback 移除其 lowering/decomposition 注册);纯 aten._softmax 按归约行宽路由,宽度超过 softmax_aclnn_max_fuse_numel(默认 256)时回退 aclnnSoftmax,否则保留 Triton 融合;行宽通过 sympy 静态求值并带 size_hint 兜底,异常时保守走 Triton。

  • npu_expand 递归修复(lowering.py:将模块首次执行时的上游 expand 句柄暂存到永不被重载的上游模块属性 _npu_upstream_expand_saved_orig_expand 恒取暂存值,避免模块二次执行时 from-import 绑定到已 patch 的 npu_expand 导致自递归;并为 tail-bcast 物化分支(Pointwise.create + realize)增加 threadlocal 重入哨兵 _expand_reentry,重入时直接返回上游结果以终止循环。

  • 新增配置开关(config.py:新增 safe_softmax_aclnn_fallbacklog_softmax_aclnn_fallback(默认 True)与 softmax_aclnn_max_fuse_numel(默认 256,0 = 关闭路由恒走 Triton),均可独立关闭用于 A/B 回归验证。

  • 注册挂载(__init__.py::_activate:在 _activate() 中与既有 _register_npu_inductor_fallbacks() 一并调用新注册函数 _register_softmax_aclnn_fallback(),使 softmax 路由在 TE 后端激活时生效。

likedislike
不准确?
atomgit-bot
atomgit-bot
20 天前 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
ascend-robotascend-robot成员
20 天前 添加了label:stat/needs-squash
此处折叠了114条消息 查看更多
ascend-robot
ascend-robot成员
11 天前 评论:
流水线 PR-pipeline_pytorch#64277 [ commitID:8aa4b704 ] 已完成
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
Build_X86_213 >>>
Build_ARM_213 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 codecheck_pre-commit >>>
check_error >>>
lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_ARM_A3_Part_01 🛑 >>>
UT_ARM_A3_Part_02 🛑 >>>
UT_ARM_A2_Part_01 >>>
UT_ARM_A2_Part_02 >>>
UT_ARM_A2_Part_03 >>>
UT_inductor_Part_01 🛑 >>>
UT_inductor_Part_02 🛑 >>>
UT_inductor_Part_03 🛑 >>>
UT_inductor_Part_04 🛑 >>>
UT_DIST_ARM_Part_01 🛑 >>>
UT_DIST_ARM_Part_02 🛑 >>>
UT_DIST_ARM_Part_03 🛑 >>>
UT_DIST_ARM_Part_04 🛑 >>>
UT_ARM_A2_Select_Part_01 >>>
UT_ARM_A2_Select_Part_02 >>>
UT_ARM_A2_Part_213 >>>
UT_inductor_Part_213 🛑 >>>
UT_DIST_ARM_Part_213 🛑 >>>
UT_ARM_A2_Select_Part_213 >>>
流水线 PR-pipeline_pytorch >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan, Jesse, WSs_321]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
ascend-robotascend-robot成员
11 天前 关闭了关联的issue
ascend-robotascend-robot成员
11 天前 合入了pull request
ascend-robot
ascend-robot成员
11 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
AtlasAccount
AtlasAccount成员
11 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#14603 [ commitID:8aa4b704 ] 已完成
likedislike