Merged
fix(radix_top_k): 修复 WS 变体 V→MTE3 同步缺失与 CreateVecIndex4TopK 越界,统一跨核前缀和累加为 int64 #6396
Nice tryAdd on 24 days ago
fix(radix_top_k): 修复 WS 变体 V→MTE3 同步缺失与 CreateVecIndex4TopK 越界,统一跨核前缀和累加为 int64 #6396
Merged
Nice tryAdd on 24 days ago
Nice try
Nice tryCollaborators
24 days ago

描述

改动原因

RadixTopK 在 int64 大 shape(sortLen 超 int32 上限)与 WS 变体路径上存在三处缺陷,且 host UT 的 tiling key 期望值只编码 3 位,int64 主路径恒测不到:

  1. WS 变体同步缺失:Add 在 V 流水线写 tileTopK,紧接着 MTE3 读同一块 UB;PipeBarrier<PIPE_V> 只保证 V 流水线内部顺序,SToMTE3Sync 只覆盖 S→MTE3,两者都管不到 V→MTE3,MTE3 可能读到 Add 之前的旧值,导致该段 k 配额错乱。同文件 AddTileHist2TileTopKInWs 对同样的 Add→CopyOut 序列用的是 VToMTE3Sync。
  2. 索引序列越界:i64 变体的多段 regime 会命中 tileNum_ > MAX_TILE_NUM_IN_UB,此时 CreateVecIndex4TopK 中的无符号相减会下溢成巨大值,再被 tileLen_ 兜住,最终 GetWithOffset(maxIndexLen, tileNumAlign_ * 4) 的偏移会写出 6144 元素 buffer。
  3. 跨核前缀和累加溢出:前驱 core 边界 bin 累计元素数总量可达 sortLen > 2^31,UB 变体用 int32 累加(cumSumValuePrev / remainCoreBoundaryNum)存在溢出风险;同时 WS 变体维护了一份只差累加器类型的 ComputeCumSumPrevI64。
  4. UT 覆盖缺口:ComputeExpectedTilingKey 只编码 sorted/largest/isLargeShape 3 位,未编码 isInt64Shape(bit3),int64 主路径(sortLen > 2^31)恒测不到。

改动方法

  1. op_kernel/radix_top_k_ws.h
    • HandleLastRoundBoundary 中 Add→CopyTileTopKUb2Ws 的同步由 SToMTE3Sync() 改为 VToMTE3Sync(),与同文件 AddTileHist2TileTopKInWs 口径一致;
    • CreateVecIndex4TopK 增加前置防御:tileNumAlign_ >= MAX_TILE_NUM_IN_UB 时直接 return 0,下游走「现算索引」分支,与 maxIndexLen==0 的既有兜底行为一致;
    • 删除 ComputeCumSumPrevI64,改用基类 ComputeCumSumPrev(出参已是 int64),消除 UB/WS 两份仅差累加器类型的实现。
  2. op_kernel/radix_top_k_common.h:ComputeCumSumPrev 出参由 int32_t& 改为 int64_t&(每 core 边界 bin 计数 < 2^31,int64 累加对 int32 形状同样安全);IsNaNValue 的 float 位模式读取由指针 reinterpret 改为 union,规避浮点相等/自比较类告警;补充提取相与计数相 NaN 规范化口径不对称的说明。
  3. op_kernel/radix_top_k_ub.h:cumSumValuePrev / remainCoreBoundaryNum 改为 int64,回写 tileTopK 时显式 static_cast<int32_t>(per-core 配额 < 该 tile 边界 bin 计数 < 2^31,int32 通道安全)。
  4. op_host/radix_top_k_tiling.cpp:新增 static_assert(LARGE_SORT_LEN < INT32_SORT_LEN_MAX),把「isInt64Shape_ 蕴含 isLargeShape_」钉死,避免后续调大 LARGE_SORT_LEN 时 i64 分支被漏掉;needWorkspace / GetWorkspaceSize 两处 || isInt64Shape_ 补充防御性冗余注释。
  5. tests/ut/op_host/test_radix_top_k_tiling.cpp:ComputeExpectedTilingKey 补齐第 4 位 isInt64Shape(与 GET_TPL_TILING_KEY(sorted, largest, isLargeShape, isInt64Shape) 一一对应,此前 int64 主路径恒测不到);新增两条用例:i64_shape_tiling_key_bit3(sortLen=2^31 ⇒ bit3+bit2、indices int64、必须申请 workspace)、i64_shape_reject_int32_indices(sortLen > 2^31 且 indices int32 必须 tiling 失败);MakeRadixTopKTilingContext 支持指定输出 indices dtype。
  6. math/radix_top_k/README.md:约束说明由「无」改为「本算子为选取类实现,输出为选中元素在排序轴上的索引序,不保证有序」。
  7. math/topk/op_api/topk.cpp:补充 int64 大 shape 分支拦截条件的注释(说明放掉 k 比例门限与 MIN_K 门限、且 int64 索引无法通过 GatherElementsV2 补有序性的原因),不改变逻辑。

