Closed
[Bug-Report|缺陷反馈]: aclnnSwiGluGrad 缺形状/dim 校验:dim 非法返回 561002、yGrad 未减半被静默接受 #7173
liuhanwen8Add on  Oct 22Closed at  22 days ago
liuhanwen8
liuhanwen8
Oct 22 Create

Describe the current behavior / 问题描述 (Mandatory / 必填)

aclnnSwiGluGrad(yGrad, x, dim, out) 有两处参数校验缺失,实测(Atlas A2 910B2 / CANN 9.2.0-beta.2):

场景 输入 期望 实测
对照(合法) yGrad=[2x4]、x=[2x8]、dim=1 0 0
dim 越界 dim=5 161002(资料 161002-3「dim 不在取值范围内」) 561002
dim 维不能被 2 整除 x=[2x7]、yGrad=[2x3]、dim=1 161002 561002
yGrad dtype 与 x 不一致 yGrad fp16、其余 fp32 161002 161002 ✓
out dtype 与 x 不一致 out fp16、其余 fp32 161002 161002 ✓
yGrad 未减半 yGrad=[2x8]、x=[2x8]、dim=1 161002(或至少拒绝) 0(静默接受)

两个问题:

  1. dim 非法返回内部错误码 561002 而不是 161002。资料错误码表明确写「dim 不在取值范围内 → 161002」,
    实测把 tiling 失败(ACLNN_ERR_INNER_TILING_ERROR)透出给调用方,无法区分"参数写错"与"算子内部故障"。
  2. yGrad 的形状约束既没写进资料、也没校验。接口语义要求 yGrad 在 dim 维恰为 x 的一半
    (公式 out_A = yGrad*(s+s(1-s)A)*B 里 A、B 各是 x 的一半),但资料错误码表只有三条
    (空指针 / dtype / dim),完全没有这一条;实测传 yGrad 与 x 同形时两段都返回 0,
    即按错误的形状静默算完。这类输入会让内核按 x 的元素数去读 yGrad(越界读)或按半形错位计算结果。

Environment Information / 环境信息 (Mandatory / 必填)

  • 昇腾硬件型号:Atlas A2 训练系列产品(910B2)
  • CANN 版本:9.2.0-beta.2(Linux aarch64)
  • 算子仓:ops-nn activation/swi_glu_grad
  • 资料:activation/swi_glu_grad/docs/aclnnSwiGluGrad.md

Steps to reproduce the issue / 重现步骤 (Mandatory / 必填)

保存为 repro.cpp,source $ASCEND_HOME_PATH/set_env.sh 后编译运行(只依赖 acl):

g++ -std=c++17 repro.cpp -I${ASCEND_HOME_PATH}/include \
    -L${ASCEND_HOME_PATH}/lib64 -lascendcl -lnnopbase -lopapi -o repro && ./repro
// aclnnSwiGluGrad:dim 非法应 161002,yGrad 未减半应被拒绝
#include <cstdio>
#include <vector>

#include "acl/acl.h"
#include "aclnnop/aclnn_swi_glu_grad.h"

static int64_t Numel(const std::vector<int64_t>& s)
{
    int64_t n = 1;
    for (auto v : s) { n *= v; }
    return n;
}

static aclTensor* Make(const std::vector<int64_t>& shape, void** dev)
{
    int64_t bytes = Numel(shape) * 4;
    if (aclrtMalloc(dev, bytes, ACL_MEM_MALLOC_HUGE_FIRST) != ACL_SUCCESS) { return nullptr; }
    std::vector<int64_t> strides(shape.size(), 1);
    for (int64_t i = static_cast<int64_t>(shape.size()) - 2; i >= 0; --i) { strides[i] = shape[i + 1] * strides[i + 1]; }
    return aclCreateTensor(shape.data(), shape.size(), ACL_FLOAT, strides.data(), 0, ACL_FORMAT_ND,
                           shape.data(), shape.size(), *dev);
}

static void Case(const char* tag, const std::vector<int64_t>& g, const std::vector<int64_t>& x,
                 int64_t dim, aclrtStream stream)
{
    void *dg = nullptr, *dx = nullptr, *dz = nullptr;
    aclTensor* tg = Make(g, &dg);
    aclTensor* tx = Make(x, &dx);
    aclTensor* tz = Make(x, &dz);
    uint64_t ws = 0;
    aclOpExecutor* exec = nullptr;
    auto rc1 = aclnnSwiGluGradGetWorkspaceSize(tg, tx, dim, tz, &ws, &exec);
    if (rc1 != ACL_SUCCESS) {
        printf("%-40s 第一段 rc=%d\n", tag, rc1);
    } else {
        void* wsa = nullptr;
        if (ws > 0) { aclrtMalloc(&wsa, ws, ACL_MEM_MALLOC_HUGE_FIRST); }
        auto rc2 = aclnnSwiGluGrad(wsa, ws, exec, stream);
        auto rc3 = aclrtSynchronizeStream(stream);
        printf("%-40s 第一段 0,第二段 rc=%d sync=%d\n", tag, rc2, rc3);
        if (wsa != nullptr) { aclrtFree(wsa); }
    }
    aclDestroyTensor(tg); aclDestroyTensor(tx); aclDestroyTensor(tz);
    aclrtFree(dg); aclrtFree(dx); aclrtFree(dz);
}

