Closed
[Bug-Report|缺陷反馈]: aclnnSoftmax 空 tensor 输入时跳过 dtype/dim 校验,非法入参返回成功 #7030
liuhanwen8Add on  Oct 21Closed at  19 days ago
liuhanwen8
liuhanwen8
Oct 21 Create

问题描述

aclnnSoftmax 在 self 为空张量(numel == 0,例如 shape = [0, 3])时,第一段接口直接返回成功,跳过了 dtype 与 dim 校验,导致两类非法入参被静默接受;而完全相同的非法参数在非空输入下会正确返回 161002。

真机(A2,CANN 9.2.0)纯 ACL 两段式直调实测:

用例 self shape dtype dim ret_gws 期望
A [0,3](空) ACL_INT8(不支持) -1 0 161002
B(对照) [4,5](非空) ACL_INT8(不支持) -1 161002 161002
C [0,3](空) ACL_FLOAT 5(rank=2 越界) 0 161002
D(对照) [4,5](非空) ACL_FLOAT 5(越界) 161002 161002

A/B、C/D 两组对照说明:校验逻辑本身是有的,只是在空张量分支被整体跳过。

对应代码位置(op_api/aclnn_softmax.cpp,CheckParams()):

static aclnnStatus CheckParams(const aclTensor* self, int64_t dim, aclTensor* out)
{
    CHECK_RET(CheckNotNull(self, out), ACLNN_ERR_PARAM_NULLPTR);
    if (self->IsEmpty()) {
        return ACLNN_SUCCESS;        // ← 早退发生在下面三项校验之前
    }
    CHECK_RET(CheckDtypeValid(self, out), ACLNN_ERR_PARAM_INVALID);  // 空张量时不会执行
    CHECK_RET(CheckDim(self, dim),        ACLNN_ERR_PARAM_INVALID);  // 空张量时不会执行
    CHECK_RET(CheckShape(self, out),      ACLNN_ERR_PARAM_INVALID);  // 空张量时不会执行
    return ACLNN_SUCCESS;
}

环境信息

  • 产品:Atlas A2 训练系列(910B2)
  • CANN 版本:9.2.0
  • 调用方式:ACL 原生两段式直调(aclnnSoftmaxGetWorkspaceSize + aclnnSoftmax),g++ 11.4.0
  • 头文件:include/aclnnop/aclnn_softmax.h

重现步骤

编译(ASCEND_HOME_PATH 为 CANN 安装路径):

g++ -std=c++17 -O2 softmax_repro.cpp -o softmax_repro \
    -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 \
    -lascendcl -lopapi -lnnopbase

运行四个用例:

./softmax_repro empty_int8      # 空张量 + 非法 dtype
./softmax_repro int8_nonempty   # 对照:非空 + 非法 dtype
./softmax_repro empty_dim       # 空张量 + 非法 dim
./softmax_repro dim5_nonempty   # 对照:非空 + 非法 dim

softmax_repro.cpp:

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_softmax.h"

