Merged
sort类算子AscendC MicroAPI整改 #4790
sort类算子AscendC MicroAPI整改 #4790
Merged
tujun6Add on Aug 17
tujun6Collaborators
Aug 17

描述

本 PR 对 sort 类算子(sort、sort_with_index、kth_value)执行 arch35 MicroAPI 旧接口别名替换整改。

CANN 9.1.0 的 arch35(ascend950)MicroAPI 中,大量旧接口名与新接口名是同一 Impl 函数的别名。本次将以下旧接口统一替换为新接口名,保证代码规范并维持二进制完全等价:

旧接口 新接口 说明
__local_mem__ __ubuf__ 存储类属性标识
DataCopy(RegTensor 首参) LoadAlign 按 Load 方向判定
DataCopy(addr 首参) StoreAlign 按 Store 方向判定
CompareScalar Compares 标量比较
ReduceSum(3 参数 MicroAPI) Reduce<ReduceType::SUM> 仅 MicroAPI 层级,排除传统高阶 4 参数 API
DataCopyGather Gather 聚集拷贝

替换统计:

算子 目录 修改文件数 替换处数 .o md5 一致
sort math/sort 5 158 ✅ 22 个 .o 全部一致
sort_with_index math/sort_with_index 1 2 ✅ 22 个 .o 全部一致
kth_value math/kth_value 2 62 ✅ 11 个 .o 全部一致
合计 8 222

注:top_k_v2 算子的同类整改已拆分到独立分支 test_tj,后续单独提交。

关联的Issue

#2665

测试

对每个算子分别执行编译验证(bash build.sh --ops=<算子> --soc=ascend950 --opkernel -j16):

  1. 编译替换后的代码,保存所有 .o 文件的 md5。
  2. git stash 暂存修改,编译原始代码,保存原始 .o 的 md5。
  3. 对比修改前后 md5,全部完全一致,证明替换是二进制等价的。
  4. git stash pop 恢复修改。

最终残留扫描:全部 28 类旧 API 零残留(不可替换项如 GatherMask、PrefixSum、传统高阶 API、参数结构体等保持不变属正常)。

文档更新

无

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for tujun6's contribution.)
Ttujun6Collaborators
Aug 17 Created pull request, commit a17d08a6
atomgit-bot
atomgit-bot
Aug 17 Comments:

变更摘要

本次 PR 主要对 math 目录下 sort 类算子(包括 sort、kth_value、sort_with_index、top_k_v2)的 AscendC MicroAPI 进行整改:将原先基于 Reg::DataCopy、__local_mem__ 等旧式写法,统一替换为语义更明确的 UB 指针类型和 Reg::LoadAlign/Reg::StoreAlign/Reg::Gather/Reg::Compares/Reg::Pack 等新 MicroAPI。改动以等价的 API 替换为主,不改变算子核心算法与数据流。

主要改动

  • UB 内存指针类型整改:将 GetPhyAddr() 返回的指针及相关形参/局部变量从 __local_mem__ 统一改为 __ubuf__,覆盖 kth_value_radix_select.h、radix_more_core_base.h、radix_sort_simd_utils.h、top_k_radix_block_sort_*.h 等文件。
  • 数据搬移接口拆分:将 Reg::DataCopy 根据读写语义替换为 Reg::LoadAlign(UB→寄存器读取)与 Reg::StoreAlign(寄存器→UB 写入),并将 Reg::DataCopyGather 替换为 Reg::Gather。
  • 比较与掩码接口替换:将 Reg::CompareScalar 替换为 Reg::Compares,Reg::MaskAnd 替换为 Reg::And,Reg::MaskSel 替换为 Reg::Select,Reg::MaskPack 替换为 Reg::Pack。
  • 归约接口替换:将 Reg::ReduceSum 统一替换为 Reg::Reduce<ReduceType::SUM>,涉及 radix_more_core_base.h 与 radix_sort_top_k.h 中的直方图状态统计和求和逻辑。
  • 保持算法逻辑不变:上述改动均为一对一的 API 等价替换,未引入新的业务逻辑、数据结构或配置变更,涉及的所有 radix sort、merge sort、top_k 核心流程保持不变。
