Closed
perf(foreach_log): arch35 张量均衡切核消除小张量列表逐核流水开销 #14246
xuejinghuiAdd on 11 days agoClosed at 4 days ago
perf(foreach_log): arch35 张量均衡切核消除小张量列表逐核流水开销 #14246
Closed
xuejinghuiAdd on 11 days agoClosed at 4 days ago
xuejinghui
xuejinghuiCollaborators
11 days ago

问题与根因

ForeachLog(arch35/Ascend950)GEIR 通路小/微张量列表用例性能单 case 不达标:35 条 gpu/npu 用例中 6 条低于带宽折算单 case 阈值 0.162(004/018/026/027/030/031,来源 https://gitcode.com/wangyueting/changwei_gaoxiang/issues/49 )。

根因(kernel/tiling 层,两层):

  1. 原 arch35 SIMT 实现对每个 tensor 串行发起一次 asc_vf_call(dim3(1024)),逐张量 launch 固定开销 ~0.85us/张;
  2. 迁移 flat RegBase 后逐张量 CopyIn/Compute/CopyOut 队列握手为同量级固定开销,且共享 flat tiler 的元素均衡切核会把多张小张量压进同一核串行处理——小张量列表的主成本仍是逐张量开销。

修改内容

  • foreach/foreach_log/op_host/arch35/foreach_log_tiling.cpp:重写 arch35 tiling——校验复用 ForeachFlatRegbaseValidation::ValidateHomogeneous<1>;平均张量 ≤32K 元素时按整张量切核(每核仅 1~2 条逐张量 DMA 流水),大张量保持与共享 flat tiler 一致的元素均衡切分;对 int32 偏移字段增加值域校验(单 tensor 元素数与核区间超 INT32_MAX 时 OP_LOGE 并返回失败,防收窄截断导致 GM 越界)。
  • foreach/foreach_log/op_kernel/arch35/foreach_log_regbase.h:新增——基于 foreach_utils 一元 RegBase 框架(Reg::Ln:fp16/fp32 原生、bf16 提升 FP32 + CAST_RINT 回写)。
  • foreach/foreach_log/op_kernel/arch35/foreach_log.cpp:改为 flat RegBase kernel entry(DTYPE_X 宏绑定 dtype 二进制)。
  • foreach/foreach_log/op_kernel/arch35/foreach_log_tiling_data.h:新增——tiling schema(字段名与 ForeachFlatTilingData 对齐以复用共享 dispatcher/mover 模板;结构体保持 ~3KB 以满足 kernel 动态共享内存 launch 预算,偏移字段为 int32 且有值域校验兜底)。
  • 删除原 SIMT 实现三件套(foreach_log_simt.h/foreach_log_tiling_data.h/foreach_log_tiling_key.h)。
  • tests/ut/op_host/arch35/test_foreach_log_tiling.cpp:UT 同步改写(双切分路径 key/字段断言 + int32 溢出拒绝用例)。

测试

环境:Ascend 950PR + CANN 9.3.0(build 20261028000328247)+ ttk geir,golden 插件 tolerance 本地适配为 stat_rel_err(官方 cross_check 需 XPU golden 机器)。

# 基线(问题复现,与 CI 吻合)
python3 -m ttk geir -i 失败用例集_ForeachLog_geir_性能失败.csv --plugin <golden.py> --device-whitelist=0 -c -t <case>
# 修复验证:全量 35 条(性能用例集_ForeachLog_geir.csv)逐条同命令
# UT:bash build.sh -u --ophost --soc=ascend950 --ops=foreach_log --ut_mode=fast

结果(npu 耗时为图执行 kernel task duration 均摊 us):

用例 基线 修复后 提升 gpu/npu(阈值 0.162)
004(111 标量 fp32) 93.8 7.1 13.2x 0.28 ✓
018(79 张量 bf16 离散) 90.3 9.6 9.4x 1.39 ✓
026 46.4 5.8 8.0x 0.27 ✓
027 75.8 7.1 10.7x 0.23 ✓
030 41.9 5.4 7.8x 0.29 ✓
031 27.5 4.9 5.7x 0.33 ✓

其余 29 条原达标用例无回归(大张量 1.9~3.1x 提升,001/005 约 8x,025 约 2x)。精度:21 条可比对用例 stat_rel_err 全 PASS;14 条 bf16 用例为本地 golden 插件(numpy 无 bf16)哨兵跳过,base 与本修改行为一致。UT 7 例全过。

影响范围

仅 ForeachLog arch35(ascend950/ascend350)kernel 与 tiling;arch22(910b 等)路径与 aclnn 入口不变;不影响其他 foreach 算子(共享框架文件零改动)。

关联

likedislike
The current Pull Request has been closed by xuejinghui.
xuejinghuixuejinghuiCollaborators
11 days ago Created pull request, commit 0b822e54
xuejinghuixuejinghuiCollaborators
11 days ago Associated issue: [Requirement|需求建议]: ForeachAddList性能优化,[性能问题][ForeachLog]ForeachLog算子存在 性能问题(单case失败),[性能问题][ForeachPowList]ForeachPowList 算子 GEIR 通路性能单case 不达标(8 条)
atomgit-bot
atomgit-bot
11 days ago Comments:

变更摘要

本 PR 针对 ForeachLog 在 arch35(Ascend950)上的小/微张量列表性能问题做优化:将原 SIMT 逐张量 asc_vf_call 实现替换为 flat RegBase kernel 通路,并在 tiling 层引入双切分策略——平均张量元素数不超过 TENSOR_BALANCED_AVG(32768)时按整张量切核,使每核仅承担少量逐张量 DMA 流水;大张量仍沿用共享 flat tiler 的元素均衡切分。同时新增对应 tiling schema 与 RegBase 计算策略,并删除原 SIMT 相关文件。

主要改动

  • arch35 tiling 双切分策略与校验收敛: foreach_log_tiling.cpp 改为复用 ForeachFlatRegbaseValidation::ValidateHomogeneous<1> 做同构 dtype 校验,按平均张量大小在小张量整张量切核与大张量元素均衡切分(LocateFlatOffset 定位 flat 偏移)之间选择,并统一 SetBlockDim、SetLocalMemorySize、workspace 与 tilingKey=0。
  • int32 偏移值域保护: tiling 中对单 tensor 元素数及核区间(startOffset/endOffset/currentCount)超 INT32_MAX 的场景报 OP_LOGE 并返回失败,避免窄化截断导致 GM 游标越界。
  • tiling 数据结构重定义为 flat schema: foreach_log_tiling_data.h 用 tensorDataCountList、tensorStartList、tensorEndList、tensorStartOffsetList、tensorEndOffsetList、tileElems 替换原 needCoreNum/totalElements/tensorElements,字段名与 ForeachFlatTilingData 对齐以复用共享 dispatcher/mover 模板,并新增 FOREACH_LOG_MAX_TENSOR_COUNT/FOREACH_LOG_MAX_CORE_COUNT 上限常量。
  • kernel 入口切换为 RegBase 一元框架: foreach_log.cpp 由模板 schMode 分派的 SIMT 入口改为 TPipe + ForeachLogRegbase<DTYPE_X, ForeachLogTilingData> 的 flat RegBase entry;新增 foreach_log_regbase.h(原 foreach_log_tiling_key.h 位置)定义 LogOperation,通过 Reg::Ln 实现 fp16/fp32 原生、bf16 提升 FP32 后 CAST_RINT 回写。
  • 删除 SIMT 实现与 UT 同步改写: 移除 foreach_log_simt.h、foreach_log_tiling_key.h 等原 SIMT 三件套;test_foreach_log_tiling.cpp 改为断言 tilingKey=0、tileElems、tensorDataCountList/tensorStart* 字段,新增覆盖小张量整张量切分、大张量元素均衡切分及 int32 溢出拒绝用例。

🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
atomgit-bot
atomgit-bot
11 days ago Comments:

代码审查

审查结论

审查发现汇总

优先级 数量
P0 0
P1 1
P2 3
P3 2

逐文件审查状态

文件 结论
foreach/foreach_log/op_host/arch35/foreach_log_tiling.cpp 5 项问题(1×P1、3×P2、1×P3):张量个数上限校验被删导致越界写;空列表下 LocateFlatOffset 越界读;GetInputInstanceInfo/GetDynamicInputShape/GetDynamicInputDesc 链式解引用无空指针校验;负元素数校验被删;平均元素数判据可能把大张量压到单核
foreach/foreach_log/op_kernel/arch35/foreach_log.cpp no issues(extern "C" + DTYPE_X 绑定正确,且各 SoC 的 binary json 均按 dtype 列条目、无残留 tilingKey 字段,SetTilingKey(0) 与之一致;workspace 未使用已显式 (void))
foreach/foreach_log/op_kernel/arch35/foreach_log_regbase.h no issues(LogOperation::Apply 契约与共享一元 RegBase 框架一致,Reg::Ln 仅对 fp16/fp32 原生、bf16 走共享提升路径;include guard 与文件名同步改名)
foreach/foreach_log/op_kernel/arch35/foreach_log_tiling_data.h no issues(字段命名与 ForeachFlatTilingData 对齐、结构体 ~3016B 在 4KB 预算内;数组容量常量的校验缺口已计入 tiling 侧 P1)
foreach/foreach_log/tests/ut/op_host/arch35/test_foreach_log_tiling.cpp 1 项问题(P3):双切分路径的断言无法区分,分支取反仍全过,且无多张量列表用例

整体风险判断:本次迁移方向(flat RegBase + 按整张量切核)与 PR 的性能目标一致,kernel/头文件侧改动干净;但 tiling 侧在重写时丢掉了旧实现的若干防御性校验(张量个数上限、负 shape、空指针),其中张量个数上限缺失会在长列表场景下越界写 tiling 结构体,属于必须修复的内存安全问题;空列表路径则同时存在越界读与空指针解引用。建议在 P1/P2 修复并补齐双切分路径断言后再合入。

类型 数量
🔴 阻塞 1
🟡 建议 3

⛔ 需要修改


🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
11 days ago Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
11 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
11 days ago 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

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
foreach ❌ 陈琦 (1/2)(You can also ask: 刘波, 陈兴宇, 王星, 任如海, 陈展熹) ❌ (0/1)(You can also ask: 陈兴宇, 刘杰, 任如海, 陈展熹, 上官秦南)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
11 days ago Set zengjuan,u010470851,chaotang233,kevin_huang1234,crystalhu,yangyang016,fanqirui,wangrui_,renruhai,su-yueming,zhajianqing123,yourealize,chenqi317,wang-xing001,liubo75,tangweiwei2,chenxingyu18,liujie12345678,east_yang,pingchuantang,Chen_HaoWen,qianzehong as approver
CANN-robotCANN-robotCollaborators
11 days ago Set u010470851,kevin_huang1234,wangrui_,renruhai,su-yueming,zhajianqing123,yourealize,wang-xing001,chenxingyu18,liujie12345678,east_yang,pingchuantang,Chen_HaoWen,qianzehong as approver
xuejinghui
xuejinghuiCollaborators
11 days ago Comments:

/compile

likedislike
xuejinghuixuejinghuiCollaborators
11 days ago Pre merge succeeded (commit_id: 4029b15220539205f2b5522dd3ae4348dc14565b)
CANN-robot
CANN-robotCollaborators
11 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
11 days ago Added label: ci-pipeline-running
atomgit-bot
atomgit-bot11 days agoStart code review1
foreach/foreach_log/op_host/arch35/foreach_log_tiling.cpp
@@ -90,2 +49,3 @@
9049 }
91- return ge::GRAPH_SUCCESS;
50+ tensorIndex = static_cast<uint16_t>(tensorCount - 1);
51+ tensorOffset = tensorDataCountList[tensorIndex] - 1;
atomgit-bot
atomgit-bot11 days agoComments:

