已合并
fix: 优化Gather Reduce融合场景核数惩罚 #1314
fix: 优化Gather Reduce融合场景核数惩罚 #1314
已合并
zhang_shengjie创建于 7月17日
zhang_shengjie成员
7月17日

描述

一、主要解决的问题

Gather 与 Reduce 融合且拆分为多个串行 Schedule Group 时,Reduce Group 沿 R 轴切分仍按 128B Cache Line 计算核数惩罚,生成 Rational(1, 32),倾向仅使用约 2 核。Gather 容易受 Vector Bound 限制,该场景需要放宽 Reduce 核数惩罚,以提升整体并行度。

二、修改方案

  1. ATT 识别非 Group Parallel、多 Schedule Group、包含 Gather 且当前 Group 包含 Reduce 的场景。
  2. 将该 Reduce Group 的核数惩罚粒度从默认 Cache Line 调整为 32B,使惩罚系数由 Rational(1, 32) 调整为 Rational(1, 8)
  3. Gather 转换为 Load 后仍按原始 op type 判断,避免 compute_type 被改写后规则失效。
  4. 增加 DEBUG 级别决策日志,输出命中状态、未命中原因和实际惩罚粒度。
  5. 补充正向、反向、Group Parallel、单 Group、无 Gather 及 Gather 已转换为 Load 的回归用例。

三、代码修改流程图

flowchart TD
    A[遍历 Schedule Group] --> B{非 Group Parallel 且多 Group}
    B -->|否| F[使用默认 Cache Line]
    B -->|是| C{当前 Group 包含 Reduce}
    C -->|否| F
    C -->|是| D{原始 op type 包含 Gather}
    D -->|否| F
    D -->|是| E[Reduce 惩罚粒度设为 32B]
    E --> G[生成 Rational 1/8 核数惩罚]
    F --> H[生成默认核数惩罚]

变更类型

关联的Issue

无。

如何测试

一、测试用例说明

1.1 单元测试

  • ATT 全量 UT:1002/1002 PASS。
  • Optimize Gather UT:1/1 PASS。
  • 覆盖 Gather + Reduce 多 Group 命中 Rational(1, 8)
  • 覆盖 Gather 的 compute_type 已转换为 Load 后仍命中 Rational(1, 8)
  • 覆盖 Group Parallel、单 Reduce Group、多 Reduce Group 无 Gather 时保持 Rational(1, 32)

1.2 系统测试

  • Backend V2 Gather Reduce codegen/E2E ST:2/2 PASS。

核对清单

其他信息

验证方法

  • clang-format:PASS。
  • pre-commit:PASS。
  • OAT:PASS。

注意事项

  • 仅对非 Group Parallel 的 Gather + Reduce 多 Group 场景生效。
  • 不修改 Schedule 阶段 Gather 转 Load 的既有逻辑。
  • 运行 Backend E2E 时需保证 CANN 运行库可被动态链接器找到。

提交记录

Commit 描述 修改文件数
99b361cd 调整 Gather Reduce 融合核数惩罚粒度 5
bfe1faf5 补充 Gather Reduce 核数惩罚多 Group 用例 2
f5f1ccba 补充 Gather Reduce 核数惩罚维测日志 2
78b79a43 按 Gather 原始类型应用 Reduce 核数惩罚 2

修改文件清单

文件路径 修改类型 说明
autofuse/att/gen_model_info/gen_model_info.cpp 修改 识别 Gather Reduce 多 Group 并配置惩罚粒度及日志
autofuse/att/gen_model_info/gen_model_info.h 修改 增加惩罚配置参数接口
autofuse/att/gen_model_info/parser/tuning_space.h 修改 保存核数惩罚 Cache Line 粒度
autofuse/att/expr_gen/generate_tiling_expr.cpp 修改 使用场景化惩罚粒度生成核数约束
autofuse/v35/optimize/graph_pass/gather_to_load.cpp 修改 补充 Gather 转 Load 的 DEBUG 日志
autofuse/tests/ut/att/testcase/gen_model_info/parser/test_ascend_graph_parser.cpp 修改 增加 Gather Reduce 多 Group 回归用例
autofuse/tests/ut/att/testcase/expr_gen/test_generate_tiling_expr_penalty.cpp 修改 增加惩罚表达式生成用例
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 zhang_shengjie 的贡献)
Zzhang_shengjie成员
7月17日 创建了 pull request,commit 62f2e1e6
atomgit-bot
atomgit-bot
7月17日 评论:

