已合并
perf(logit_grad): logitgrad算子A5性能优化 #8743
luoyufan7创建于 11 天前
perf(logit_grad): logitgrad算子A5性能优化 #8743
已合并
luoyufan7创建于 11 天前
luoyufan7
11 天前

优化内容

对 aclnnLogitGrad 的 kernel 实现(logit_grad.h)进行 A5 MicroAPI 融合优化:

  • FP16 (G5r):将 Cast + Muls + Adds + Mul + Div + Compare + Select 融合到单个 MicroAPI 寄存器循环,零 PipeBarrier,零中间 UB 搬运
  • BF16 (BF16_FUSED_V1):同样的单 VF 循环融合
  • FP32:保持不变

性能数据

测量方法:batch100 event(torch.npu.Event),2100 条全量用例,5 轮 ABBA 交替配对

dtype >=1M 中位数 >=1M 平均
FP16 1.27x 1.28x
BF16 1.24x 1.34x
FP32 1.00x 1.00x

精度验证

与基线完全一致

改动范围

logit_grad.h

平台兼容性

通过条件编译 #if (CCE_AICORE == 310) 隔离 A5 优化路径,A2/A3 走原始高阶 API 路径,不受影响。

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 luoyufan7 的贡献)
Lluoyufan7
11 天前 创建了 pull request,commit a9aac1e7
atomgit-bot
atomgit-bot
11 天前 评论:

变更摘要

该 PR 针对 aclnnLogitGrad 的 kernel 实现文件 loss/logit_grad/op_kernel/logit_grad.h,在 A5(__CCE_AICORE__ == 310)上引入基于 AscendC MicroAPI 的融合计算链:对 FP16 和 BF16 分别新增 ComputeFusedFp16ComputeFusedBf16 路径,将原有的 ComputeStepOne + ComputeStepTwo 两步计算融合为一次向量计算链,减少 PipeBarrier 与中间数据搬运;FP32 仍保持原有计算路径不变。

主要改动

  • 新增 FP16→FP32 的 CastTrait 常量: 在 __CCE_AICORE__ == 310 条件下新增 G5_FP16_TO_FP32_CAST_TRAIT,为 MicroAPI 的 Cast 指定寄存器布局、饱和模式、掩码合并模式与舍入模式。
  • Process() 增加按类型分发的融合路径: 对 half 调用 ComputeFusedFp16,对 bfloat16_t 调用 ComputeFusedBf16,其他类型仍走 ComputeStepOne + ComputeStepTwo
  • CopyInAndCast()half 提前返回: 在 310 上跳过原有的 x1TensorFp32/x2TensorFp32 显式 Cast 及对应的 PipeBarrier<PIPE_V>,改由后续融合计算内完成搬入与类型转换。
  • 新增 ComputeFusedFp16(): 使用 MicroAPI 的 DataCopyDIST_UNPACK_B16)、CastMulsAddsMulDivCompareMaskAndSelect 构建融合链,完成 dy / (x * (1 - x)) 及范围裁剪,并将结果写回 x1TensorFp32
  • 新增 ComputeFusedBf16(): 基于已转换为 FP32 的 x1TensorFp32/x2TensorFp32 数据,使用与 FP16 相同的 MicroAPI 融合计算链,并将结果写回 x1TensorFp32
likedislike
atomgit-bot
atomgit-bot
11 天前 评论:

代码审查

✅ 未发现问题

likedislike
CANN-robotCANN-robot成员
11 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
11 天前 评论:

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
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
loss 汤平川, 钱泽洪 (2/2) 汤平川 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
CANN-robot
CANN-robot成员
11 天前 评论:

CLA Signature Pass

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

