Closed
[Bug-Report|缺陷反馈]: [Bug] aclnnMoeFusedTopk 空 tensor 时短路全部参数校验,y/indices 非空却未写入 #7237
liuhanwen8Add on  21 days agoClosed at  12 days ago
liuhanwen8
liuhanwen8
21 days ago Create

关联:#7236(同机理:空 tensor 早退,非空输出未写;另一算子)

现象

资料要求 x 为 2D (numToken, expertNum)(moe/moe_fused_topk/docs/aclnnMoeFusedTopk.md:141)、y/indices 为 (numToken, topK)(同文件 :261/:271),并规定 topK 必须大于 0(:207)、TopK 小于等于 expertNum(:394)、TopN 小于等于 expertNum / groupNum(:395);参数或 shape 不满足约束时返回 161002(:329-338)。

实测 expertNum=0(x=[4,0],y/indices 为 [4,2] 非空)时:

组 x addNum y/indices 实测
对照组 [4,4] [4] [4,2] 两段均 0,y=0.5、indices=0/1
缺陷组 [4,0] [0] [4,2] GetWorkspaceSize=0、execute=0,y/indices 整块未被写入

源码侧(master 5adf800c91c9375e44720d38265a6e7fd433900c)op_host/op_api/aclnn_moe_fused_topk.cpp:

  • CheckShape 首行 if (x->IsEmpty()) { return true; }(:109-111)——空 tensor 时整段约束校验被短路,包括 topK>0、topK<=expertNum、topN<=expertNum/groupNum、y 与 x 第 0 维一致;
  • GetWorkspaceSize 在 :280-284 再次 if (x->IsEmpty()) 直接 return ACLNN_SUCCESS,不生成任何写 y/indices 的算子。

x=[4,0] 的元素数为 0(IsEmpty() 为真),但 y/indices 的 4*2 个元素非空。

期望

expertNum=0 与 topK>0、topK<=expertNum 无法同时满足,属资料约束违规,第一段应返回 ACLNN_ERR_PARAM_INVALID(161002)。即便按空 tensor 放行,也不应在 y/indices 非空时返回 SUCCESS 而不写入。

环境

Atlas A2 训练服务器(910B2),npu-smi Software Version 26.1.1;CANN 9.3.0(/usr/local/Ascend/ascend-toolkit/latest),Linux aarch64;cann/ops-transformer master 5adf800c91c9375e44720d38265a6e7fd433900c;复现日期 2026-11-08。

  • 复核基线:2026-11-08 08:56 于 cann/ops-transformer master 5adf800c91c9375e44720d38265a6e7fd433900c 复核,问题仍存在(moe/moe_fused_topk/op_host/op_api/aclnn_moe_fused_topk.cpp:109-111 的 CheckShape 首行 if (x->IsEmpty()) { return true; } 短路了其后全部约束(topK>0、topK<=expertNum、topN、y/indices shape 等);且 :280-283 再次 if (x->IsEmpty()) { *workspaceSize = 0; ... return ACLNN_SUCCESS; } 不写 y/indices)

复现步骤

  1. 保存下方脚本为 repro.cpp
  2. 编译:g++ -std=c++14 -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lnnopbase -lopapi -lopapi_transformer -Wl,--allow-shlib-undefined
  3. ./repro:对照组 gws=0 exec=0、y=0.5、indices=0/1;缺陷组 gws=0 exec=0,y/indices 未写入

复现脚本

#include <cstdio>
#include <vector>
#include <cstdint>
#include "acl/acl.h"
#include "aclnnop/aclnn_moe_fused_topk.h"

