已合并
sparse_segment_mean分核策略优化 #7050
z30075199创建于 7月6日
sparse_segment_mean分核策略优化 #7050
已合并
z30075199创建于 7月6日
z30075199成员
7月6日

描述

优化sparse_segment_mean分核策略
row上的核尽可能少(每个核处理尽可能多的索引),同时,col上的每个核处理的数据块根据segment_id重复度不能小于min(4k/8K/9K, innerSize)

关联的Issue

https://gitcode.com/cann/ops-nn/issues/3889

测试

泛化用例500条,平均性能提升25%

文档更新

类型标签

  • Bug修复
  • 新特性
  • 性能优化
  • 文档更新
  • 其他,请描述:

AI/Agent生成声明

  • AI辅助编写
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 z30075199 的贡献)
Zz30075199成员
7月6日 创建了 pull request,commit 82ded1c9
CANN-robotCANN-robot成员
7月6日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
7月6日 评论:

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 /approve or /lgtm
  • Commenting /approve implies 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. 👍

likedislike
CANN-robotCANN-robot成员
7月6日 将crystalhu,yangyang016,fanqirui,chenqi317,liubo75,tangweiwei2,gcw_kUomxQ2l,Andy-lb设为评审人
CANN-robotCANN-robot成员
7月6日 将gcw_kUomxQ2l,Andy-lb设为审查人
z30075199成员
7月6日 评论:

compile

likedislike
Zz30075199成员
7月6日 update merge request[project id: 7665709, iid: 7050, commit_id: 18a857e7a6952e7e86ec3a6e04ff411aa0376c70] virtual merging success
CANN-robotCANN-robot成员
7月6日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月6日 评论:

流水线任务触发成功
任务链接 [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执行结束

likedislike
CANN-robot
CANN-robot成员
7月6日 评论:

流水线任务触发成功
任务链接 [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执行结束

likedislike
CANN-robotCANN-robot成员
7月6日 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
7月6日 添加了label:ci-pipeline-passed
atomgit-bot
atomgit-bot
7月6日 评论:

变更摘要

此 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 感知的列分块约束: 函数签名新增 inputBytessegmentNum 参数,在排序后根据 rowTotalNum / segmentNum 的平均重复度(≥8、≥2、≥1、<1)分别施加不同的列最小尺寸限制(4K / 8K / 9K / 10K),从候选分块列表中剔除不满足条件的组合。
  • AutoTilingRowCol 排序优先级调整: 分块候选列表的排序键从原来的 (delta差值, 行分块数) 改为 (行分块数, delta差值),使行方向的分块数成为主要排序依据。
  • DoBlockTiling 新增低索引数回退机制: 在自动分块完成后,若单核平均处理的索引数小于 MIN_INDICES_NUM(5),则将 usedCoreNum 减半并重新调用 AutoTilingRowCol 进行分块,避免单核负载过小导致的效率问题。
likedislike
atomgit-bot
atomgit-bot
7月6日 评论:

代码审查

我已全面审查了两个变更文件。以下是我的审查结论。


关闭摘要

共发现 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 /= TWOusedCoreNum == 1 时变为 0,导致后续 AutoTilingRowCol 内部 ComputeFactors(0) 返回空集,allTiling[0] 越界崩溃。新增的分段阈值 while 循环逻辑经过仔细审查,排序变更(MIndex 替代 NIndex)、segmentNum 除法(有上游校验保证 >0)、inputBytes 除法(仅取值 2 或 4)均无问题。

总体风险判断:中等。P1 缺陷在单核场景下可导致进程崩溃,但多核生产环境(coreNum ≥ 2)不受影响。建议在上线前修复。

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

⛔ 需要修改

likedislike
atomgit-bot
atomgit-bot7月6日进行代码检视2
index/sparse_segment_mean/op_host/arch35/sparse_segment_mean_tiling_simd_tiling.cpp
@@ -129,0 +169,4 @@
169+ // 如果自动分核完后,平均一个核处理的索引个数小于5,则可用核数减半
170+ if (Ops::Base::CeilDiv(inputData.outterSize, rowTileNum) < std::min(MIN_INDICES_NUM, inputData.outterSize)) {
171+ usedCoreNum /= TWO;
172+ AutoTilingRowCol(rowTileNum, colTileNum, usedCoreNum, rowTotalNum, colTotalNum, inputData.inputBytes, inputData.segmentNum);
atomgit-bot
atomgit-bot7月6日评论:

🟠 High Priority

DoBlockTiling() 中,第 170-172 行新增的逻辑:当首次 AutoTilingRowCol 返回的 rowTileNum 使得平均每核处理的索引个数小于 MIN_INDICES_NUM(5) 时,执行 usedCoreNum /= TWO 将核数减半后重试。

问题:usedCoreNum 初始值为 hardwareData.coreNum(≥1)。当 coreNum == 1(单核场景、单元测试等)且触发减半条件时,整数除法 1 / 2 = 0usedCoreNum 变为 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 == 1CeilDiv(outterSize, rowTileNum) < 5outterSize >= 5

likedislike
z30075199成员
7月6日 评论:
likedislike
chenqi317成员
7月6日 评论:

/lgtm
/approve

likedislike
Zz30075199成员
7月6日 修改了pull request 的描述
Zz30075199成员
7月6日 解决了最后一个问题
liulun0308成员
7月7日 评论:

/approve

likedislike
CANN-robotCANN-robot成员
7月7日 添加了label:lgtmapproved
CANN-robotCANN-robot成员
7月7日 关闭了关联的issue
CANN-robotCANN-robot成员
7月7日 合入了pull request
CANN-robot
CANN-robot成员
7月7日 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike