已合并
[inductor]Disable fully static Welford optimization in scenarios with inplace memory update #44708
[inductor]Disable fully static Welford optimization in scenarios with inplace memory update #44708
已合并
rain-666创建于 4 天前
rain-666
rain-666成员
4 天前

【合入来源】

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

【修改方案】

请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列
如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

【资料变更】

请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

【接口变更】

请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

【功能验证】

说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤
新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 rain-666 的贡献)
rain-666rain-666成员
4 天前 创建了 pull request,commit 70c4f70a
atomgit-bot
atomgit-bot
4 天前 评论:

变更摘要

该 PR 针对 inductor 的完全静态 Welford 优化在存在原地内存更新(inplace update)场景下可能产生的正确性问题进行了修复。核心改动是在 torch_npu/_inductor/codegen/triton.py 中新增缓冲区变异风险分析逻辑,并在索引最终确定阶段重新评估 full_static_welford_reduction,从而在检测到跨归约且被原地修改的缓冲区时禁用完全静态 Welford 优化;同时在 test/_inductor/test_var_mean.py 中新增了对应的回归测试用例。

主要改动

  • 新增 _full_static_welford_mutation_hazards 分析方法: 在 NPUIndexTritonKernel 中遍历调度节点,分别收集归约前/后使用的缓冲区、Welford 源/后置缓冲区、别名组与被修改缓冲区,并通过别名闭包展开计算跨归约且被修改的缓冲区集合,作为完全静态 Welford 的变异风险来源。
  • 新增 finalize_indexing 覆盖逻辑: 在索引最终确定阶段,当缓冲区复用与原地更新已经确定但加载代码尚未生成时,重新调用 _full_static_welford_mutation_hazards,若存在变异风险则关闭 full_static_welford_reduction
  • 调整完全静态 Welford 的判定条件: 原逻辑在 self.mutations 非空时直接禁用完全静态 Welford,现改为仅依据归约轴长度判断其静态性,再结合后续变异风险分析决定是否真正启用。
  • 新增回归测试 test_welford_full_static_mutation_hazard: 构造一个归约输入被原地复用/更新的 layer norm 场景,验证结果正确性,并断言生成代码中不存在 'vectorized_welford_axis':,同时包含 mutated_arg_names': ['in_out_ptr
likedislike
atomgit-bot
atomgit-bot
4 天前 评论:

代码审查

审查总结

我已逐一审查了本次变更的两个文件:

  • test/_inductor/test_var_mean.py:新增的 test_welford_full_static_mutation_hazard 测试逻辑合理(构造了 pre_norm 同时作为 reduction 输入、后置计算输入并发生 in-place mutation 的场景),断言覆盖了优化被禁用(vectorized_welford_axis 不存在)与数值一致性检查。未发现需要报告的问题
  • torch_npu/_inductor/codegen/triton.py:新增的 _full_static_welford_mutation_hazards 分析逻辑本身(inside_reduction 初始值、别名传递闭包、cross_reduction_buffers & mutated_buffers 判定)经核对对目标场景分类正确;主要风险在于 finalize_indexing 中重算 full_static_welford_reduction 的执行时机相对 create_inductor_meta(line 2356 读取该标志)不确定。

问题统计

  • P0:0
  • P1:0
  • P2:1
  • P3:0

整体风险判断:本次修复方向正确、分析逻辑基本完备,但存在一个中等风险点——hazard 重算依赖基类 finalize_indexing 的调用时机,而该时机在当前文件中不可见且 codegen_body/codegen_kernel 均被完全重写、未调用 super(),可能导致 meta 与循环消除决策不一致或优化未被真正禁用。建议显式保证重算发生在 create_inductor_meta 之前并补充对应验证。

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

⛔ 需要修改

likedislike
ascend-robotascend-robot成员
4 天前 添加了label:needs-issue
此处折叠了43条消息 查看更多
ascend-robot
ascend-robot成员
4 天前 评论:

The MR can not be merged, because of CodeReview discussion not resolved

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

likedislike
rain-666rain-666成员
4 天前 解决了最后一个问题
ascend-robotascend-robot成员
4 天前 合入了pull request
ascend-robot
ascend-robot成员
4 天前 评论:

Pull Request 已合并或已关闭。

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

likedislike
ascend-robot
ascend-robot成员
4 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#14124 [ commitID:7414dd8c ] 已完成
likedislike