🟡 Medium Priority

变更行:foreach_log_tiling.cpp:50-51(新增的 LocateFlatOffset 兜底分支)在循环一次都没命中时执行
tensorIndex = static_cast<uint16_t>(tensorCount - 1); tensorOffset = tensorDataCountList[tensorIndex] - 1;

当 tensorCount == 0 时 tensorCount - 1 转成 uint16_t 后为 65535,tensorDataCountList[65535] 对只有 256 个元素的数组(foreach_log_tiling_data.h:33)是一次约 522KB 的越界读。

可达性由本文件自身给出:122 行的分支条件是 if (tensorCount > 0 && ...),即作者显式允许 tensorCount == 0 落入 else(元素均衡分支),于是 157-159 必然以 tensorCount = 0 调用 LocateFlatOffset(flatCursor + currentCount - 1 此时为 -1,同样进兜底分支)。旧实现把空列表视为合法(旧注释:“An empty tensor list is legal (consistent with the aclnn entry)”),本算子契约上存在空列表入口。

失败模式:空张量列表做 tiling 时读越界内存,取到垃圾 offset 写入 tensorStartList/tensorEndList;轻则生成的核区间无意义(kernel 侧按该 index 取值会访问不存在的张量),重则越界地址未映射直接段错误,使 tiling 崩溃。

