Merged
fix(topk): A2/A3 rank-0输入增加L0防御校验(#3968) #6808
baijinxiang_hwAdd on 8 days ago
fix(topk): A2/A3 rank-0输入增加L0防御校验(#3968) #6808
Merged
baijinxiang_hwAdd on 8 days ago
baijinxiang_hwCollaborators
8 days ago

描述

问题

TopkCommon 在构造输出形状和访问最后轴之前没有 rank-0 防御。A2/A3 使用相同的 DAV_2201 非 RegBase 路径,self 为 rank-0 时 GetDim(dimNum - 1) 会形成无效的最后轴访问,后续可能触发设备侧异常。

兼容性取舍

  • A2/A3:同属 DAV_2201 路径,实测 rank-0、k=1 无法正常完成,增加前置防御。
  • A5:RegBase/DAV_3510 路径与 A2/A3 不同,实测 rank-0、k=0/1 可正常返回;为保持 A5 现有兼容行为,本 PR 不拦截 A5。
  • 本修改限定在 TopkCommon 的 L0 防御,只对 NpuArch::DAV_2201 生效,rank>=1 路径不变。

边界说明

aclnnTopkV2GetWorkspaceSize 的公共 ACLNN wrapper 不在本仓;第一段统一返回 161002 的检查需要在对应 ACLNN 代码中完成。本 PR 只负责阻止 A2/A3 的 L0 负索引访问,不能替代 wrapper 的第一段校验。

关联的Issue

https://gitcode.com/cann/ops-math/issues/3968

测试

  • A2:fp32/fp16/bf16 rank-0 k=1 实测失败;int32 rank-0 k=1 也失败,错误表现不同。
  • A5:Ascend950PR 上 fp32/fp16/bf16/int32 rank-0 k=0/1 实测均为 gws=0, exec=0, sync=0;k=1 回读 value=1/index=0,k=2 返回 161002。
  • 兼容性:A5 不满足 DAV_2201 条件,因此保持现有 rank-0 行为;合法 rank>=1 路径不改变。
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for baijinxiang_hw's contribution.)
Bbaijinxiang_hwCollaborators
8 days ago Created pull request, commit fb5284ff
Bbaijinxiang_hwCollaborators
8 days ago Associated issue: [Bug-Report|缺陷反馈]: aclnnTopkV2 rank-0 self 未被第一段拦截,内核触发设备侧 vector core exception(phase1=0 / phase2=561103 / sync=507035)
atomgit-bot
atomgit-bot
8 days ago Comments:

变更摘要

本次变更针对 TopkCommon 缺乏 rank 防护的问题:在 math/topk/op_api/topk.cpp 的 TopkCommon 入口新增 rank-0 防御性校验。当 self 的维度数为 0(超出文档声明的 1–8 维支持范围)时,记录参数错误日志并直接返回空结果,从而在后续 GetDim(dimNum - 1) 负索引越界读、SetDim(dim, k) 越界写以及 tiling 失败后内核仍被下发导致设备侧异常之前完成拦截。rank≥1 的各条路径行为保持不变。

主要改动

  • 新增 rank 入口校验:在 TopkCommon 起始处通过 self->GetViewShape().GetDimNum() == 0 判断,将 rank-0 标量输入前置拦截,避免负索引越界与矛盾元数据下发。
  • 返回参数错误并输出日志:以 OP_LOGE(ACLNN_ERR_PARAM_INVALID, ...) 记录可诊断的参数错误信息,明确 TopK 输入张量 rank 需在 [1, 8] 范围内。
  • 失败时返回空结果:校验不通过时返回 std::tuple<aclTensor*, aclTensor*>(nullptr, nullptr),使调用方在构造输出形状之前即终止,不再进入后续 outShape.SetDim(dim, k) 等处理流程。

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

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

代码审查

✅ 未发现问题


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

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
8 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
8 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
repo-cann/ops-math ✅ 陈兴宇, yue-ma (2/2) ✅ yue-ma, 陈兴宇 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
8 days ago Set yue-ma,zhou-qilong,wangrui_,rxtfeng,loov1,zhangzijie,jia0liang,zl_hw,songkai111,chenxingyu18,pingchuantang,llimwang,gubaocheng,andong_hw,xuejinghui,onanfield,zhanw_coding,clinglai0517 as approver
CANN-robotCANN-robotCollaborators
8 days ago Set yue-ma,zhou-qilong,wangrui_,rxtfeng,loov1,zhangzijie,jia0liang,zl_hw,songkai111,chenxingyu18,pingchuantang,llimwang,gubaocheng,andong_hw,xuejinghui,onanfield,zhanw_coding,clinglai0517 as approver
baijinxiang_hwCollaborators
8 days ago Comments:

/compile

likedislike
Bbaijinxiang_hwCollaborators
8 days ago Pre merge succeeded (commit_id: 6d40838e9fdc8fc9ce7e270e5ee63ca026cad100)
CANN-robot
CANN-robotCollaborators
8 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
8 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
8 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
8 days ago Added label: ci-pipeline-passed
chenyifengCollaborators
8 days ago Comments:

您好,PR已满足合入指标,请尽快联系Committer进行合入。如暂时不需要合入,请在PR标题前加[WIP]

likedislike
Bbaijinxiang_hwCollaborators
7 days ago Pre merge succeeded (commit_id: 689c612ac4dcd9992d08c76bf41791a520f3a6a7)
Bbaijinxiang_hwCollaborators
7 days ago Force-pushed 1 commits:3d1ead96-fix(topk): A2/A3 rank-0输入增加L0防御校验(#3968)
Bbaijinxiang_hwCollaborators
7 days ago Pre merge succeeded (commit_id: b5138e2bc379d35f6257d16e4f6c0343ffdef39a)
CANN-robotCANN-robotCollaborators
7 days ago Removed label: cann-cla/yes
CANN-robotCANN-robotCollaborators
7 days ago Removed label: ci-pipeline-passed
CANN-robot
CANN-robotCollaborators
7 days ago Comments:

Notification

This pull request has been changed(code update) or closed, so removes the following label(s): ci-pipeline-passed.

likedislike
CANN-robotCANN-robotCollaborators
7 days ago Added label: cann-cla/yes
Bbaijinxiang_hwCollaborators
7 days ago Change title to "fix(topk): A2/A3 rank-0输入增加L0防御校验(#3968)", originally titled "fix(topk): l0op TopkCommon新增rank>=1防御校验,避免rank-0设备异常(#3968)".
Bbaijinxiang_hwCollaborators
7 days ago Modify description of pull request
baijinxiang_hwCollaborators
7 days ago Comments:

/compile

likedislike
Bbaijinxiang_hwCollaborators
7 days ago Pre merge succeeded (commit_id: a2eef8eed3b4d689124315485df87701963c3d6c)
CANN-robot
CANN-robotCollaborators
7 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
7 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
7 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
7 days ago Added label: ci-pipeline-passed
chenxingyu18Collaborators
7 days ago Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
7 days ago Added label: approved
yue-maCollaborators
7 days ago Comments:

/lgtm
/approve

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