int main(int argc, char** argv) {
    std::string mode = (argc > 1) ? argv[1] : "empty_int8";

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

    int64_t shape[2], strides[2];
    aclDataType dt;
    size_t W;
    int64_t dim = -1;

    if (mode == "empty_int8") {          // 空张量 + 不支持的 dtype
        shape[0] = 0; shape[1] = 3; strides[0] = 3; strides[1] = 1;
        dt = ACL_INT8; W = 1;
    } else if (mode == "int8_nonempty") { // 对照
        shape[0] = 4; shape[1] = 5; strides[0] = 5; strides[1] = 1;
        dt = ACL_INT8; W = 1;
    } else if (mode == "dim5_nonempty") { // 对照
        shape[0] = 4; shape[1] = 5; strides[0] = 5; strides[1] = 1;
        dt = ACL_FLOAT; W = 4; dim = 5;
    } else {                              // 空张量 + dim 越界(rank=2,dim=5)
        shape[0] = 0; shape[1] = 3; strides[0] = 3; strides[1] = 1;
        dt = ACL_FLOAT; W = 4; dim = 5;
    }

    size_t bytes = (size_t)shape[0] * shape[1] * W;
    if (bytes == 0) bytes = 64;           // 空张量也给合法地址

    void* devIn = nullptr;
    void* devOut = nullptr;
    aclrtMalloc(&devIn, bytes, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(&devOut, bytes, ACL_MEM_MALLOC_HUGE_FIRST);
    std::vector<unsigned char> zero(bytes, 0);
    aclrtMemcpy(devIn, bytes, zero.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(devOut, bytes, zero.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE);

    aclTensor* self = aclCreateTensor(shape, 2, dt, strides, 0, ACL_FORMAT_ND, shape, 2, devIn);
    aclTensor* out  = aclCreateTensor(shape, 2, dt, strides, 0, ACL_FORMAT_ND, shape, 2, devOut);

    uint64_t wsSize = 0;
    aclOpExecutor* exec = nullptr;
    aclnnStatus retGws = aclnnSoftmaxGetWorkspaceSize(self, dim, out, &wsSize, &exec);
    printf("mode=%s shape=[%lld,%lld] dim=%lld dtype=%d ret_gws=%d ws=%llu\n",
           mode.c_str(), (long long)shape[0], (long long)shape[1], (long long)dim,
           (int)dt, (int)retGws, (unsigned long long)wsSize);
    if (retGws != 0) return 0;

    void* ws = nullptr;
    if (wsSize > 0) aclrtMalloc(&ws, wsSize, ACL_MEM_MALLOC_HUGE_FIRST);
    aclnnStatus retExec = aclnnSoftmax(ws, wsSize, exec, stream);
    aclrtSynchronizeStream(stream);
    printf("ret_exec=%d\n", (int)retExec);
    return 0;
}

真机输出:

mode=empty_int8    shape=[0,3] dim=-1 dtype=2  ret_gws=0      ws=0
mode=int8_nonempty shape=[4,5] dim=-1 dtype=2  ret_gws=161002 ws=0
mode=empty_dim     shape=[0,3] dim=5  dtype=0  ret_gws=0      ws=0
mode=dim5_nonempty shape=[4,5] dim=5  dtype=0  ret_gws=161002 ws=0

预期结果

按接口资料 docs/aclnnSoftmax.md:

  • 「第一段接口 aclnnSoftmaxGetWorkspaceSize 完成入参校验」;
  • 返回码 161002 的说明包含「self 的数据类型不在支持的范围内」与「dim 的值不在 self 维度范围内」。

因此四个用例应统一返回 161002,与实际空/非空行为不一致。建议把 IsEmpty() 早退移到 CheckDtypeValid / CheckDim 之后(CheckShape 可按需保留宽松),或在早退分支内先完成 dtype 与 dim 校验;并补一条「空张量 + 非法 dtype/dim」的回归用例。

日志截图

无报错日志(两段接口均返回成功,故无任何异常输出)。复现仅依赖上面的 ret_gws 返回值。

备注

  • 该问题与本仓 #6605(aclnnDynamicQuant 空张量提前返回)属同一类「空张量早退跳过校验/写入」,但算子与点位不同,非重复报告。
  • self 为空张量在预处理链路(padding 后 batch=0)中常见,非法 dtype/dim 会被静默放行并使配置错误延迟到下游才暴露。
likedislike
liuhanwen8liuhanwen8
Oct 21 Added label: bug-report
yuning_chen
yuning_chenCollaborators
Oct 21 Comments:

初步分析

感谢反馈。

现象理解:aclnnSoftmax 遇到空 tensor 时跳过了 dtype 和 dim 校验,非法入参也返回成功。

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

涉及范围:softmax 的空 tensor 早退路径。

下一步:用空 tensor 加非法 dtype 或 dim 看是否仍然成功。确认后,早退前先做这些校验。

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

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

likedislike
yuning_chenyuning_chenCollaborators
Oct 22 Removed responsible person ASCEND222
yuning_chenyuning_chenCollaborators
Oct 22 Assign liu-wei as responsible person
liu-weiliu-weiCollaborators
Oct 22 Removed responsible person liu-wei
yuning_chenyuning_chenCollaborators
Oct 22 Assign east_yang as responsible person
east_yangeast_yangCollaborators
24 days ago Assign qiansunchi159 as responsible person
east_yangeast_yangCollaborators
24 days ago Removed responsible person east_yang
qiansunchi159qiansunchi159Collaborators
24 days ago Assign flag2233 as responsible person
flag2233flag2233Collaborators
23 days ago Associated pull request
liuhanwen8
liuhanwen8
22 days ago Comments:

补充实测(2026-10-30,910B2 + 910B3 双机复核)

本轮 aclnnSoftmax 六线挖掘(800 条用例)复现了本 issue 的根因,并发现同一早退分支还跳过第三项校验(out shape 一致性),一并补在这里。

1. 空 Tensor 下六种非法组合全部 rc1=0 / rc2=0(两段都成功)

aclnnSoftmaxGetWorkspaceSize 与 aclnnSoftmax 逐条直调,out 缓冲预填 NaN 金丝雀:

用例 self dtype dim out ret_gws ret_exec 期望
NV-045 [0,3] FLOAT 5(rank=2 越界) [0,3] 0 0 161002
NV-046 [0,3] FLOAT -5(越界) [0,3] 0 0 161002
NV-047 [0,3] INT32 -1 [0,3] 0 0 161002
NV-051 [0,5] INT8 -1 [0,5] 0 0 161002
NV-054 [0,5] BOOL -1 [0,5] 0 0 161002
NV-048 [0,3] FLOAT -1 [3,0](shape 不一致) 0 0 161002

NV-048 是本 issue 原文表格里没有的子项:self 与 out 的元素数都是 0 但形状不同,
CheckShape 里的 OP_CHECK_SHAPE_NOT_EQUAL 同样被 IsEmpty() 早退跨过。

2. 同参数的对照(非空输入)全部正确拒绝

组 用例数 结果
dim 越界(rank 1/2/3/4 × 4 个方向) 16 全部 161002
非法 dtype(int8/int16/int32/int64/uint8/bool/complex64/complex128) 19 全部 161002
out shape 不一致(4 组不同形态) 4 全部 161002

合计 39 条非空对照全部 161002 —— 印证「校验逻辑本身存在,只是被空 Tensor 分支整体跳过」。

3. 机型覆盖

机型 CANN 空 Tensor 六条 非空对照
Atlas A2 训练系列(910B2,179.162.4.2) 9.2.0 6/6 rc1=rc2=0 39/39 161002
Atlas A2 训练系列(910B3,179.167.3.3) 9.2.0 6/6 rc1=rc2=0 39/39 161002

两机型一致,说明不是单机环境问题。

4. 影响

空 batch 场景(如 [0, seq, hidden])下,用户把 dim 写错或 dtype 传错时不会得到任何报错,
问题要到第一个非空 batch 才暴露;out 形状写错同样静默通过。属"参数校验延后暴露",不产生脏数据,但会掩盖调用方 bug。

归档:ascend-qa-tools misc-notes 分支 Deepseek-V4.1-Flash/op_bug_mining_1030/aclnnSoftmax_910B2_20261030/
(evidence/nv_result.json 与 nv_result_910b3.json 为原始逐条结果)。

likedislike
flag2233flag2233Collaborators
22 days ago Associated pull request
yampolskiy
yampolskiy
20 days ago Comments:

@flag2233 @qiansunchi159 @east_yang Thank you for documenting the shared empty-tensor convention. I will not propose a Softmax-only change to dtype/dim validation against that decision.

I found related forward/backward output-shape cases during CPU execution of source-extracted host helpers (not a native SDK/NPU run). Would you prefer these recorded as regression examples for a future library-wide contract task, keeping the current empty-input success behavior until that contract is agreed? I can help assemble a cross-operator matrix distinguishing legitimate empty batches, mismatched output shapes, invalid dtype/dim, and nonempty controls. Please point me to the umbrella task if one exists; no parallel Softmax patch is being opened. AI-assisted review: Codex (GPT-6).

likedislike
flag2233flag2233Collaborators
19 days ago changed custom state from 进行中 to 已完成
flag2233flag2233Collaborators
19 days ago Close issue
guijianweiguijianweiCollaborators
11 days ago Associated pull request