已合并
[Inductor] fix rms norm reduction #42071
[Inductor] fix rms norm reduction #42071
已合并
luqichao创建于 7月18日
luqichao
luqichao成员
7月18日
Pull Request已成功合入, 合并人@ascend-robot
(感谢 luqichao 的贡献)
luqichaoluqichao成员
7月18日 创建了 pull request,commit 1c32ebce
atomgit-bot
atomgit-bot
7月18日 评论:

变更摘要

本次 PR 主要修复了 Inductor 后端在多轴 reduction(特别是 RMS norm 权重梯度计算)场景下的代码生成问题。核心改动集中在 NPUIndexTritonKernelReductionAnalysis 两个类中:对多 reduction 轴且连续 reduction 的情况新增了专门的代码生成路径,包括循环体结构重组、store 写入时机调整、以及带 permute 的 reshape 处理。同时补充了对应的单元测试用例。

主要改动

  • 多轴 reduction 的 golden var list 选择:在 kernel_analysis.pyReductionAnalysis 中,当 reduction 轴多于 1 且为连续 reduction 时,若 golden_var_list 为空则主动调用 select_golden_varlist(),并使用 reduction 轴对应的 golden var 数量作为结果。

  • 多轴 reduction 循环体代码生成重构:在 triton.pyNPUIndexTritonKernel 中新增 is_first_reduction_tilinguse_outer_reduction_post_loop 判断逻辑,区分首个 reduction tiling 轴与非首个轴,调整 post_loop_combinepost_loop_store 以及普通 stores 的写入时机与顺序,确保多轴 reduction 场景下代码生成正确。

  • 带 permute 的 dense reshape 支持:在 _map_tuple_or_scalar 相关代码中,对多轴连续 reduction 场景计算 golden_var_list 的排列顺序,必要时生成 tl.reshape(v.permute(...), ...) 而非直接 tl.reshape(v, ...),同时 mask 条件表达式也应用相同的 permute 变换。

  • 多轴 reduction 的 promote_to_tensor reshape:在 promote_to_tensor 方法中增加对多轴连续 reduction 的处理,使用 golden_var_list 确定哪些轴参与 reduction 并将对应维度置为 "1",生成正确的 reshape 字符串。

  • 新增 RMS norm 权重梯度测试用例:在 test_var_mean_add_mul.py 中新增 rms_norm_weight_gradcheck_rms_norm_weight_grad 辅助方法以及 test_rms_norm_weight_grad_head_seq_permutetest_rms_norm_weight_grad_batch_seq_permute 两个测试方法,覆盖 head 维度和 batch 维度 permute 的 RMS norm 权重梯度计算场景。

likedislike
atomgit-bot
atomgit-bot
7月18日 评论:

代码审查

I've now completed the review of all three changed files. Let me summarize:

Closing Summary

