Pull Request已成功合入, 合并人@CANN-robot
(感谢 baijinxiang_hw 的贡献)变更摘要
本次变更针对 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:




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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


/compile


| 🚀 CI 流水线已启动 |
|---|
| 📋 执行详情: 点击查看流水线 |


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


/compile


| 🚀 CI 流水线已启动 |
|---|
| 📋 执行详情: 点击查看流水线 |


/lgtm
/approve


描述
问题
TopkCommon在构造输出形状和访问最后轴之前没有 rank-0 防御。A2/A3 使用相同的DAV_2201非 RegBase 路径,self为 rank-0 时GetDim(dimNum - 1)会形成无效的最后轴访问,后续可能触发设备侧异常。兼容性取舍
DAV_2201路径,实测 rank-0、k=1无法正常完成,增加前置防御。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
测试
fp32/fp16/bf16 rank-0 k=1实测失败;int32 rank-0 k=1也失败,错误表现不同。fp32/fp16/bf16/int32 rank-0 k=0/1实测均为gws=0, exec=0, sync=0;k=1回读value=1/index=0,k=2返回161002。DAV_2201条件,因此保持现有 rank-0 行为;合法 rank>=1 路径不改变。