likedislike
Incorrect?
CANN-robot
CANN-robotCollaborators
Aug 17 Comments:

Hi @tujun6, welcome to submitting your first PR to ops-math!

PR Merge Steps

1. CLA Signing

If the current PR label includes cann-cla/yes, it means you have signed the CLA and can proceed to the next step. If the label includes cann-cla/no, please sign the CLA first. If you have any questions, please refer to the FAQ.

2. CI Check

Please comment /compile to trigger the CI pipeline check. If the CI run is successful, the PR will be tagged with ci-pipeline-passed and you can proceed to the next step. If the CI run fails, the PR will be tagged with ci-pipeline-failed, please check the CI logs to fix the issues in the PR. If you have any questions, please refer to the FAQ.

3. Code Review

After CI passes, please refer to the PR Approval Progress and proactively @ the committers in the table to review the code. After approval, committers will comment /lgtm and /approve. Once the lgtm and approved labels are successfully added, the PR will be merged automatically.

likedislike
atomgit-bot
atomgit-bot
Aug 17 Comments:

代码审查

审查完成。以下为逐文件审查结论与总体风险判断。

逐文件审查结论

文件 结论
math/kth_value/op_kernel/arch35/kth_value_merge_sort_one_core.h no issues
math/kth_value/op_kernel/arch35/kth_value_radix_select.h no issues
math/sort/op_kernel/arch35/common/non_last_small_axis_base.h no issues
math/sort/op_kernel/arch35/common/radix_more_core_base.h no issues
math/sort/op_kernel/arch35/common/radix_sort_simd_utils.h no issues
math/sort/op_kernel/arch35/sort_merge_sort.h no issues
math/sort/op_kernel/arch35/sort_non_last_small_axis.h no issues
math/sort_with_index/op_kernel/arch35/merge_sort_simd.h no issues
math/top_k_v2/op_kernel/arch35/radix_sort_top_k.h no issues
math/top_k_v2/op_kernel/arch35/radix_sort_topk_b16.h no issues
math/top_k_v2/op_kernel/arch35/radix_sort_topk_b32.h no issues
math/top_k_v2/op_kernel/arch35/radix_sort_topk_b64.h no issues
math/top_k_v2/op_kernel/arch35/radix_sort_topk_b8.h no issues
math/top_k_v2/op_kernel/arch35/top_k_merge_sort_simd.h no issues
math/top_k_v2/op_kernel/arch35/top_k_radix_block_sort_b16.h 1 issue(P1)
math/top_k_v2/op_kernel/arch35/top_k_radix_block_sort_b32.h 1 issue(P1)
math/top_k_v2/op_kernel/arch35/top_k_radix_block_sort_b64.h 1 issue(P1)
math/top_k_v2/op_kernel/arch35/top_k_radix_block_sort_b8.h 1 issue(P1)

汇总

  • P0:0 个
  • P1:4 个
  • P2:0 个
  • P3:0 个

总体风险判断:高风险。本次 MicroAPI 整改整体是机械且方向正确的(DataCopy→LoadAlign/StoreAlign、CompareScalar→Compares、ReduceSum→Reduce<SUM>、MaskAnd/MaskSel/MaskPack→And/Select/Pack、DataCopyGather→Gather、__local_mem__→__ubuf__ 等均方向无误),但 top_k_radix_block_sort_b16/b32/b64/b8.h 四个文件中 GetGlobalExcusiveSum 的「vsts to global」写回段被统一误改成了 LoadAlign,导致全局 exclusive 前缀和不再写回,会破坏 radix sort 的 scatter 偏移、产生错误排序结果(或编译失败),影响核心排序/ top_k 算子正确性,需在合入前修复。

类型 数量
🔴 阻塞 4
🟡 建议 0

⛔ 需要修改

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 17 Added label: cann-cla/yes
82 messages are collapsed here。 See more
TangPC
TangPCCollaborators
Aug 19 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 19 Added label: approved
songkai111Collaborators
Aug 19 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Aug 19 Added label: lgtm
CANN-robotCANN-robotCollaborators
Aug 19 Merged pull request