已合并
feat: support DVM MM template fusion #42503
feat: support DVM MM template fusion #42503
已合并
SorryNaCN创建于 7月23日
SorryNaCN
SorryNaCN成员
7月23日

【合入来源】

关联图模式 Issue:https://gitcode.com/Ascend/pytorch/issues/1978

移植来源:https://gitcode.com/Ascend/pytorch/merge_requests/40027

【修改方案】

  1. torch_npu._inductor.dvm.config 中增加 enable_matmul_fusion。该开关默认关闭,仅在设置 INDUCTOR_DVM_ENABLE_MATMUL_FUSION=1 时注册 DVM matmul template lowering,未启用时保持原有 DVM/Inductor 路径不变。

  2. aten.mmaten.bmmaten.addmmaten.baddbmm 注册 DVM template lowering,并新增 DvmTemplateBuffer 保存 matmul template FX 图及逻辑 placeholder 到实际 IR 输入的绑定关系。生成 wrapper 调用前恢复真实参数;不满足 DVM shape/type 规则的场景继续走原有 fallback。

  3. 保留 addmm 的原始算子形态以进入 template lowering,补齐 baddbmm 的转置标注、图构建参数透传和 DVM codegen;保持 addmm/baddbmmalphabeta 语义。K=1 的 mmbmm 下沉为逐元素 mul,K=1 的 addmm 下沉为 muladd 组合。

  4. NpuDvmScheduling 的 template codegen 中复用 NpuMetaScheduling 的 traced-graph 构图与回退能力。DVM matmul template 仅支持合法 pointwise epilogue 的纵向融合;prologue、horizontal fusion、reduction、template-to-template、group/numel 不一致及不支持的 broadcast 场景均不融合。

  5. 移除 _is_view_only_graph 对纯 viewreshape_unsafe_view epilogue 的额外拒绝逻辑,统一由既有 pointwise、shape、依赖和广播合法性检查决定是否进入 DVM template 融合路径。

  6. 补充 mmbmmaddmmbaddbmm 的 template fusion 回归覆盖,并覆盖 K=1、multi-user 输出、view/view+pointwise epilogue、view 输入及同一 buffer 多 view 场景。

【资料变更】

不涉及。

【接口变更】

不涉及客户可见接口变更。

【功能验证】

  • 对全部变更 Python 文件执行 python3 -m py_compile,通过。
  • 执行 git diff --check,通过。
  • 完整 NPU 定向用例由 CI 执行。

性能收益

该优化将 matmul 与下游 pointwise/view 融合为单个 DVM mix kernel,减少中间张量读写和 kernel launch。以下为 v2.9.0 同功能实测(端到端统计已排除首次编译与 warm-up):

网络 / 指标(每 step) Template OFF Template ON 时延收益
GLM-4-9B Chat LoRA BF16(batch 1、seq 512,稳态端到端) 279.752 ms 255.837 ms 8.55%
Qwen2-VL-2B-Instruct LoRA BF16(batch 1、max length 512,step 11-51) 773.792 ms 747.664 ms 3.38%
BERT_pytorch BF16(稳态端到端) 23.3781 ms 21.8458 ms 6.55%
GLM-4-9B LoRA BF16(batch 1、seq 128,稳态端到端) 219.3782 ms 204.9709 ms 6.57%
Llama3-8B LoRA BF16(batch 1、seq 128,稳态端到端) 409.4111 ms 393.3233 ms 3.93%

GLM-4-9B Chat 对应吞吐约提升 9.34%。GPT-OSS-20B 的 device kernel duration 为 -0.62%,当前不作为性能收益结论。

详细测试配置与完整数据见:https://gitcode.com/Ascend/pytorch/merge_requests/40027

【CheckList】

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 SorryNaCN 的贡献)
SorryNaCNSorryNaCN成员
7月23日 创建了 pull request,commit 7784bf4c
SorryNaCNSorryNaCN成员
7月23日 关联了issue:[Feature]: 26.1.0版本图模式功能增强
atomgit-bot
atomgit-bot
7月23日 评论:

变更摘要

