Merged
feat: RadixTopK 新增 int64 索引变体并修复 largest 场景 NaN 与 WS 同步问题 #6335
feat: RadixTopK 新增 int64 索引变体并修复 largest 场景 NaN 与 WS 同步问题 #6335
Merged
Nice tryAdd on Oct 14
Nice try
Nice tryCollaborators
Oct 14

描述

本次改动为 RadixTopK 算子新增 int64 索引变体 RadixTopKWsI64,使排序轴长度 sortLen 超过 int32 上限(2^31-1)的超大 shape 场景可正常执行;同时修复 largest=true 场景下 ±NaN 的计数/提取口径不一致缺陷,以及 WS 变体在多轮归约、多块搬运中的同步问题。

改动原因

  • 原实现受 int32 值域限制:sortLen 超过 int32 上限时索引值域与跨核计数会溢出,IsRadixTopKSupport 直接判不支持,算子无法处理超大 shape。
  • largest=true 时 NaN 在直方图(key 域)与提取掩码(x > b)两处口径不一致:−NaN 被计入配额但 x > b 对其恒为 false,导致槽位被背景元素顶替(被顶替槽数恰等于注入的 NaN 数)。
  • WS 变体在跨核归约与多块 tileHist 搬运处缺必要同步,超大 shape 下出现 tile 级 ±1 配额错位。

改动方法

host 侧

  • radix_top_k_tiling.cpp/.h:新增 isInt64Shape_(sortLen > INT32_SORT_LEN_MAX=2^31-1);新增 CalcInt64TilingParams(int64 输出索引使 UB 开销系数由 16 提到 20);GetTilingKey 增加第 4 个模板参数 isInt64Shape(追加在最高位,原有 3 参数 key 编码不变);int64 变体强制 needWorkspace=true 并复用 large shape 的 workspace 布局;新增防御性校验(indices 输出必须 int64、kValue 与 per-core 元素数不超 int32 上限)。
  • radix_top_k_binary.json(ascend910b / ascend910_93):新增 RadixTopK_x_{bfloat16,float16}_indices_int64_k_int32 两个编译配置。
  • topk.cpp(op_api):IsRadixTopKSupport 增加 indicesDType 入参,sortLen > int32 上限时要求 indices 为 int64 且 k > 0;TopkCommon 在超大 shape 且 indices 为 int64 时按 int64 分配输出索引。

kernel 侧

  • radix_top_k.cpp:kernel 模板新增 isInt64Shape 参数并路由到 RadixTopKWsI64。
  • 新增 radix_top_k_ws_i64.h:RadixTopKWsI64 继承 RadixTopKWs,完整复用其 WS 搬运与同步结构,仅把触及 int64 值域的部分改为标量 int64 实现——跨核 globalHist 归约、边界查找、边界计数跨核前缀和;索引输出在 int32 域压缩 tile 内索引,再逐元素加 int64 tileOffset 后向扩展写入,代价 O(kValue)。per-core 直方图/tileTopK/coreTopK 等 int32 安全域保持不变。
  • radix_top_k_common.h:NegateDataForLargest 重命名为 CanonicalizeDataForLargest,不再取反,改为把 ±NaN 规范化为同一个正 NaN(降序改由 key 域取补承担);TwiddleInB16 对 largest=false 在末尾做键域取补;SubTopKAndCopyOut 在 largest=true 且非 EQ 时改用 NOT(LE) 掩码,使 NaN 与 key 域配额口径一致,并对非 EQ 通过按 tile 配额封顶避免越界写。
  • radix_top_k_ws.h:私有成员访问级别改为 protected 供 i64 子类复用;SubProcess 增加 SyncAll,保证快核下一轮 CopyOut2Ws 覆盖前所有核已完成 CopyInGlobalHist 读取。
  • radix_top_k_ws_utils.h:CopyTileHistInWs 多块循环迭代间补 MTE3ToMTE2Sync,避免 GM→UB 覆盖尚未搬走的头部数据。
  • radix_top_k_ub.h / radix_top_k_ws.h:HandleLastRoundBoundary 补同步说明并统一 SToVSync 口径。
  • radix_top_k_tiling_key.h / radix_top_k_struct.h:模板参数声明新增 isInt64Shape、注释对齐。

