已合并
opt maxpool3dgradwithargmax perf. #6893
opt maxpool3dgradwithargmax perf. #6893
已合并
liuchuangdev创建于 7月2日
liuchuangdev成员
7月2日

描述

优化 MaxPool3DGradWithArgmax 算子在 arch35 上的 SIMD 内核性能,主要改动如下:

1. DataCopy 优化

  • 将原来基于 LoopMode 的多层循环 DataCopy(按 d/h/high 维度分循环搬运)改为单次 DataCopy 整块 D×H×W 平面,减少 DataCopy 调度开销
  • 新增 dhwPlaneAligned_ 成员,将 D×H×W 平面对齐到 MAX_DATA_NUM_IN_ONE_BLOCK,使单次 DataCopy 可行

2. 移除 helpBuf_ 辅助缓冲区

  • 删除 TBuf<QuePosition::VECCALC> helpBuf_ 及所有 fullLoad 函数的 helpAddr 参数
  • 索引计算从 UB 预计算表改为寄存器直接生成(GenInitial3DIndicesFastGenInitial4DIndicesFast 等),消除 DataCopy 开销

3. 快速除法优化

  • 坐标索引生成使用 PrecomputeDiv/DivMagic 快速整数除法替代 MicroAPI::Div
  • high 维输出索引计算使用 GetUintDivMagicAndShift + FastDivImpl 替代 MicroAPI::Div

4. 循环合并

  • fullLoadMultipleLineProcessVF2:将 4 个 for highBlockIdx 循环两两合并为 2 个,4 个尾部循环两两合并为 2 个,减少循环开销和重复 offset 计算
  • 非全载模板 multipleLineProcessVF2 同步应用循环合并优化

涉及文件

  • max_pool3d_grad_with_argmax_simd.h:新增 fast div 工具函数
  • max_pool3d_grad_with_argmax_simd_full_load.h:全载模板核心优化
  • max_pool3d_grad_with_argmax_simd_impl.h:非全载模板同步优化
  • max_pool3d_grad_with_argmax_simd_tiling.cpp:tiling 参数适配

关联的Issue

#3739

测试

主线性能用例测试、门槛用例回归、冒烟测试

文档更新