Findings by Priority

  • P1: 1 finding — 在 codegen_body 中,当多 reduction 轴场景下唯一的 reduction tiling 恰好也是最后一个 tiling 时,prefix 和所有 post-loop 代码(post_loop_combine、post_loop_store、stores、deferred_reduction_stores)被静默跳过,导致生成的 Triton kernel 不完整(triton.py:2701-2702

  • P2: 1 finding — 在 is_first_reduction_tiling 分支中使用 writeline 写入 IndentedBuffer._lines 已带缩进的内容,可能导致重复缩进(triton.py:2737-2740

Files Reviewed

File Result
test/_inductor/test_var_mean_add_mul.py no issues
torch_npu/_inductor/codegen/kernel_analysis.py no issues
torch_npu/_inductor/codegen/triton.py 2 issues (1×P1, 1×P2)

Overall Risk Judgment

中等偏高。P1 问题在特定条件下(多 reduction 轴被合并为单个 reduction tiling 且非 persistent 模式)会导致 prefix 和 post-loop 代码被完全跳过,产生不完整/无效的 Triton kernel。这正是 Issue #2543 所描述的 rms norm codegen error 场景。建议在合入前修复 P1 问题。

类型 数量
🔴 阻塞 1
🟡 建议 0

⛔ 需要修改

likedislike
ascend-robotascend-robot成员
7月18日 添加了label:ascend-cla/yes
ascend-robot
ascend-robot成员
7月18日 评论:

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
test crazyDannyBoy, TonyYA (2/2) crazyDannyBoy (1/1)
torch_npu/_inductor crazyDannyBoy, TonyYA (2/2) crazyDannyBoy (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
ascend-robotascend-robot成员
7月18日 添加了label:needs-issue
ascend-robot
ascend-robot成员
7月18日 评论:

Linking Issue Notice

@luqichao , the pull request must be linked to at least one issue.
If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue .

likedislike
ascend-robot
ascend-robot成员
7月18日 评论:

当前仓库存在以下 保护分支

Protected Branch Version Release
master
v2.7.1
v2.11.0-26.1.0
v2.9.0-26.1.0
v2.9.0
v2.10.0
v2.12.0
v2.7.1-26.1.0
v2.10.0-26.1.0
v2.11.0
v2.12.0-26.1.0
ci-test

评论 /sync <branch1> <branch2> ... 可将当前 PR 修改同步到其它分支(创建同步 PR):
a) 如果当前 PR 是 Open 状态,同步操作将延迟到 PR 被合并时执行
b) 如果当前 PR 已经 Merged,将立即执行同步操作

注意:

  1. /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
  2. 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭
likedislike
luqichaoluqichao成员
7月18日 关联了issue:[Inductor] rms norm codegen error
ascend-robotascend-robot成员
7月18日 删除了label:needs-issue
luqichao
luqichao成员
7月18日 评论:

compile

likedislike
ascend-robotascend-robot成员
7月18日 添加了label:ci-pipeline-running
ascend-robotascend-robot成员
7月18日 删除了label:ci-pipeline-running
ascend-robot
ascend-robot成员
7月18日 评论:
流水线 PR-pipeline_pytorch#46827 [ commitID:ee777a45 ] 已终止运行
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 🕚 >>>
Build_ARM 🕚 >>>
Build_LibTorch_x86 🕚 >>>
Build_LibTorch_ARM 🕚 >>>
Build_X86_torchair 🕚 >>>
Build_ARM_torchair 🕚 >>>
patch_test 🕚 >>>
恶意代码检查 Antipoison 🟨 >>>
编码安全与规范检查 CodeCheck 🟨 >>>
check_error 🟨 >>>
CodeCheck_lintrunner 🟨 >>>
开源片段检查 SCA 🟨 >>>
开发者测试 UT_X86_Part_01 🕚 >>>
UT_X86_Part_02 🕚 >>>
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 🕚 >>>
流水线 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]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
ascend-robotascend-robot成员
7月18日 添加了label:ci-pipeline-running
ascend-robot
ascend-robot成员
7月18日 评论:

ascend docs pipeline is running...

likedislike
ascend-robotascend-robot成员
7月18日 添加了label:docs-ci-pipeline-running
ascend-robot
ascend-robot成员
7月18日 评论:

ascend docs pipeline is running...

likedislike
ascend-robot
ascend-robot成员
7月18日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
ascend-robotascend-robot成员
7月18日 删除了label:docs-ci-pipeline-running
ascend-robotascend-robot成员
7月18日 添加了label:docs-ci-pipeline-success
ascend-robot
ascend-robot成员
7月18日 评论:

✅ 跳过 docs ci 检查,没有需要检查的文档文件

likedislike
atomgit-bot
atomgit-bot7月18日进行代码检视2
torch_npu/_inductor/codegen/triton.py
@@ -2694,2 +2701,2 @@
2694- self.post_loop_combine.clear()
2695- self.post_loop_store.clear()
2701+ if use_outer_reduction_post_loop:
2702+ pass
atomgit-bot
atomgit-bot7月18日评论:

🟠 High Priority