修复方向:LocateFlatOffset 开头对 tensorCount <= 0 直接返回安全值(index 0 / offset -1),或在上游把空列表单独提前处理(不进入任何切分分支)。

建议:在兜底分支前加空列表守卫:tensorCount <= 0 时直接置 tensorIndex=0、tensorOffset=-1 后返回,避免用 uint16_t(0-1)=65535 索引 256 元素的数组。

likedislike
Incorrect?
atomgit-bot
atomgit-bot11 days agoStart code review1
foreach/foreach_log/op_host/arch35/foreach_log_tiling.cpp
@@ -167,0 +98,4 @@
98+ OP_CHECK_IF(dataCount > std::numeric_limits<int32_t>::max(),
99+ OP_LOGE(context, "tensor %d has %lld elements, exceeding the int32 offset limit", tensorIndex,
100+ static_cast<long long>(dataCount)),
101+ return ge::GRAPH_FAILED);
atomgit-bot
atomgit-bot11 days agoComments:

🟡 Medium Priority

变更行:98-101 只校验了上界 dataCount > INT32_MAX,dataCount < 0 会直接通过并写入 tilingData->tensorDataCountList[tensorIndex](102 行)。

旧实现明确拒绝负值:OP_CHECK_IF(numel < 0, OP_LOGE(context, "tensor %lu has unknown or negative shape size", i), return ge::GRAPH_FAILED);,注释说明“negative size means an unresolved/unknown dimension (e.g. -1/-2) and cannot be converted into a concrete element count”。本次改成 flat tiling 时把这条下界校验连同 GetDimNum() > 8 的上限一起删掉了,只剩上界检查。

