Pull Request已成功合入, 合并人@ascend-robot
(感谢 SorryNaCN 的贡献)变更摘要
此 PR 为 DVM 后端引入 MatMul 模板融合能力,通过新增 enable_matmul_fusion 配置开关,为 aten.mm、aten.bmm、aten.addmm 和 aten.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下沉为mul与add组合,避免生成不必要的 matmul 调用。 -
baddbmm的补齐与op_emitter调整:在fx_pass.py的annotate_mm_transpose_flags中扩展支持baddbmm;在op_emitter.py中将mm_rule和addmm发射器统一处理baddbmm,移除对小输出的限制(删除check_output),并修正alpha/beta语义的 codegen 路径。 -
配置与入口控制:在
config.py中新增enable_matmul_fusion,默认关闭,仅通过环境变量INDUCTOR_DVM_ENABLE_MATMUL_FUSION=1启用;mlir_fusion.py的DvmMlirFusionPatch仅在开关打开时调用patch_dvm_matmul_template_fusion(),未启用时保持原有路径不变。


代码审查
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 |
💬 仅评论


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




【合入来源】
关联图模式 Issue:https://gitcode.com/Ascend/pytorch/issues/1978
移植来源:https://gitcode.com/Ascend/pytorch/merge_requests/40027
【修改方案】
在
torch_npu._inductor.dvm.config中增加enable_matmul_fusion。该开关默认关闭,仅在设置INDUCTOR_DVM_ENABLE_MATMUL_FUSION=1时注册 DVM matmul template lowering,未启用时保持原有 DVM/Inductor 路径不变。为
aten.mm、aten.bmm、aten.addmm与aten.baddbmm注册 DVM template lowering,并新增DvmTemplateBuffer保存 matmul template FX 图及逻辑 placeholder 到实际 IR 输入的绑定关系。生成 wrapper 调用前恢复真实参数;不满足 DVM shape/type 规则的场景继续走原有 fallback。保留
addmm的原始算子形态以进入 template lowering,补齐baddbmm的转置标注、图构建参数透传和 DVM codegen;保持addmm/baddbmm的alpha、beta语义。K=1 的mm、bmm下沉为逐元素mul,K=1 的addmm下沉为mul与add组合。在
NpuDvmScheduling的 template codegen 中复用NpuMetaScheduling的 traced-graph 构图与回退能力。DVM matmul template 仅支持合法 pointwise epilogue 的纵向融合;prologue、horizontal fusion、reduction、template-to-template、group/numel 不一致及不支持的 broadcast 场景均不融合。移除
_is_view_only_graph对纯view、reshape、_unsafe_viewepilogue 的额外拒绝逻辑,统一由既有 pointwise、shape、依赖和广播合法性检查决定是否进入 DVM template 融合路径。补充
mm、bmm、addmm、baddbmm的 template fusion 回归覆盖,并覆盖 K=1、multi-user 输出、view/view+pointwise epilogue、view 输入及同一 buffer 多 view 场景。【资料变更】
不涉及。
【接口变更】
不涉及客户可见接口变更。
【功能验证】
python3 -m py_compile,通过。git diff --check,通过。性能收益
该优化将 matmul 与下游 pointwise/view 融合为单个 DVM mix kernel,减少中间张量读写和 kernel launch。以下为 v2.9.0 同功能实测(端到端统计已排除首次编译与 warm-up):
GLM-4-9B Chat 对应吞吐约提升 9.34%。GPT-OSS-20B 的 device kernel duration 为 -0.62%,当前不作为性能收益结论。
详细测试配置与完整数据见:https://gitcode.com/Ascend/pytorch/merge_requests/40027
【CheckList】