Closed
[Bug-Report|缺陷反馈]: aclnnMatmul 非法 out shape/负维度被误报为 561103 内部异常(应为 161002,同仓 aclnnMm 正确拒绝) #7997
liuhanwen8Add on  19 days agoClosed at  10 days ago
liuhanwen8
liuhanwen8
19 days ago Create

关联:#7793(同一议题:Matmul 系列 Host 错误上报分类不规范)。本条差异:具体到 aclnnMatmul 的 out 契约缺失,且给出 161002→561000→561103 三段改写链的 plog 实证。
同族 561103 单:#7881 / #7851 / #7925 / #7859 / #7873(不同接口),本条为 aclnnMatmul。

现象

aclnnMatmul 在 out 元素数与推导结果不符、或任一输入维度为负数时,第一段接口 aclnnMatmulGetWorkspaceSize 返回 561103(ACLNN_ERR_INNER_NULLPTR,内部异常),而非 161002(ACLNN_ERR_PARAM_INVALID,参数非法)。同一份错误输入喂给同目录的 aclnnMm,后者正确返回 161002。

输入 aclnnMm aclnnMatmul 说明
(2,4)@(4,2),out=(2,2) 0 0 对照组
(2,4)@(4,2),out=(2,3) 161002 561103 缺陷组,6≠4
(-2,4)@(4,2),out=(-2,2) — 561103 缺陷组,负维度
self=INT8(不支持) — 161002 对照组,161002 通路完好

期望

docs/aclnnMatmul.md 第一段接口「返回值」只声明 161001(空指针)与 161002(dtype/format 不支持、无法推导、推导类型无法转 out),未声明任何 561xxx 内部异常码;docs/zh/context/aclnn_return_code.md 第 28 行把 561103 定义为「内部异常:出现了 nullptr 的异常」。

故非法 out 应返回 161002,不应返回代表 API 自身故障的 561103。

环境

  • 硬件:Atlas A2 训练系列产品(Ascend910B2),npu-smi 26.1.1
  • CANN:9.2.0,Linux aarch64,g++ 11.4.0
  • 仓库与提交:cann/ops-nn 118c0c719ba37c65e984c8da707863ccce6c4fad(git fetch 后 master 最新,2026-11-03 核对)
  • 复现日期:2026-11-02

复现步骤

  1. 保存下方脚本为 repro.cpp
  2. g++ -std=c++17 -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lopapi -lnnopbase
  3. ./repro
  4. 观察输出:Case2/Case3 应为 161002,实测 561103

复现脚本

#include <acl/acl.h>
#include <aclnn/acl_meta.h>
#include "aclnnop/aclnn_matmul.h"
#include "aclnnop/aclnn_mm.h"
#include <cstdio>
#include <vector>

static aclTensor* mk(const int64_t* d, uint64_t n, aclDataType dt, void* p, const int64_t* so = nullptr)
{
    std::vector<int64_t> st(n, 1);
    for (int i = (int)n - 2; i >= 0; i--) st[i] = st[i + 1] * d[i + 1];
    return aclCreateTensor(d, n, dt, so ? so : st.data(), 0, ACL_FORMAT_ND, d, n, p);
}