int main()
{
    aclInit(nullptr);
    aclrtSetDevice(0);
    aclrtStream stream = nullptr;
    aclrtCreateStream(&stream);

    Case("对照 yGrad[2x4] x[2x8] dim=1", {2, 4}, {2, 8}, 1, stream);
    Case("dim=5 越界(应 161002)",      {2, 4}, {2, 8}, 5, stream);
    Case("x[2x7] 不能被 2 整除(应 161002)", {2, 3}, {2, 7}, 1, stream);
    Case("yGrad 未减半 [2x8](应拒绝)",  {2, 8}, {2, 8}, 1, stream);

    aclrtDestroyStream(stream);
    aclrtResetDevice(0);
    aclFinalize();
    return 0;
}

实测输出:

对照 yGrad[2x4] x[2x8] dim=1          第一段 0,第二段 rc=0 sync=0
dim=5 越界(应 161002)               第一段 rc=561002
x[2x7] 不能被 2 整除(应 161002)     第一段 rc=561002
yGrad 未减半 [2x8](应拒绝)          第一段 0,第二段 rc=0 sync=0

error-scenario 线独立复现了第一条:
NV-SwiGluGrad-161002-2 MISMATCH expect=161002 got=561002。

Describe the expected behavior / 预期结果 (Mandatory / 必填)

  1. dim < -x.dim() || dim >= x.dim() 或 x.size(dim) % 2 != 0 → 第一段返回 ACLNN_ERR_PARAM_INVALID(161002),
    与资料 161002-3 一致;
  2. yGrad.dim() != x.dim() 或 yGrad.size(dim) * 2 != x.size(dim) → 161002
    (并建议把这条约束补进资料的错误码表 —— 现在资料里没有这一条)。
对照 yGrad[2x4] x[2x8] dim=1          第一段 0,第二段 rc=0 sync=0
dim=5 越界(应 161002)               第一段 rc=561002
x[2x7] 不能被 2 整除(应 161002)     第一段 rc=561002
yGrad 未减半 [2x8](应拒绝)          第一段 0,第二段 rc=0 sync=0

[errscenario] [det=none] NV-SwiGluGrad-161002-2  MISMATCH  expect=161002 got=561002

Special notes for this issue/备注 (Optional / 选填)

  • 查重:已检索 ops-nn 最近 1000 条 issue(标题+正文)里 SwiGlu 相关条目,未见本条。
  • 同批 aclnnSituGluGrad 有同类问题(dim 非法返回 561002),已另提 issue。
  • yGrad 未减半这一条比错误码更值得注意:结果静默错误,且资料没声明这个约束,
    调用方即使照资料写也会踩到。建议同时补资料与 CheckParams。
likedislike
yuning_chen
yuning_chenCollaborators
Oct 22 Comments:

初步分析

感谢反馈。

现象理解:aclnnSwiGluGrad 对非法 dim 返回 561002,yGrad 在该维没有减半时被静默接受。资料要求这两类都返回 161002。

当前判断:初步判断,待复现确认。

涉及范围:activation/swi_glu_grad 的 dim 和 yGrad 形状检查。

下一步:用越界 dim,以及 yGrad 与 x 同形状的输入看返回码。确认后在入口返回 161002。

likedislike
yuning_chenyuning_chenCollaborators
Oct 22 Assign shilulu as responsible person
yuning_chen
yuning_chenCollaborators
Oct 22 Comments:

您好,该问题已收到,当前问题 @east_yang 处理中

likedislike
yuning_chenyuning_chenCollaborators
Oct 22 Removed responsible person shilulu
yuning_chenyuning_chenCollaborators
Oct 22 Assign east_yang as responsible person
east_yangeast_yangCollaborators
24 days ago Assign shilulu as responsible person
east_yangeast_yangCollaborators
24 days ago Removed responsible person east_yang
shilulu
shiluluCollaborators
22 days ago Comments:

感谢反馈,问题已确认并处理。

核对结论:dim 越界、dim 维不能被 2 整除两类输入,校验位于 tiling 层(SetTotalShape),失败后由框架统一映射为 ACLNN_ERR_INNER_TILING_ERROR(561002)——这是所有生成式接入算子的统一行为,因此将资料错误码表对齐为 561002(原161002 声明有误)。yGrad 未减半场景原实现确实静默接受,存在按错误形状计算与越界读风险,已在资料错误码表补充该约束声明(yGrad 与 x 形状不匹配 → 561002),对应的 tiling 层形状校验代码随另一个 PR 合入,合入后该场景不再静默。

修复已随 PR https://gitcode.com/cann/ops-nn/pull/13166 提交(资料部分),请基于该 PR 及后续 tiling PR 验证。

likedislike
CANN-robotCANN-robotCollaborators
22 days ago Close issue
CANN-robotCANN-robotCollaborators
22 days ago Added label: resolved