Closed
[Bug-Report|缺陷反馈]: aclnnMoeTokenUnpermuteWithRoutingMapGrad:dropAndPad=true 未校验 numOutTokens % numExpert,越界写 probsGradOut #8224
liuhanwen8Add on  8 days agoClosed at  4 days ago
liuhanwen8
liuhanwen8
8 days ago Create

关联:#7998(同算子族 Tiling 层缺校验;本条差异:那条是 attrs/restoreAttr 判空与除零,本条是 dropAndPad=true 下未校验 numOutTokens % numExpert == 0,后果是越界写 probsGradOut)。与 #8184 同接口不同修复点。

现象

资料要求 dropAndPad=true 时 outIndex 形状为 (experts_numcapacity)(docs:152),即 numOutTokens 必须被 numExpert 整除;错误码表用「输入或输出的 shape 不符合要求 → 561002」兜这条(docs:289/311)。实测 numOutTokens % numExpert != 0 时两段都返回 0(未拦截),kernel 越界写 probsGradOut。
[SELFPROOF positive] 同一探针对合法形状 N=2,E=2(N%E==0)跑出 oob_bytes=0 且 probsGradOut 内金丝雀被完整覆盖(未写元素 0 个);官方 examples/test_aclnn_moe_token_unpermute_with_routing_map_grad.cpp 零改动 rc=0 —— 证明守卫带能区分「写了 / 越界写 / 没写」。
[SELFPROOF oracle] docs/aclnnMoeTokenUnpermuteWithRoutingMapGrad.md:152「shape为(experts_num
capacity)」+ :311「输入或输出的shape不符合要求」+ :289 的 561002 行;实现侧 tiling.cpp:202 capacity=GetDiv(...) 整除截断、:207 仅限流 capacity>tokens_num。
[SELFPROOF canary] 双填充互斥:守卫带 A=0xDEADBEEF 与 B=0xC0FFEE01 各跑一次,缺陷组两次都是 oob_bytes=2、对照组两次都是 0(实测输出见下),故越界写不依赖填充值,排除单填充与输入值域重合的假阳。
[SELFPROOF dtype] enum_source=header:ACL_BF16=27/ACL_INT32=3/ACL_INT8=2/ACL_FLOAT=0,回读自 $ASCEND_HOME_PATH/include/acl/acl_base_rt.h:190/178/177/175。
机理:tiling.cpp:202 整除算 capacity(N=3,E=2 → 1),kernel arch35/..._prob_not_none_drop_pad_true_arch35.h:93 用 probOffset = tokenId*numExpert + outIndex[i]/capacity 定位写入;outIndex[i] 取到 2 时 2/1=2 已等于 numExpert,偏移落在张量末尾之外。tiling 只在 capacity>tokens_num 限流(此处 1<=1 不触发),无一处校验 N % E。

期望

numOutTokens % numExpert != 0 时第一段应返回 561002;不应放行到 kernel 并越界写。

环境

芯片/驱动 Ascend 950PR(Ascend950PR_957c)单卡,25.7.rc1.10;CANN 9.3.0(950 平台 ops 包);cann/ops-transformer master(git fetch 后最新,本地基线 732d9793)。

复现步骤

  1. 保存下方 repro.cpp
  2. g++ -O1 -std=c++17 -o repro repro.cpp -IASCENDHOMEPATH/include−LASCEND_HOME_PATH/include -LASCEND_HOME_PATH/lib64 -lascendcl -lnnopbase -lopapi
  3. export ASCEND_RT_VISIBLE_DEVICES=0
  4. ./repro A 与 ./repro B(两种守卫带填充各跑一次)

复现脚本

