Closed
[Bug-Report|缺陷反馈]: aclnnRsubs 的 IsScalarOne 把 int64 alpha 窄化为 int32 判等,alpha=2^32+1 被当成 1 导致输出漏乘 #4247
liuhanwen8Add on  19 days agoClosed at  8 days ago
liuhanwen8
liuhanwen8
19 days ago Create

aclnnRsubs:int64 alpha 被 IsScalarOne 截断为 int32 后误判为 1,输出漏乘 alpha

现象

aclnnRsubs 在 alpha 为 int64 且取值 ≡ 1 (mod 2^32) 时把它当成 1,走 Sub(other, self) 快路径,输出 other - self 而非 other - self*alpha。

输入(self=[1,2,3,4] int64,other=0) 两段返回 输出 说明
alpha=2 / alpha=2^32+2 0 / 0 [-2,-4,...] / [-2^32-2,...] 对照与阴性对照:正确
alpha=2^32+1 / alpha=-2^32+1 0 / 0 [-1,-2,-3,-4] 缺陷:按 alpha=1 计算

期望

资料 math/sub/docs/aclnnRsubs.md「功能说明 / 计算公式」写明 「out_i = other - self_i * alpha」,参数表对 alpha 取值无范围限制,只要求能转成推导类型。
alpha=4294967297 可被 ACL_INT64 精确表示且能转成 INT64,属合法值,应输出 other - self*4294967297。

环境

  • 硬件:Atlas A3(npu-smi 26.1.1,SoC ascend910_93);Atlas A2 910B2 同样复现
  • CANN:9.2.0(V100R001C12SPC001B237),Linux aarch64,g++ 10.3.1
  • 仓库与提交:cann/ops-math c171dab56e5662a3a5cf75d01f1fda8fadfd5f37(git fetch 后 master)
  • 复现日期:2026-10-29

复现步骤

  1. 保存下方脚本为 repro.cpp
  2. g++ -std=c++17 -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lnnopbase -lopapi
  3. ./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;
}

实测输出:

alpha          说明                 实际 out           正确 out            结果
1              alpha=1              [-1,-2]             [-1,-2]              OK
2              alpha=2              [-2,-4]             [-2,-4]              OK
4294967297     2^32+1               [-1,-2]             [-4294967297,-8589934594] 错误(按 alpha=1 计算)   <== 缺陷
4294967298     2^32+2(阴性对照)    [-4294967298,-8589934596] [-4294967298,-8589934596] OK
-4294967295    -2^32+1              [-1,-2]             [4294967295,8589934590]    错误(按 alpha=1 计算)   <== 缺陷

影响

调用 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 窄化,触发值与缺陷行均不同,非重复。

likedislike
liuhanwen8
liuhanwen8
19 days ago Comments:

PLACEHOLDER

likedislike
liuhanwen8
liuhanwen8
19 days ago Comments:

补充:同一根因在 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 后,建议同时跑:

  1. aclnnRsubs(本 issue 原复现);
  2. aclnnRsub(本条复现)—— 两者走的 promoteType 推导路径不同(PromoteType vs PromoteTypeScalar),需分别确认。

若维护者认为应拆成独立单跟进 aclnnRsub,可告知,我再单独提。

likedislike
liuhanwen8
liuhanwen8
19 days ago Comments:

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

有效内容见评论 id 196914246。

likedislike
陈思
陈思Collaborators
18 days ago Comments:

您好,核对 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 复现,本轮未执行设备测试或提交修复。

likedislike
陈思陈思Collaborators
18 days ago Assign chen_0715 as responsible person
陈思陈思Collaborators
18 days ago Added label: pending-confirmation
陈思陈思Collaborators
18 days ago Removed label: pending-confirmation
CchenyifengCollaborators
18 days ago Assign gcw_SUaZx3UQ as responsible person
CchenyifengCollaborators
18 days ago Removed responsible person chen_0715
Zzhangqijia1Collaborators
15 days ago Associated pull request
CANN-robotCANN-robotCollaborators
8 days ago Close issue
CANN-robotCANN-robotCollaborators
8 days ago changed custom state from 进行中 to 已完成
CANN-robotCANN-robotCollaborators
8 days ago Added label: resolved