Closed
[Bug-Report|缺陷反馈]: aclnnInverse 对 out 与 self 的 dtype 不一致不做校验(只查可否 cast),fp32→fp16/bf16 静默降精度返回 0 #7880
liuhanwen8Add on 20 days agoClosed at 8 days ago
20 days ago Added label: bug-report
20 days ago Modify description of issue
19 days ago Assign east_yang as responsible person
yuning_chen
19 days ago Comments:
19 days ago Comments:
yuning_chen
19 days ago Comments:
19 days ago Comments:
您好,该问题已收到,当前问题 @east_yang 正在处理中


east_yang
19 days ago Comments:
19 days ago Comments:
/unassign @east_yang


19 days ago Removed responsible person east_yang
19 days ago Assign bartonfang as responsible person
yuning_chen
19 days ago Comments:
19 days ago Comments:
您好,该问题已收到,当前问题 @bartonfang 正在处理中


10 days ago Assign yue-ma as responsible person, remove original responsible person: bartonfang
10 days ago Assign yue-ma as responsible person, remove original responsible person: bartonfang
9 event messages are collapsed here。 See more
2 days ago Assign chenjiayi_ as responsible person
chenjiayi_
2 days ago Comments:
2 days ago Comments:
补充说明本问题的后续处理:
已修复并随关联 PR 合入。
处理结果:
- !14386:docs(matrix_inverse): 修正 aclnnInverse out dtype 约束
验证:
- 关联 PR(2026-11-13)已合入,变更可从 PR 记录追溯。
- 本次以 PR 记录与 issue 讨论核对为主,未重新执行复现或真机验证。
说明:合入分支与版本以 PR 记录为准;如仍有问题,请重新提 issue 并附版本、shape 与日志。
如果还有后续需要,请重新提 issue。


13 hours ago Removed responsible person chenjiayi_
Describe the current behavior / 问题描述 (Mandatory / 必填)
aclnnInverse在out的 dtype 与self不一致时,第一段接口aclnnInverseGetWorkspaceSize返回 0(成功),并继续执行,把结果按out的 dtype 静默降精度写入,全程无任何告警。
资料对该参数的原文是:
实测(
self=fp32[2x2],其余参数完全相同,只改out的 dtype):0.6, -0.7, -0.2, 0.4(正确基线)0.6001, -0.7002, -0.19995, 0.39990.60156, -0.69922, -0.2002, 0.400390.6, -0.7, -0.2, 0.40.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 / 必填)
npu-smi info显示 910B2)opp/version.infotimestamp=20261015_201912253)optest_lhw环境)Steps to reproduce the issue / 重现步骤 (Mandatory / 必填)
self = fp32 [2x2]对角阵[[3, 0], [0, 7]](逆为[[1/3, 0], [0, 1/7]])。out,dtype 设为 float16(与 self 的 float32 不一致),shape 保持[2x2]。aclnnInverseGetWorkspaceSize(self, out, &workspaceSize, &executor)→ 返回 0(未拒绝)。
aclnnInverse(workspace, workspaceSize, executor, stream)→ 返回 0,aclrtSynchronizeStream后读取out,得到 fp16 精度的结果。对照:把第 2 步的
outdtype 换成 float32,其余不变,得到正确的 fp32 结果;把
outdtype 换成 int32,则第 3 步正确返回 161002。Describe the expected behavior / 预期结果 (Mandatory / 必填)
按资料「
out的数据类型和 shape 需要与 self 一致」,第 3 步应当在aclnnInverseGetWorkspaceSize阶段返回 161002(ACLNN_ERR_PARAM_INVALID),而不是放行后静默降精度。
参考框架 PyTorch 也是拒绝的(torch 2.14.0 实测):
即「dtype 必须相等」是业界一致口径,不是本仓特有约束。
Related log / screenshot / 日志 / 截图 (Mandatory / 必填)
复现程序(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 安装根):实测输出(原样复跑所得):
上机对照:对角阵
[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(mastere121f9f27)::43-52CheckDtypeValid()只做两件事:OP_CHECK_DTYPE_NOT_SUPPORT(self, DTYPE_SUPPORT_LIST, ...)—— 只校验self在支持列表内;OP_CHECK_RESULT_DTYPE_CAST_FAILED(self->GetDataType(), out->GetDataType(), ...)—— 校验的是「能否 cast」,不是「是否相等」。
:79-90CheckParams()依次调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/outdtype 不一致时返回ACLNN_ERR_PARAM_INVALID(161002);若确实想支持「out 可降精度」,则资料应改写为「dtype 需可 cast 且不小于 self」,
两者择一,不要保留现状。