已合并
perf: 修复 scatter reduce include_self 宽行场景性能回退(含 fp16/bf16) #10096
perf: 修复 scatter reduce include_self 宽行场景性能回退(含 fp16/bf16) #10096
已合并
BartonFang创建于 9月8日
BartonFang
BartonFang成员
9月8日

背景

在 9.2.0-beta.2 上复现用户场景 [4096,64640] fp32、dim≈1、index[4096,1](每行仅一个 scatter 元素、include_self=true、reduce=add)出现性能回退:总耗时 21603us,其中 ScatterElementsV2 19855us(回退前基线约 1.8ms)。!10043 通过整体回退 #6460/#6624/#7736/#8714 验证性能可恢复,但同时移除了 min/max/mean 等新功能。本 PR 定位到真正的热点并做正向修复,保留上述 4 个 PR 的功能。

根因

该簿记机制由 #6460(Add min, max, and mean modes to the scatter_reduce operator, d382188b4)引入:该 PR 把 arch22 legacy 内核从单一 MODE 模板的 add 实现重写为通用的运行时 mode reduce 内核,新增 countLocal/InitHitCount/ReduceValue 以支持 min/max/mean 与 include_self=false 语义,同时把原先直接累加的 add 路径也并入了 hit-count 路径。重写后的内核:

  • ProcessSmall/ProcessScatter 每个 chunk 先调用 InitHitCount(inputAlign),按对齐后的整行宽度把 countLocal 清零;
  • ScatterSetValue 对每次 scatter 都执行 countLocal.GetValue/SetValue。

宽行(64640)+ 每行只有 1 个元素需要更新的场景下,countLocal 的全量初始化/维护 O(row×chunk) 成为绝对热点。而 hit count 仅在以下场景必需:

  1. include_self=false:需要区分“首次命中直接写 update”与后续累加;
  2. mean:需要命中次数做除法。

include_self=true 的 add/mul/min/max 每个位置都有确定初值 self、命中同权归约,结果与“第几次命中”无关,计数是纯写不读的死开销。另外同批 #6624 仅放宽 host 侧 cache-op xDim1 保护(扩大暴露面),#7736(AICPU 拦截新 mode)与 #8714(文档)与本热点无直接关系。性能劣化本质不是新增 min/max/mean 功能的固有代价,而是通用化把 include_self=true 的 sum 也并入了 hit-count 内核,因此可以按本 PR 方式正向修复,无需整体回退。

修改

index/scatter_elements_v2/op_kernel/scatter_elements_v2.h(两个 commit,共 ~35 行变更)

  1. 066f2db35:InitHitCount/ScatterSetValue 仅在 NeedHitCount() 为真时读写 count;fp32 的 include_self=true add/mul/min/max 不再做整行清零与逐命中计数,count buffer 也改为按需分配。
  2. 5adb7d38b:把 IsCastFloat(fp32 中间累加开关)从 NeedHitCount 中解耦。fp16/bf16 归约仍走 fp32 中间累加 + CAST_RINT 单次写回(精度行为不变),但 include_self=true 的 add/mul/min/max 同样跳过计数。

最终逻辑:

  • NeedHitCount = isReduceMode && (includeSelf == 0 || mode == MEAN);
  • IsCastFloat = 低精度 dtype && isReduceMode(与计数无关)。

仅影响 legacy kernel 内部簿记与 buffer 分配,不改 tiling、接口与运算语义。

验证(910B 实机 ATK,同 [4096,64640]/每行 1 命中场景)

性能(performance_device,--performance_data 300,100,80)

版本 dtype 总耗时 ScatterElementsV2
beta2(含 4 PR) fp32 21603us 19855us
revert(!10043) fp32 3548us 1799us
本 PR fp32 ~3538us 1792us
fix1(仅 fp32 改动) fp16 — 21208us
本 PR fp16 ~1846us 925us

fp16 数据说明:#6460 的计数簿记同样拖累 fp16/bf16 原 add 路径(保留计数时 21.2ms),本次一并修复。

正确性

  • 仓库自带 atk_aclnnScatterReduce.json 29/29 Pass(fp32 sum、include_self=true);
  • fp32 大 shape accuracy md5 比对 Pass;
  • fp16 大 shape accuracy(CPU golden,single_bm)Pass;
  • fp16 同输入下“保留计数版本 vs 本 PR”输出 md5 完全一致(逐位相同),证明删除计数不改变任何结果;
  • include_self=false、mean、mul/min/max 分支经代码核对仍完整走原 hit-count/cast 路径,不受本次收紧影响;min/max/mean/include_self=false 等扩展模式受 upstream 既有 aclnnScatterReduce host 参数检查限制(仅支持 float32+sum+include_self=true)无法经该接口直测。

相关:!10043(回退验证,证明性能问题成立);本 PR 为正向修复方案,可替代整体回退。

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 BartonFang 的贡献)
BartonFangBartonFang成员
9月8日 创建了 pull request,commit d2f48d62
atomgit-bot
atomgit-bot
9月8日 评论:

变更摘要

本 PR 用于修复 9.2.0-beta.2 中 scatter_elements_v2 legacy 内核(tilingKey=110)在宽行、每行仅一个 scatter 元素且 include_self=true 场景下的性能回退。根因是旧实现对所有 reduction 模式统一启用 hit-count 簿记(每 chunk 全量初始化/维护 countLocal,代价 O(row×chunk)),而 include_self=true 的 add/mul/min/max 语义上并不需要命中计数。修改将 scatter_elements_v2.h 中的 NeedHitCount() 判定收紧为仅在 include_self=false、SCATTER_MODE_MEAN、fp16/bf16 cast 路径下返回 true,并让 ScatterSetValue 仅在需要时读写 countLocal,在恢复性能(回退前约 1.8ms 基线)的同时保留 min/max/mean 等既有功能。

主要改动

  • 收紧 NeedHitCount() 判定: 命中计数需求从"所有 reduction 模式"收窄为 includeSelf == 0 || mode == SCATTER_MODE_MEAN || IsCastFloatType(),非 reduce 模式直接返回 false,使 include_self=true 的 add/mul/min/max 等模式不再进入命中计数逻辑。
  • ScatterSetValue 条件化命中计数簿记: 将 countLocal.GetValue/SetValue 及 useUpdateOnly 的首次命中判断整体包入 if (NeedHitCount()) 分支,跳过每 chunk 的 countLocal 清零/维护,消除宽行场景的主要热点。
  • 保留 cast 精度修复路径: NeedHitCount() 中保留 IsCastFloatType() 条件,fp16/bf16 的 fp32 中间累加 + 单次写回路径仍走原 hit-count 逻辑,不影响既有精度修复行为。
likedislike
不准确?
atomgit-bot
atomgit-bot
9月8日 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
CANN-robotCANN-robot成员
9月8日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
9月8日 评论:

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

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

Module Approval Details

module lgtm status approve status
index ✅ 陈兴宇, 陈琦 (2/2) ✅ 陈兴宇 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robot成员
9月8日 评论:

CLA Signature Pass

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

likedislike
CANN-robotCANN-robot成员
9月8日 将zengjuan,chenfeng61,chaotang233,kevin_huang1234,FelixTang7,crystalhu,yangyang016,fanqirui,wangrui_,su-yueming,chenqi317,wang-xing001,liubo75,tangweiwei2,chenxingyu18,pingchuantang,qianzehong设为评审人
CANN-robotCANN-robot成员
9月8日 将chenfeng61,kevin_huang1234,FelixTang7,wangrui_,su-yueming,wang-xing001,chenxingyu18,pingchuantang,qianzehong设为审查人
BartonFangBartonFang成员
9月8日 修改了pull request 的描述
yuning_chen
yuning_chen成员
9月8日 评论:

您好,pr合入已准备就绪,请尽快联系committer进行检视,谢谢!

likedislike
BartonFangBartonFang成员
9月9日 预合并成功(commit_id: 76d65dfaee1271b7aa4311b084a60180df7dcebb)
此处折叠了6条事件消息 查看更多
BartonFangBartonFang成员
9月9日 修改了pull request 的描述
wuyushu
wuyushu成员
9月12日 评论:

compile

likedislike
BartonFangBartonFang成员
9月12日 virtual merging failed, update merge request[project_id: 7665709, iid: 10096, target_commit_sha: 60e6fc3e53e00e6600f3050b694663116d045665], message: You are not allowed to merge to this ref
BartonFangBartonFang成员
9月12日 virtual merging failed, update merge request[project_id: 7665709, iid: 10096, target_commit_sha: 60e6fc3e53e00e6600f3050b694663116d045665], message: You are not allowed to merge to this ref
CANN-robot
CANN-robot成员
9月12日 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
9月12日 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
9月12日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
9月12日 添加了label:ci-pipeline-passed
yuning_chen
yuning_chen成员
9月12日 评论:

您好,pr合入已准备就绪,请尽快联系committer进行检视,谢谢!

likedislike
chenxingyu18成员
9月14日 评论:

/lgtm
/approve

likedislike
CANN-robotCANN-robot成员
9月14日 添加了label:approved
chenqi317成员
9月14日 评论:

/lgtm

likedislike
CANN-robotCANN-robot成员
9月14日 添加了label:lgtm
CANN-robotCANN-robot成员
9月14日 合入了pull request