Closed
[Bug-Report|缺陷反馈]: aclnnInverse 对 out 与 self 的 dtype 不一致不做校验(只查可否 cast),fp32→fp16/bf16 静默降精度返回 0 #7880
liuhanwen8Add on  20 days agoClosed at  8 days ago
liuhanwen8
liuhanwen8
20 days ago Create

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

aclnnInverse 在 out 的 dtype 与 self 不一致时,第一段接口
aclnnInverseGetWorkspaceSize 返回 0(成功),并继续执行,把结果按 out
的 dtype 静默降精度写入,全程无任何告警。

资料对该参数的原文是:

out(aclTensor*) 输出:输出逆矩阵。
使用说明:数据类型和shape需要与self一致。

实测(self=fp32[2x2],其余参数完全相同,只改 out 的 dtype):

self out 返回码 输出(首 4 元素)
fp32 fp32 0 0.6, -0.7, -0.2, 0.4(正确基线)
fp32 fp16 0 0.6001, -0.7002, -0.19995, 0.3999
fp32 bf16 0 0.60156, -0.69922, -0.2002, 0.40039
fp16 fp32 0 0.6, -0.7, -0.2, 0.4
fp16 fp16 0 0.6001, -0.7002, -0.19995, 0.3999(基线)

用可精确表示的对角阵把误差显出来(self=fp32 对角 [3, 7]):
fp32→fp32 得 0.333333, 0.142857;fp32→bf16 得 0.33398, 0.14258。
调用方以为算的是 fp32,实际拿到 bf16 精度结果 —— 静默精度损失。

边界:fp32→int32 会正确返回 161002(cast 不允许),说明不是完全不校验;
fp32→fp64 也返回 0(同样与资料「需一致」不符)。

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

  • 昇腾硬件型号:Ascend 910B2(npu-smi info 显示 910B2)
  • CANN 版本:9.2.0(opp/version.info timestamp=20261015_201912253)
  • 驱动 / 固件:26.1.1
  • Python 版本:3.12(miniconda optest_lhw 环境)
  • PyTorch:2.12.0+cpu;torch_npu:2.12.0.post3
  • 操作系统:aarch64 Linux

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

  1. 准备 self = fp32 [2x2] 对角阵 [[3, 0], [0, 7]](逆为 [[1/3, 0], [0, 1/7]])。
  2. 准备 out,dtype 设为 float16(与 self 的 float32 不一致),shape 保持 [2x2]。
  3. 调用 aclnnInverseGetWorkspaceSize(self, out, &workspaceSize, &executor)
    → 返回 0(未拒绝)。
  4. 调用 aclnnInverse(workspace, workspaceSize, executor, stream) → 返回 0,
    aclrtSynchronizeStream 后读取 out,得到 fp16 精度的结果。

对照:把第 2 步的 out dtype 换成 float32,其余不变,得到正确的 fp32 结果;
把 out dtype 换成 int32,则第 3 步正确返回 161002。

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

按资料「out 的数据类型和 shape 需要与 self 一致」,第 3 步应当在
aclnnInverseGetWorkspaceSize 阶段返回 161002(ACLNN_ERR_PARAM_INVALID),
而不是放行后静默降精度。

参考框架 PyTorch 也是拒绝的(torch 2.14.0 实测):

self=fp32 out=float16    -> RuntimeError: Expected out tensor to have dtype float, but got c10::Half instead
self=fp32 out=bfloat16   -> RuntimeError: Expected out tensor to have dtype float, but got c10::BFloat16 instead
self=fp32 out=float64    -> RuntimeError: Expected out tensor to have dtype float, but got double instead

即「dtype 必须相等」是业界一致口径,不是本仓特有约束。

复现程序(C++,独立最小用例):

#include <aclnn/aclnn_base.h>
#include <aclnnop/aclnn_inverse.h>
#include <acl/acl.h>
#include <cstdio>
#include <vector>

#define CHK(x) do { auto _r = (x); if (_r != 0) { printf("L%d rc=%d\n", __LINE__, (int)_r); return 1; } } while (0)

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

    // self = fp32 2x2 对角阵 [3, 7](逆为 [1/3, 1/7],可精确对照)
    std::vector<float> h = {3.f, 0.f, 0.f, 7.f};
    void* d = nullptr;
    CHK(aclrtMalloc(&d, h.size() * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST));
    CHK(aclrtMemcpy(d, h.size() * sizeof(float), h.data(),
                    h.size() * sizeof(float), ACL_MEMCPY_HOST_TO_DEVICE));

    int64_t sh[2] = {2, 2};
    int64_t st[2] = {2, 1};
    aclTensor* self = aclCreateTensor(sh, 2, ACL_FLOAT, st, 0, ACL_FORMAT_ND, sh, 2, d);
    void* od = nullptr;
    CHK(aclrtMalloc(&od, 4 * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST));

    // 关键:out 用 ACL_FLOAT16,与 self(fp32) 不一致
    aclTensor* out = aclCreateTensor(sh, 2, ACL_FLOAT16, st, 0, ACL_FORMAT_ND, sh, 2, od);

    uint64_t ws = 0;
    aclOpExecutor* ex = nullptr;
    auto rc = aclnnInverseGetWorkspaceSize(self, out, &ws, &ex);
    printf("aclnnInverseGetWorkspaceSize -> %d  (0 = 被接受,资料要求应拒)\n", (int)rc);
    if (rc == 0) {
        void* w = nullptr;
        if (ws) CHK(aclrtMalloc(&w, ws, ACL_MEM_MALLOC_HUGE_FIRST));
        rc = aclnnInverse(w, ws, ex, s);
        printf("aclnnInverse -> %d\n", (int)rc);
        CHK(aclrtSynchronizeStream(s));
        std::vector<uint16_t> oh(4);
        CHK(aclrtMemcpy(oh.data(), 4 * sizeof(uint16_t), od,
                        4 * sizeof(uint16_t), ACL_MEMCPY_DEVICE_TO_HOST));
        printf("out(fp16) raw = %u %u %u %u\n", oh[0], oh[1], oh[2], oh[3]);
    }
    aclDestroyTensor(self); aclDestroyTensor(out);
    aclrtFree(d); aclrtFree(od);
    aclrtDestroyStream(s); aclrtResetDevice(0); aclFinalize();
    return 0;
}

