Merged
perf(pass): Optimize DuplicateOp and AssignMemoryType for large graphs #4959
CatherieAdd on Jul 20
perf(pass): Optimize DuplicateOp and AssignMemoryType for large graphs #4959
Merged
CatherieAdd on Jul 20
Catherie
CatherieCollaborators
Jul 20

对于 20 万+ op 的大图(如 3D bf16 Conv,205,913 ops),DuplicateOp 和 AssignMemoryType 两个 Pass 占总编译耗时的 84%。经 dump 计算图分析,根因是 3D Conv 滑窗复用产生了极端的 fanin/fanout 漏斗结构(单 tensor fanin=32000、fanout=8000),导致基于指针遍历的算法退化为 O(fanin×fanout)。

本 PR 分两部分独立优化,互不耦合。


一、Pass 性能优化

1. AssignMemoryType(55.1s → 4.3s,12x)

问题:Operations() 在 sorted_=false 时触发 GENERAL 拓扑排序(单次 ~51s)。AddRawOperation 插入 op 后会将 sorted_ 置为 false,导致后续 Operations() 重复触发昂贵的 GENERAL 排序。

优化:

  • 在 AssignMemoryType::RunOnFunction 入口显式调用 SortOperations(LIGHTWEIGHT),覆盖只读子步骤
  • 在 InsertConvertOpsAndInferShape 的 DoInsertion 之后再次调用 SortOperations(LIGHTWEIGHT),避免插入 convert op 后触发 GENERAL 排序(单次 51.4s → 0.8s)
  • 将 ConvertInserter 中的 visitedTensor 由 vector 改为 unordered_set,查找复杂度从 O(n) 降为 O(1)
  • DoInsertion 中用 GetOperationSize() 替代 Operations().size(),避免仅为读取数量而触发一次排序

2. DuplicateOp(77.2s → 0.24s,322x)

问题:Process 方法从 outcast 反向 DFS 遍历计算图,对每个 op 遍历其所有输入 tensor 的 producer。原代码用 unordered_set<Operation*> visited 做点级去重,但在高 fanin tensor 上退化为 O(fanin×fanout):magic=143 同时有 fanin=32000 和 fanout=8000,8000 个 consumer 每个都遍历 32000 个 producer,共 2.56 亿次迭代。此外 DFS 结束后无条件执行 DCE+SortOperations,即使未复制任何 op 也做无用功。

优化:

  • 新增 unordered_set visitedTensors 做边级去重:每个 tensor 的 producer 只遍历一次,总迭代从 2.58 亿降到 20.6 万(1248x)
  • visitedOps 和 visitedTensors 均存 magic(int)而非指针,更省内存且 hash 更快
  • 提取 EnqueueProducers 辅助函数,将 Process 方法深度从 6 降到 4(CodeCheck 规则要求 ≤5)
  • 无新增 op 时跳过 DCE + SortOperations
  • visited.reserve + 合并 find+insert 为单次操作 + push 前检查 visited

二、Skill 脚本改进

pypto-pass-perf-optimizer scripts

  • parse_pass_perf.py:增强 Pass 耗时日志解析的健壮性,处理拆分日志文件、多 pid 场景
  • run_with_timeout.sh:改进超时控制脚本,支持优雅退出和自定义超时时间

验证结果

Pass 优化前 最终 提升
AssignMemoryType 55.1s 4.3s 12x
DuplicateOp 77.2s 0.24s 322x
总 Pass 耗时 132.3s 4.5s 29x
  • UT:AssignMemoryType 45/45 通过
  • conv_case.py:退出码 0

计算图结构分析(3D bf16 Conv)

经 dump ExpandFunction 之后的计算图 JSON 分析,发现 5 个高 fanout tensor 和 4 个高 fanin tensor,其中 magic=143 是极端漏斗节点:

  [1, 2, 16, 2, 16] ×32000 个输入
        │
  NCDHW2NDC1HWC0 ×32000
        │
  [1, 80, 4, 80, 80, 16]  ← magic=143(fanin=32000, fanout=8000)
        │
  L1_COPY_IN_CONV ×8000
        │
  [1, 1, 1, 32, 64, 16] 等 ×8000 个输出

其他 20w 规模算子(如 attention/matmul)虽然 op 数量类似,但 fanout 通常 ≤3,没有这种 1→N→1→N 漏斗结构,所以编译快。

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for Catherie's contribution.)
CatherieCatherieCollaborators
Jul 20 Created pull request, commit 0d1ea4bd
atomgit-bot
atomgit-bot
Jul 20 Comments:

变更摘要

