PLACEHOLDER


补充:同一根因在 aclnnRsub(非 aclnnRsubs)上同样成立,已独立取证。
本 issue 标题与复现均针对 aclnnRsubs(alpha 为标量、other 也是标量)。经核对源码,IsScalarOne 是 math/sub/op_api/aclnn_rsub.cpp:149 的同一份 static 函数,被两个接口共用:
aclnn_rsub.cpp:322——aclnnRsubGetWorkspaceSize内的IsEqualToOne(promoteType, alpha)aclnn_rsub.cpp:447——aclnnRsubsGetWorkspaceSize内的同一调用
因此修复 IsScalarOne 会一次性解掉两边。但为了让回归能按接口分别验证,下面补上 aclnnRsub 的独立复现。
aclnnRsub 复现(self/other 都是 tensor)
// self = [1, 2] INT64, other = [0, 0] INT64, alpha = 4294967297 (2^32+1) INT64
// out 期望 [-4294967297, -8589934594]
aclnnRsubGetWorkspaceSize(self, other, alpha, out, &ws, &executor); // ret = 0
aclnnRsub(wsAddr, ws, executor, stream); // ret = 0
实测(Atlas A2 910B2 / CANN 9.2.0 / ops-math 1b415030):
GetWorkspaceSize ret=0
aclnnRsub ret=0
i self other out
0 1 0 -1
1 2 0 -2
期望 out = [-4294967297, -8589934594]
实际 out = [-1, -2]
与 issue 正文的 aclnnRsubs 结果一致:两段均返回 0,输出塌成 other - self。
命中面(aclnnRsub,逐 alpha 实测)
| alpha(INT64) | 实测 out[0] | 真值 | 判定 |
|---|---|---|---|
2^32+1 (4294967297) |
-1 | -4294967297 | 错误 |
2^33+1 (8589934593) |
-1 | -8589934593 | 错误 |
3*2^32+1 (12884901889) |
-1 | -12884901889 | 错误 |
2^40+1 (1099511627777) |
-1 | -1099511627777 | 错误 |
-2^32+1 (-4294967295) |
-1 | 4294967295 | 错误(符号也反) |
2^32+2 (4294967298) |
-4294967298 | 同 | 正确(阴性对照) |
(self=1, other=0)所有 ≡1 (mod 2^32) 的值全命中,非 1 模的 2^32+2 正常——与 issue 正文的判据一致。
框架内规模验证
aclnnRsub 的 714 条精度用例(8 dtype × 0–5 维 × 14 个边界 × alpha 池)中:
- 20 条 FAIL 全部为 int64,
mismatch_count == total(逐元素全错) max |Δ|从4.29e9到1.29e11,恰等于max|self| * |alpha-1|- 其余 dtype(fp32/fp16/bf16/int8/uint8/int16)全 PASS
修复验证建议
修复 IsScalarOne 后,建议同时跑:
aclnnRsubs(本 issue 原复现);aclnnRsub(本条复现)—— 两者走的promoteType推导路径不同(PromoteTypevsPromoteTypeScalar),需分别确认。
若维护者认为应拆成独立单跟进 aclnnRsub,可告知,我再单独提。


(勘误:上一条正文为 PLACEHOLDER 的评论是接口调试时的误发,内容无效,请以下方“补充:同一根因……”那一条为准。GitCode v5 当前不支持编辑/删除 issue 评论(DELETE /issues/{n}/comments/{id} 与 PATCH 均返 404),故只能在此声明。)
有效内容见评论 id 196914246。


您好,核对 ae087b184,IsEqualToOne / IsScalarOne 的非 RegBase 分支仍将整型 alpha 经 ToInt32() 后与 1 比较;Rsubs 在该判断成立后直接调用 Sub(other,self),跳过乘法。这与您给出的 INT64 2^32+1、-2^32+1 被当作 1 的结果相符。接口公式 为 other-self*alpha,这些可表示的 INT64 值不应因窄化比较而丢掉乘法。
建议修正快路径判等,使其与标量类型及实际计算类型一致,避免把 INT64 窄化成 INT32;同时审查 DOUBLE 经 ToFloat() 比较的精度边界。回归至少覆盖您给出的正负大整数、真正的 1、2^32+2 对照,以及 Tensor-other 的 Rsub 入口——两种入口共用这段判断。需保留各产品分支和特殊值行为,不能直接用另一分支替换而不核对类型语义。
目前已确认固定版本代码仍存在该窄化路径;设备现象依据您提供的 A2/A3 复现,本轮未执行设备测试或提交修复。