codegen_body()codegen_range 内,当以下条件同时满足时,最后 tiling 轴分支(range_val.is_tiling_axis and last_tiling,第 2684-2718 行)会静默跳过 prefix 和所有 post-loop 代码(post_loop_combine、post_loop_store、stores、deferred_reduction_stores):

  1. self.numof_reduction_axis() > 1(多 reduction 轴)
  2. 唯一的 reduction tiling 轴恰好也是最后一个 tiling 轴(即没有独立的"第一个 reduction tiling"和"最后一个 tiling"两个轴)
  3. not self.persistent_reduction 导致 self.prefix._lines 非空,因此 use_outer_reduction_post_loop = True

此时:

而由于 is_first_reduction_tiling 在此场景下同样为 True 但代码进入了第一分支(last_tiling),不会进入第二分支(elif range_val.is_tiling_axis:),因此第二分支中处理 post-loop 的补救逻辑(第 2735-2746 行)也不会执行。最终产生的 Triton kernel 缺少累加器初始化和最终 reduction 结果写回,是不完整的代码。

该场景正是 rms_norm weight grad(对 4D tensor 的 dim (0,1,2) 连续 reduction,可能被合并为单个 tiling 轴)的典型情况,与 Issue #2543 描述的 "rms norm codegen error" 高度相关。

改动建议
2702
+ if use_outer_reduction_post_loop:
2703
+ if is_first_reduction_tiling:
2704
+ # 唯一的 reduction tiling 也是最后一个 —— 自己处理 prefix 和 post-loop
2705
+ if self.numof_reduction_axis() > 1:
2706
+ self.body.splice(self.prefix)
2707
+ self.body.splice(self.post_loop_combine)
2708
+ self.body.splice(self.post_loop_store)
2709
+ for store_line in self._deferred_reduction_stores:
2710
+ self.body.writeline(store_line)
2711
+ self._deferred_reduction_stores.clear()
2712
+ if self.numof_reduction_axis() > 1:
2713
+ self.body.splice(self.stores)
2702
- pass
2714
+ self.stores.clear()
2715
+ self.post_loop_combine.clear()
2716
+ self.post_loop_store.clear()
2717
+ # else: 外层(第一个 reduction tiling)会处理,这里 pass 是正确的
应用建议
likedislike
luqichao
luqichao成员
7月20日 评论:

use_outer_reduction_post_loop跟is_first_reduction_tiling 的前提都是self.numof_reduction_axis() > 1, 也就是指超过一个reduction轴, 该触发条件不可能满足

ascend-robotascend-robot成员
7月18日 删除了label:ci-pipeline-running
ascend-robotascend-robot成员
7月18日 添加了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
7月18日 评论:
流水线 PR-pipeline_pytorch#46828 [ commitID:ee777a45 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 >>>
Build_ARM >>>
Build_LibTorch_x86 >>>
Build_LibTorch_ARM >>>
Build_X86_torchair 🛑 >>>
Build_ARM_torchair 🛑 >>>
patch_test 🛑 >>>
恶意代码检查 Antipoison >>>
编码安全与规范检查 CodeCheck >>>
check_error >>>
CodeCheck_lintrunner >>>
开源片段检查 SCA >>>
开发者测试 UT_X86_Part_01 🛑 >>>
UT_X86_Part_02 🛑 >>>
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 >>>
流水线 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]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
pangjiayi
pangjiayi
7月20日 评论:

/lgtm

likedislike
luqichaoluqichao成员
7月20日 解决了最后一个问题
dezheng889成员
7月20日 评论:

/lgtm
/approve

likedislike
ascend-robotascend-robot成员
7月20日 添加了label:approvedlgtm
ascend-robotascend-robot成员
7月20日 合入了pull request
ascend-robot
ascend-robot成员
7月20日 评论:

Pull Request 已合并或已关闭。

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

likedislike
ascend-robot
ascend-robot成员
7月20日 评论:
流水线 pytorch_gitcode_PR_multiVersion#12810 [ commitID:ee777a45 ] 已完成
likedislike
luqichaoluqichao成员
5 天前 修改了pull request 的描述
ascend-robotascend-robot成员
5 天前 删除了label:ci-pipeline-passed
ascend-robot
ascend-robot成员
5 天前 评论:

The following label is not ready.

ci-pipeline-passed: The ci-pipeline-passed label is expired. Please compile again.

likedislike