Merged
perf(in_training_reduce_v2): 优化短规约连续行计算 #13846
perf(in_training_reduce_v2): 优化短规约连续行计算 #13846
Merged
raoliang_sacAdd on 16 days ago
raoliang_sac
raoliang_sacCollaborators
16 days ago

描述

优化Ascend 950上INTrainingReduceV2的短规约AR路径。原实现按每个N/C行搬运、对齐并执行水平规约,R较小时逐行开销较大。复测原issue报告失败的88例时,仅有R=1优化的版本仍有13例不达标,规约长度为3~12。

  • R=1:连续搬运多行,直接计算FP32原值和平方。
  • 2≤R≤64:连续搬运多行,每个向量通道处理一个输出行,按规约列Gather并累加,减少逐行搬运和水平规约开销。使用补偿求和,并对FP32输入采用二次幂缩放保护中间累加范围;平方仍基于原输入计算。短R的累加顺序发生变化,数学定义不变。
  • Host侧按输入和两个输出的双缓冲UB预算、AIV核数分块,保留64位GM偏移,并限制VF循环计数及DMA字节数。小UB场景保留原路径回退。
  • 复用现有tiling key和数据结构。R>64及多通道ARA路径保持原运算逻辑;优化仅涉及arch35。新增6项Host UT和8条小规模仓内ST,原有20条ST保留。

本PR仅涉及INTrainingReduceV2的7个文件。提交已整理为一个,受测提交为8de2039bbd2b1f27ef112d9f949ece15b68afb20,基于本轮实际fetch的官方master 47b9e324a4686f7f42c54da69eab4dbfd1c2e83a完成rebase。squash前后同基点完整diff及Git tree一致;rebase后任务diff及算子运行源码哈希保持一致。

关联的Issue

主仓跟踪:#8324。

原始问题来源:wangyueting/changwei #206。已按原附件全部300例复测,包括原报告的88个失败用例;下列结论来自本地950PR/A100实测,未代替原L20环境执行复测。

测试

线上CI:已提交compile触发并回查,受测提交与PR HEAD一致;此处仅确认触发,线上最终结果待平台返回。

环境:Ascend950PR_9579、A100-PCIE-40GB;任务内独立CANN_Master/CANN_Revise,CANN 9.3.0、20261028包;TTK提交a996a41594dc9e81430d7f169b0ad42f917d16e9。所有TTK执行显式--pc=1,所有构建不超过8并行任务。

本轮按原issue的eager Torch组合计算作为GPU主验收基准。NPU/A100共用冻结输入,与未修改的仓内FP64 golden执行cross_check L1,两个输出均检查。没有修改golden或放宽精度阈值;原R=1阶段的compiled GPU结果仅作为补充,不用于本轮原issue闭环。

同带宽折算:(A100耗时/950PR耗时)×(1.55/1.6),要求≥0.1。NPU每例采集10轮设备耗时,排除首轮后取9轮中位数;A100按原eager计算使用CUDA profiler,预热后采集10轮,统计设备计算时间。

验证范围 结果
原附件全部300例 三方精度300/300、性能300/300通过,最低折算0.110197
最终掩码整理后的43个受影响原始用例 三方精度和性能43/43通过
新增8个大规模短R边界用例 三方精度和性能8/8通过,最低折算1.507567
最终包kernel release回归100例 100/100通过,全部BINARY_MATCH,两个输出均通过
最终包GEIR 28例 静态、动态两种模式的两个输出均通过
最终包OOM专项8例 实际OOM编译和执行通过,核对FP16/FP32产物的OOM元数据,越界检查通过
最终包手工数值专项16例 消减、大小数混合等场景的两个输出均通过三方精度检查
950/A2本地CI 定向编包、Host/Kernel检查、pre-commit、OAT及文档链接检查通过;Host UT分别106项、12项通过

全量300例运行后,仅将VF中的三目掩码计数改为等价算式。5个FP32 kernel二进制和5个Host共享库逐字节一致;5个FP16二进制的变化限定在短R VF及对应调用长度/地址重定位。最终包重测全部43个受影响原始用例,另外257例使用已通过的未变计算路径结果;最终包同时完成上述100例release、28例GEIR、8例OOM和16例数值回归。

剩余13个失败场景修复后,相对仅有R=1优化的版本,NPU加速比为85.7~343.2,输入及FP64 golden哈希一致。示例:

原始用例 短R优化前NPU/μs 最终NPU/μs A100 eager/μs 同带宽折算
013,FP16,R=9 956.777 11.161 94.0 8.1590
019,FP32,R=7 5290.612 28.206 195.0 6.6974
142,FP32,R=4 13813.188 40.250 383.0 9.2182

全量中最低折算来自219(FP32、ND、[271,213,137]):NPU 1353.825μs、A100 154.0μs,折算0.110197。大R对照358:NPU 761.521μs、A100 141.0μs,折算0.179370。本轮原始300例及新增8例均无低于0.1的场景,不以这些结果推断未测试的全部shape。

A2隔离检查:编译命令中没有本算子的arch35源文件,A2 Host产物无本算子的950 tiling符号,12项Host UT为通用推导测试,没有混入arch35 tiling UT。