此 PR 针对大图(20 万+ 算子)场景下 DuplicateOp 和 AssignMemoryType 编译耗时过长的问题进行性能优化。核心思路是:避免因 Operations() 调用意外触发昂贵的 GENERAL 拓扑排序(单次约 51 秒),在必要位置显式调用 SortOperations(LIGHTWEIGHT) 替代;同时将热点路径中的线性查找改为哈希查找,并优化 DuplicateOp::Process 的遍历逻辑,减少冗余操作。验证结果显示 AssignMemoryType 从 51.4 秒降至 4.3 秒(约 12 倍提升),DuplicateOp 降至 20.1 秒。

主要改动

  • AssignMemoryType 显式轻量排序:在 RunOnFunction 入口和 InsertConvertOpsAndInferShape 中 DoInsertion 之后,显式调用 function.SortOperations(SortOperationsMode::LIGHTWEIGHT),并将后续遍历改为 function.Operations(false) 以避免触发 GENERAL 重排序。
  • ConvertInserter 查找结构优化:将 visitedTensor 从 std::vector<int> 改为 std::unordered_set<int>,SkipOperand 中的 std::find 线性查找同步替换为 O(1) 的 unordered_set::find;同时用 function.GetOperationSize() 替代 function.Operations().size(),避免仅为获取计数而触发排序。
  • DuplicateOp::Process 遍历优化:为 visited 集合预分配容量 (reserve);将 find + insert 两步合并为 visited.insert(CurrentOp).second 一次判断;向栈推送 producer 前先检查是否已访问,避免重复入栈;当 newOps 为空时跳过 DeadOperationEliminator 和后续排序。
  • 辅助脚本增强:run_with_timeout.sh 启用 set -m 作业控制并采用三级信号升级策略(SIGINT → SIGTERM → SIGKILL),确保超时信号能传播到 C++ 子进程;parse_pass_perf.py 新增 fmt_dur 自适应时间格式化函数,避免亚毫秒级耗时显示为 0.000。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Jul 20 Comments:

代码审查

Now I have a complete picture. Let me finalize the review with the closing summary.

审查总结

我逐一审查了全部 6 个变更文件。以下是结论:

各文件审查结果

文件 结果
.agents/skills/pypto-pass-perf-optimizer/scripts/parse_pass_perf.py 发现 2 个 P3 显示一致性问题(fmt_dur 舍入边界)
.agents/skills/pypto-pass-perf-optimizer/scripts/run_with_timeout.sh 无问题(进程组信号传播方案设计正确,set -m 搭配三级信号升级稳健;shift + 数组方式比旧的字符串拼接更安全)
framework/src/passes/tile_graph_pass/data_path/assign_memory_type.cpp 无问题(LIGHTWEIGHT 排序 + Operations(false) 配合正确,sorted_ 状态管理一致)
framework/src/passes/tile_graph_pass/data_path/convert_op_inserter.cpp 无问题(visitedTensor 改为 unordered_set 正确,O(1) 查找,GetOperationSize() 避免触发排序)
framework/src/passes/tile_graph_pass/data_path/convert_op_inserter.h 无问题(头文件新增 #include <unordered_set>,SkipOperand 签名同步更新)
framework/src/passes/tile_graph_pass/graph_optimization/duplicate_op.cpp 无问题(reserve + insert().second + 推栈前检查 + newOps.empty() 提前返回均正确)

发现统计

  • P0: 0
  • P1: 0
  • P2: 0
  • P3: 2(均为 fmt_dur 函数在单位阈值边界的显示一致性问题,不影响正确性)

整体风险评估

低风险。 此 PR 是一个精心设计的性能优化变更,核心逻辑正确,无安全或正确性问题:

  • C++ 侧:SortOperations(LIGHTWEIGHT) 替代 GENERAL 排序、unordered_set 替代 vector 线性查找、GetOperationSize() 避免触发排序、visited.reserve() 预分配、合并 insert+check、推栈前过滤已访问节点、newOps.empty() 跳过 DCE——每项优化都正确维护了语义等价性。
  • Shell 侧:set -m + 进程组信号传播方案比原 kill -INT $PID 更可靠,三级信号升级(SIGINT→SIGTERM→SIGKILL)确保子进程能被正确终止,shift + 数组传参也比旧字符串方式更安全。
  • Python 侧:fmt_dur 自适应格式增强了可读性,两个 P3 边界问题影响极小且仅涉及显示。

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

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Jul 20 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Jul 20 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, cam_bxh (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
116 messages are collapsed here。 See more
zhangyuebing
zhangyuebingCollaborators
Jul 22 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Jul 22 Added label: approved
Hujun
HujunCollaborators
Jul 22 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Jul 22 Added label: lgtm
CANN-robotCANN-robotCollaborators
Jul 22 Merged pull request