Pull Request已成功合入, 合并人@ascend-robot
(感谢 luqichao 的贡献)变更摘要
本次 PR 主要修复了 Inductor 后端在多轴 reduction(特别是 RMS norm 权重梯度计算)场景下的代码生成问题。核心改动集中在 NPUIndexTritonKernel 和 ReductionAnalysis 两个类中:对多 reduction 轴且连续 reduction 的情况新增了专门的代码生成路径,包括循环体结构重组、store 写入时机调整、以及带 permute 的 reshape 处理。同时补充了对应的单元测试用例。
主要改动
-
多轴 reduction 的 golden var list 选择:在
kernel_analysis.py的ReductionAnalysis中,当 reduction 轴多于 1 且为连续 reduction 时,若golden_var_list为空则主动调用select_golden_varlist(),并使用 reduction 轴对应的 golden var 数量作为结果。 -
多轴 reduction 循环体代码生成重构:在
triton.py的NPUIndexTritonKernel中新增is_first_reduction_tiling和use_outer_reduction_post_loop判断逻辑,区分首个 reduction tiling 轴与非首个轴,调整post_loop_combine、post_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_tensorreshape:在promote_to_tensor方法中增加对多轴连续 reduction 的处理,使用golden_var_list确定哪些轴参与 reduction 并将对应维度置为"1",生成正确的 reshape 字符串。 -
新增 RMS norm 权重梯度测试用例:在
test_var_mean_add_mul.py中新增rms_norm_weight_grad、check_rms_norm_weight_grad辅助方法以及test_rms_norm_weight_grad_head_seq_permute、test_rms_norm_weight_grad_batch_seq_permute两个测试方法,覆盖 head 维度和 batch 维度 permute 的 RMS norm 权重梯度计算场景。


代码审查
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 |
⛔ 需要修改


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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


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 .


当前仓库存在以下 保护分支 :
| 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,将立即执行同步操作
注意:
- /sync 命令可以指定同步到多个分支,仅最后一个 /sync 命令生效
- 如果创建的同步 PR 不正确,可通过向同步 PR 的源分支提交轻量级 PR 完善,或使用 /close 命令关闭


compile


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 | 🟨 | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


ascend docs pipeline is running...


ascend docs pipeline is running...


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


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


🟠 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):
self.numof_reduction_axis() > 1(多 reduction 轴)- 唯一的 reduction tiling 轴恰好也是最后一个 tiling 轴(即没有独立的"第一个 reduction tiling"和"最后一个 tiling"两个轴)
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
| - |
|
2714 | + self.stores.clear() |
|
2715 | + self.post_loop_combine.clear() |
|
2716 | + self.post_loop_store.clear() |
|
2717 | + # else: 外层(第一个 reduction tiling)会处理,这里 pass 是正确的 |


| 阶段 | 任务名 | 状态 | 详情 |
|---|---|---|---|
| 编译构建 | 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 | ✅ | >>> |
- compile、compile_inductor、compile_torchair : 运行流水线
- retry : 重试流水线所有失败子任务
- retry <任务名> : 仅重试指定失败子任务
- stop : 停止流水线


/lgtm


/lgtm
/approve


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.




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


同步PR https://gitcode.com/Ascend/pytorch/pull/39499