Pull Request已成功合入, 合并人@CANN-robot
(感谢 huang-wei-chen 的贡献)变更摘要
此 PR 将 RadixTopK 算子的 AscendC 编译选项 auto_sync 从 true 改为 false,关闭了编译器自动插入同步屏障的功能。为保证正确性,在多个内核文件中手动补入了显式的流水线同步原语(PipeBarrier<PIPE_V>()、MTE3ToSSync、MTE3ToVSync、VToMTE3Sync 等),并新增了三个同步工具函数以支持 MTE3→S、MTE3→V、S→MTE2 方向的事件同步。该改动属于 Bug 修复类别。
主要改动
- 关闭 RadixTopK 自动同步:
scripts/kernel/binary_config/ascendc_config.json中将RadixTopK配置项的auto_sync从true改为false,禁用编译器自动插入同步屏障的行为。 - 新增同步工具函数:在
radix_top_k_utils.h中新增MTE3ToSSync()、MTE3ToVSync()、SToMTE2Sync()三个内联函数,封装了 MTE3→S、MTE3→V、S→MTE2 方向的事件同步逻辑,供其他内核文件调用。 - radix_top_k_common.h 中补入 PipeBarrier:在
TwiddleInB16、DoAndMask、CalcCumsumHistogram16、NegateDataForLargest、CopyOut2Ws、SubTopKAndCopyOut、ReduceGlobalHist等函数的向量运算之间手动插入PipeBarrier<PIPE_V>(),确保 V 流水线操作的有序执行。 - radix_top_k_ub.h 中补入同步原语:在
ClearHist中新增VToSSync/MTE3ToSSync/SToVSync组合,在Update、AddTileHistToTileTopK、HandleLastRoundBoundary、TileTopK等函数的关键操作前后插入PipeBarrier<PIPE_V>()。 - radix_top_k_ws.h 与 radix_top_k_ws_utils.h 中补入同步原语:在 WS(Workspace)路径的
ClearTileTopKInWs、ClearTileHistInWs、ClearHistInWs、SubProcess、WriteCoreTopKFromWs、TileTopK等函数中插入VToMTE3Sync/MTE3ToVSync/MTE3ToSSync等同步调用,并将HandleLastRoundBoundary中的MTE2ToVSync替换为PipeBarrier<PIPE_V>()。


