Closed
[Task|任务跟踪]: A2A3卷积算子编译性能优化 #2685
CatherieAdd on  Jul 20Closed at  Sep 10
Catherie
CatherieCollaborators
Jul 20 Create

任务描述

问题背景

在 A2/A3 的 3D bf16 Conv 场景中,ExpandFunction 后的 Tile Graph 规模达到 205,913 个 Op。多个 Pass 的编译耗时从几十秒增长到数百秒,部分 Pass 超时,无法满足该规模下单 Pass 20.59s 的性能目标。

进一步分析发现,瓶颈并非单纯由 Op 数量大导致,而是 3D Conv 的滑窗复用和格式转换在图中形成了少量但极端的高 fanin/fanout tensor。典型漏斗节点同时连接 32,000 个 producer 和 8,000 个 consumer:

32,000 个 NCDHW2NDC1HWC0
              ↓
       同一个中间 tensor
       fanin=32,000
       fanout=8,000
              ↓
8,000 个 L1_COPY_IN_CONV

整张图中高 fanin/fanout tensor 的数量并不多,但旧实现会在多个 Pass 中反复展开和遍历其生产消费关系,使本应接近线性的处理退化为 fanin × fanout 级别的重复计算。例如,同一 tensor 的 8,000 个 consumer 分别遍历 32,000 个 producer,会产生约 2.56 亿次迭代。相比之下,其他同为 20 万 Op 规模的 attention/matmul 图通常 fanout 不超过 3,因此不会出现同等级别的编译性能问题。

除极端图结构外,还存在以下共性开销:

  1. Function::Operations() 在图未排序时隐式触发 GENERAL 拓扑排序,单次约 51s;部分 Pass 在插入、删除 Op 后会重复触发排序。
  2. 即使图没有发生修改,部分流程仍无条件执行 DCE、EraseOperations 和重新排序。
  3. GraphPartition 类 Pass 按 Op 展开依赖边,并大量使用 std::set、std::map、字符串哈希和临时容器,在大图上产生额外分配和 cache miss。
  4. 多个 consumer 对同一高 fanin tensor 重复计算 producer、拓扑 hash、color 和输入输出关系,缺少 tensor 级去重、摘要与缓存。

解决方案概述

本任务从“减少无效全图操作”和“避免展开高 fanin/fanout 关系”两条主线进行优化:

  1. 采用 tensor 级去重和依赖摘要:对同一 tensor 的 producer/consumer 关系只遍历和聚合一次,缓存拓扑 hash、producer/color 等公共信息,避免形成 producer 与 consumer 的笛卡尔积。
  2. 控制排序和 DCE 开销:在适用的 Tile Graph 阶段显式使用 LIGHTWEIGHT 排序;只读取 Op 数量时不触发排序;图未发生修改时跳过 DCE、EraseOperations 和重新排序。
  3. 优化 GraphPartition 数据结构与算法:使用数值哈希替代字符串拼接,使用连续内存容器和时间戳去重替代红黑树结构,预构建 tensor→producer 映射,并将递归/排序型计算改为迭代和顺序无关组合。
  4. 压缩 NBuffer/L1 依赖模型:以 tensor dependency summary 和 tensor barrier topo 替代完整的 Op 级 in/out graph;预计算输入 topo hash 和 color 信息,减少同一依赖关系的重复扫描。

阶段性优化结果

关联 PR 主要优化范围 优化前 优化后 状态
#4959 AssignMemoryType 55.1s 4.3s 已合入
#4959 DuplicateOp 77.2s 0.24s 已合入
#5011 RemoveRedundantOp 168.58s 3.18s 已合入
#5011 ProcessAtomic 236.77s 0.03s 已合入
#5011 InferDiscontinuousInput 252s+(超时) 0.40s 已合入
#5058 GraphPartition 541s+(超时) 18.9s 已合入
#5241 NBufferMerge 279.971s 1.773s 进行中
#5241 L1CopyInReuseMerge 123.396s 10.203s 进行中

上述数据均来自 205,913 Op 的 3D bf16 Conv 基准场景。各 PR 同时补充或执行了对应 Pass UT,并通过完整 Conv case 验证。

任务目标

  1. 定位 A2/A3 3D Conv 大图中主要超时 Pass 的性能瓶颈,避免算法复杂度随极端 fanin/fanout 关系成乘积增长。
  2. 将目标场景下各主要 Pass 的编译耗时优化至 20.59s 目标阈值内。
  3. 保持 Pass 原有图变换语义和拓扑正确性,相关 UT、构建及完整 Conv case 验证通过。
  4. 沉淀可复用的 tensor 级依赖分析和大图优化方法,降低后续相似算子图触发性能退化的风险。

关联改动

likedislike
CatherieCatherieCollaborators
Jul 20 Assign Catherie as responsible person
CatherieCatherieCollaborators
Jul 20 Added label: Passes
zhaosenqiuzhaosenqiuCollaborators
Jul 20 Associated pull request
CatherieCatherieCollaborators
Jul 21 Associated pull request
CatherieCatherieCollaborators
Jul 22 Associated pull request
CatherieCatherieCollaborators
Jul 24 Associated pull request
zhaosenqiuzhaosenqiuCollaborators
Jul 29 Associated pull request
zhaosenqiuzhaosenqiuCollaborators
Jul 30 Associated pull request
zhaosenqiuzhaosenqiuCollaborators
Jul 30 Associated pull request
CatherieCatherieCollaborators
Aug 4 Deleted associated pull request: fix(pass): Fix split reshape miscalculate valid shape of reshape output
CatherieCatherieCollaborators
Aug 4 Deleted associated pull request: fix(pass): Fix split reshape miscalculate valid shape of reshape output
CatherieCatherieCollaborators
Aug 4 Deleted associated pull request: feat(pass): Pass and Frontend adapt slice and contract.
CatherieCatherieCollaborators
Aug 4 Deleted associated pull request: fix(pass): Fix split reshape miscalculate valid shape of reshape output
CatherieCatherieCollaborators
Aug 4 Modify description of issue
CatherieCatherieCollaborators
Aug 8 Associated pull request
CatherieCatherieCollaborators
Sep 10 Associated pull request
CatherieCatherieCollaborators
Sep 10 changed custom state from 进行中 to 已完成
CatherieCatherieCollaborators
Sep 10 Close issue
CANN-robotCANN-robotCollaborators
Sep 10 Added label: resolved