失败模式(触发条件:列表中某个成员张量的 storage shape 仍含未解析维,GetShapeSize() 返回负值):负元素数被写入 tensorDataCountList → totalDataCount 变小/为负 → 122 行 totalDataCount / tensorCount 判定偏向整张量切核 → 135-136 行 tensorEndOffsetList = tensorDataCountList[endTensor] - 1 写出现更小的负数,且 160-165 的 int32 值域校验只查 > INT32_MAX,负值一路漏过;kernel 侧再按该 count 计算 DMA 长度(无符号回绕)即可能出现超大搬运 → GM 越界读写。

修复方向:把上界校验扩展成 dataCount < 0 || dataCount > INT32_MAX 的双边校验并报错返回。

建议:把 98-101 行的校验改为双边:dataCount < 0(未解析/未知维)或 dataCount > INT32_MAX 都 OP_LOGE 并返回失败,恢复旧实现对负元素数的拒绝。

Suggestions
101
+ OP_CHECK_IF(dataCount < 0 || dataCount > std::numeric_limits<int32_t>::max(),
102
+ OP_LOGE(context, "tensor %d has an invalid element count %lld", tensorIndex,
103
+ static_cast<long long>(dataCount)),
101
104
  return ge::GRAPH_FAILED);
Apply suggestion
likedislike
Incorrect?
atomgit-bot
atomgit-bot11 days agoStart code review1
foreach/foreach_log/op_host/arch35/foreach_log_tiling.cpp
@@ -167,0 +104,4 @@
104+ }
105+ tilingData->tensorCount = tensorCount;
106+ 
107+ ge::DataType dataType = context->GetDynamicInputDesc(0, 0)->GetDataType();
atomgit-bot
atomgit-bot11 days agoComments:

🟡 Medium Priority

变更行:107 行 ge::DataType dataType = context->GetDynamicInputDesc(0, 0)->GetDataType(); 以及 83 行 computeNodeInfo->GetInputInstanceInfo(0)->GetInstanceNum()、93 行 context->GetDynamicInputShape(0, tensorIndex)->GetStorageShape() 全部是无校验的链式解引用。

旧实现对每一处都做了 OP_CHECK_NULL_WITH_CONTEXT(idxInstanceInfoPtr、memberDesc、shapePtr),并且对 tensorNum == 0 做了专门短路:旧注释“An empty tensor list is legal (consistent with the aclnn entry): keep the default dtype for the tiling key and a zero element total”。本次删除该校验后,空列表路径(GetDynamicInputDesc(0, 0) 对 0 个实例取第 0 个成员,返回 nullptr)会在 tiling 阶段直接空指针解引用;本文件 122 行仍保留 tensorCount > 0 && 条件,说明作者并未排除空列表到达这里的可能。

失败模式:两种都不好——(a) 空 TensorList 输入时 tiling 空指针崩溃(tiling 进程 segfault,图编译失败);(b) 若共享校验器 ValidateHomogeneous<1> 已经拒绝空列表,则本算子对空列表的行为由“合法并通过 tiling(与 aclnn 入口一致)”被动变成“tiling 失败”,与 arch22/aclnn 入口契约不一致,属于行为回归。