aclnnRsubs:int64 alpha 被 IsScalarOne 截断为 int32 后误判为 1,输出漏乘 alpha
现象
aclnnRsubs在 alpha 为 int64 且取值 ≡ 1 (mod 2^32) 时把它当成 1,走Sub(other, self)快路径,输出other - self而非other - self*alpha。[1,2,3,4]int64,other=0)alpha=2/alpha=2^32+2[-2,-4,...]/[-2^32-2,...]alpha=2^32+1/alpha=-2^32+1[-1,-2,-3,-4]期望
资料
math/sub/docs/aclnnRsubs.md「功能说明 / 计算公式」写明 「out_i = other - self_i * alpha」,参数表对 alpha 取值无范围限制,只要求能转成推导类型。alpha=4294967297可被 ACL_INT64 精确表示且能转成 INT64,属合法值,应输出other - self*4294967297。环境
npu-smi26.1.1,SoCascend910_93);Atlas A2 910B2 同样复现V100R001C12SPC001B237),Linux aarch64,g++ 10.3.1cann/ops-mathc171dab56e5662a3a5cf75d01f1fda8fadfd5f37(git fetch后 master)复现步骤
repro.cppg++ -std=c++17 -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lnnopbase -lopapi./repro,观察alpha=4294967297行应为[-4294967297,-8589934594],实测[-1,-2]复现脚本
#include <cstdio> #include <vector> #include "acl/acl.h" #include "aclnnop/aclnn_rsub.h" struct T { aclTensor* t; void* dev; }; static T MkT(const std::vector<int64_t>& sh, const std::vector<int64_t>& host) { int64_t n = 1; for (auto d : sh) n *= d; T o; aclrtMalloc(&o.dev, n * 8, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMemcpy(o.dev, n * 8, host.data(), n * 8, ACL_MEMCPY_HOST_TO_DEVICE); std::vector<int64_t> st(sh.size(), 1); for (int i = (int)sh.size() - 2; i >= 0; i--) st[i] = sh[i + 1] * st[i + 1]; o.t = aclCreateTensor(sh.data(), sh.size(), ACL_INT64, st.data(), 0, ACL_FORMAT_ND, sh.data(), sh.size(), o.dev); return o; } static int Run(aclrtStream s, int64_t alphaVal, std::vector<int64_t>* got) { std::vector<int64_t> sh = {4}; T self = MkT(sh, {1, 2, 3, 4}), out = MkT(sh, {0, 0, 0, 0}); int64_t zero = 0; aclScalar* other = aclCreateScalar(&zero, ACL_INT64); aclScalar* alpha = aclCreateScalar(&alphaVal, ACL_INT64); uint64_t ws = 0; aclOpExecutor* ex = nullptr; int rc = (int)aclnnRsubsGetWorkspaceSize(self.t, other, alpha, out.t, &ws, &ex); if (rc == 0) { void* p = nullptr; if (ws > 0) aclrtMalloc(&p, ws, ACL_MEM_MALLOC_HUGE_FIRST); rc = (int)aclnnRsubs(p, ws, ex, s); aclrtSynchronizeStream(s); got->assign(4, 0); aclrtMemcpy(got->data(), 32, out.dev, 32, ACL_MEMCPY_DEVICE_TO_HOST); if (p) aclrtFree(p); } aclDestroyTensor(self.t); aclDestroyTensor(out.t); aclDestroyScalar(other); aclDestroyScalar(alpha); aclrtFree(self.dev); aclrtFree(out.dev); return rc; } int main() { aclInit(nullptr); aclrtSetDevice(0); aclrtStream s = nullptr; aclrtCreateStream(&s); struct { int64_t a; const char* note; } items[] = { {1LL, "alpha=1"}, {2LL, "alpha=2"}, {4294967297LL, "2^32+1"}, {4294967298LL, "2^32+2(阴性对照)"}, {-4294967295LL, "-2^32+1"}}; for (auto& it : items) { std::vector<int64_t> got; int rc = Run(s, it.a, &got); if (rc != 0) { printf("%-14lld %-22s phase1=%d\n", (long long)it.a, it.note, rc); continue; } int64_t w0 = -1LL * it.a, w1 = -2LL * it.a; printf("%-14lld %-22s got=[%lld,%lld] want=[%lld,%lld] %s\n", (long long)it.a, it.note, (long long)got[0], (long long)got[1], (long long)w0, (long long)w1, (got[0] == w0) ? "OK" : "错误(按 alpha=1 计算)"); } aclrtDestroyStream(s); aclrtResetDevice(0); aclFinalize(); return 0; }实测输出:
影响
调用
aclnnRsubs(或经 op-plugin 路由的torch.rsub(input, other_scalar, alpha=...))且alpha以ACL_INT64传入、数值超 2^32 的整型场景会踩到。结果是静默错误,不只差 1 ulp——alpha 被整体丢弃,
out = other - self;错值会被下游直接消费,无 NaN/Inf 之类可自查信号。只能靠把 alpha 降到 int32 范围规避,无自动检测手段。
建议
math/sub/op_api/aclnn_rsub.cpp:149-157的IsScalarOne用return alpha->ToInt32() == 1;判整型 alpha。ToInt32()对 INT64 标量做窄化转换,2^32+1低 32 位恰为1,故被误判。建议按标量自身 dtype 判等(INT64 走
ToInt64() == 1,或先GetDataType()分支再取对应宽度);IsFloatingType分支的ToFloat()对 DOUBLE 标量同样会丢精度,建议一并处理。关联
同函数
IsEqualToOne的 RegBase 分支 NaN alpha 误判已由 #3460 修复(!6154/!6177);本条是非 RegBase 分支IsScalarOne的 int64 窄化,触发值与缺陷行均不同,非重复。