编译($ASCEND_HOME_PATH 为 CANN 安装根):

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

实测输出(原样复跑所得):

aclnnInverseGetWorkspaceSize -> 0  (0 = 被接受,资料要求应拒)
aclnnInverse -> 0
out(fp16) raw = 13653 0 0 12434

上机对照:对角阵 [3, 7] 的逆应为 [1/3, 1/7] = 0.333333, 0.142857。
而 4 个 fp16 半精度字 0x3555, 0, 0, 0x3092(十进制 13653 / 12434)
解出来是 0.33325, 0.14282 —— 精度已从 fp32 被降成 fp16。
若 out 用 fp32(合法基线),同一份输入得到 0.333333, 0.142857。

重复性:在 3 张卡(dev 0 / 1 / 2)上各跑一次,输出完全一致
(均 rc=0、raw 均为 13653 0 0 12434),非单卡偶发。

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

定位线索 / 代码锚点

index/matrix_inverse/op_host/op_api/aclnn_inverse.cpp(master e121f9f27):

  • :43-52 CheckDtypeValid() 只做两件事:
    1. OP_CHECK_DTYPE_NOT_SUPPORT(self, DTYPE_SUPPORT_LIST, ...) —— 只校验 self 在支持列表内;
    2. OP_CHECK_RESULT_DTYPE_CAST_FAILED(self->GetDataType(), out->GetDataType(), ...)
      —— 校验的是「能否 cast」,不是「是否相等」。
  • :79-90 CheckParams() 依次调 CheckNotNull / CheckDtypeValid / CheckShape,
    其中 CheckShape 用 OP_CHECK_SHAPE_NOT_EQUAL(out, self, ...) 做了 shape 必须相等,
    但 dtype 没有对应的「必须相等」检查 —— 两个约束不对称。

fp32 能 cast 到 fp16/bf16/fp64,所以 OP_CHECK_RESULT_DTYPE_CAST_FAILED 放行;
资料要的是「必须一致」,正确做法应是
OP_CHECK_DTYPE_NOT_SAME(或直接比较 GetDataType()),与 shape 侧对称。

建议

在 CheckDtypeValid() 增加与 shape 侧对称的「dtype 必须相等」检查,
使 self/out dtype 不一致时返回 ACLNN_ERR_PARAM_INVALID(161002);
若确实想支持「out 可降精度」,则资料应改写为「dtype 需可 cast 且不小于 self」,
两者择一,不要保留现状。

likedislike
liuhanwen8liuhanwen8
20 days ago Added label: bug-report
liuhanwen8liuhanwen8
20 days ago Modify description of issue
yuning_chenyuning_chenCollaborators
19 days ago Assign east_yang as responsible person
yuning_chen
yuning_chenCollaborators
19 days ago Comments:

初步分析

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

现象理解:Inverse 不校验 out 与 self dtype 一致性,仅检查可 cast
当前判断:初步判断,待复现确认。疑似校验过松,可能导致静默精度/类型问题
涉及范围:aclnnInverse 校验逻辑
下一步:按 fp32/其他 dtype 不一致用例复核并明确是否应 161002

likedislike
yuning_chen
yuning_chenCollaborators
19 days ago Comments:

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

likedislike
east_yang
east_yangCollaborators
19 days ago Comments:

/unassign @east_yang

likedislike
CANN-robotCANN-robotCollaborators
19 days ago Removed responsible person east_yang
yuning_chenyuning_chenCollaborators
19 days ago Assign bartonfang as responsible person
yuning_chen
yuning_chenCollaborators
19 days ago Comments:

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

likedislike
yuning_chenyuning_chenCollaborators
10 days ago Assign yue-ma as responsible person, remove original responsible person: bartonfang
9 event messages are collapsed here。 See more
chenjiayi_chenjiayi_Collaborators
2 days ago Assign chenjiayi_ as responsible person
chenjiayi_
chenjiayi_Collaborators
2 days ago Comments:

补充说明本问题的后续处理:

已修复并随关联 PR 合入。

处理结果:

  • !14386:docs(matrix_inverse): 修正 aclnnInverse out dtype 约束

验证:

  • 关联 PR(2026-11-13)已合入,变更可从 PR 记录追溯。
  • 本次以 PR 记录与 issue 讨论核对为主,未重新执行复现或真机验证。

说明:合入分支与版本以 PR 记录为准;如仍有问题,请重新提 issue 并附版本、shape 与日志。

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

likedislike
chenjiayi_chenjiayi_Collaborators
14 hours ago Removed responsible person chenjiayi_