static void* Buf(size_t n, unsigned char f)   // canary 预填;n=0 时仍给 8 字节保证指针非空
{
    size_t m = n ? n : 8;
    void* p = nullptr; aclrtMalloc(&p, m, ACL_MEM_MALLOC_HUGE_FIRST);
    std::vector<unsigned char> h(m, f);
    aclrtMemcpy(p, m, h.data(), m, ACL_MEMCPY_HOST_TO_DEVICE);
    return p;
}
static aclTensor* Mk(std::vector<int64_t> s, aclDataType dt, void* p)
{
    std::vector<int64_t> st(s.size(), 1);
    for (int i = (int)s.size() - 2; i >= 0; --i) st[i] = s[i + 1] * st[i + 1];
    return aclCreateTensor(s.data(), s.size(), dt, st.data(), 0, ACL_FORMAT_ND, s.data(), s.size(), p);
}
static bool Untouched(void* p, size_t n)   // true = 整块逐字节仍是 0x7F
{
    std::vector<unsigned char> h(n, 0);
    aclrtMemcpy(h.data(), n, p, n, ACL_MEMCPY_DEVICE_TO_HOST);
    for (size_t i = 0; i < n; ++i) if (h[i] != 0x7f) return false;
    return true;
}
static void Run(const char* tag, int E, aclrtStream s)
{
    const int T = 4, K = 2;
    void* xp = Buf((size_t)T * E * 4, 0);
    void* ap = Buf((size_t)E * 4, 0);
    void* yp = Buf((size_t)T * K * 4, 0x7f);
    void* ip = Buf((size_t)T * K * 4, 0x7f);
    uint64_t ws = 0; aclOpExecutor* ex = nullptr;
    int g = (int)aclnnMoeFusedTopkGetWorkspaceSize(
        Mk({T, E}, ACL_FLOAT, xp), Mk({E}, ACL_FLOAT, ap), nullptr, nullptr,
        2, 1, 2, K, 0, false, 1.0f, false,
        Mk({T, K}, ACL_FLOAT, yp), Mk({T, K}, ACL_INT32, ip), &ws, &ex);
    int e = -1;
    if (g == 0 && ex != nullptr) {
        void* wq = nullptr;
        if (ws > 0) aclrtMalloc(&wq, ws, ACL_MEM_MALLOC_HUGE_FIRST);
        e = (int)aclnnMoeFusedTopk(wq, ws, ex, s);
        aclrtSynchronizeStream(s);
        if (wq != nullptr) aclrtFree(wq);
    }
    std::vector<float> yh((size_t)T * K, 0.f); std::vector<int32_t> ih((size_t)T * K, 0);
    aclrtMemcpy(yh.data(), yh.size() * 4, yp, yh.size() * 4, ACL_MEMCPY_DEVICE_TO_HOST);
    aclrtMemcpy(ih.data(), ih.size() * 4, ip, ih.size() * 4, ACL_MEMCPY_DEVICE_TO_HOST);
    printf("[%s] GetWorkspaceSize = %d ; execute = %d | y[0..3]=", tag, g, e);
    for (int i = 0; i < 4; ++i) printf(" %.6g", yh[i]);
    printf(" | indices[0..3]=");
    for (int i = 0; i < 4; ++i) printf(" %d", ih[i]);
    printf(" | y未写入=%d indices未写入=%d\n", (int)Untouched(yp, yh.size() * 4), (int)Untouched(ip, ih.size() * 4));
    aclrtFree(xp); aclrtFree(ap); aclrtFree(yp); aclrtFree(ip);
}
int main()
{
    aclInit(nullptr); aclrtSetDevice(0);
    aclrtStream s = nullptr; aclrtCreateStream(&s);
    Run("对照组 expertNum=4", 4, s);
    Run("缺陷组 expertNum=0", 0, s);
    aclrtDestroyStream(s); aclrtResetDevice(0); aclFinalize();
    return 0;
}

实测输出(A2 910B2 / CANN 9.3.0):

[对照组 expertNum=4] GetWorkspaceSize = 0 ; execute = 0 | y[0..3]= 0.5 0.5 0.5 0.5 | indices[0..3]= 0 1 0 1 | y未写入=0 indices未写入=0
[缺陷组 expertNum=0] GetWorkspaceSize = 0 ; execute = 0 | y[0..3]= 3.39615e+38 3.39615e+38 3.39615e+38 3.39615e+38 | indices[0..3]= 2139062143 2139062143 2139062143 2139062143 | y未写入=1 indices未写入=1

影响

调用方拿到 rc=0 后直接读取 y/indices,得到未初始化显存(实测 canary 读回 3.39615e+38 / 2139062143)。同一函数里 x->IsEmpty() 既让非法参数绕过校验,又让非空输出被静默跳过,属 B11 与 B12-3 叠加。

建议

CheckShape 不要以 x->IsEmpty() 整体短路,至少保留标量约束(topK>0、topK<=expertNum、topN<=expertNum/groupNum)与 y/indices 的 shape 一致性;GetWorkspaceSize 的早退只在 y、indices 同样为空时生效,否则应写零或返回 161002。

likedislike
liuhanwen8liuhanwen8
21 days ago Added label: bug-report
周云飞
周云飞Collaborators
21 days ago Comments:

问题说明

按报告中的约束,expertNum=0 与 topK=2 不相容,应在第一阶段报参数错误,而不是成功后保留非空输出的原值。您提供的结果显示,y、indices 各有 8 个元素,执行后仍保留预填值;结合所述两处早退,问题指向空输入绕过形状校验并跳过输出计算。具体早退分支尚缺独立核实。

处理建议

可考虑调整 CheckShape,在空输入早退前检查标量约束及输出形状,并先确认 groupNum>0 再计算分组约束。该非法组合应返回 ACLNN_ERR_PARAM_INVALID(161002),不宜以写零代替报错。GetWorkspaceSize 的空输入成功早退应限于参数合法且输出也为空的情况。重点验证本例被拒绝、合法空输出仍可通过,以及原有非空输入结果不变。

likedislike
liuhanwen8liuhanwen8
21 days ago Modify description of issue
周云飞周云飞Collaborators
20 days ago Assign huang-wei-chen as responsible person
huang-wei-chenhuang-wei-chenCollaborators
13 days ago Assign defeng20201 as responsible person
huang-wei-chenhuang-wei-chenCollaborators
13 days ago Removed responsible person huang-wei-chen
ohohCollaborators
13 days ago Assign huang-wei-chen as responsible person
ohohCollaborators
13 days ago Associated pull request
CANN-robotCANN-robotCollaborators
12 days ago Close issue
CANN-robotCANN-robotCollaborators
12 days ago changed custom state from 待办的 to 已完成
周云飞
周云飞Collaborators
12 days ago Comments:

已通过 PR #16062 调整 aclnnMoeFusedTopk 的参数校验:移除 CheckShape 中输入 x 为空时直接返回成功的逻辑,使空 tensor 场景也会执行后续参数和 shape 校验。

PR 中的复现验证显示,expertNum=0 的缺陷场景修复后返回 ACLNN_ERR_PARAM_INVALID,不再直接返回成功。

关联 PR:PR #16062

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