Merged
优化mc2 fusionpass,避免无锚点时多余匹配空转 #13266
优化mc2 fusionpass,避免无锚点时多余匹配空转 #13266
Merged
chenyifanAdd on Sep 30
chenyifan
chenyifanCollaborators
Sep 30

描述

优化 MC2 Transpose 相关 Graph Fusion Pass:无锚点图跳过 pattern 匹配空转,并收敛公共解析逻辑。

背景与原因

PatternFusionPass 以全局 Stage 注册(REG_FUSION_PASS),即使目标图不含 MC2 锚点算子,仍会进入 Patterns() / MatchNext。本批 Pass pattern 组合多(数十~上百种),再叠加 FE 多轮图编译,无锚点场景下会出现明显空转开销。

改动方法

  1. Run 前置剪枝(8 个 Pass)
    重写 PatternFusionPass::Run:对 graph.GetDirectNode() 做锚点 op type 探测,无锚点则直接 return SUCCESS,跳过 Patterns() / 匹配;有锚点则回落到 PatternFusionPass::Run。
Pass 锚点类型
AllGatherMatmulV2TransposeA5FusionPass AllGatherMatmulV2
MatmulReduceScatterV2TransposeA5FusionPass MatmulReduceScatterV2
AllToAllMatmulTransposeA5FusionPass AlltoAllMatmul
MatmulAllToAllTransposeA5FusionPass MatmulAlltoAll
MatmulAllReduceTransposeA5FusionPass MatmulAllReduce
MatmulAllReduceTransposeFusionPass MatmulAllReduce / ARN / InplaceARN
AlltoAllvQuantGroupedMatMulTransposeFusionPass AlltoAllvQuantGroupedMatMul
QuantGroupedMatMulAlltoAllvTransposeFusionPass QuantGroupedMatMulAlltoAllv
  1. 公共轻量 utils(新增 mc2/common/utils/mc2_fusion_pass_utils.h,header-only)

    • GraphHasAnyOpType:Run 剪枝用
    • GetPatternNameStr / GetCapturedMc2Node:去掉各 Pass 本地薄封装,统一走 ops::
  2. 同路径去重解析
    MeetRequirements / BuildReplaceGraph 入口解析一次 patternNameStr(及需要时的 MC2 节点),向下传递,避免同一路径内多次读 MatchResult。

  3. 注释清理
    去掉「对齐 canndev」类溯源前缀,只保留功能说明;删除过时悬空注释。

行为影响

  • 功能语义不变:Meet / Replace 的 perm 校验、attr flip/set、Bitcast 保留等逻辑未改。
  • 性能:无对应锚点的图不再做本批 Pass 的 pattern 枚举与匹配;有锚点路径与改前一致(多一次轻量 GetDirectNode 扫描)。
  • 范围:仅 GetDirectNode 直接节点;子图内锚点不在本剪枝覆盖内(与当前 FE 目标图用法一致)。

涉及文件

  • 新增:mc2/common/utils/mc2_fusion_pass_utils.h
  • 修改:上述 8 个 Pass 的 .h / .cpp(声明/实现 Run,调用 utils,去重解析)

关联的Issue

https://gitcode.com/cann/ops-transformer/issues/5834

测试

A2/A3/A5 RDV

文档更新

无文档更新。

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for chenyifan's contribution.)
chenyifanchenyifanCollaborators
Sep 30 Created pull request, commit 7cd34f3c
CANN-robotCANN-robotCollaborators
Sep 30 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 30 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
mc2 ✅ huangshuai59, wang-minbo (2/2) ✅ huangshuai59, wang-minbo (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

mutex_lock, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
Sep 30 Set captainmiaow,monologue815,jiang-lirui,Allan_Yu,wang-minbo,tgwsakiko_,liudan12,chenjunjian11,yangzeheng,liuboxi,luobaiqing,zhong-zixin,libohao6,mabing1118,macech as approver
11 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Oct 8 Removed label: stat/needs-squash
CANN-robot
CANN-robotCollaborators
Oct 8 Comments:

Notification

This pull request has been changed(code update) or closed, so removes the following label(s): stat/needs-squash.

likedislike
CANN-robotCANN-robotCollaborators
Oct 8 Added label: cann-cla/yes
CANN-robotCANN-robotCollaborators
Oct 8 Set huangshuai59 as approver
CANN-robotCANN-robotCollaborators
Oct 8 Set huangshuai59 as approver
chenyifanchenyifanCollaborators
Oct 8 Modify description of pull request
chenyifanchenyifanCollaborators
Oct 8 Draft state has been cancelled
chenyifan
chenyifanCollaborators
Oct 8 Comments:

compile

likedislike
chenyifanchenyifanCollaborators
Oct 8 Pre merge succeeded (commit_id: a8cb6ade593724f470413d56f8d6ff4aba829aec)
CANN-robot
CANN-robotCollaborators
Oct 8 Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
Oct 8 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Oct 8 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Oct 8 Added label: ci-pipeline-passed
chenyifanchenyifanCollaborators
Oct 9 Change title to "优化mc2 fusionpass,避免无锚点时多余匹配空转", originally titled "优化fusionpass,避免无锚点时多余匹配空转".
chenyifan
chenyifanCollaborators
Oct 9 Comments:

代码检视报告

检视概览

项 内容
代码文件 commit 68ae33008 变更 17 文件(MC2 Transpose Fusion Pass + mc2_fusion_pass_utils.h)
侧别 Host / GE Fusion Pass(类比 Tiling/Host,非 Kernel)
匹配规则文档 cpp-secure.md、cpp-general.md、cpp-style.md
总条例数(本次命中检视) 28(含风格专项;不含 AscendC Kernel/API 条例)
设计文档 未做 design-check(范围限定变更 + C++ 规则)
检视时间 2026-10-09 16:20
检视范围 仅 diff 变更部分

变更摘要

  • 新增 mc2/common/utils/mc2_fusion_pass_utils.h:GraphHasAnyOpType / GetPatternNameStr / GetCapturedMc2Node
  • 各 Pass 重写 Run():无锚点算子时早退,避免 Patterns() 空转
  • 删除各文件重复 helper,上提 patternNameStr 复用
  • 伴随大量指针/引用声明格式调整(Type * → Type*)

检视统计

判定 数量 说明
PASS 22 安全/通用命中条例无违规或变更未引入风险
FAIL 0 安全/通用无 ≥70% 自信值违规

PASS 摘要(C++ 安全 / 通用)

条例 判定 说明
cpp-secure 1.2 内存安全 PASS Run() 对 graph == nullptr 防护;matchResult 解引用沿用框架回调约定
cpp-secure 7.1 禁止空指针构造 string PASS GetPatternNameStr 对 GetString() 判空,相对旧版直接赋值是改进
cpp-secure 8.2 LOG 格式占位符 PASS OP_LOGE_IF(..., "%s", errMsg) 数量/类型一致
cpp-general 1.3 清理冗余 PASS 删除重复 helper,上提 patternNameStr,减少重复取名
cpp-general 2.2/2.3 头文件 PASS utils 头无循环依赖;include 均为所用类型/宏
cpp-general 2.6 using namespace PASS 头文件未 using namespace
cpp-general 10.2/10.4 智能指针 PASS 继续使用 unique_ptr<MatchResult>,无裸 new/delete
cpp-general 10.6 const 形参 PASS 只读图/节点使用 const 引用
cpp-general 13.3 override PASS 新增 Run(...) override
cpp-general 14.1 RAII PASS 无额外裸资源管理
cpp-general 15.2/15.3 传参 PASS 入参 const T&、出参 T&;unique_ptr 按引用观察不转移所有权
cpp-general 7.1 reinterpret_cast PASS(范围外) perm 解析中 reinterpret_cast 为既有逻辑,本提交仅改空格附着,未新增转换

其余命中但与本次 diff 无关或无触发特征的条例(如 new/delete、安全函数库、switch default 等)未展开,默认不适用或未命中。


结论

功能向改动(公共工具抽取 + 无锚点早退)符合 C++ 通用/安全主线,未见 HIGH 级安全 FAIL。


附录:检视文件

  • mc2/common/utils/mc2_fusion_pass_utils.h(新建)
  • mc2/all_gather_matmul_v2/op_graph/fusion_pass/all_gather_matmul_v2_transpose_a5_fusion_pass.{h,cpp}
  • mc2/allto_all_matmul/op_graph/fusion_pass/all_to_all_matmul_transpose_a5_fusion_pass.{h,cpp}
  • mc2/allto_allv_quant_grouped_mat_mul/op_graph/fusion_pass/allto_allv_quant_grouped_mat_mul_transpose_fusion_pass.{h,cpp}
  • mc2/matmul_all_reduce/op_graph/fusion_pass/matmul_all_reduce_transpose_a5_fusion_pass.{h,cpp}
  • mc2/matmul_all_reduce/op_graph/fusion_pass/matmul_all_reduce_transpose_fusion_pass.{h,cpp}
  • mc2/matmul_allto_all/op_graph/fusion_pass/matmul_all_to_all_transpose_a5_fusion_pass.{h,cpp}
  • mc2/matmul_reduce_scatter_v2/op_graph/fusion_pass/matmul_reduce_scatter_v2_transpose_a5_fusion_pass.{h,cpp}
  • mc2/quant_grouped_mat_mul_allto_allv/op_graph/fusion_pass/quant_grouped_mat_mul_allto_allv_transpose_fusion_pass.{h,cpp}
likedislike
huangshuai59
huangshuai59Collaborators
Oct 10 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Oct 10 Added label: approved
wang-minbo
wang-minboCollaborators
Oct 10 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Oct 10 Added label: lgtm
CANN-robotCANN-robotCollaborators
Oct 10 Closed associated issue
CANN-robotCANN-robotCollaborators
Oct 10 Merged pull request