Closed
[Bug-Report|缺陷反馈]: aclnnMoeUpdateExpert 在 balanceMode=0 且 F < worldSize+1 时按资料允许的 F 配置读越界,输出被写成 0 #7597
liuhanwen8Add on  13 days agoClosed at  23 hours ago
liuhanwen8
liuhanwen8
13 days ago Create

现象

aclnnMoeUpdateExpert 资料把 eplbTable 列数 F 声明为 [2, worldSize+1],即 F=2 是合法配置。
但 balanceMode=0 分支要求能读到 flat[off + rankId],rankId 上界是 worldSize:

  • op_kernel/moe_update_expert.h:192-195:modeValue = Ceil(worldSize, count);
    rankId = localRankId/modeValue + 1(≤ worldSize);flat[tableOffset + rankId]
  • op_host/op_tiling/moe_update_expert_tiling.cpp:394-398:只校验 1 < F <= worldSize+1,
    不校验 F-1 >= max(rankId) = worldSize

当 count == worldSize 时 modeValue = 1、rankId 最大取到 worldSize,
于是需要 F >= worldSize+1;而资料允许 F=2 ⇒ 读越过整张表。

实测(worldSize=8、localRankId=7、eplbTable 第 0 行 count=8、其余行实例号全为 999):

F 读的线性下标 表末下标 越界 rc1/rc2 输出 [:,0]
9 8 26 否 0 / 0 108(= 100+8,合法)
3 8 8 边界 0 / 0 0(实为下一行 count 列)
2 8 5 是 0 / 0 0(垃圾)

放大验证:BS=512, K=16, F=2, count=8, rank=7 → 输出唯一值 [0],而合法实例号是 999。

期望

二选一(任一即可自洽):

  1. balanceMode=0 时 tiling 补 F - 1 >= max(rankId) = worldSize(即 F >= worldSize+1),
    不满足返回 561002;或
  2. 把资料里 F 的下界从 2 改为 worldSize + 1,并说明 count == worldSize 时的实例号数量要求。

环境

  • 硬件 / 系统:Ascend950PR(Ascend950PR_957c,8 卡机,用 NPU6);Ubuntu 24.04 / kernel 6.8 / 驱动 25.7.rc1
  • CANN:9.3.0(Ascend-cann-950 算子包,2026-11-11 构建)
  • 源码:cann/ops-transformer master a1e54cce(2026-11-14),mc2/moe_update_expert
  • 探针为纯 ACL C++(不经 torch / torch_npu)

复现步骤

  1. source ${ASCEND_HOME_PATH}/set_env.sh;ASCEND_RT_VISIBLE_DEVICES=6
  2. 编译:g++ -O2 -std=c++17 probe_mue_oob.cpp -o probe_mue_oob -I${ASCEND_HOME_PATH}/include -I${ASCEND_HOME_PATH}/include/aclnnop -L${ASCEND_HOME_PATH}/lib64 -lascendcl -lnnopbase -lopapi -lpthread
  3. 运行 ./probe_mue_oob,观察 F=9 / F=3 / F=2 三档的 rc1/rc2 与 col0
  4. 判据自查:把 e[0] 改成 1(modeValue=8、rankId=1),此时 F=3 不越界,输出应为 101

复现脚本

#include <cstdio>
#include <cstdint>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_moe_update_expert.h"
template <typename T> static aclTensor* Make(const std::vector<T>& h,
    std::vector<int64_t> sh, void** d, aclDataType dt) {
  int64_t n = 1; for (auto v : sh) n *= v; size_t b = n * sizeof(T);
  aclrtMalloc(d, b, ACL_MEM_MALLOC_HUGE_FIRST);
  aclrtMemcpy(*d, b, h.data(), b, ACL_MEMCPY_HOST_TO_DEVICE);
  std::vector<int64_t> st(sh.size(), 1);
  for (int64_t i = (int64_t)sh.size() - 2; i >= 0; --i) st[i] = st[i+1] * sh[i+1];
  return aclCreateTensor(sh.data(), sh.size(), dt, st.data(), 0,
                         ACL_FORMAT_ND, sh.data(), sh.size(), *d);
}
// F 可变;count=8 = worldSize,使 modeValue=1、rankId=8(rank=7)
static void Run(int F) {
  const int BS = 8, K = 2, MOE = 3, RANK = 7, WS = 8;
  std::vector<int32_t> ids(BS * K, 0);            // 全部指向第 0 行
  std::vector<int32_t> e(MOE * F, 0);
  e[0] = 8;                                        // count = worldSize → modeValue = 1
  for (int c = 1; c < F; ++c) e[c] = 100 + c;
  for (int r = 1; r < MOE; ++r) { e[r*F] = 1; e[r*F+1] = 999; }
  std::vector<int32_t> oi(BS*K, 0); std::vector<uint8_t> om(BS*K, 0); void *di=0,*de=0,*d1=0,*d2=0;
  aclTensor *ti=Make(ids,{BS,K},&di,ACL_INT32), *te=Make(e,{MOE,F},&de,ACL_INT32);
  aclTensor *t1=Make(oi,{BS,K},&d1,ACL_INT32), *t2=Make(om,{BS,K},&d2,ACL_BOOL);
  uint64_t ws=0; aclOpExecutor* ex=nullptr;
  auto r1 = aclnnMoeUpdateExpertGetWorkspaceSize(ti,te,nullptr,nullptr,nullptr,
                                                 RANK,WS,0,t1,t2,&ws,&ex);
  printf("F=%-2d read=flat[%d] last=flat[%d] oob=%d ", F, 0*F+RANK+1, MOE*F-1,
         (0*F+RANK+1) > (MOE*F-1));
  if (r1) { printf("rc1=%d (rejected)\n", (int)r1); return; }
  void* w=nullptr; if (ws) aclrtMalloc(&w, ws, ACL_MEM_MALLOC_HUGE_FIRST);
  aclrtStream s=nullptr; aclrtCreateStream(&s);
  auto r2 = aclnnMoeUpdateExpert(w, ws, ex, s); aclrtSynchronizeStream(s);
  std::vector<int32_t> g(BS*K,-1);
  aclrtMemcpy(g.data(), BS*K*4, d1, BS*K*4, ACL_MEMCPY_DEVICE_TO_HOST);
  printf("rc1=%d rc2=%d col0=[", (int)r1, (int)r2);
  for (int i=0;i<BS;++i) printf("%d%s", g[i*K], i+1<BS?",":"");
  printf("]\n");
}
int main() {  aclInit(nullptr); aclrtSetDevice(0);
  aclrtContext c; aclrtCreateContext(&c, 0); aclrtSetCurrentContext(c);
  Run(9); Run(3); Run(2);  aclrtDestroyContext(c); aclFinalize(); return 0; }