类型标签

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 liuchuangdev 的贡献)
Lliuchuangdev成员
7月2日 创建了 pull request,commit a3ea108c
Lliuchuangdev成员
7月2日 关联了issue:[Bug-Report|缺陷反馈]: maxpool3dgradwithargmax性能问题
CANN-robotCANN-robot成员
7月2日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
7月2日 评论:

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


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
pooling 黄迪, 章武 (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

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

likedislike
liuchuangdev成员
7月2日 评论:

compile

likedislike
Lliuchuangdev成员
7月2日 update merge request[project id: 7665709, iid: 6893, commit_id: 3fb49d8910c16d0a25ea8e4866d5ebb651c868ee] virtual merging success
Lliuchuangdev成员
7月2日 修改了pull request 的描述
CANN-robotCANN-robot成员
7月2日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月2日 评论:

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

任务名称状态日志下载链接
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_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_harmony-infer-chs-nn ✅ SUCCESS >>>>>
UT_Test_ophost ❌ FAILED
UT_Test_opapi ✅ SUCCESS
UT_Test_kernel ✅ SUCCESS
UT_Test_opgraph ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>
likedislike
CANN-robot
CANN-robot成员
7月2日 评论:

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

任务名称状态日志下载链接
codecheck ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_checkpr ✅ SUCCESS
StaticCheck_codespell_check ✅ SUCCESS
StaticCheck_link_validity_check ✅ SUCCESS
StaticCheck_resource_existence_check ✅ SUCCESS
StaticCheck_tag_closed_check ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_style ✅ SUCCESS >>>>>
codecheck_precommit ⚠️ WARNING >>>>> >>>>>
SCA ❌ FAILED
likedislike
CANN-robotCANN-robot成员
7月2日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
7月2日 添加了label:ci-pipeline-failed
Lliuchuangdev成员
7月2日 update merge request[project id: 7665709, iid: 6893, commit_id: 6319df8bed18b71c3f374ed08547e4f491374e07] virtual merging success
Lliuchuangdev成员
7月2日 强制推送  1 个提交:8097852b-opt maxpool3dgradwithargmax perf.
Lliuchuangdev成员
7月2日 update merge request[project id: 7665709, iid: 6893, commit_id: 91f2f993ab872775d53b794cb91bae7418c4d378] virtual merging success
atomgit-bot
atomgit-bot
7月2日 评论:

变更摘要

此 PR 对 MaxPool3DGradWithArgmax 算子在 arch35 上的 SIMD 内核进行了性能优化。核心思路包括:将多层循环的 DataCopy 合并为整块 D×H×W 平面单次搬运;删除 helpBuf_ 辅助缓冲区,改为寄存器直接生成索引;引入 DivMagic/PrecomputeDiv 快速除法替代 MicroAPI::Div;在全载模板 fullLoadMultipleLineProcessVF2 中对 highBlockIdx 循环进行两两合并;同时对 FilterMask3DIndexConvNcdhwFastDiv 等函数签名与实现做了适配性改造。

主要改动

  • DataCopy 平面化优化: 在 max_pool3d_grad_with_argmax_simd_full_load.hCopyIn 中,将原先基于 LoopMode 按 d/h 维度分次搬运的 DataCopyPad 改为单次搬运整块 highAxisActual_ × dhwPlaneBytes 平面,新增 dhwPlaneAligned_ 成员将 D×H×W 对齐到 MAX_DATA_NUM_IN_ONE_BLOCK,消除多层 DataCopy 调度开销。

  • 移除 helpBuf_ 辅助缓冲区: 从 MaxPool3DGradWithArgmaxNCDHWKernelMaxPool3DGradWithArgmaxNCDHWFullLoadKernel 中删除 TBuf<QuePosition::VECCALC> helpBuf_ 成员及其初始化,所有 fullLoad* 函数不再接收 helpAddr 参数;索引生成改用 GenInitial3DIndicesFastGenInitial4DIndicesFast 等寄存器直接计算方式,不再通过 UB 预计算表 + DataCopy 读取。

  • 快速除法替代 MicroAPI::Div: 新增 DivMagic 结构体和 PrecomputeDiv/FastDivInt32 工具函数,在 max_pool3d_grad_with_argmax_simd.h 中新增 GenInitial*IndicesFastGen3DIndexOneFastGen4DIndexOneFastGen3DHighIndexOneFast 等系列函数,使用 GetUintDivMagicAndShift + FastDivImpl 进行坐标索引的快速整数除法;IndexConvNcNcdhwFullLoad 中的 high 维输出索引计算同步改用快速除法。

  • 循环合并减少开销: 在 fullLoadMultipleLineProcessVF2 中将原先 4 个独立的 for highBlockIdx 主循环两两合并为 2 个、4 个尾部循环两两合并为 2 个,并将 highArgmaxOffset/highOutputOffset 的计算外提到循环外部通过步进累加实现;非全载模板 multipleLineProcessVF2 同步应用相同合并策略。

  • FilterMask3D 与索引函数签名改造: FilterMask3D 的掩码生成从串行 gtMask 逐维比较-取交改为并行生成 hMask/wMask/dMask 后一次性 MaskAndIndexConvNcdhwFastDiv 参数从 int32_tdIndexReg/hIndexReg/wIndexReg 改为 uint32_tdTmpReg/hTmpReg/wTmpReg,移除了 curDIndex/curHIndex/curWIndex 偏移减法,改用 baseOffset 统一偏移。

likedislike
liuchuangdev成员
7月2日 评论:

compile

likedislike
Lliuchuangdev成员
7月2日 update merge request[project id: 7665709, iid: 6893, commit_id: 29cff63a9d4112f7434ae225c8ae8a5875a96189] virtual merging success
CANN-robotCANN-robot成员
7月2日 删除了label:ci-pipeline-failed
CANN-robotCANN-robot成员
7月2日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月2日 评论:

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

任务名称状态日志下载链接
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_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_harmony-infer-chs-nn ✅ SUCCESS >>>>>
UT_Test_ophost ✅ SUCCESS
UT_Test_opapi ✅ SUCCESS
UT_Test_kernel ✅ SUCCESS
UT_Test_opgraph ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>

[2026-07-02 12:23:12]    CI执行结束

likedislike
CANN-robot
CANN-robot成员
7月2日 评论:

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

任务名称状态日志下载链接
codecheck ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_checkpr ✅ SUCCESS
StaticCheck_codespell_check ✅ SUCCESS
StaticCheck_link_validity_check ✅ SUCCESS
StaticCheck_resource_existence_check ✅ SUCCESS
StaticCheck_tag_closed_check ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_style ✅ SUCCESS >>>>>
codecheck_precommit ⚠️ WARNING >>>>> >>>>>
SCA ✅ SUCCESS >>>>>

[2026-07-02 12:18:32]    CI执行结束

likedislike
CANN-robotCANN-robot成员
7月2日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
7月2日 添加了label:ci-pipeline-passed
atomgit-bot
atomgit-bot
7月2日 评论:

代码审查

Closing Summary

已逐一审查了全部 5 个变更文件:

文件 审查结果
max_pool3d_grad_with_argmax_simd_tiling.cpp 1 个 P3 发现(KSIZE_STRIDE_RATIO_THRESHOLD 未使用)
max_pool3d_grad_with_argmax_simd.h 无问题(快速除法工具函数、FilterMask3D 语义修正均正确)
max_pool3d_grad_with_argmax_simd_full_load.h 1 个 P3 发现(indexIncReg 参数未使用)
max_pool3d_grad_with_argmax_simd_impl.h 无问题(非全载模板同步优化正确)
test_max_pool3d_grad_with_argmax_tiling.cpp 1 个 P3 发现(dlog_setlevel 被注释)

发现统计: P0: 0, P1: 0, P2: 0, P3: 3

整体风险评估:低。

此 diff 是一个性能优化变更,主要涉及:(1) DataCopy 从 LoopMode 改为单次 DataCopyPad 整块搬运;(2) 移除 helpBuf_ 辅助缓冲区并用寄存器直接生成索引;(3) 引入快速整数除法 (FastDivInt32/PrecomputeDiv) 替代 MicroAPI::Div;(4) 循环合并减少开销;(5) 条件化 isCheckRange 在无膨胀时跳过范围检查。

核心逻辑(FilterMask3D 边界检查语义、DataCopyPad 对齐计算、快速除法等效性)经审查均为正确。没有发现正确性、安全性或可靠性方面的 P0–P2 级别问题。三个 P3 发现均为代码清洁度问题,不影响功能。

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

💬 仅评论

likedislike
atomgit-bot
atomgit-bot7月2日进行代码检视1
pooling/max_pool3d_grad_with_argmax/op_kernel/arch35/max_pool3d_grad_with_argmax_simd_impl.h
@@ -933,30 +700,30 @@
933700 }
934701 }
935702 }
@@ -938,8 +705,7 @@ __aicore__ inline void MaxPool3DGradWithArgmaxNCDHWKernel<T1, T2, IS_CHECK_RANGE
938705}
939706template <typename T1, typename T2, const uint32_t IS_CHECK_RANGE>
940707__aicore__ inline void MaxPool3DGradWithArgmaxNCDHWKernel<T1, T2, IS_CHECK_RANGE>::multipleLineProcessVF2(
941- __local_mem__ computeType* yAddr, __local_mem__ T1* gradAddr, __local_mem__ T2* argmaxAddr,
942- __local_mem__ uint32_t* helpAddr)
708+ __local_mem__ computeType* yAddr, __local_mem__ T1* gradAddr, __local_mem__ T2* argmaxAddr)
943709{
944710 int64_t wOutput = wOutput_;
945711 int64_t hOutput = hOutput_;
@@ -948,10 +714,12 @@ __aicore__ inline void MaxPool3DGradWithArgmaxNCDHWKernel<T1, T2, IS_CHECK_RANGE
948714 int64_t hOutputActual = hOutputActual_;
949715 int64_t dOutputActual = dOutputActual_;
950716 int32_t highOutputPlaneActual = wOutputAligned * hOutputActual * dOutputActual;
717+ int32_t hwOutputAligned = int32_t(hOutputActual * wOutputAligned);
951718 int64_t highAxisActual = highAxisActual_;
952719 int64_t curDIndex = dAxisIndex_ * dOutputInner_;
953720 int64_t curHIndex = hAxisIndex_ * hOutputInner_;
954721 int64_t curWIndex = wAxisIndex_ * wOutputInner_;
722+ int32_t baseOffsetConst = int32_t(-curHIndex * wOutputAligned - curWIndex - curDIndex * hwOutputAligned);
955723 int64_t wArgmaxAligned = wArgmaxAligned_;
956724 int64_t wArgmaxActual = wArgmaxActual_;
957725 uint16_t hArgmaxActual = hArgmaxActual_;
@@ -970,7 +738,7 @@
970738 uint16_t highBlockConcurrentCount = highAxisActual / highConcurrentCount;
971739 uint16_t highBlockRemainTail = highAxisActual - highBlockConcurrentCount * highConcurrentCount;
972740 int64_t depthStride = hArgmaxActual * wArgmaxAligned * dProBatchSize;
973741 int64_t highStride = dArgmaxActual * hArgmaxActual * wArgmaxAligned;
atomgit-bot
atomgit-bot7月2日评论:

🟡 Medium Priority

multipleLineProcessVF2 函数第 741 行:
dArgmaxActualhArgmaxActual 均为 uint16_t 类型。根据 C++ 整型提升规则,uint16_t * uint16_t 会在 int(32 位有符号)域中计算,最大值 65535 × 65535 ≈ 4.29×10⁹ 超过 INT32_MAX(2.14×10⁹),触发有符号整数溢出(UB)。溢出后的错误值随后才提升为 int64_twArgmaxAligned 相乘。

highStride 被用于 GenInitial4DIndicesFastGen4DIndexOneFast 的索引生成(第 833-844 行调用),错误的高维步长将导致梯度累加到错误的输出位置,产生精度错误。

虽然当前 UB 容量(256KB)限制了 dArgmaxActual * hArgmaxActual * sizeof(T2) 的实际取值,使该溢出在现有硬件上不易触发,但这属于项目规范明确禁止的"乘法链转换滞后"模式(32 位中间乘法后赋给 64 位),且若未来 UB 容量增大或 T2 为更小类型时将成为真实 bug。

建议:在乘法链首显式转换为 int64_t,使整个乘法在 64 位域中完成:

改动建议
741
- int64_t highStride = dArgmaxActual * hArgmaxActual * wArgmaxAligned;
741
+ int64_t highStride = static_cast<int64_t>(dArgmaxActual) * hArgmaxActual * wArgmaxAligned;
应用建议
likedislike
kevin_huang1234成员
7月2日 评论:

/lgtm
/approve

likedislike
CANN-robotCANN-robot成员
7月2日 添加了label:approved
zhang-wu成员
7月2日 评论:

/lgtm

likedislike
CANN-robotCANN-robot成员
7月2日 添加了label:lgtm
CANN-robotCANN-robot成员
7月2日 解决了最后一个问题
CANN-robotCANN-robot成员
7月2日 合入了pull request
CANN-robot
CANN-robot成员
7月2日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
CANN-robot
CANN-robot成员
7月2日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
CANN-robot
CANN-robot成员
7月2日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike