已合并
修复MoeInitRoutingQuantV2第三个输出结果精度问题 #8354
修复MoeInitRoutingQuantV2第三个输出结果精度问题 #8354
已合并
wuxiyuan创建于 7月7日
wuxiyuan成员
7月7日

描述

在 CumsumComputeSimt 中,当处理到最后一个元素(curIndex == totalLength - 1)时,原代码直接 return 跳过了该元素的 cumsum 写入,仅通过 lastExpertIdCunsumAddr 缓存最后一条 expert 的 ID 和 cumsum 值到 ubuf。随后在 Process() 中从 ubuf 读取该缓存值来填充尾部 expert 的 cumsum 输出。
这种依赖 ubuf 跨线程传递缓存值的方式存在可靠性问题,导致第三个输出结果精度不正确。

具体变更

  1. 移除冗余的成员变量和 TBuf 分配
  • 删除 expertIdCunsumTBuf_(TBuf)、lastExpertIdCunsum_(LocalTensor)、lastExpertIdCunsumAddr_(ubuf 指针)
  • 删除 Init() 中对应的 InitBuffer 和地址获取逻辑
  1. 新增 GlobalTensor 直接访问 GM
  • 新增 expandedExpertIdxGmGT_(GlobalTensor<int32_t>),在 Init() 中绑定到 workspace 中 expandedExpertIdx 的 GM 地址
  1. 简化 CumsumComputeSimt 逻辑
  • 移除函数签名中的 lastExpertIdCunsumAddr 参数
  • 移除最后一个元素时向 ubuf 写入缓存值的分支逻辑,直接 return
  1. 修正 Process() 中尾部 expert 的 cumsum 填充
  • expertId 改为通过 expandedExpertIdxGmGT_.GetValue(totalLength_ - 1) 直接从 GM 读取
  • cunsumNum 改为直接使用 totalLength_(强转 int32_t),不再依赖 ubuf 缓存值

关联的Issue

https://gitcode.com/cann/ops-transformer/issues/3598

测试

已完成自验证 + 二级冒烟 + ST验证

文档更新

类型标签

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 wuxiyuan 的贡献)
Wwuxiyuan成员
7月7日 创建了 pull request,commit fc699e93
atomgit-bot
atomgit-bot
7月7日 评论:

变更摘要

该 PR 修复了 MoeInitRoutingQuantV2 算子中第三个输出结果的精度问题。问题根因在于 CumsumComputeSimt SIMT kernel 内通过 UBUF (lastExpertIdCunsumAddr_) 跨线程传递最后一个元素的 expertId 和 cumsum 索引时存在竞争条件,导致读取到的值可能不正确。修复方案是移除 UBUF 中转机制,改为在 Process 方法中直接通过 GlobalTensor (expandedExpertIdxGmGT_) 从 GM 读取最后一个元素的 expertId,并以 totalLength_ 直接作为 cumsum 计数,彻底消除了线程间同步带来的精度隐患。

主要改动

  • 移除 UBUF 缓冲区和相关成员变量:删除了 expertIdCunsumTBuf_(VECCALC 缓冲区)、lastExpertIdCunsum_(LocalTensor)和 lastExpertIdCunsumAddr_(UBUF 指针),不再使用 UBUF 作为跨线程数据传递的中介。
  • 新增 expandedExpertIdxGmGT_ 成员:引入 GlobalTensor<int32_t> 类型的 expandedExpertIdxGmGT_,在 Init 中通过 SetGlobalBuffer 将其指向与 expandedExpertIdxGm_ 相同的 workspace 内存区域,便于后续直接从 GM 读取数据。
  • 简化 CumsumComputeSimt 函数签名与逻辑:移除 lastExpertIdCunsumAddr 参数,并删除函数末尾向 UBUF 写入 curExpertIdcurIndex + 1 的代码(原先由 threadIdx.y == 0 的线程执行),消除了潜在的多线程写入竞争。
  • 重写 Process 末尾的 expertId/cunsumNum 获取方式:将原来从 lastExpertIdCunsumAddr_ 读取改为通过 expandedExpertIdxGmGT_.GetValue(this->totalLength_ - 1) 获取 expertId,cunsumNum 直接取 static_cast<int32_t>(this->totalLength_),确保数据来源可靠。
likedislike
CANN-robotCANN-robot成员
7月7日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
7月7日 评论:

CLA Signature Pass

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

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

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)
likedislike
CANN-robotCANN-robot成员
7月7日 将monologue815,jiang-lirui,Allan_Yu,liudan12,mabing1118,gcw_kUomxQ2l,Andy-lb设为评审人
CANN-robotCANN-robot成员
7月7日 将gcw_kUomxQ2l,Andy-lb设为审查人
Wwuxiyuan成员
7月7日 修改了pull request 的描述
wuxiyuan成员
7月7日 评论:

compile

likedislike
Wwuxiyuan成员
7月7日 update merge request[project id: 7673863, iid: 8354, commit_id: 6c2f409075071665b0d795645f0ffa0d55886dc2] virtual merging success
CANN-robotCANN-robot成员
7月7日 添加了label:ci-pipeline-running
CANN-robot
CANN-robot成员
7月7日 评论:

流水线任务触发成功
任务链接 [424176c7bf98467cbc13c30b9f18131a][流水线指导]

任务名称状态日志下载链接
Compile_Ascend_X86 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_mobile_station ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental ✅ SUCCESS >>>>> >>>>>
Compile_Pre ✅ SUCCESS >>>>>
pre_comment ✅ SUCCESS >>>>>
Compile_X86_monitor_910b ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_910c ✅ SUCCESS >>>>> >>>>>
Compile_X86_monitor_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_X86_experimental_950 ✅ SUCCESS >>>>> >>>>>
Compile_Ascend_ARM_experimental_950 ✅ SUCCESS >>>>> >>>>>
Compile_harmony-infer-chs-transformer ✅ SUCCESS >>>>>
UT_Test ✅ SUCCESS
PreSmoke_A900 ✅ SUCCESS >>>>>
PreSmoke_ATK_Test_A2 ✅ SUCCESS >>>>>

[2026-07-07 10:19:45]    CI执行结束

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

流水线任务触发成功
任务链接 [18fd3a4071d24e13bcdf930b7272fcfa][流水线指导]

任务名称状态日志下载链接
codecheck ✅ SUCCESS >>>>>
SCA ✅ SUCCESS >>>>>
antipoison ✅ SUCCESS >>>>>
codecheck_style ✅ SUCCESS >>>>>
StaticCheck_codespell_check ✅ SUCCESS
StaticCheck_link_validity_check ✅ SUCCESS
StaticCheck_resource_existence_check ✅ SUCCESS
StaticCheck_tag_closed_check ✅ SUCCESS
StaticCheck_markdownlint ✅ SUCCESS
codecheck_checkpr ✅ SUCCESS
codecheck_precommit ⚠️ WARNING >>>>> >>>>>

[2026-07-07 10:04:55]    CI执行结束

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

/approve

likedislike
CANN-robotCANN-robot成员
7月7日 添加了label:approved
jiang-lirui成员
7月7日 评论:

/lgtm

likedislike
CANN-robotCANN-robot成员
7月7日 添加了label:lgtm
CANN-robotCANN-robot成员
7月7日 合入了pull request