Merged
perf(pass): Optimize NBuffer and L1 copy reuse compile time #5241
perf(pass): Optimize NBuffer and L1 copy reuse compile time #5241
Merged
CatherieAdd on Aug 3
Catherie
CatherieCollaborators
Aug 3

本 PR 优化 NBufferMerge 和 L1CopyInReuseMerge 在极端 tensor fanin/fanout 图上的编译性能。/mnt/workspace/gitCode/cann/PyPTO_Test/run_conv_case.py 对应图在 ExpandFunction 后有 205,913 个 op,其中存在单个 tensor 连接 32,000 个 producer 和 8,000 个 consumer 的漏斗结构。旧实现会在 pass 内反复展开 op 级生产消费关系,导致两个 graph partition pass 编译耗时明显超标。

issue: https://gitcode.com/cann/pypto/issues/2685

优化点

1. 按 tensor 压缩依赖信息

旧逻辑在 pass 内围绕 op 构造和遍历生产消费关系。对于普通图这类结构可以接受,但在本 case 的漏斗 tensor 上,同一个 tensor 会关联大量 producer 和 consumer,op 级展开会把依赖规模放大到 producer/consumer 交叉关系,后续 hash、color 关系和拓扑修正都会重复扫描这批边。

本次在 RescheduleUtils 中新增 tensor 级依赖摘要:每个 tensor 只聚合保存 producer op、producer color 和 consumer color。构建阶段按 op list 线性扫描输入输出 tensor,随后对依赖向量排序去重。两个 pass 后续都基于这份压缩后的 tensor dependency 做局部计算,避免在各自 pass 内重复恢复完整 op 级 in/out graph。

2. 复用公共 topo hash primitive

NBufferMerge 和 L1CopyInReuseMerge 都需要根据输入 producer 的拓扑信息构造 hash。旧实现里两个 pass 各自维护相似逻辑,并且对共享 tensor 的 producer 集合会被多个 consumer 反复计算。

本次把公共逻辑收敛到 RescheduleUtils:包括 opcode hash、tensor producer hash contribution、hash contribution 应用逻辑,以及输入 topo hash 列表构建。对同一个 tensor,producer hash contribution 构建后可以缓存复用;consumer 侧只需要把 contribution 组合进当前 op hash。这样两个 pass 共享同一套语义和实现,也减少了重复代码带来的维护风险。

3. NBufferMerge 去掉 op 级 in/out graph

NBufferMerge 原来在初始化时构造 inGraph_ / outGraph_,后续在 GetColorHash、color 输入输出关系统计和 color topo 修正中反复访问这些 op 级边。极端 fanin/fanout tensor 会让这些边非常密集,导致 NBuffer 阶段主要时间耗在构图和重复遍历上。

本次将 NBuffer 的核心数据流改为 tensor dependency summary:

  • InitParam 只负责线性收集 color、op 分组和 cycle 信息,不再并行构造 producer/consumer op graph。
  • GetColorHash 直接消费 RescheduleUtils::BuildInputTopoHashList 产出的 op topo hash。
  • BuildColorDependencySummary 只为 merge 排序提取每个 color 的代表性外部输入/输出邻居,不再承担拓扑正确性保证。
  • BuildColorTopoOrder 使用 tensor barrier 模型处理 color 间真实依赖:只有当一个 tensor 的 producer color 按规则释放后,对应 consumer color 才能进入 topo order。

这里需要区分两个概念:merge 排序用的 input/output color summary 只是分组启发信息;合图后的拓扑正确性由 tensor barrier topo 保证。

4. L1CopyInReuseMerge 预计算输入 color 与 hash

L1CopyInReuseMerge 原来在 GetMaxInColor 中通过输入图查找当前候选 op 的最大输入 color,同时在 color hash 计算中单独处理 topo hash。对同一个高 fanin tensor,这会造成多轮重复 producer 扫描。

本次新增 L1 私有的 BuildTopoHashAndMaxInputColors:一次遍历 tensor dependency,同时完成两类预计算。

  • topo hash:复用公共 tensor producer hash contribution,生成每个 op 的输入拓扑 hash。
  • top-two input color:为每个 op 记录最大和次大的输入 subgraph id。GetMaxInColor 遇到当前 color 时取次大值,否则取最大值,从而保持旧逻辑“排除当前 color 后找最大输入 color”的语义。

这个 top-two 结构是 L1 的私有需求,没有放进公共工具;公共部分只保留两个 pass 都真实复用的 tensor dependency 和 topo hash primitive。

5. 拆分复杂流程并补充关键注释

本次同步规整了两个 pass 的长函数和容易遗忘的算法细节。NBuffer 的 color barrier topo 拆分为 barrier 构建、producer ready 更新和 topo order 应用;L1 的 Run、Phase1、semantic label 处理拆成配置解析、候选合并、结果汇总和 override 写回。

同时补充注释说明几个非直观点:为什么 tensor barrier 可以替代展开边、remainingProducerXor 为什么能在 remaining count 为 1 时 O(1) 找到最后一个 producer color、以及 color dependency summary 为什么只服务 merge 排序而不负责拓扑正确性。