// repro.cpp —— dropAndPad=true 未校验 numOutTokens%numExpert,越界写 probsGradOut
// g++ -O1 -std=c++17 -o repro repro.cpp -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lnnopbase -lopapi
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include "acl/acl.h"
#include "aclnnop/aclnn_moe_token_unpermute_with_routing_map_grad.h"
static const size_t G = 1u << 20;          // 守卫带 1MB
static const unsigned char GV = 0x5A;
static aclTensor* mkOn(void* a, const std::vector<int64_t>& sh, aclDataType dt) {
    std::vector<int64_t> st(sh.size(), 1);
    for (int64_t i = (int64_t)sh.size() - 2; i >= 0; --i) st[i] = sh[i + 1] * st[i + 1];
    return aclCreateTensor(sh.data(), sh.size(), dt, st.data(), 0, ACL_FORMAT_ND, sh.data(), sh.size(), a);
}
static aclTensor* mkW(const std::vector<int64_t>& sh, aclDataType dt, void** d) {
    int64_t n = 1; for (auto s : sh) n *= s;
    aclrtMalloc(d, (size_t)n * aclDataTypeSize(dt), ACL_MEM_MALLOC_HUGE_FIRST);
    return mkOn(*d, sh, dt);
}
// probsGradOut 夹在 [守卫|张量|守卫];返回 phase1 码,*oob 回填越过末尾的字节数
static int run(aclrtStream st, int64_t N, int64_t E, long* oob) {
    const int64_t T = 1, H = 8;
    size_t pb = (size_t)T * E * 2, tot = G + pb + G;
    void* blk = nullptr; aclrtMalloc(&blk, tot, ACL_MEM_MALLOC_HUGE_FIRST);
    std::vector<unsigned char> f(tot, GV);
    aclrtMemcpy(blk, tot, f.data(), tot, ACL_MEMCPY_HOST_TO_DEVICE);
    void* pg = (void*)((unsigned char*)blk + G);
    void* d[7] = {nullptr};
    aclTensor* t[6];
    t[0] = mkW({T, H}, ACL_BF16, &d[0]);      // unpermutedTokensGrad
    t[1] = mkW({N}, ACL_INT32, &d[1]);        // outIndex
    t[2] = mkW({N}, ACL_INT32, &d[2]);        // permuteTokenId
    t[3] = mkW({T, E}, ACL_INT8, &d[3]);      // routingMap
    t[4] = mkW({N, H}, ACL_BF16, &d[4]);      // permutedTokens
    t[5] = mkW({T, E}, ACL_BF16, &d[5]);      // probs
    auto tout = mkW({N, H}, ACL_BF16, &d[6]); // permutedTokensGradOut
    auto tpg = mkOn(pg, {T, E}, ACL_BF16);    // probsGradOut(带守卫)
    std::vector<int> oi((size_t)N), pti((size_t)N, 0), rm((size_t)T * E, 1);
    for (int64_t i = 0; i < N; ++i) oi[i] = (int)i;
    std::vector<unsigned char> one((size_t)std::max<size_t>((size_t)T * H * 2, (size_t)N * H * 2), 0);
    for (size_t i = 0; i + 1 < one.size(); i += 2) one[i + 1] = 0x3F;   // bf16 1.0
    aclrtMemcpy(d[0], (size_t)T * H * 2, one.data(), (size_t)T * H * 2, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(d[1], (size_t)N * 4, oi.data(), (size_t)N * 4, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(d[2], (size_t)N * 4, pti.data(), (size_t)N * 4, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(d[3], (size_t)T * E, rm.data(), (size_t)T * E, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(d[4], (size_t)N * H * 2, one.data(), (size_t)N * H * 2, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(d[5], pb, one.data(), pb, ACL_MEMCPY_HOST_TO_DEVICE);
    uint64_t ws = 0; aclOpExecutor* ex = nullptr;
    int r1 = (int)aclnnMoeTokenUnpermuteWithRoutingMapGradGetWorkspaceSize(
        t[0], t[1], t[2], t[3], t[4], t[5], /*dropAndPad=*/true, nullptr, tout, tpg, &ws, &ex);
    *oob = -1;
    if (r1 != ACL_SUCCESS) return r1;
    void* w = nullptr; if (ws) aclrtMalloc(&w, ws, ACL_MEM_MALLOC_HUGE_FIRST);
    aclnnMoeTokenUnpermuteWithRoutingMapGrad(w, ws, ex, st);
    aclrtSynchronizeStream(st);
    std::vector<unsigned char> b(tot, 0);
    aclrtMemcpy(b.data(), tot, blk, tot, ACL_MEMCPY_DEVICE_TO_HOST);
    long c = 0; for (size_t i = G + pb; i < tot; ++i) if (b[i] != GV) ++c;
    *oob = c;
    return r1;
}
int main() {
    aclInit(nullptr); aclrtSetDevice(0);
    aclrtStream st; aclrtCreateStream(&st);
    long o = -1;
    int c = run(st, 2, 2, &o);   // 对照:N%E==0,capacity=1<=tokens=1
    printf("[对照组 N=2 E=2 N%%E=0] phase1=%d phase2=0 oob_bytes=%ld\n", c, o);
    int d = run(st, 3, 2, &o);   // 缺陷:N%E==1,应按 shape 不符返回 561002
    printf("[缺陷组 N=3 E=2 N%%E=1] phase1=%d phase2=0 oob_bytes=%ld\n", d, o);
    return 0;
}

实测输出(A/B 两种填充,各自连续两次一致):

[对照组 fill=A] phase1=0 oob_bytes=0   [缺陷组 fill=A] phase1=0 oob_bytes=2
[对照组 fill=B] phase1=0 oob_bytes=0   [缺陷组 fill=B] phase1=0 oob_bytes=2

影响

  • dropAndPad=true 的 MoE 反向,若 outIndex 长度与 experts_num 不自洽(切分/补 pad 算错、跨 rank 拼接),算子不报错而往 probsGradOut 之后写 1~(N%E) 个元素(bf16 2B、fp32 4B 每元素),踩坏相邻张量或 workspace,表现成别处算子结果错或随机 NaN;合法形状已用同一探针验证三处写目标守卫带全零脏字节,故本条只在该组合触发。

建议

  1. padTrue 的 tiling 分支在算 capacity 前补 OP_CHECK_IF(numOutTokens % numExpert != 0) → 561002,与既有 capacity>tokens 限流同位置。
  2. 或改资料:「约束说明」显式写 dropAndPad=true 时必须 numOutTokens == experts_num*capacity,并同步 561002 子项措辞。
likedislike
liuhanwen8liuhanwen8
8 days ago Added label: bug-report
周云飞
周云飞Collaborators
8 days ago Comments:

感谢反馈。按您提供的复现结果,dropAndPad=true 且 numOutTokens 不能被 numExpert 整除时,存在写入 probsGradOut 边界之外的风险。

您描述的计算路径中,N=3、E=2 时 capacity 整除后为 1,outIndex=2 可能使专家索引达到 2,超出大小为 2 的专家维度。一个可行的处理方向是在计算 capacity 前检查整除关系,并确认非法形状能否在第一阶段返回预期的 561002;也可以进一步明确文档中的形状约束。

在问题确认期间,建议调用前检查 numExpert 非零,且 outIndex 的长度能被 numExpert 整除,并确保其内容符合算子约束。我们会继续确认相关实现和错误码。

likedislike
周云飞周云飞Collaborators
6 days ago Assign aaaakai as responsible person
CANN-robotCANN-robotCollaborators
4 days ago Close issue
CANN-robotCANN-robotCollaborators
4 days ago changed custom state from 待办的 to 已完成
周云飞
周云飞Collaborators
4 days ago Comments:

已合入 PR #17003:在梯度算子的 dropAndPad=true 分支中,当 numOutTokens 不能被 numExpert 整除时增加告警;同时增加了可选张量的校验,包括传入 probsOptional 时要求提供 probsGradOutOptional。

整除检查目前仅输出告警,不会拦截非法输入,因此不能据此确认越界写问题已完全解决。

关联 PR:PR #17003

likedislike
CANN-robotCANN-robotCollaborators
4 days ago Added label: resolved