Pull Request已成功合入, 合并人@CANN-robot
(感谢 z30075199 的贡献)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 |
|---|---|---|
| ** | ✅ gcw_kUomxQ2l, 陈琦 (2/2) | ✅ gcw_kUomxQ2l (1/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
z30075199, thanks for your pull request. All authors of the commits have signed the CLA. 👍


流水线任务触发成功
任务链接 [7a20a3341ecd45e194de8f52814bb0e9][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| Compile_Ascend_X86_mobile_station | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_experimental | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Pre | ✅ SUCCESS | >>>>> | |
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910b | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910c | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_harmony-infer-chs-nn | ✅ SUCCESS | >>>>> | |
| UT_Test_ophost | ✅ SUCCESS | ||
| UT_Test_opapi | ✅ SUCCESS | ||
| UT_Test_kernel | ✅ SUCCESS | ||
| UT_Test_opgraph | ✅ SUCCESS | ||
| PreSmoke_A900 | ✅ SUCCESS | >>>>> | |
| PreSmoke_ATK_Test_A2 | ✅ SUCCESS | >>>>> |
[2026-07-06 10:27:29] CI执行结束


流水线任务触发成功
任务链接 [730b63ad259a48bebfe7a8eaddc7045b][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| codecheck | ✅ SUCCESS | >>>>> | |
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_checkpr | ✅ SUCCESS | ||
| StaticCheck_codespell_check | ✅ SUCCESS | ||
| StaticCheck_link_validity_check | ✅ SUCCESS | ||
| StaticCheck_resource_existence_check | ✅ SUCCESS | ||
| StaticCheck_tag_closed_check | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS | ||
| codecheck_style | ✅ SUCCESS | >>>>> | |
| codecheck_precommit | ⚠️ WARNING | >>>>> | >>>>> |
| SCA | ✅ SUCCESS | >>>>> |
[2026-07-06 10:23:55] CI执行结束


变更摘要
此 PR 主要针对 sparse_segment_mean 算子的分核策略进行优化,涉及两个 tiling 实现文件。在 full_load_tiling 中引入容忍度阈值来放宽单核索引分配的限制条件;在 simd_tiling 中根据 segment 平均重复度动态调整列分块的最小尺寸约束,并在索引数过少时自动将可用核数减半重新分块,以提升多核负载均衡和计算效率。
主要改动
IsCapable准入条件引入容忍度: 新增TOLERANCE_THRESHOLD常量(值为 20),将单核分配的 indices 个数条件从严格的gatherSize > outterSize / usedCoreNum放宽为gatherSize > (outterSize + outterSize / TOLERANCE_THRESHOLD) / usedCoreNum,允许约 5% 的索引重复度偏离。AutoTilingRowCol新增 segment 感知的列分块约束: 函数签名新增inputBytes和segmentNum参数,在排序后根据rowTotalNum / segmentNum的平均重复度(≥8、≥2、≥1、<1)分别施加不同的列最小尺寸限制(4K / 8K / 9K / 10K),从候选分块列表中剔除不满足条件的组合。AutoTilingRowCol排序优先级调整: 分块候选列表的排序键从原来的(delta差值, 行分块数)改为(行分块数, delta差值),使行方向的分块数成为主要排序依据。DoBlockTiling新增低索引数回退机制: 在自动分块完成后,若单核平均处理的索引数小于MIN_INDICES_NUM(5),则将usedCoreNum减半并重新调用AutoTilingRowCol进行分块,避免单核负载过小导致的效率问题。


代码审查
我已全面审查了两个变更文件。以下是我的审查结论。
关闭摘要
共发现 1 个问题:
| 优先级 | 数量 |
|---|---|
| P1 | 1 |
| P0, P2, P3 | 0 |
按文件分类:
-
index/sparse_segment_mean/op_host/arch35/sparse_segment_mean_tiling_full_load_tiling.cpp— 无问题。该文件仅在IsCapable()中增加了 5% 的索引重复度容忍阈值,逻辑正确,tolerance = outterSize / 20在 outterSize 较小时退化为 0(等同于原行为),除零和溢出风险均不存在。 -
index/sparse_segment_mean/op_host/arch35/sparse_segment_mean_tiling_simd_tiling.cpp— 发现 1 个 P1 问题:DoBlockTiling()中usedCoreNum /= TWO在usedCoreNum == 1时变为 0,导致后续AutoTilingRowCol内部ComputeFactors(0)返回空集,allTiling[0]越界崩溃。新增的分段阈值 while 循环逻辑经过仔细审查,排序变更(MIndex 替代 NIndex)、segmentNum除法(有上游校验保证 >0)、inputBytes除法(仅取值 2 或 4)均无问题。
总体风险判断:中等。P1 缺陷在单核场景下可导致进程崩溃,但多核生产环境(coreNum ≥ 2)不受影响。建议在上线前修复。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 1 |
| 🟡 建议 | 0 |
⛔ 需要修改


🟠 High Priority
在 DoBlockTiling() 中,第 170-172 行新增的逻辑:当首次 AutoTilingRowCol 返回的 rowTileNum 使得平均每核处理的索引个数小于 MIN_INDICES_NUM(5) 时,执行 usedCoreNum /= TWO 将核数减半后重试。
问题:usedCoreNum 初始值为 hardwareData.coreNum(≥1)。当 coreNum == 1(单核场景、单元测试等)且触发减半条件时,整数除法 1 / 2 = 0,usedCoreNum 变为 0。随后再次调用 AutoTilingRowCol(..., usedCoreNum=0, ...),内部 ComputeFactors(0) 中 upbound = ceil(sqrt(0)+1) = 1,循环 for (m=1; m<1; ++m) 不执行,返回空 std::set,导致 allTiling 为空向量。随后第 122 行 allTiling[0] 访问越界,进程崩溃(SIGSEGV)。
触发条件:coreNum == 1 且 CeilDiv(outterSize, rowTileNum) < 5 且 outterSize >= 5。


/approve


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


描述
优化sparse_segment_mean分核策略
row上的核尽可能少(每个核处理尽可能多的索引),同时,col上的每个核处理的数据块根据segment_id重复度不能小于min(4k/8K/9K, innerSize)
关联的Issue
https://gitcode.com/cann/ops-nn/issues/3889
测试
泛化用例500条,平均性能提升25%
文档更新
类型标签
AI/Agent生成声明