Merged
fix(optim): advance_step V2 独立队列整改,消除 sampled/specToken 双周期 UB 重写 #14501
fix(optim): advance_step V2 独立队列整改,消除 sampled/specToken 双周期 UB 重写 #14501
Merged
zhaixiao26Add on 6 days ago
zhaixiao26
zhaixiao26Collaborators
6 days ago

问题背景

对 advance_step(含 PR#14119 整改后 master)执行 mssanitizer 四工具全量内存扫描(Ascend950PR_9599,kernel 用例集 53 功能用例,按 ops-test-kit/docs/tools/mssanitizer_guide.md 流程),racecheck 检出 ERROR 级数据竞争 156 处:

====== ERROR: Potential WAW hazard detected at UB in dyn_op_advance_step_v2_spec1_2:
======    PIPE_MTE2 Write at WAW()+0x0 ... (serialNo:6211)
======    PIPE_MTE2 Write at WAW()+0x0 ... (serialNo:6341)

根因:V2 路径 buffer1/buffer2(TQueBind)在同一 loop 内经历两轮"MTE2 写→消费→归还"周期——CopyIn() 写入 inputPositions/seqLens 后,CopyInInputTokens() 对同一队列再次 MTE2 写入 sampledTokenIds/specToken,第二轮写与第一轮消费完成之间缺乏 sanitizer 可证明的排序,11 个 V2 用例稳定复现(spec1 密度最高 84 处)。

整改内容

文件 改动
op_kernel/advance_step_spec.h 新增 buffer4/buffer5 独立队列承接 sampledTokenIds/specToken 搬入,CopyInInputTokens/ComputeInputTokens/CopyOutInputTokens 全链路切换,每队列单轮 MTE2 写
op_host/advance_step_tiling.cpp UB 预算公式(perCoreInBufSize)同步追加两队列项,perLoopMaxSeqs 自适应收紧,接受面不变

验证结果

build.sh --ops=advance_step --soc=ascend950 --pkg --mssanitizer 构建,四工具全量复扫:

指标 整改前 整改后
racecheck ERROR 156 18(WAW 全部清零)
memcheck ERROR 0 0
initcheck ERROR 0 0
synccheck WARNING 1,308,216 1,332,840(持平)
精度(golden 比对) 53/53 ×4 轮 53/53 ×4 轮,无回归

残留 18 处全部集中于 v2_shape_large(唯一多 loop 大 shape 用例)的单缓冲跨 loop 复用 Potential 判定:队列 AllocTensor 阻塞语义保证实际有序(四轮精度全过佐证),属 sanitizer 对队列语义的建模盲区,建议按已知豁免登记,不随本 PR 处理。

合规检查

  • pre-commit 全量通过:clang-format 18.1.8 / OAT 合规扫描 / codespell / 基础检查
  • 自测:上述 mssanitizer 四工具扫描 + 精度比对(950PR 真机)

关联

  • 基于 PR#14119(AdvanceStep 静态扫描整改)之后的 master
  • 扫描原始日志与整改前后对比:见验证环境 mss_results_before/ 与 mss_results/
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for zhaixiao26's contribution.)
zhaixiao26zhaixiao26Collaborators
6 days ago Created pull request, commit a65e8c0a
atomgit-bot
atomgit-bot
6 days ago Comments:

变更摘要

本 PR 针对 advance_step 的 V2 路径中 buffer1/buffer2 在同一 loop 内被两轮 MTE2 写入(CopyIn() 与 CopyInInputTokens())而触发的 racecheck WAW 数据竞争进行整改:在 KernelAdvanceStepSpec 中新增 buffer4/buffer5 两个独立输入队列专门承接 sampledTokenIds/specToken 的搬入,并将 CopyInInputTokens、ComputeInputTokens、CopyOutInputTokens 整条链路的队列操作切换到新队列,使每个队列在单轮内只有一次 MTE2 写;同时同步更新 host 侧 Tiling4Seqs() 的 UB 预算公式,保持 UB 分配与 kernel 侧一致。

主要改动

  • 新增独立输入队列: 在 advance_step_spec.h 中为 KernelAdvanceStepSpec 增加 TQue<QuePosition::VECIN, 1> 类型的 buffer4/buffer5 成员,分别用于 sampledTokenIds 与 specToken,避免与 buffer1/buffer2 复用同一 UB 槽位。
  • 队列初始化扩展: 在 Init() 中按 tokenEachReqs_ 与 specNum_ 的行宽分别 InitBuffer 分配 buffer4/buffer5,容量随 perLoopSeqs_ 缩放。
  • 搬入流程切换队列: CopyInInputTokens() 由原先从 buffer1/buffer2 的 DeQue 改为对 buffer4/buffer5 执行 AllocTensor 并在 DataCopyPad 后 EnQue,使 MTE2 写与消费在同一队列内闭环。
  • 消费与搬出流程切换队列: ComputeInputTokens() 改为从 buffer4 取用并在末尾 FreeTensor,CopyOutInputTokens() 改为从 buffer5 取用并 FreeTensor,与新增队列一一对应。
  • UB 预算公式同步: advance_step_tiling.cpp 的 Tiling4Seqs() 中 perCoreInBufSize 追加两个队列的按序开销项(基于 tokenEachReqs_ 的对齐行宽),使 perLoopMaxSeqs 相应收紧而算子接受面不变。

🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
atomgit-bot
atomgit-bot
6 days ago Comments:

代码审查

审查结论

变更文件逐项确认

  • optim/advance_step/op_host/advance_step_tiling.cpp:no issues。新增的两个 UB 预算项(:385-386)逐项核对后与 kernel 侧 buffer4/buffer5 的 InitBuffer 完全一致:host AlignBytes(x) 默认 bytes=sizeof(int64_t)=8,故 AlignBytes(te+4)=AlignUp(8·te+32,32)=AlignUp(8·te,32)+32,与 AlignBytes(te,sizeof(T))+BLOCK_BYTES 逐 seq 相等;tokenEachReqs_-1 == specNum_ 对应 buffer5,亦相等;tokenEachReqs_ >= 1 不会出现负入参;perLoopMaxSeqs_ 收紧后仍由 :403 的拒绝检查保证 ≥1,不会引入 kernel 侧除零。
  • optim/advance_step/op_kernel/advance_step_spec.h:发现 2 处问题(见下)。

发现清单

优先级 数量 说明
P0 0 —
P1 0 —
P2 1 buffer1/buffer2 在改动后失去唯一的 FreeTensor 配对(释放职责被搬到 buffer4/buffer5),打破 kernel 内其余队列(buffer3/tempQue_/buffer4/buffer5 均为每轮一次 Alloc↔Free)的既有不变量;curCoreLoopNum_ >= 2 时第二轮 CopyIn() 的 AllocTensor 可能等待无人设置的反向释放事件而停顿,缓冲亦永不归还。
P3 1 buffer5 以 TQue<VECIN> 承接实为 MTE2 搬入、MTE3 拷出的数据,丢失原 TQueBind<VECIN,VECOUT> 的出入双向事件,MTE2↔MTE3 与跨 loop WAR 排序退化为依赖 :520 的偶然屏障(可选加固项)。

总体风险判断

WAW 整改方向正确,host 侧 UB 预算与 kernel 实际分配逐项对齐,无明显溢出或乘积/收窄类问题,137 行内的改动风险集中在 kernel 队列生命周期:buffer1/buffer2 的 Alloc/Free 失衡是本 PR 确定引入的行为回归(需重点复核多 loop 大 shape),buffer5 的队列类型选择则是隐性同步契约弱化。建议合入前至少修掉 P2 项,并按注释说明或加固 buffer5 的同步来源,同时补一次多 loop 大 shape 的复扫确认。

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

💬 仅评论


🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
6 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
6 days ago 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
optim ✅ 王星, 汤平川 (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

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

likedislike
44 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
5 days ago Added label: lgtm
CANN-robot
CANN-robotCollaborators
5 days ago Comments:

The MR can not be merged, because of CodeReview discussion not resolved

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

likedislike
zhaixiao26zhaixiao26Collaborators
5 days ago Resolved final issue
CANN-robotCANN-robotCollaborators
5 days ago Merged pull request
CANN-robot
CANN-robotCollaborators
5 days ago Comments:

Pull Request 已合并或已关闭。

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

likedislike