安装环境说明:主线公共graph库安装校验涉及aclsysGetVersionNum加载依赖,仅安装器进程预加载同一套CANN的libascendcl.so。实际TTK执行均无LD_PRELOAD,未改公共模块或绕过推导回调。

文档更新

按当前文档指引,将README_en.md中产品差异表整理为“Constraints”下的分产品列表,保留原有支持范围和约束事实。中文README和调用示例未改,无公开接口、数据类型、格式或数学定义变化。tiling字段注释同步说明短R路径的复用含义。

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for raoliang_sac's contribution.)
raoliang_sacraoliang_sacCollaborators
16 days ago Created pull request, commit 4a74d599
raoliang_sacraoliang_sacCollaborators
16 days ago Associated issue: [性能问题][INTrainingReduceV2]INTrainingReduceV2算子存在 性能问题(单case失败)
atomgit-bot
atomgit-bot
16 days ago Comments:

变更摘要

本 PR 针对 INTrainingReduceV2 在 Ascend 950 上的 AR 全规约路径,在规约轴长度 R=1 时新增了一条专用处理路径:不再逐行对齐、搬移并做水平规约,而是把 N*C 个元素视为连续的一维数据,按 UB 容量与 AIV 核数分块,一次连续搬运后用向量指令同时得到 sum 与 square_sum。Host 侧新增 DoR1Tiling() 生成该路径的 tiling 参数,Kernel 侧新增 ProcessR1() / CalculateR1Vf() 执行连续搬运与向量计算,并复用原有 tiling key 与数据结构,对 R>1、ARA、sub-R 路径的逻辑保持不变。

主要改动

  • 新增 R=1 展平 tiling 入口: 在 IsCapable() 中当 r == 1 时提前转入新方法 DoR1Tiling(elemSize),并在 in_training_reduce_v2_tiling.h 中声明该私有方法,使 R=1 走独立的 tiling 计算分支。
  • 按 UB 容量与核数计算分块: DoR1Tiling() 以 ubSize - RESERVE_FOR_ALIGN 为可用空间,按 DOUBLE_BUFFER_COUNT * (elemSize + REDUCE_OUTPUT_COUNT * sizeof(float)) 计入输入与两路输出双缓冲占用,推导 maxTileElements;再用 CeilDiv/CeilAlign 做多核均衡,并以 numeric_limits<uint16_t>::max() * vectorElements 限制 VF 循环上限,最后回填 cInner、cOuter、cTail、perCoreCnt、totalRows、totalElements、totalTiles 与缓冲区字节数字段。
  • Kernel 侧连续搬运与向量计算: 新增 CalculateR1Vf<T_X> 模板,FP32 直接 LoadAlign、FP16 走 DIST_UNPACK_B16 加载后 Cast,用 UpdateMask 处理尾块,Mul 得到平方后分别 StoreAlign 到 sumUb 与 squareUb;配套新增 ProcessR1() 按 tile 遍历、单次 CopyInX(offset, 1, count, count) 搬运并输出,CalculateR1() 按 VL_FP32 计算 loops 后调用 asc_vf_call。
  • R=1 时跳过无用 scratch 缓冲: 在 InitBuffer() 中当 numR_ == 1 时直接返回,不再为 binaryAddBuf_ 与 squareBinaryAddBuf_ 分配 scratch 空间,降低 UB 占用。
  • tiling 字段注释更新与 Host UT 补充: INTrainingReduceV2ARFullReduceTilingData 中 cInner、cOuter、cTail、totalTiles 的注释补充了 R=1 时复用为展平 N*C 语义的说明;UT 中 RunTiling() 新增 blockDimOut 出参,并新增 4 个测试用例,覆盖跨 N/C 实例边界与多 shape 分块、ChannelFirst/单通道格式路由、64 位总元素数与 VF 循环上限、以及最小 UB 容量边界。

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

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

代码审查

✅ 未发现问题


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

likedislike
Incorrect?
raoliang_sacraoliang_sacCollaborators
16 days ago Modify description of pull request
CANN-robotCANN-robotCollaborators
16 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
16 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

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

Module Approval Details

module lgtm status approve status
norm ✅ 汤平川, 陈展熹 (2/2) ✅ 汤平川, 陈展熹 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
16 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
7 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
15 days ago Added label: cann-cla/yes
raoliang_sac
raoliang_sacCollaborators
15 days ago Comments:

compile

likedislike
raoliang_sacraoliang_sacCollaborators
15 days ago Pre merge succeeded (commit_id: f375448bb2bc244aeda4cae8a78ceaea0be2c589)
CANN-robot
CANN-robotCollaborators
15 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
15 days ago Added label: ci-pipeline-running
raoliang_sacraoliang_sacCollaborators
15 days ago Modify description of pull request
CANN-robotCANN-robotCollaborators
15 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
15 days ago Added label: ci-pipeline-passed
yuning_chen
yuning_chenCollaborators
15 days ago Comments:

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

likedislike
陈展熹
陈展熹Collaborators
15 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
15 days ago Added label: approved
TangPC
TangPCCollaborators
13 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
13 days ago Added label: lgtm
CANN-robotCANN-robotCollaborators
13 days ago Closed associated issue
CANN-robotCANN-robotCollaborators
13 days ago Merged pull request