Merged
[perf] [master] Optimize DVM GroupNorm, matmul, allocation and optional permute lowering #47449
SorryNaCNAdd on Sep 30
[perf] [master] Optimize DVM GroupNorm, matmul, allocation and optional permute lowering #47449
Merged
Pull Request has been successfully merged by ascend-robot.
(Thank you for SorryNaCN's contribution.)Sep 30 Associated issue: benchmark网络验证
atomgit-bot
Sep 30 Comments:
Sep 30 Comments:
变更摘要
本改动主要围绕 torch_npu/_inductor/dvm/decomp.py 中的 DVM 分解配置:一方面将 aten.native_group_norm 从 decomps_to_exclude_npu 排除列表中移除,使 GroupNorm 交由 DVM 分解而非保留给 CANN;另一方面在 patch_decomp() 初始化阶段导入并调用 _override_matmul_should_fold_for_npu,复用已有的 matmul 折叠覆盖逻辑来做 matmul folding,避免在 DVM 中重复实现。改动目标是降低 GroupNorm 与广播 BMM 相关模型的设备时间,并保持算子精度。
主要改动
- 启用
native_group_norm分解: 从decomps_to_exclude_npu中删除aten.native_group_norm,该算子不再被排除,从而参与 DVM 分解流程。 - 复用 matmul 折叠覆盖: 在
patch_decomp()中新增from ..decomposition import _override_matmul_should_fold_for_npu导入,并调用_override_matmul_should_fold_for_npu()完成 matmul folding 覆盖。 - 覆盖调用位置: 该调用位于
_disable_cia_decompositions()与remove_decompositions(inductor_decomp.decompositions, decomps_to_exclude_npu)之前,使覆盖在 DVM 分解初始化阶段即生效。


Incorrect?
atomgit-bot
Sep 30 Comments:
Sep 30 Comments:
Sep 30 Added label: ascend-cla/yes
109 messages are collapsed here。 See more
ascend-robot
Oct 9 Comments:
Oct 9 Comments:
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.


Oct 9 Resolved final issue
Oct 9 Merged pull request
ascend-robot
Oct 9 Comments:
Oct 9 Comments:
Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


AtlasAccount
Oct 9 Comments:
Oct 9 Comments:
流水线 pytorch_gitcode_PR_multiVersion-onetrack#287 [ commitID:3e76f0ec ] 已完成


【合入来源】
DVM 性能优化,合入目标为
master。关联社区 Issue:[perf] DVM GroupNorm、matmul、内存分配与可选 permute lowering 优化。Fixes https://gitcode.com/Ascend/pytorch/issues/5282
【修改方案】
本 PR 涉及
torch_npu/_inductor/dvm/的config.py、decomp.py、mlir_fusion.py和新增的wrapper.py。aten.native_group_norm,默认展开其计算,使 Inductor / DVM 可以继续进行融合。patch_decomp()初始化阶段复用_override_matmul_should_fold_for_npu(),将适用的 3D×2D matmul 转为 MM,减少权重广播和 BMM 开销。NpuDvmWrapperCodeGen,继承NpuMlirWrapperCodeGen并在 DVM 后端注册中使用。普通 NPU 分配直接复用 Triton-ex 的NPUWrapperCodeGen.make_allocation,生成_empty_strided_npu调用;非 NPU 分配、内存生命周期跟踪和 pinned 分配沿用父类路径,子图沿用父类创建路径。aten.permute固定放入生成白名单,新增默认关闭的enable_fusion_permute,参照enable_cat控制 DVM 融合规则。开启后可生成reinterpret_tensor视图。详细设计:组件关系与执行顺序
patch_decomp()→ 复用 NPU matmul folding 覆盖并更新分解配置 → Inductor 分解和调度 → DVM 融合与代码生成。NpuDvmWrapperCodeGen→ 分配条件判断 → 复用 Triton-ex 快速分配或调用父类分配逻辑。类关系为NpuDvmWrapperCodeGen继承NpuMlirWrapperCodeGen,按条件复用NPUWrapperCodeGen.make_allocation。GENERATE_LIST注册aten.permute→ 融合规则读取enable_fusion_permute→ 开启时允许进入 DVM lowering,关闭时由规则拒绝 DVM 融合。Triton-ex 文件及
_generate_extern_kernel_alloc_helper不在本 PR 修改范围。【资料变更】
不涉及独立资料文件变更。设计说明及新增配置的用法在本 PR 的【修改方案】和【接口变更】中给出。
【接口变更】
新增 DVM 配置
torch_npu._inductor.dvm.config.enable_fusion_permute,类型为bool,默认值为False。在 DVM 后端加载前设置为True可开启 permute 融合;保持False时禁用该融合规则。跨组件复用现有
_override_matmul_should_fold_for_npu和NPUWrapperCodeGen.make_allocation,其接口签名不变。未新增公开算子或 C++ 接口。【功能验证】
以下为原 PR 已记录的验证结果,本次仅整理描述,未重新运行测试。
测试环境与方法
rtol=atol=1e-4。aten.permute调用为 0。git diff --check通过。验证范围与待补充项
3e76f0ecf45429273b2a20a2fc7c2a1b9898c6f2的流水线PR-pipeline_pytorch#71518已通过,代码检查无异常;流水线结果不代表上述 GroupNorm 全网严格精度问题已解决。【CheckList】
自检依据:新增配置已有注释,分配与融合路径包含相应条件判断;空指针及新增错误日志不涉及。标题使用
[perf]类型标签;当前提交的 CI 和代码检查已通过。