修复方向:对 GetInputInstanceInfo(0)、GetDynamicInputShape(0, i)、GetDynamicInputDesc(0, 0) 的结果补空指针校验;并对 tensorCount == 0 走与旧实现一致的短路返回(BlockDim=1、设置 local memory/workspace/tilingKey 后返回成功),保证空列表语义不变。

建议:为 GetInputInstanceInfo(0)、GetDynamicInputShape(0, i)、GetDynamicInputDesc(0, 0) 补空指针检查;空列表(tensorCount==0)按旧实现提前返回成功(BlockDim=1、设置 UB/workspace/tilingKey),保持与 aclnn 入口一致的合法性。

likedislike
Incorrect?
atomgit-bot
atomgit-bot11 days agoStart code review1
foreach/foreach_log/op_host/arch35/foreach_log_tiling.cpp
@@ -167,0 +80,4 @@
80+ return ge::GRAPH_FAILED;
81+ }
82+ 
83+ const int32_t tensorCount = static_cast<int32_t>(computeNodeInfo->GetInputInstanceInfo(0)->GetInstanceNum());
atomgit-bot
atomgit-bot11 days agoComments:

🟠 High Priority

变更行:foreach_log_tiling.cpp:83 取 tensorCount = GetInputInstanceInfo(0)->GetInstanceNum() 后直接进入 92-104 的循环,写入 tilingData->tensorDataCountList[tensorIndex];该数组在 foreach_log_tiling_data.h:33 只有 FOREACH_LOG_MAX_TENSOR_COUNT = 256 个元素,而新的 tiling 全文没有再把这个常量与 tensorCount 比较过(grep 确认 FOREACH_LOG_MAX_TENSOR_COUNT 仅在头文件声明处出现)。

被删除的旧实现明确做过这个校验:OP_CHECK_IF(tensorNum > MAX_TENSOR_NUM, ...),注释即写明“tiling data arrays are sized to MAX_TENSOR_NUM; accepting more members would silently drop the tail, so reject the oversized list instead”。本次改动删掉了该守卫,但保留了 256 数组,属于单边改动造成的新不一致。

失败模式:x 是 DYNAMIC(列表)输入,op_def 未限制实例数;当图上一个 ForeachLog 覆盖超过 256 个张量时,循环会把 int64_t 逐个写到 tensorDataCountList[256..tensorCount-1],越界覆盖紧邻的 tensorStartList/tensorEndList/tensorStartOffsetList/tensorEndOffsetList 乃至 tiling 缓冲区之外的内存 → tiling 数据被破坏(静默错算/核区间错乱)或 tiling 进程堆写坏崩溃。随后 LocateFlatOffset/核区间计算也建立在被踩坏的数组上。

修复方向:在取到 tensorCount 之后立即用 FOREACH_LOG_MAX_TENSOR_COUNT 校验并报错返回(同时也可顺带拒绝非正值)。

建议:在拿到 tensorCount 后立刻做上限校验:tensorCount 超过 FOREACH_LOG_MAX_TENSOR_COUNT(256) 时 OP_LOGE 并返回 GRAPH_FAILED,避免后续写 tensorDataCountList 越界。

Suggestions
83
- const int32_t tensorCount = static_cast<int32_t>(computeNodeInfo->GetInputInstanceInfo(0)->GetInstanceNum());
83
+ const int32_t tensorCount = static_cast<int32_t>(computeNodeInfo->GetInputInstanceInfo(0)->GetInstanceNum());
84
+ OP_CHECK_IF(tensorCount > static_cast<int32_t>(FOREACH_LOG_MAX_TENSOR_COUNT),
85
+ OP_LOGE(context, "tensorCount[%d] exceeds the limit %u", tensorCount,
86
+ static_cast<uint32_t>(FOREACH_LOG_MAX_TENSOR_COUNT)),
87
+ return ge::GRAPH_FAILED);
Apply suggestion
likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
11 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
11 days ago Added label: ci-pipeline-passed
yuning_chen
yuning_chenCollaborators
11 days ago Comments:

您好,pr合入已准备就绪,请尽快联系committer进行检视,谢谢!
@zhajianqing123 @u010470851 @fanqirui @wang-xing001 @wangrui_ @Chen_HaoWen @kevin_huang1234

likedislike
chenqi317
chenqi317Collaborators
9 days ago Comments:

/lgtm

likedislike
xuejinghuixuejinghuiCollaborators
4 days ago Close pull request