此 PR 为 DVM 后端引入 MatMul 模板融合能力,通过新增 enable_matmul_fusion 配置开关,为 aten.mmaten.bmmaten.addmmaten.baddbmm 注册 DVM template lowering,并在调度阶段支持合法的 pointwise epilogue 纵向融合。同时处理了 K=1 退化场景的下沉、baddbmm 的转置标注补齐,以及 alpha/beta 语义的保持。

主要改动

  • 新增 DvmTemplateBuffer 与模板融合调度:在 template.py 中引入 DvmTemplateBuffer(继承 ir.TemplateBuffer)和 _DvmTemplateGraph,保存 matmul 模板的 FX 图及逻辑 placeholder 到实际 IR 输入的绑定关系;在 patch_dvm_matmul_template_fusion() 中为 mm/bmm/addmm/baddbmm 注册 lowering,并在 NpuDvmScheduling.codegen_template 中实现 wrapper 调用前恢复真实参数、处理 need_trans_input 转置标记。

  • 融合合法性校验 can_fuse_dvm_epilogue:新增 epilogue 融合判定函数,仅允许 DVM matmul 模板与非模板、非 reduction、且满足 numel 匹配的 pointwise 节点进行纵向融合;拒绝 prologue、horizontal fusion、template-to-template 以及存在不合法 broadcast 的 epilogue。

  • K=1 退化场景处理:在模板 lowering 中对 K=1 的 mm/bmm 下沉为逐元素 mul,对 K=1 的 addmm 下沉为 muladd 组合,避免生成不必要的 matmul 调用。

  • baddbmm 的补齐与 op_emitter 调整:在 fx_pass.pyannotate_mm_transpose_flags 中扩展支持 baddbmm;在 op_emitter.py 中将 mm_ruleaddmm 发射器统一处理 baddbmm,移除对小输出的限制(删除 check_output),并修正 alpha/beta 语义的 codegen 路径。

  • 配置与入口控制:在 config.py 中新增 enable_matmul_fusion,默认关闭,仅通过环境变量 INDUCTOR_DVM_ENABLE_MATMUL_FUSION=1 启用;mlir_fusion.pyDvmMlirFusionPatch 仅在开关打开时调用 patch_dvm_matmul_template_fusion(),未启用时保持原有路径不变。

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

代码审查

Closing Summary

共审查 7 个变更文件,报告 4 个问题

优先级 数量 说明
P2 1 _run_and_get_code_with_dvm 缺少 try/finally 导致环境变量泄漏
P3 3 os.environ.pop 缺少默认值、断言弱化、pass_patterns 硬编码索引脆弱

各文件审查结论:

  • test/_inductor/test_dvm_mlir_fusion.py — 发现 3 个问题(P2×1, P3×2)
  • torch_npu/_inductor/dvm/config.py — 无问题
  • torch_npu/_inductor/dvm/fx_pass.py — 无问题
  • torch_npu/_inductor/dvm/graph_build.py — 无问题
  • torch_npu/_inductor/dvm/mlir_fusion.py — 无问题
  • torch_npu/_inductor/dvm/op_emitter.py — 无问题
  • torch_npu/_inductor/dvm/template.py — 发现 1 个问题(P3×1)

整体风险判断: 本次变更风险较低。核心的 DVM matmul template fusion 逻辑(template 注册、scheduling、codegen)经过仔细审查,没有发现逻辑正确性或安全性问题。最值得关注的是测试辅助方法 _run_and_get_code_with_dvm 中环境变量清理缺少异常保护(P2),可能导致测试间状态污染。其余 P3 问题为防御性改进建议。

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

💬 仅评论

likedislike
此处折叠了67条消息 查看更多
ascend-robotascend-robot成员
7月25日 添加了label:approvedlgtm
ascend-robotascend-robot成员
7月25日 解决了最后一个问题
ascend-robotascend-robot成员
7月25日 合入了pull request
ascend-robot
ascend-robot成员
7月25日 评论:

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月25日 评论:
流水线 pytorch_gitcode_PR_multiVersion#13174 [ commitID:a3f0a81f ] 已完成
likedislike