int main()
{
    aclInit(nullptr);
    aclrtSetDevice(0);
    const int64_t aD[2] = {2, 4}, bD[2] = {4, 2}, okD[2] = {2, 2}, badD[2] = {2, 3};
    void *dA, *dB, *dOk, *dBad;
    aclrtMalloc(&dA, 16, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMalloc(&dB, 16, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(&dOk, 8, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMalloc(&dBad, 12, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemset(dA, 16, 0, 16); aclrtMemset(dB, 16, 0, 16);

    aclTensor* tA = mk(aD, 2, ACL_FLOAT16, dA);   aclTensor* tB = mk(bD, 2, ACL_FLOAT16, dB);
    aclTensor* tOk = mk(okD, 2, ACL_FLOAT16, dOk); aclTensor* tBad = mk(badD, 2, ACL_FLOAT16, dBad);
    uint64_t ws = 0; aclOpExecutor* ex = nullptr;

    int r1 = (int)aclnnMatmulGetWorkspaceSize(tA, tB, tOk, 0, &ws, &ex);
    printf("Case1 correct out (2,2)      -> %d (expect 0)\n", r1);
    ws = 0; ex = nullptr;
    int r2 = (int)aclnnMatmulGetWorkspaceSize(tA, tB, tBad, 0, &ws, &ex);
    printf("Case2 wrong out (2,3)        -> %d (expect 161002)\n", r2);
    ws = 0; ex = nullptr;
    int r3 = (int)aclnnMmGetWorkspaceSize(tA, tB, tBad, 0, &ws, &ex);
    printf("Case3 aclnnMm same wrong out -> %d (expect 161002)\n", r3);

    const int64_t nA[2] = {-2, 4}, nO[2] = {-2, 2}, sA[2] = {-4, 1}, sO[2] = {-2, 1};
    void *dNA, *dNO;
    aclrtMalloc(&dNA, 16, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMalloc(&dNO, 16, ACL_MEM_MALLOC_HUGE_FIRST);
    aclTensor* tNA = mk(nA, 2, ACL_FLOAT16, dNA, sA); aclTensor* tNO = mk(nO, 2, ACL_FLOAT16, dNO, sO);
    ws = 0; ex = nullptr;
    int r4 = (int)aclnnMatmulGetWorkspaceSize(tNA, tB, tNO, 0, &ws, &ex);
    printf("Case4 negative dim (-2,4)    -> %d (expect 161002)\n", r4);

    ws = 0; ex = nullptr;
    int r5 = (int)aclnnMatmulGetWorkspaceSize(mk(aD, 2, ACL_INT8, dA), tB, tOk, 0, &ws, &ex);
    printf("Case5 control int8 self      -> %d (expect 161002)\n", r5);

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

实测输出:

Case1 correct out (2,2)          -> 0 (expect 0)
Case2 wrong out (2,3)            -> 561103 (expect 161002)      <-- 缺陷
Case3 aclnnMm same wrong out     -> 161002 (expect 161002)
Case4 negative dim (-2,4)        -> 561103 (expect 161002)      <-- 缺陷
Case5 control int8 self          -> 161002 (expect 161002)

影响

调用方普遍按返回码分支处理:收到 561xxx 会判定为 CANN 内部故障,去排查安装/环境/内存,而真实原因是自身 out 传错。out 元素数不符属易犯错误(多算少算一维、batch 维漏乘)。

建议

三处配合修改,matmul/mat_mul_v3/op_host/op_api/aclnn_matmul.cpp:

  1. 补 out 契约校验:CheckInputParams(第 229-249 行)目前只校验 CheckNotNull、dtype 规则、CheckShapeValid(self, mat2)、CheckFormat,没有比对 out 与推导 shape。建议参照同目录 aclnn_mm.cpp 的 CheckOutputShapeValid(第 55-71 行)新增等价函数并接入校验链;aclnnMatmul 因支持 1~8 维与 1D 补维移除规则,推导需多一步,骨架可复用。
  2. 补负维度拦截:CheckShapeValid(第 164-213 行)目前只校验维数上限、维数非 0、K 轴相等、batch 可广播,未校验任一维度 < 0。建议增加该判断并返回 ACLNN_ERR_PARAM_INVALID。
  3. 错误码语义不覆盖:matmul/common/op_host/op_api/matmul_util.cpp 第 2483 行 l0op::Reshape(convOut, output->GetViewShape(), executor) 在元素数不符时返回 nullptr,第 2477 行将其归为 ACLNN_ERR_INNER_NULLPTR。元素数不符属可在第一段预判的用户错误,应在第一段拦截;第二段保留内部错误语义。
likedislike
liuhanwen8liuhanwen8
19 days ago Added label: bug-report
yuning_chenyuning_chenCollaborators
19 days ago Assign fanqirui as responsible person
yuning_chen
yuning_chenCollaborators
19 days ago Comments:

初步分析

感谢反馈,我们已开始核对这个问题。

现象理解:[Bug-Report|缺陷反馈]: aclnnMatmul 非法 out shape/负维度被误报为 561103 内部异常(应为 161002,同仓 aclnnMm 正确拒绝)。
当前判断:初步判断,待复现确认,还不能当成已确认根因。
涉及范围:aclnnMatmul、aclnnMm、aclnnMatmulGetWorkspaceSize、docs/aclnn、dtype/format、docs/zh
下一步:我们会继续对照代码/文档定位。

likedislike
yuning_chen
yuning_chenCollaborators
19 days ago Comments:

您好,该问题已收到,当前问题 @fanqirui 正在处理中

likedislike
范其瑞范其瑞Collaborators
19 days ago Assign linzehui2058 as responsible person
范其瑞范其瑞Collaborators
19 days ago Removed responsible person fanqirui
林泽辉林泽辉Collaborators
16 days ago Associated pull request
林泽辉林泽辉Collaborators
16 days ago Deleted associated pull request: fix_issue_11_5
林泽辉林泽辉Collaborators
16 days ago Associated pull request
林泽辉林泽辉Collaborators
16 days ago Deleted associated pull request: fix_issue_11_5
林泽辉林泽辉Collaborators
16 days ago Associated pull request
CANN-robotCANN-robotCollaborators
10 days ago Close issue
CANN-robotCANN-robotCollaborators
10 days ago changed custom state from 进行中 to 已完成
chenjiayi_
chenjiayi_Collaborators
10 days ago Comments:

补充修复结果(2026-11-12):

本问题由 PR#13786 修复并合入(作者 linzehui2058,合入时间 2026-11-12)。

修复内容:

  • matmul/mat_mul_v3/op_host/op_api/aclnn_matmul.cpp:247-305 新增 CheckOutputShapeValid():out 任一维为负时返回 161002;输入元素数大于 0 时按广播语义推导元素数,与 out 声明元素数相等时通过,存在差异时返回 161002。
  • :330 在 CheckInputParams() 中调用该检查,越界 out 在第一段 GetWorkspaceSize 即被拒绝。
  • 同 MR 一并修复 batch_matmul 空 batch 的对称场景(aclnn_batch_matmul.cpp、batch_matmul_util.cpp)。

验证:UT matmul/mat_mul_v3/tests/ut/op_host/test_aclnn_matmul.cpp 已随 MR 更新。

如果还有后续需要,请重新提 issue。

likedislike
CANN-robotCANN-robotCollaborators
9 days ago Added label: resolved
chenjiayi_chenjiayi_Collaborators
9 days ago Assign chenjiayi_ as responsible person
chenjiayi_chenjiayi_Collaborators
23 hours ago Removed responsible person chenjiayi_