Merged
[bugfix] broadcast tiling preferMultiCore 分块调整增加上界clamp #1066
[bugfix] broadcast tiling preferMultiCore 分块调整增加上界clamp #1066
Merged
hahaha22Add on Oct 23
hahaha22
hahaha22Collaborators
Oct 23

描述

DoBrodcastTiling 的 preferMultiCore 分块调整中,恢复表达式 (maxElemNum + 2*CACHE_LINE - 1)/CACHE_LINE*CACHE_LINE(对齐上取整并补偿一条 CACHE_LINE)在对齐增量与补偿作用下可越出进入分支时的原始容量 BroadcastGetMaxElemNum(ubSize, computeParams):

  • 容量非 CACHE_LINE 对齐(fp32/int32/int64 等 minDtypeBits≥32 的 dtype,对齐粒度 2048/minDtypeBits)且循环执行一轮:roundup(M−128)+128 = M+64
  • 容量对齐但循环未执行:roundup(M)+128 = M+128

越出后的 maxElemNum 传入 ubFormer 与 DMA 尺寸计算,破坏容量不变量,可产生物理 UB 越界。Ascend960 侧模板已强制开启 preferMultiCore(isAscend960_),该路径覆盖全部 broadcast 模板算子。

修复:进入分支时保存原始容量,恢复表达式保持不变,其后增加上界 clamp(+4 行):

const uint64_t originMaxElemNum = maxElemNum;
...
maxElemNum = (maxElemNum + CACHE_LINE + CACHE_LINE - 1) / CACHE_LINE * CACHE_LINE;
if (maxElemNum > originMaxElemNum) {
    maxElemNum = originMaxElemNum;
}
  • clamp 命中时取 originMaxElemNum——该值即容量推导结果,天然满足 dtype 对齐粒度,为合法容量
  • +128 补偿保留(服务于循环下界退出路径的减法回退),越界场景由 clamp 统一兜底
  • clamp 不命中时行为与修改前完全一致,改动仅收敛越界场景

关联的Issue

修复 #561
https://gitcode.com/cann/opbase/issues/561

测试

  • 修复前实测(950 / Ascend950PR):int32 触发形态 tiling 观测 origin maxElemNum: 7872 → maxElemNum: 7936,折合字节越过物理 UB 1024B;
  • 修复后 tiling dump:三触发形态最终容量 7936/3968/10624 → 7872/3936/10560(=原始容量,越界消除);
  • 非越界用例 tiling 与修改前逐字段一致(容量 4480 / block 54 不变);
  • ST 回归(floor_mod / mod / pow 仓内 ST 用例 205 例):floor_mod 4/5、mod 100/100,失败例与基线一致(同一 int64 除零语义失败例);pow 确定性失败集与基线一致(10×0-d 标量动态编译限制、11×float 极值精度,均为既有非相关问题);另有 2 例 fp16 用例(SpanBERT、NCHW broadcast)在修复版批量运行中失败,原始版本自身多轮批量运行中亦出现同值失败(81.7% / 99.9%),两种版本隔离单跑均 100% 通过,属批间数据流波动、与本次修复无关;全部 205 例 block 分配与修改前逐字段一致。

文档更新

无(纯 tiling 逻辑改动,不涉及对外文档)。

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for hahaha22's contribution.)
hahaha22hahaha22Collaborators
Oct 23 Created pull request, commit 734effde
atomgit-bot
atomgit-bot
Oct 23 Comments:

变更摘要

本次变更修复 DoBrodcastTiling 中 preferMultiCore 分块调整逻辑的两处边界缺陷:一是在按 CACHE_LINE 递减 maxElemNum 的循环中,将下界判断前移到减法之前,避免初值小于 CACHE_LINE 时无符号减法回绕导致 tiling 无法返回;二是在最终对齐恢复表达式后增加上界钳制,保证 maxElemNum 不超过进入分支前保存的原始 UB 容量。改动集中在 broadcast_tiling.cpp,仅影响 preferMultiCore=true 且 blockNum < coreNum 的多核切分调整路径。

主要改动

  • 保存原始容量基准:新增 originMaxElemNum 记录进入多核调整分支前的 maxElemNum,作为后续上界钳制的比较基准。
  • 循环下界前置检测:将退出条件改为 maxElemNum <= CACHE_LINE_512 + CACHE_LINE 并在减法前判断,消除无符号下溢死循环,同时保持原有“减后仍大于 CACHE_LINE_512 才继续”的推进语义。
  • 恢复表达式简化与上界钳制:对齐上取整改为 (maxElemNum + CACHE_LINE - 1) / CACHE_LINE * CACHE_LINE,去掉额外的 CACHE_LINE 补偿,并在结果超过 originMaxElemNum 时回退为原始容量,维持 maxElemNum 不超过原始 UB 容量的不变量。
  • 保证多核切分目标可达:通过上界钳制避免对齐增量抬高容量或使已达标的多核切分因子回落,使 fusedProduct、blockFormer 与 blockNum 的后续更新基于合法容量计算。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Oct 23 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Oct 23 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Oct 23 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.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
repo-cann/opbase ✅ 宋恺, 汤平川 (2/2) ✅ 宋恺, 汤平川 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

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

likedislike
69 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Oct 27 Added label: approved
TangPC
TangPCCollaborators
Oct 27 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Oct 27 Added label: lgtm
CANN-robotCANN-robotCollaborators
Oct 27 Closed associated issue
CANN-robotCANN-robotCollaborators
Oct 27 Merged pull request