最终性能对比

测试脚本:/mnt/workspace/gitCode/cann/PyPTO_Test/run_conv_case.py
目标 function:TENSOR_LOOP_L1_woutIdx_Unroll1_PATH0_hiddenfunc0_20
Op 数量:205,913
目标阈值:20.59s

Pass Baseline Final 收益 状态
NBufferMerge 279.971s 1.773s 约 157.9x,降低 99.37% 达标
L1CopyInReuseMerge 123.396s 10.203s 约 12.1x,降低 91.73% 达标
两个目标 Pass 合计 403.367s 11.976s 约 33.68x,降低 97.03% 达标

验证结果

  • python3 build_ci.py -f=cpp -u="NBufferMergeTest.*" -j=24 -c:14/14 passed
  • python3 build_ci.py -f=cpp -u="L1CopyInReuseTest.*" -j=24:21/21 passed
  • python3 build_ci.py -f=python3 --build_type Release --disable_auto_execute:success
  • pip install ./build_out/pypto-*.whl --force-reinstall:success
  • run_conv_case.py Release 性能基准:目标 pass 完成并达标
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for Catherie's contribution.)
CatherieCatherieCollaborators
Aug 3 Created pull request, commit ec44549d
atomgit-bot
atomgit-bot
Aug 3 Comments:

变更摘要

此 PR 主要针对具有极端张量扇入/扇出(fanin/fanout)的卷积图场景,对 NBufferMerge 和 L1CopyInReuseMerge 两个图分区 Pass 进行编译期性能优化。核心思路是将原先基于 Op 级全量 in/out graph 的遍历方式,替换为基于张量级依赖摘要、拓扑哈希缓存和颜色屏障(color barrier)的增量计算方式,在大规模图(约 20 万 Op)上 NBufferMerge 获得约 158 倍加速(279.97s → 1.77s),L1CopyInReuseMerge 获得约 12 倍加速(123.40s → 10.20s)。

主要改动

  • 在 RescheduleUtils 中新增张量依赖与拓扑哈希原语:新增 BuildTensorDependencyInfo 构建 TensorDependencyMap(以 LogicalTensor* 为键,存储去重排序后的 producer/consumer 颜色列表),新增 BuildInputTopoHashList(含缓存)和 GetOpcodeHash/BuildTopoHashContribution 等哈希函数替代原有的逐 Op 在线哈希;同时简化 GetInOutGraphs 实现,移除 functionmagic 参数及多个重载,新增 tensor2ProducerIdx 缓存避免重复扫描 tensor→producer 映射。

  • NBufferMerge 用颜色屏障机制替代 Op 级 in/out graph:新增 ColorTensorDependency 结构体,以 XOR 方式追踪剩余 producer 颜色和 pendingBarrierCount,通过 BuildColorTensorDependencies + RunColorBarrierTopo + BuildColorTopoOrder 实现颜色拓扑排序,替代原有的 ColorTopo(基于 Op 级 inGraph_/outGraph_ 遍历);移除 inGraph_、outGraph_ 成员字段及 GetOpHash/GetOpHashReverse 方法;新增 BuildColorDependencySummary 用张量级摘要替代 InitParam 中原有的并行 Op 级 producer 遍历。

  • L1CopyInReuseRunner 新增预计算的拓扑哈希与最大输入颜色:新增 BuildTopoHashAndMaxInputColors 方法,一次性预计算所有 Op 的 hashTileOp 和 maxInputColors_(每 Op 记录前两大输入颜色),使 GetMaxInColor 从遍历 inGraph_ 变为直接查表;构造函数不再接受 inGraph_ 参数,inGraph_ 成员被移除;GetColorHash 改为接收预计算的 hashTileOp 参数,避免每次重复计算。

  • Pass 流程拆分与语义标签覆写逻辑解耦:NBufferMergeProcess 拆分为 BuildHashMergeNum、RunMergeByMode、LogHashOverview;L1CopyInReuseRunner::Run 拆分为 ConfigureRunSettings、LogL1ReuseHashOverview、RunL1ReusePhase、RunCubeNBufferPhase、ValidateSubgraphIds;语义标签覆写逻辑分别拆出 Collect*LabelOverrides 与 Apply*LabelOverrides 方法。

  • Operations 获取统一使用 LIGHTWEIGHT 排序模式:func.Operations(true, SortOperationsMode::LIGHTWEIGHT) 替换多处原有的 func.Operations() 遍历,避免完整的 Op 排序开销;EraseOperations 同样传入 SortOperationsMode::LIGHTWEIGHT。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 3 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 3 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 3 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


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
repo-cann/pypto ✅ zhangyuebing, dai-zhentao (2/2) ✅ zhangyuebing (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
55 messages are collapsed here。 See more
zhangyuebing
zhangyuebingCollaborators
Aug 5 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 5 Added label: approved
dai-zhentao
dai-zhentaoCollaborators
Aug 6 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Aug 6 Added label: lgtm
CANN-robotCANN-robotCollaborators
Aug 6 Merged pull request