Closed
[Bug-Report|缺陷反馈]: foreach inplace 系列 aclnn golden 整型标量转 float 导致 int32 用例精度假红 #8601
严明奎Add on  11 days agoClosed at  11 days ago
严明奎
严明奎Collaborators
11 days ago Create

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

TTK aclnn 通路 golden 的 _tp_num() 将整型标量统一转成 Python float,torch 类型提升会因此把 int32 张量抬到 fp32:

  • 运算丢精度(>2^24,compare 日志的 Expected 出现 2147482624/2147483136 等 256 倍量化值);
  • 越界后 _keep_dtype 窄回 int32 时,越界 float→int 转换产生 INT32_MIN(-2147483648),导致精度假红。

受影响算子(均为 tests/assets/golden.py,非算子实现):

  • foreach_mul_scalar_inplace(int32 用例 8 个)
  • foreach_sub_scalar_inplace(int32 用例 1 个,精度 0%)
  • foreach_sub_list_inplace(int32 用例 3 个,精度 50%)

根因:aclnn 通路 golden 的整型口径与 kernel 通路 _tp_scalar 不一致。

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

  • 昇腾硬件型号:NPU(本地 TTK 验证环境)
  • CANN 版本:以所验证 ops-nn master(c9392e717) 编包环境为准
  • Python 版本:未记录

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

  1. 用 TTK 运行上述三个算子的 aclnn 用例;
  2. 选取 int32 输入张量与整型标量,例如 x=-1601190518, scalar=2;
  3. 观察 Comparison 结果与 Expected 值。

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

NPU 对 int32 溢出按二补码回绕,golden 应与之一致。例如 x=-1601190518, scalar=2 时 NPU 输出 1092586260,修复前 golden 输出 -2147483648。

x = -1601190518 (int32), scalar = 2
NPU  : 1092586260
golden(修复前): -2147483648   # 越界 float→int 转换结果

foreach_sub_list_inplace 的 compare 日志中 Expected 出现 2147482624、2147483136 等 256 倍量化值。

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

修复方式:aclnn golden 的 _tp_num() 对整型返回 Python int(weak-typed,张量保持 int32,按二补码回绕),浮点仍返回 float,与 kernel 通路 _tp_scalar 口径一致;标量归一同样走 _tp_bf16_carrier(np.asarray(v))。

验证:三个算子原失败用例分别 8/8、1/1、3/3 PASS,完整 l0 用例集回归全 PASS(浮点无回归)。

关联 PR:https://gitcode.com/cann/ops-nn/merge_requests/14179

likedislike
严明奎严明奎Collaborators
11 days ago Added label: bug-report
CANN-robotCANN-robotCollaborators
11 days ago Close issue
CANN-robotCANN-robotCollaborators
11 days ago changed custom state from 待办的 to 已完成
CANN-robotCANN-robotCollaborators
10 days ago Added label: resolved