实测输出(probe_mue_oob,Ascend950PR / CANN 9.3.0):

F=9  read=flat[8]  last=flat[26] oob=0 rc1=0 rc2=0 col0=[108,108,108,108,108,108,108,108]
F=3  read=flat[8]  last=flat[8]  oob=0 rc1=0 rc2=0 col0=[0,0,0,0,0,0,0,0]
F=2  read=flat[8]  last=flat[5]  oob=1 rc1=0 rc2=0 col0=[0,0,0,0,0,0,0,0]

设备侧 plog 对这三次调用无任何 ERROR 行。

影响

  • 资料允许 F=2..worldSize 的配置,配合 count=worldSize 就进入越界读;读到的相邻内存被当作专家号
    写入输出,后续 aclnnMoeDistributeDispatchV2/V3 按此分发 token,表现为「token 全丢」或
    「路由到错误专家」,且两段返回码都是 0。
  • 越界读的量随 BS*K 放大(BS=512,K=16 时整块输出被写成 0),不影响其它算子内存,
    但输出不可恢复。

建议

  1. CheckInputShape 增补:balanceMode=0 时要求 F - 1 >= worldSize,否则 561002。
  2. 或在 tiling 里按 count 的实际最大值算 max(rankId) 并校验 F,允许 F 取更小区间的同时
    不越界(例如 modeValue 用 Ceil(W, count) 后校验 rankId <= F-1)。
  3. 资料侧同步:若保留 F ∈ [2, worldSize+1],需说明「count == worldSize 时实例号列数必须 ≥ worldSize」。
likedislike
liuhanwen8liuhanwen8
13 days ago Added label: bug-report
周云飞
周云飞Collaborators
13 days ago Comments:

感谢反馈。按您提供的索引计算,balanceMode=0 且 count=worldSize 时,rankId 最大可达 worldSize;若 F 不足,可能读到其他行甚至表外数据。您报告的 F=3 和 F=2 结果分别符合这两种情况,但具体实现和资料约束仍需进一步核实。

临时规避可将 F 设为 worldSize+1,并为每行填入与实际 count 对应的有效实例号。处理方向上,可以评估在 balanceMode=0 时增加列数校验,或按实际访问范围检查;前者可能限制原本可用的较小 F,后者需要确认 tiling 阶段能否取得 count。资料中的列数与实例号要求也需要一并核对,错误码 561002 是否适用尚待确认。

likedislike
周云飞周云飞Collaborators
12 days ago Assign weixin_49300047 as responsible person
周云飞周云飞Collaborators
3 days ago Assign Yuyu-Li as responsible person
周云飞周云飞Collaborators
3 days ago Removed responsible person weixin_49300047
周云飞周云飞Collaborators
3 days ago Associated pull request
周云飞周云飞Collaborators
1 day ago Assign SuperYuan as responsible person
Yuyu-LiYuyu-LiCollaborators
1 day ago Associated pull request
CANN-robotCANN-robotCollaborators
23 hours ago Close issue
CANN-robotCANN-robotCollaborators
23 hours ago changed custom state from 待办的 to 已完成
周云飞
周云飞Collaborators
23 hours ago Comments:

已更新 aclnnMoeUpdateExpert 的中英文文档,明确映射表每行的实例数 count 须满足 1 ≤ count ≤ min(worldSize, F - 1);当 count 小于 worldSize 时,F 不必等于 worldSize + 1。同时修正了负载均衡的专家映射计算示例。

此次相关改动为文档修正,未修改算子实现,不能据此认定越界及输出异常问题已解决。

关联 PR:PR #17182、PR #17447

likedislike
CANN-robotCANN-robotCollaborators
21 hours ago Added label: resolved