关联的Issue

测试

  • op_kernel UT 链接桩同步更新模板参数(isInt64Shape=false)。
  • fp16 / bf16 ±NaN 边界:largest=true/false 两个方向与 torch 语义逐项比对(±NaN、±Inf、±0、次正规码),修正后 NaN 入选与配额统计一致。
  • 超大 shape(sortLen > int32 上限):以 K=240000 等 i64 场景验证 tile 级配额正确、int64 索引输出正确。

文档更新

无

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for Nice try's contribution.)
Nice tryNice tryCollaborators
Oct 14 Created pull request, commit 2fb1bc46
atomgit-bot
atomgit-bot
Oct 14 Comments:

变更摘要

本次改动为 RadixTopK 算子引入了 int64 大 shape 变体:当排序轴长度 sortLen 超过 int32 上限(INT32_SORT_LEN_MAX)时,走新的 int64 模板并输出 int64 索引;同时统一了 ±NaN 在计数相与提取相的语义(largest=true 时 NaN 必入选、largest=false 时 NaN 不入选)。改动范围覆盖 tiling 判定与 TilingKey 模板参数、内核模板分发、op_api 支持判断及二进制组合配置。

主要改动

  • 新增 int64 大 shape 内核变体: 新增 radix_top_k_ws_i64.h,RadixTopKWsI64 继承 RadixTopKWs,将跨核 globalHist 归约、边界查找、边界计数前缀和等改为 int64 标量累加,并通过 VectorMergeI64 向量拼装 int64 索引、CopyOutResultI64 写回 int64 输出,outIndexBuf_ 按 int64 加倍;radix_top_k_ws.h 相应将相关成员由 private 改为 protected 供子类复用。
  • Tiling 判定、校验与 TilingKey 扩展: radix_top_k_tiling.cpp/.h 新增 isInt64Shape_ 标志、常量 INT32_SORT_LEN_MAX 与 CalcInt64TilingParams,在 int64 场景校验输出 indices 必须为 DT_INT64、kValue 与 per-core 元素数不超 int32 上限,并让 needWorkspace 覆盖 int64 变体;GET_TPL_TILING_KEY 追加 isInt64Shape 参数,radix_top_k_tiling_key.h 相应新增该 BOOL 模板参数(追加在最高位,保证既有 key 编码不变)。
  • 内核模板分发扩展: radix_top_k.cpp 主 kernel 模板参数由 <sorted, largest, isLargeShape> 扩展为 <sorted, largest, isLargeShape, isInt64Shape>,并包含、优先分派到 RadixTopKWsI64 实例。
  • NaN 语义统一处理: radix_top_k_common.h 将 NegateDataForLargest 重命名为 CanonicalizeDataForLargest,把 ±NaN 规范化为同一正 NaN(bf16 借 fp32 域、fp16 原生 Muls(+1));TwiddleInB16 对 largest=false 追加键域取补以实现降序并将 NaN 落到序键最底端;SubTopKAndCopyOut 对 largest=true 的非 EQ 通过改用 NOT(LE) 掩码(= (x > b) ∪ isNaN),并将 rsvdCntV 按 tile 配额 curTileK 封顶避免越界写。
  • op_api 支持与二进制配置: topk.cpp 的 IsRadixTopKSupport 增加 indicesDType 参数,sortLen > MAX_INT32_INPUTSIZE 且 indices 为 DT_INT64 时通过并分配 int64 索引输出;ascend910b、ascend910_93 的 radix_top_k_binary.json 新增 bfloat16/float16 + indices_int64(indices_dtype=9)的组合项。

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

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Oct 14 Comments:

代码审查

✅ 未发现问题


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

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Oct 14 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Oct 14 Added label: cann-cla/yes
83 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Oct 15 Added label: approved
chenxingyu18Collaborators
Oct 15 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Oct 15 Added label: lgtm
CANN-robotCANN-robotCollaborators
Oct 15 Closed associated issue
CANN-robotCANN-robotCollaborators
Oct 15 Merged pull request