Merged
feat(operation): Optimize TopK RadixSelect #5239
刘奕哲Add on Aug 3
feat(operation): Optimize TopK RadixSelect #5239
Merged
刘奕哲Add on Aug 3
刘奕哲
Aug 3

描述

通过缩减指令等方法优化 TopK Radix Select 算法性能。

测试

A5上完成泛化用例测试

关联Issue

#2857

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for 刘奕哲's contribution.)
刘奕哲刘奕哲
Aug 3 Created pull request, commit e015acf2
刘奕哲刘奕哲
Aug 3 Associated issue: [Requirement|需求建议]: TopK RadixSelect性能优化
atomgit-bot
atomgit-bot
Aug 3 Comments:

变更摘要

本 PR 为性能优化(对应 issue #2857),通过批量行处理、预计算零值 tile、精简直方图/行最小值 gather 指令等手段优化 TopK RadixSelect 算法性能。核心改动集中在 framework/src/interface/tileop/vector/radix_select/ 下的 radix_select_b1.h、radix_select_b2.h、radix_select_b4.h 与 radix_select_util.h,同时在 framework/src/interface/operation/vector/sort.cpp 的 TiledTopKRadixSelect 中调整了临时张量尺寸;测试已在 A5 上完成泛化用例验证。

主要改动

  • 批量行处理(多行 tile): RadixSelectCalcB1/B2/B4 移除内层 n3Index 逐行循环,改为按 srcTileH 批量处理多行;所有工作区(srcMaskAddr、histogramAddr、highAddr 等)与 tile 定义均乘以 srcTileH,并新增 RadixSelectBatchAssign 批量计算 src/value/index 地址;sort.cpp 的 TiledTopKRadixSelect 在 cur > 0 时按 input.tileInfo.shape[cur - 1] 扩大临时张量 tmpShape[0]。

  • 预计算零值 tile 减少指令: b2/b4 新增 tciAddr 工作区与 RadixSelectTCI,先以一次 TCI 生成全零 tciTile,后续迭代用 TMOV(histogramTmpUInt32Tile, tciTile) 复用,替代原先每轮迭代重复执行的 TCI 清零指令。

  • 精简直方图/行最小值 gather 逻辑: 移除重复的 TEXPANDS(gatherIntTile, 0)、双重 TGATHER 与 TSELS 序列,改用 TADDS(rowMinTile, highTile, 7) 计算偏移并直接从 histogramInt32PreTile(histogramAddr - 32 的前置 tile)一次 gather,并以 RadixSelectCmps、RadixSelectFinalSelect 封装比较与最终选择(GT/EQ gather + TCONCAT)。

  • 工具层重构(radix_select_util.h): DefineWorkSpace 由运行时尺寸改为编译期模板尺寸 DefineWorkSpace<T, size>;RSTile/DefineTile 模板参数顺序调整并新增 RSExchangeTile 派生单行 tile;新增按行循环的 RadixSelectGather(两个重载)、RadixSelectHistogram/RadixSelectHistogramB4、RadixSelectSel、RadixSelectBatchAssign、RadixSelectFinalSelect 等模板函数;PTO_RS_SORT_ADDR_DEFINE/PTO_RS_SORT_TILE_DEFINE/PTO_RS_COMMON_TILE_DEFINE 宏同步改为多行(srcTileH 倍数)工作区与 tile。

  • RadixSelectSortTwoBit 排序指令缩减: RadixSelectSortTwoBitCalc 增加 SRC2 参数以复用中间 tile,按 bit % 4 == 0 分支交替输入源,合并/减少 TGATHER、TCONCAT、TMOV 指令(bit == 0 时改为一次 TGATHER(tmp, src, select1, ...) 并省去末尾 TMOV(src, tmp))。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 3 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 3 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 3 Comments:

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robotCollaborators
Aug 3 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


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
docs ✅ 尹杰, congqiannan, lyz0328 (3/2) ✅ 尹杰, lyz0328 (2/1)
repo-cann/pypto ✅ 尹杰, congqiannan (2/2) ✅ 尹杰 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)
likedislike
刘奕哲
刘奕哲
Aug 3 Comments:

compile

likedislike
刘奕哲刘奕哲
Aug 3 Pre merge succeeded (commit_id: 712dc19ea99aefbb79160fe13dd9bc94c14fe156)
CANN-robotCANN-robotCollaborators
Aug 3 Added label: ci-pipeline-running
CANN-robot
CANN-robotCollaborators
Aug 3 Comments:

流水线任务触发成功
任务链接 [b4dd3c074b874868ae2e2c4ed0f1bd82][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_ubuntu24 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_ubuntu24 ✅ SUCCESS >>>>> >>>>>
pre_comment ✅ SUCCESS >>>>>
UT_Test_Py3_ninja_simulation ✅ SUCCESS
UT_Test_Py3_ninja ✅ SUCCESS
UT_Test_Cpp_make_gnu_part_1 ✅ SUCCESS
UT_Test_Cpp_make_gnu_part_2 ✅ SUCCESS
UT_Test_Cpp_make_gnu_part_3 ✅ SUCCESS
UT_Test_Cpp_make_clang_part1 ✅ SUCCESS
UT_Test_Cpp_make_clang_part2 ✅ SUCCESS
UT_Test_Cpp_make_clang_part3 ✅ SUCCESS
UT_Test_kirinx90 ✅ SUCCESS
UT_Test_kirin9030 ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>

[2026-08-03 11:01:23]    CI执行结束

likedislike
CANN-robot
CANN-robotCollaborators
Aug 3 Comments:

流水线任务触发成功
任务链接 [65e5d47d25054248a57400444e556aaf][流水线指导]

任务名称状态日志下载链接
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_Pr ✅ SUCCESS
StaticCheck_codespell ✅ SUCCESS
StaticCheck_link_validity ✅ SUCCESS
StaticCheck_resource_existence ✅ SUCCESS
StaticCheck_tag_closed ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_codestyle ✅ SUCCESS >>>>>
codecheck_precommit ✅ SUCCESS >>>>>
StaticCheck_md ✅ SUCCESS

[2026-08-03 10:34:49]    CI执行结束

likedislike
CANN-robot
CANN-robotCollaborators
Aug 3 Comments:

流水线任务触发成功
任务链接 [f0beea71ac2c43ae8d9a8ec57205252c][流水线指导]

任务名称状态日志下载链接
get_pr_file ✅ SUCCESS
static-check ✅ SUCCESS
compile ✅ SUCCESS

[2026-08-03 11:01:58]    CI执行结束

likedislike
CANN-robotCANN-robotCollaborators
Aug 3 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Aug 3 Added label: ci-pipeline-passed
lyz0328
lyz0328Collaborators
Aug 4 Comments:

/approve

likedislike
Jie Yin
Jie YinCollaborators
Aug 4 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 4 Added label: approved
congqiannan
congqiannanCollaborators
Aug 4 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Aug 4 Added label: lgtm
CANN-robotCANN-robotCollaborators
Aug 4 Closed associated issue
CANN-robotCANN-robotCollaborators
Aug 4 Merged pull request