likedislike
CANN-robotCANN-robot成员
11 天前 将zengjuan,chenfeng61,chaotang233,crystalhu,yu-xinjie62,yangyang016,fanqirui,su-yueming,chenqi317,wang-xing001,liubo75,tangweiwei2,pingchuantang,qianzehong设为评审人
CANN-robotCANN-robot成员
11 天前 将chenfeng61,yu-xinjie62,su-yueming,wang-xing001,pingchuantang,qianzehong设为审查人
Lluoyufan7
11 天前 关联了issue:[Requirement|需求建议]: Logit/LogitGrad 算子 A5 性能优化
Lluoyufan7
11 天前 修改了pull request 的描述
luoyufan7
11 天前 评论:

compile

likedislike
Lluoyufan7
11 天前 update merge request[project id: 7665709, iid: 8743, commit_id: 12644ba0f9a5c3f3909a8c48a8eb5b23a4197efc] virtual merging success
CANN-robotCANN-robot成员
11 天前 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
11 天前 评论:

流水线任务触发成功
任务链接 [291c3dd0783e4bcf930357ca604307a1][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_experimental ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_950 ✅ SUCCESS >>>>> >>>>>
Compile_Pre ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_classify ✅ SUCCESS >>>>>
Compile_Ascend_X86_mobile_station_9030_ubuntu24 ✅ SUCCESS >>>>> >>>>>

[2026-08-15 15:58:14]    CI执行结束

likedislike
CANN-robot
CANN-robot成员
11 天前 评论:

流水线任务触发成功
任务链接 [6298aad84f9847df9c4be6d5e399f578][流水线指导]

任务名称状态日志下载链接
antipoison ✅ SUCCESS >>>>>
codecheck_checkpr ✅ SUCCESS
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_precommit ❌ FAILED >>>>> >>>>>
SCA ✅ SUCCESS >>>>>
likedislike
Lluoyufan7
11 天前 修改了pull request 的描述
Lluoyufan7
11 天前 update merge request[project id: 7665709, iid: 8743, commit_id: 26f8c5f80257d42dabf180fadaddb24fcfb4bdc1] virtual merging success
Lluoyufan7
11 天前 强制推送  1 个提交:ff265ef6-perf(logit_grad): A5 MicroAPI fused FP16/BF16 compute chains
Lluoyufan7
11 天前 update merge request[project id: 7665709, iid: 8743, commit_id: ceac83d2c658e66ddb3b201997ba7085a6bfdfa9] virtual merging success
CANN-robotCANN-robot成员
11 天前 删除了label:cann-cla/yes
CANN-robotCANN-robot成员
11 天前 添加了label:cann-cla/yes
luoyufan7
11 天前 评论:

compile

likedislike
Lluoyufan7
11 天前 update merge request[project id: 7665709, iid: 8743, commit_id: 2dad201e48e46ce1c2e7aeac622d55397b52917e] virtual merging success
CANN-robotCANN-robot成员
11 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
11 天前 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
11 天前 添加了label:ci-pipeline-failed
CANN-robotCANN-robot成员
11 天前 删除了label:ci-pipeline-failed
CANN-robot
CANN-robot成员
11 天前 评论:

流水线任务触发成功
任务链接 [f8e32470433c49ca99f1dc9a092db522][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_experimental ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_950 ✅ SUCCESS >>>>> >>>>>
Compile_Pre ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_single_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_experimental_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_950_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_classify ✅ SUCCESS >>>>>
Compile_Ascend_X86_mobile_station_9030_ubuntu24 ✅ SUCCESS >>>>> >>>>>
UT_Test_ophost ✅ SUCCESS
UT_Test_opapi ✅ SUCCESS
UT_Test_kernel ✅ SUCCESS
UT_Test_opgraph ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
API_Check ✅ SUCCESS >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>
UT_Test_report_lcov ✅ SUCCESS >>>>>

[2026-08-15 16:14:29]    CI执行结束

likedislike
CANN-robot
CANN-robot成员
11 天前 评论:

流水线任务触发成功
任务链接 [6c2fe91261c044e9bc478947aef8a5f1][流水线指导]

任务名称状态日志下载链接
antipoison ✅ SUCCESS >>>>>
codecheck_checkpr ✅ SUCCESS
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_precommit ✅ SUCCESS >>>>>
SCA ✅ SUCCESS >>>>>

[2026-08-15 16:00:32]    CI执行结束

likedislike
Lluoyufan7
11 天前 修改标题为 “perf(logit_grad): logitgrad算子A5性能优化”,原标题为“perf(logit_grad): A5 MicroAPI fused FP16/BF16 compute chains”
CANN-robotCANN-robot成员
11 天前 添加了label:api-check-pass
CANN-robotCANN-robot成员
11 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
11 天前 添加了label:ci-pipeline-passed
TangPC
TangPC成员
11 天前 评论:

/lgtm
/approve

likedislike
CANN-robotCANN-robot成员
11 天前 添加了label:approved
qianzehong成员
11 天前 评论:

/lgtm

likedislike
CANN-robotCANN-robot成员
11 天前 添加了label:lgtm
CANN-robotCANN-robot成员
11 天前 合入了pull request
qianzehong成员11 天前进行代码检视1
loss/logit_grad/op_kernel/logit_grad.h
@@ -28,6 +28,15 @@ constexpr int64_t PP_ELEMENT_NUM = 8 * 1024;
2828constexpr int64_t ONE_REPEAT_ELE_NUM_FP32 = 64;
2929constexpr int64_t ALIGN = 16;
3030 
31+#if defined(__CCE_AICORE__) && (__CCE_AICORE__ == 310)
qianzehong11 天前评论:

950判断用 NPU_ARCH == 3510

likedislike
qianzehong成员11 天前进行代码检视1
loss/logit_grad/op_kernel/logit_grad.h
@@ -215,0 +279,2 @@
279+ AscendC::MicroAPI::DataCopy<half, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>(regXHalf,
280+ xAddr + i * vfLen);
281+ AscendC::MicroAPI::DataCopy<half, AscendC::MicroAPI::LoadDist::DIST_UNPACK_B16>(regDyHalf,
qianzehong11 天前评论:

此条代码评论区间+279+281

使用LoadAlign,DataCopy会逐渐日落

likedislike
qianzehong成员11 天前进行代码检视1
loss/logit_grad/op_kernel/logit_grad.h
@@ -215,0 +292,4 @@
292+ AscendC::MicroAPI::Compare<float, AscendC::CMPMODE::LE>(maskLE, regX, regHi, preg0);
293+ AscendC::MicroAPI::MaskAnd(maskValid, maskGE, maskLE, preg0);
294+ AscendC::MicroAPI::Select<float>(regOut, regDy, regInvalid, maskValid);
295+ AscendC::MicroAPI::DataCopy<float, AscendC::MicroAPI::StoreDist::DIST_NORM_B32>(outAddr + i * vfLen, regOut,
qianzehong11 天前评论:

使用StoreAlign

likedislike
qianzehong成员11 天前进行代码检视1
loss/logit_grad/op_kernel/logit_grad.h
@@ -215,0 +333,4 @@
333+ for (uint16_t i = 0; i < vfLoopNum; i++) {
334+ uint32_t rem = count - static_cast<uint32_t>(i) * vfLen;
335+ preg0 = AscendC::MicroAPI::UpdateMask<float>(rem);
336+ AscendC::MicroAPI::DataCopy<float, AscendC::MicroAPI::LoadDist::DIST_NORM>(regX, xAddr + i * vfLen);
qianzehong11 天前评论:

同上

likedislike
qianzehong成员11 天前进行代码检视1
loss/logit_grad/op_kernel/logit_grad.h
@@ -215,0 +345,4 @@
345+ AscendC::MicroAPI::Compare<float, AscendC::CMPMODE::LE>(maskLE, regX, regHi, preg0);
346+ AscendC::MicroAPI::MaskAnd(maskValid, maskGE, maskLE, preg0);
347+ AscendC::MicroAPI::Select<float>(regOut, regDy, regInvalid, maskValid);
348+ AscendC::MicroAPI::DataCopy<float, AscendC::MicroAPI::StoreDist::DIST_NORM_B32>(xAddr + i * vfLen, regOut,
qianzehong11 天前评论:

同上

likedislike