代码审查
关闭总结
我已逐个审查了全部 6 个变更文件,以下是审查结果:
| 文件 | 审查结论 |
|---|---|
math/radix_top_k/op_kernel/radix_top_k_common.h |
无问题。所有 PipeBarrier<PIPE_V>() 插入位置正确,V 流水线指令间数据依赖均有覆盖。 |
math/radix_top_k/op_kernel/radix_top_k_ub.h |
发现 1 个 P2 问题:HandleLastRoundBoundary 的 if/else 两分支均在 Cast 之后缺少 PipeBarrier<PIPE_V>()(Cast→ReduceSum 之间无 V→V 屏障)。同文件 Update 函数 line 249-251 正确示范了应有模式。 |
math/radix_top_k/op_kernel/radix_top_k_utils.h |
发现 1 个 P3 问题:新增的 SToMTE2Sync() 函数被定义但无任何调用点,属于死代码。其余 MTE3ToSSync、MTE3ToVSync 实现正确且被正确使用。 |
math/radix_top_k/op_kernel/radix_top_k_ws.h |
发现 1 个 P2 问题:HandleLastRoundBoundary 的 if/else 两分支均在 Cast 之后、ReduceSum 循环之前缺少 PipeBarrier<PIPE_V>()。同文件 WriteCoreTopKFromWs line 383-385 正确示范了应有模式。另外 MTE2ToVSync()→PipeBarrier<PIPE_V>() 的替换本身是正确的。 |
math/radix_top_k/op_kernel/radix_top_k_ws_utils.h |
无问题。ClearTileTopKInWs、ClearTileHistInWs、ClearHistInWs 中新增的 VToMTE3Sync、MTE3ToVSync、MTE3ToSSync 和 SToMTE3Sync 均放置正确,补齐了跨流水线同步缺口。 |
scripts/kernel/binary_config/ascendc_config.json |
无问题。auto_sync 从 true 改为 false,JSON key 拼写正确,值类型正确。 |
按严重度统计
- P0: 0
- P1: 0
- P2: 2(UB 和 WS 变体的
HandleLastRoundBoundary均缺少Cast→ReduceSum的PipeBarrier) - P3: 1(
SToMTE2Sync死代码)
整体风险判断
中等风险。两个 P2 问题均位于最后一轮边界 bin 元素分配路径(HandleLastRoundBoundary),缺少 V→V 屏障可能导致 ReduceSum 读到 Cast 未完成的输出,进而使边界 bin 的元素计数产生误差。不过,该路径仅在 roundId == 0 && remainK_ > 0 时触发(即最后一轮且尚未满足 k 值),并非每条数据都经过此路径。建议在合入前修复这两处缺失的 PipeBarrier。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 3 |
💬 仅评论


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 |
|---|---|---|
| repo-cann/ops-math | ✅ 冯彤, 宋恺 (2/2) | ✅ 冯彤, 宋恺 (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
huang-wei-chen, thanks for your pull request. All authors of the commits have signed the CLA. 👍


compile


流水线任务触发成功
任务链接 [b14717d02f1d497e86e23a9c4e744cdb][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_harmony-infer | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_ARM_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910b | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910c | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_A5_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_A5_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station_9030_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single | ✅ SUCCESS | >>>>> | >>>>> |
| UT_Test | ✅ SUCCESS | ||
| UT_Test_experimental | ✅ SUCCESS | ||
| UT_Test_kernel | ✅ SUCCESS | ||
| PreSmoke_A900 | ✅ SUCCESS | >>>>> | |
| API_Check | ✅ SUCCESS | >>>>> | |
| PreSmoke_ATK_Test_A2 | ✅ SUCCESS | >>>>> | |
| UT_Test_harmony-infer-chs-math | ✅ SUCCESS |
[2026-07-22 03:18:51] CI执行结束


流水线任务触发成功
任务链接 [b09a16c8a78a42058b476af5f81e26b6][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| 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 | ❌ FAILED | >>>>> | >>>>> |


🟡 Medium Priority
在 radix_top_k_ub.h 的 HandleLastRoundBoundary 函数中(UB 变体),两处 Cast(tileHistFp32, ...) 均在 V 流水线上写入 tileHistFp32(if 分支 line 306,else 分支 line 309),随后的 ReduceSum(tileHistFp32, ...) (line 318) 同样在 V 流水线上读取同一缓冲区。由于 auto_sync 已关闭,Cast 与 ReduceSum 之间缺少 PipeBarrier<PIPE_V>(),可能导致 ReduceSum 读到未完成的 Cast 结果,产生错误的边界 bin 元素计数,进而导致 TopK 元素分配错误(取多或取少)。
同文件 Update 函数 (line 249-251) 中正确地在 Cast 与 ReduceSum 之间插入了 PipeBarrier<PIPE_V>(),此处遗漏是疏忽。if 分支虽有 PipeBarrier (line 305) 但位于 Cast 之前而非之后;else 分支完全没有 V→V 屏障。
建议:在两处 Cast 调用之后、进入 ReduceSum 循环(line 315)之前,各添加一个 PipeBarrier<PIPE_V>()。参考同文件 Update 函数 line 249-251 的模式:Cast(...); PipeBarrier<PIPE_V>(); ReduceSum(...);


🟡 Medium Priority
在 radix_top_k_ws.h 的 HandleLastRoundBoundary 函数中(WS 变体),Cast(tileHistFp32, resTensor, ...) 在 V 流水线上写入 tileHistFp32,随后的 ReduceSum(tileHistFp32, ...) 也运行在 V 流水线上并读取同一缓冲区。由于 auto_sync 已关闭,两个 V 指令之间缺少 PipeBarrier<PIPE_V>(),可能导致 ReduceSum 在 Cast 完成之前读取到脏数据,产生错误的 tile 边界累加结果,进而影响 TopK 元素分配的精度。
对比同文件中 WriteCoreTopKFromWs (line 384) 和 radix_top_k_ub.h 的 Update (line 250),均正确地在 Cast 与 ReduceSum 之间插入了 PipeBarrier<PIPE_V>(),此处遗漏是明显的疏忽。
该问题存在于 if 分支(boundaryBinPrev < numValue_)和 else 分支两处:if 分支的 PipeBarrier<PIPE_V>()(line 292)仅位于 Cast 之前,else 分支则完全没有 V→V 屏障。
建议:在两处 Cast 调用之后、进入 ReduceSum 循环之前,各添加一个 PipeBarrier<PIPE_V>()。if 分支在 Cast (line 293) 之后添加;else 分支在 Cast (line 298) 之后添加。参考 WriteCoreTopKFromWs 的 line 383-385 模式。


🟡 Medium Priority
在 radix_top_k_ws.h 的 HandleLastRoundBoundary 函数 else 分支(line 294-298,当 boundaryBinPrev >= numValue_ 时),CopyTileHistWs2Ub(MTE2 搬入)→ MTE2ToVSync(MTE2→V 同步)→ Cast(V 写入 tileHistFp32)→ 随后的 ReduceSum(V 读取 tileHistFp32)。在 Cast 与 ReduceSum 之间没有 PipeBarrier<PIPE_V>()。
同一函数 if 分支(line 287-293)中 diff 已将旧有的 MTE2ToVSync() 改为 PipeBarrier<PIPE_V>(),但这只确保了 Sub(V)→Cast(V) 的 V→V 顺序,并未覆盖 Cast→ReduceSum。else 分支则完全没有任何 V→V PipeBarrier。
对比同文件 WriteCoreTopKFromWs(line 383-385)中正确地在 Cast 与 ReduceSum 之间插入了 PipeBarrier<PIPE_V>(),此处两分支均为遗漏。由于 auto_sync 已关闭,编译器不会自动插入屏障,这可能导致 ReduceSum 读到 Cast 未完成的输出,产生错误的 tile 边界累加结果,最终使最后一轮边界 bin 元素分配数量出错。
建议:在 else 分支的 Cast(line 298)之后添加 PipeBarrier<PIPE_V>();同时在 if 分支的 Cast(line 293)之后也添加一个(当前 line 292 的 PipeBarrier 在 Cast 之前,不足以保证 Cast→ReduceSum 顺序)。参考 WriteCoreTopKFromWs line 383-385 的模式。


compile


流水线任务触发成功
任务链接 [34bcea17e51541adad57a17a0d23dd67][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_harmony-infer | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_ARM_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910b | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910c | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_A5_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_A5_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station_9030_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single | ✅ SUCCESS | >>>>> | >>>>> |
| UT_Test | ✅ SUCCESS | ||
| UT_Test_experimental | ✅ SUCCESS | ||
| UT_Test_kernel | ✅ SUCCESS | ||
| PreSmoke_A900 | ✅ SUCCESS | >>>>> | |
| API_Check | ✅ SUCCESS | >>>>> | |
| PreSmoke_ATK_Test_A2 | ✅ SUCCESS | >>>>> | |
| UT_Test_harmony-infer-chs-math | ✅ SUCCESS |
[2026-07-22 11:47:31] CI执行结束


流水线任务触发成功
任务链接 [81e586620d1340d1a8613aaa7909704f][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| 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 | ⚠️ WARNING | >>>>> | |
| codecheck_precommit | ✅ SUCCESS | >>>>> |
[2026-07-22 11:39:55] CI执行结束


/lgtm
/approve


The following users do not have permission to comment /lgtm or /approve on any module in this PR:
唐燕峰


The following users do not have permission to comment /lgtm or /approve on any module in this PR:
唐燕峰


The following users do not have permission to comment /lgtm or /approve on any module in this PR:
唐燕峰


The following users do not have permission to comment /lgtm or /approve on any module in this PR:
唐燕峰


/lgtm
/approve


The following users do not have permission to comment /lgtm or /approve on any module in this PR:
唐燕峰


描述
关闭RadixTopK自动同步,手动插入同步,优化算子性能。
关联的Issue
关联Issue #2286
测试
算子泛化200+用例精度通过,性能提升2%-5%
文档更新
类型标签