关联的Issue

测试

  • 本次提交包含 host UT 变更(math/radix_top_k/tests/ut/op_host/test_radix_top_k_tiling.cpp):修正 tiling key 期望值编码并新增 int64 主路径两条用例;本地未执行,预期由 CI 运行。
  • kernel 侧改动(VToMTE3Sync、CreateVecIndex4TopK 防御、int64 累加)建议结合 RadixTopK int64 大 shape 场景回归验证(实施类用例未包含在本次提交内)。

文档更新

更新 math/radix_top_k/README.md 约束说明(补充输出无序约束);另含 op_host/op_kernel 注释更新,不涉及其他文档。

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for Nice try's contribution.)
Nice tryNice tryCollaborators
24 days ago Created pull request, commit 5c73ce72
CANN-robotCANN-robotCollaborators
24 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
24 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
*/*/README.md ✅ 蔡雯雯, 陈兴宇, yue-ma (3/2) ✅ 蔡雯雯 (1/1)
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

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

likedislike
CANN-robotCANN-robotCollaborators
24 days ago Set yue-ma,zhou-qilong,gitee-duhuiping,wangrui_,rxtfeng,loov1,zhangzijie,jia0liang,zl_hw,songkai111,gitcode-chenjiao,chenxingyu18,pingchuantang,llimwang,gubaocheng,andong_hw,fengying555682,xuejinghui,zhanw_coding,clinglai0517,caiwenwen,yanglu-1 as approver
CANN-robotCANN-robotCollaborators
24 days ago Set yue-ma,zhou-qilong,gitee-duhuiping,wangrui_,rxtfeng,loov1,zhangzijie,jia0liang,zl_hw,songkai111,gitcode-chenjiao,chenxingyu18,pingchuantang,llimwang,gubaocheng,andong_hw,fengying555682,xuejinghui,zhanw_coding,clinglai0517,caiwenwen,yanglu-1 as approver
Nice tryNice tryCollaborators
24 days ago Change title to "[WIP]fix(radix_top_k): 修复 WS 变体 V→MTE3 同步缺失与 CreateVecIndex4TopK 越界,统一跨核前缀和累加为 int64", originally titled "[WIP]update comment and add host ut".
Nice tryNice tryCollaborators
24 days ago Modify description of pull request
Nice try
Nice tryCollaborators
24 days ago Comments:

compile

likedislike
Nice tryNice tryCollaborators
24 days ago Pre merge succeeded (commit_id: 9c2993abb2553cf8743d78948eb7064b297edad4)
CANN-robot
CANN-robotCollaborators
24 days ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
24 days ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
24 days ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
24 days ago Added label: ci-pipeline-passed
sunchunCollaborators
23 days ago Comments:

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

likedislike
Nice tryNice tryCollaborators
23 days ago Draft state has been cancelled
yanglu-1Collaborators
23 days ago Comments:

/lgtm
/approve

likedislike
caiwenwenCollaborators
23 days ago Comments:

/lgtm
/approve

likedislike
caiwenwenCollaborators23 days agoStart code review2
math/radix_top_k/README.md
Outdated
@@ -98,3 +98,3 @@
9898## 约束说明
9999 
100-无
100+- 本算子为选取类实现,输出为选中元素在排序轴上的索引序,不保证有序
caiwenwen23 days agoComments:

少了句号

likedislike
System
System MessageSystem
23 days ago Comments:

changed this line on 6ff7b42b view diff detail

Nice tryNice tryCollaborators
23 days ago Pre merge succeeded (commit_id: 3ec20e4784df0efd28f21187e8ec2093f646dd80)
Nice tryNice tryCollaborators
23 days ago Force-pushed 1 commits:6ff7b42b-update comment and add host ut
Nice tryNice tryCollaborators
23 days ago Pre merge succeeded (commit_id: 5c33476a0d48adbaf04c9596ab593faf9239dcde)
CANN-robotCANN-robotCollaborators
23 days ago Removed label: cann-cla/yes
CANN-robotCANN-robotCollaborators
23 days ago Removed label: ci-pipeline-passed
CANN-robot
CANN-robotCollaborators
23 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
23 days ago Added label: cann-cla/yes
Nice try
Nice tryCollaborators
23 days ago Comments:

compile

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

/lgtm
/approve

likedislike
Nice tryNice tryCollaborators
23 days ago Resolved final issue
chenxingyu18Collaborators
23 days ago Comments:

/lgtm
/approve

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

/lgtm
/approve

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