变更摘要

此 PR 修复了 Gather 与 Reduce 融合且拆分为多个串行 Schedule Group 时,Reduce Group 沿 R 轴切分的核数惩罚过重的问题。原逻辑按 128B Cache Line 计算惩罚系数 Rational(1, 32),导致仅使用约 2 核。修改后,在特定场景下将 Reduce 核数惩罚粒度从默认 Cache Line 调整为 32B,使惩罚系数放宽至 Rational(1, 8),提升整体并行度。场景识别条件包括:非 Group Parallel、多 Schedule Group、原始图中包含 Gather 算子(即使已降级为 Load 仍按原始 op type 判断)、当前 Group 包含 Reduce。同时在 TuningSpace 中新增 penalty_cache_line_size 字段支持独立粒度配置,并增加 DEBUG 级别决策日志及回归测试。

主要改动

  • TuningSpace 新增 penalty_cache_line_size 字段:在 tuning_space.h 中增加 uint32_t penalty_cache_line_size{0},允许 Reduce 核数惩罚使用独立于物理 Cache Line 的粒度,0 表示沿用默认配置。
  • CalcPenaltyCoreNumRatio 支持独立惩罚粒度:在 generate_tiling_expr.cpp 中,核数惩罚计算优先使用 tuning_space_->penalty_cache_line_size,若为 0 则回退到 GetCacheLineSize(),使特殊融合场景可覆盖默认粒度。
  • 新增 ShouldEnableGatherReducePenalty 场景识别函数:在 gen_model_info.cpp 中新增该函数及辅助函数 HasComputeTypeHasGatherNode,按条件判断是否启用 32B 放宽惩罚,并在各分支输出 DEBUG 日志说明命中/未命中原因。
  • GenerateModelInfo 重载扩展:新增接受 enable_gather_reduce_penalty 参数的重载版本,在 ProcessAndSetScheduleGroupInfo 中调用场景识别函数并传递给下游,同时保留原始单参接口向后兼容。
  • GatherToLoadPass 保留原始 compute_type:在 gather_to_load.cpp 中先保存 original_compute_type 再改写为 kComputeLoad,确保后续 HasGatherNode 按原始 op type(而非 Load)判断 Gather 是否存在,并在日志中输出类型变更详情。
likedislike
不准确?
atomgit-bot
atomgit-bot
7月17日 评论:

代码审查

经过对所有 7 个变更文件的逐一审查,包括调用链分析、边界条件检查和测试覆盖率验证,以下是审查结论:

Closing Summary

  • P0: 0
  • P1: 0
  • P2: 0
  • P3: 1(gather_to_load.cppint32_t%d 格式说明符的类型可移植性隐患,confidence 0.3)

Overall risk judgment: 此变更风险较低。核心逻辑正确:Gather+Reduce 融合场景的惩罚粒度判断链完整(非 Group Parallel → 多 Group → 当前 Group 含 Reduce → 全局存在 Gather),且 HasGatherNode 使用 op type 而非 compute_type 来识别已 Lower 为 Load 的 Gather 节点,与 PR 描述一致。新增测试覆盖了正向、Gather→Load、Group Parallel、单 Group、无 Gather 等回归场景。TearDown 新增 PlatformContext::Reset() 调用属于良好的测试清理实践。

各文件审查结果

