Merged
fix(pooling): 修复MaxPoolGradWithArgmax NHWC小C场景部分用例精度fail的问题 #13770
fix(pooling): 修复MaxPoolGradWithArgmax NHWC小C场景部分用例精度fail的问题 #13770
Merged
小王!Add on 16 days ago
小王!
小王!Collaborators
16 days ago

描述

修复 MaxPoolGradWithArgmax NHWC 小 C 场景下 C 轴切分导致的 Argmax 解码错误和精度 FAIL。

NHWC 的 Argmax 使用全局 (h * W + w) * C + c 编码。MergeHWC/MergeWC Kernel 的 DoMulCNhwc() 在还原 h/w 时使用块内 C(cOutputActual)作为除数;当 Tiling 把 C 切分后 cOutputActual < cOutput,解码出的 h/w 错误,梯度被散射到错误位置或被 Range Check 丢弃。

原始 FAIL 用例均命中 Key 501(MergeHWC)且块内 C 严格小于完整 C:

用例 完整 C 原块内 C 结果
fp32_int64_NHWC_000023 2 1 FAIL
bf16_int64_NHWC_000093 3 1 FAIL
fp32_int32_NHWC_000101 15 2 FAIL
fp32_int64_NHWC_000103 3 1 FAIL

Tiling 主流程 AdjustInnerSplitForMultiCore() 在 N/H/W 调整后仍无条件执行 SearchOuterSingle(splitData.cOutputInner, 1),会把已经等于完整 C 的 cOutputInner 继续收缩到 1/2 等小值。

本 PR 在 Tiling 侧按 BigC 边界限制 C 轴收缩:

const int64_t bigCDomainThreshold = baseData.vRegSize / (2 * baseData.indexBytes);
if (inputData->cX > bigCDomainThreshold) {
    SearchOuterSingle(splitData.cOutputInner, 1);
}
  • 小 C(非 BigC):不再切 C,保证 cOutputActual == cOutput,MergeHWC/MergeWC 解码恢复正确。
  • 大 C(BigC):仍允许切 C 利用多核;BigC Kernel 使用完整 cOutput 做 TransHWC2HW,本身不依赖块内 C,不会触发该精度问题。
  • 阈值与 BigC 模板 IsCapable() 的 cGrad > vRegSize / (2 * indexBytes) 边界一致。

说明:本 PR 为 Tiling 侧规避,未修改 Kernel 解码逻辑;原始 Kernel 根因仍保留在 DoMulCNhwc(),后续如要彻底消除潜在缺陷,可另行评估完整 C 解码的 Kernel 修复方案。

关联的Issue

#8366
https://gitcode.com/cann/ops-nn/issues/8366

测试

  • 已完成静态分析:原始 4 条 FAIL 用例的完整 C 分别为 2、3、15、3,均小于等于对应阈值(int32=32、int64=16),修复后不再切 C。
  • 本地测试通过,全量批跑用例无更能、性能回退,4条精度fail的用例修复后全部pass,冒烟已过

文档更新

不涉及

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for 小王!'s contribution.)
小王!小王!Collaborators
16 days ago Created pull request, commit c26c84bd
atomgit-bot
atomgit-bot
16 days ago Comments:

变更摘要

本 PR 修复 MaxPoolGradWithArgmax 在 NHWC 布局且 Tiling 对 C 维切分时(如 Tiling Key 501)的精度问题。根因是 Argmax 值按 (h * W + w) * C + c 编码,反向解码必须使用完整通道数 C,而 MergeHWC/MergeWC 共用的 DoMulCNhwc() 在调用 TransHWC2HW() 时传入的是块内通道数 cOutputActual;当 cOutputInner < cOutput 时解码出的 h/w 偏大,导致梯度散射到错误位置或被 Range Check 丢弃。改动为 DoMulCNhwc() 增加完整通道数参数并在解码处使用,同时在各 NHWC 反向 Kernel 的调用点传入 this->cOutput_,块内通道数 cOutputActual 仍仅用于 lane/通道布局与搬运宽度。

主要改动

  • DoMulCNhwc() 接口新增完整通道数参数: pool_grad_nhwc_scatter_index.h 中 DoMulCNhwc() 增加 int32_t cOutput 形参,TransHWC2HW<T3>() 的解码依据由 cOutputActual 改为 cOutput,使 Argmax 还原 (h, w) 使用完整 C 维。
  • NHWC MergeHWC Kernel 调用点传入完整通道数: max_pool_grad_with_argmax_nhwc_merge_hwc_kernel_common.h 中全部 DoMulCNhwc 调用(mask0~mask5 各分支及 W 尾段)在原有 cOutputActual 之后新增 static_cast<int32_t>(cOutput)。
  • int64 变体合并路径同步修正: max_pool_grad_with_argmax_nhwc_merge_hwc_int64_kernel_common.h 的对应 mask0~mask5 调用点同样追加 static_cast<int32_t>(cOutput),保证 int64 Argmax 索引在 C 切分下解码一致。
  • MergeWC Kernel 引入并传递完整通道数: max_pool_grad_with_argmax_nhwc_merge_wc_kernel_common.h 新增局部变量 int64_t cOutput = this->cOutput_;,并在 mask0~mask2 的 DoMulCNhwc 调用中传入该值。
  • C 切分布局逻辑保持不变: cOutputActual 仍作为块内通道布局与搬运宽度参数保留,cOutputInner == cOutput 时新旧逻辑等价,不涉及 Tiling、UB 占用与并行度调整。

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

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

代码审查

✅ 未发现问题


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

likedislike
Incorrect?
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
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

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

likedislike
83 messages are collapsed here。 See more
sgqt12
sgqt12Collaborators
13 days ago Comments:

/lgtm

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
CANN-robot
CANN-robotCollaborators
13 days ago Comments:

Pull Request 已合并或已关闭。

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

likedislike