已合并
feat: support DVM MM template fusion #42505
feat: support DVM MM template fusion #42505
已合并
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 189085ba
SorryNaCNSorryNaCN成员
7月23日 关联了issue:[Feature]: 26.1.0版本图模式功能增强
atomgit-bot
atomgit-bot
7月23日 评论:

变更摘要

此 PR 为 DVM(Ascend MLIR 后端)引入 matmul template 融合能力,通过新增 enable_matmul_fusion 配置开关(默认关闭,由环境变量 INDUCTOR_DVM_ENABLE_MATMUL_FUSION=1 控制),为 aten.mmaten.bmmaten.addmmaten.baddbmm 注册 DVM template lowering。新增 DvmTemplateBuffer 保存 matmul template FX 图及逻辑 placeholder 到实际 IR 输入的绑定关系,支持合法的 pointwise epilogue 纵向融合,并处理 K=1 时的下沉优化和 alpha/beta 语义保持。


主要改动

  • 新增 template.py 模块:引入 DvmTemplateBuffer 类(继承 ir.TemplateBuffer)及 patch_dvm_matmul_template_fusion() 入口函数,为四种 matmul 算子注册 DVM template lowering;K=1 时 mm/bmm 下沉为 muladdmm 下沉为 mul + add 组合;同时实现 can_fuse_dvm_epilogue() 控制仅允许合法 pointwise epilogue 的纵向融合。

  • mlir_fusion.py 调度器扩展NpuDvmScheduling 新增 codegen_template() 方法,在 codegen 阶段恢复 DvmTemplateBuffer.input_bindings 中的真实参数;can_fuse_vertical()can_fuse_horizontal() 分别处理 template 节点的融合规则(仅允许 epilogue 纵向融合,禁止 template 间及 horizontal 融合);条件启用由 enable_matmul_fusion 控制。

  • op_emitter.py 规则与代码生成调整mm_rule 扩展以支持 aten.baddbmm.default,并移除原有的小输出尺寸(SMALL_OUTPUT_MAX)拒绝逻辑;matmul_bias 签名简化,addmm 函数参数统一为 inp/mat1/mat2 并同时注册到 aten.baddbmm.default,确保 alpha/beta 语义在 template 路径中正确保留。

  • graph_build.pyfx_pass.py 补充DvmCodegenInterpreterktype 初始化逻辑抽取为 set_kernel_ktype() 方法,mix kernel 类型判断与 spec 回退逻辑明晰化;annotate_mm_transpose_flagsaten.baddbmm.default 纳入转置标注范围,使其与 addmm 共享统一的 bias/转置处理路径。

  • config.py 新增配置项:添加 enable_matmul_fusion 布尔开关,由环境变量 INDUCTOR_DVM_ENABLE_MATMUL_FUSION 控制,默认关闭,仅在显式设置时启用 DVM matmul template 路径。

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

代码审查

审查总结

审查了全部 7 个变更文件:

文件 审查结果
test/_inductor/test_dvm_mlir_fusion.py 2 个问题:环境变量清理回退 (P2),断言弱化 (P2)
torch_npu/_inductor/dvm/config.py 无问题
torch_npu/_inductor/dvm/fx_pass.py 无问题
torch_npu/_inductor/dvm/graph_build.py 1 个问题:set_kernel_ktype 优先级变更 (P2)
torch_npu/_inductor/dvm/mlir_fusion.py 无问题
torch_npu/_inductor/dvm/op_emitter.py 1 个问题:移除 check_output 保护 (P3)
torch_npu/_inductor/dvm/template.py 1 个问题:_DvmTemplateGraph 跳过 super().__init__() (P3)

问题统计:

  • P2: 3 个
  • P3: 2 个
  • P0/P1: 0 个

整体风险评估:中等偏低。 主要风险集中在:

  1. 测试代码中环境变量清理缺少异常保护,可能在测试失败时污染后续测试环境;
  2. set_kernel_ktype 中 "mix" 和 "spec" 的优先级变更可能与 DVM runtime 的 kernel 类型能力不完全匹配;
  3. 测试断言从精确计数弱化为存在性检查,降低了回归检测能力。

核心逻辑(template fusion、codegen、lowering)无明显正确性问题,参数传递和语义保持正确。

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

💬 仅评论

likedislike
此处折叠了65条消息 查看更多
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#13176 [ commitID:97990755 ] 已完成
likedislike