文件 结论
autofuse/att/gen_model_info/expr_gen/generate_tiling_expr.cpp no issues
autofuse/att/gen_model_info/gen_model_info.cpp no issues
autofuse/att/gen_model_info/gen_model_info.h no issues
autofuse/att/gen_model_info/parser/tuning_space.h no issues
autofuse/tests/ut/att/testcase/gen_model_info/expr_gen/test_generate_tiling_expr_penalty.cpp no issues
autofuse/tests/ut/att/testcase/gen_model_info/parser/test_ascend_graph_parser.cpp no issues
autofuse/v35/optimize/graph_pass/gather_to_load.cpp 1 个 P3 建议

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
不准确?
CANN-robotCANN-robot成员
7月17日 添加了label:stat/needs-squash
CANN-robotCANN-robot成员
7月17日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
7月17日 评论:

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


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
repo-cann/graph-autofusion zhanj, xchu42, wangxiaotian995 (3/2) zhanj (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robot成员
7月17日 将yangyongqiang0606,xchu42,depeng1994,wqtshg_wt,peiyang,shengnan666,XuebinYang,sjtulxh,zhanj,zhujingjing,xuyafei,wangxiaotian995设为评审人
CANN-robotCANN-robot成员
7月17日 将yangyongqiang0606,xchu42,depeng1994,wqtshg_wt,peiyang,shengnan666,XuebinYang,sjtulxh,zhanj,zhujingjing,xuyafei,wangxiaotian995设为审查人
zhang_shengjie成员
7月17日 评论:

compile

likedislike
Zzhang_shengjie成员
7月17日 update merge request[project id: 8005834, iid: 1314, commit_id: 776e08294c5f59dba71d7dcc19d27f7b78c5d7db] virtual merging success
CANN-robotCANN-robot成员
7月17日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月17日 评论:

流水线任务触发成功
任务链接 [5118f17432234a70a9a4e86c9b5ac80a][流水线指导]

任务名称状态日志下载链接
UT_Test_Python_superkernel ✅ SUCCESS >>>>>
ST_Test_Python_superkernel ✅ SUCCESS >>>>>
UT_Test_superkernel ✅ SUCCESS >>>>>
UT_Test_autofuse_framework ✅ SUCCESS >>>>>
ST_Test_autofuse_framework ✅ SUCCESS >>>>>
UT_Test_autofuse_ascendc_api ✅ SUCCESS >>>>>
ST_Test_autofuse_ascendc_api ✅ SUCCESS >>>>>
ST_Test_autofuse_e2e ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>

[2026-07-17 13:38:54]    CI执行结束

likedislike
CANN-robot
CANN-robot成员
7月17日 评论:

流水线任务触发成功
任务链接 [b464a4e0ccc54ff7bb634b98db25a9fa][流水线指导]

任务名称状态日志下载链接
codecheck ✅ SUCCESS >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_checkpr ✅ SUCCESS
pre_comment ✅ SUCCESS >>>>>
codecheck_codestyle ⚠️ WARNING >>>>>
codecheck_precommit ✅ SUCCESS >>>>>

[2026-07-17 13:26:55]    CI执行结束

likedislike
CANN-robot
CANN-robot成员
7月17日 评论:

流水线任务触发成功
任务链接 [33b2fd1a14ce4ef4b2eee4c7591e578e][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
pre_comment ✅ SUCCESS >>>>>
PreSmoke_A900_npupool ✅ SUCCESS >>>>>

[2026-07-17 13:34:05]    CI执行结束

likedislike
CANN-robotCANN-robot成员
7月17日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
7月17日 添加了label:ci-pipeline-passed
wangxiaotian995成员
7月17日 评论:

/lgtm

likedislike
xchu42
xchu42成员
7月17日 评论:

/lgtm

likedislike
CANN-robotCANN-robot成员
7月17日 添加了label:lgtm
zhanj
zhanj成员
7月18日 评论:

/approve

likedislike
CANN-robotCANN-robot成员
7月18日 添加了label:approved
CANN-robotCANN-robot成员
7月18日 合入了pull request