Closed
[Bug-Report|缺陷反馈]: aclnnReduceNansum 浮点 self 配整型 out 返回 161002,与资料《互转换关系》声明的浮点→整型转换不符(15 组合全拒) #4310
liuhanwen8Add on  18 days agoClosed at  1 day ago
liuhanwen8
liuhanwen8
18 days ago Create

现象

aclnnReduceNansumGetWorkspaceSize 在 self 为浮点、dtype/out 为整型 时第一段直接返回 161002,
5 种整型 × 3 种浮点共 15 个组合全部被拒:

self \ dtype=out INT8 INT16 INT32 INT64 UINT8
FLOAT16 / FLOAT32 / BFLOAT16 161002 161002 161002 161002 161002

plog 给出准确的拒绝点(aclnn_reduce_nansum.cpp:96,对应 CheckPromoteType):

[ERROR] OP(...)[aclnn_reduce_nansum.cpp:96][NNOP][CheckPromoteType] errno[161002]
        OpName:[aclnnReduceNansum_0] Result type DT_FLOAT can't be cast to the desired output type DT_INT32.

同一份 shape / 轴 / keepDims 只把出参换成浮点(self=fp32, dtype=fp32)时 rc1=rc2=0 且结果正确(对照腿)。

期望

资料 math/reduce_nansum/docs/aclnnReduceNansum.md 的参数表写明:

  • self 的数据类型:FLOAT16、FLOAT32、INT8、INT16、INT32、INT64、UINT8、BOOL、BFLOAT16,
    「数据类型需和 out 的 dtype 满足可转换关系(参见互转换关系)」;
  • out 的数据类型同上,同样引用互转换关系;
  • dtype(返回张量所需数据类型)同样列出全部整型。

它引用的 docs/zh/context/conversion_relationship.md 明确写:

  • 浮点类型间可以转换,也支持往整型、复数类型转换。
  • BOOL 支持往整数、浮点、复数类型转换。

即「浮点 → 整型」是资料承诺支持的转换。因此 self=fp32, dtype=ACL_INT32, out=int32 应当
rc1=0、rc2=0,结果为按 INT32 累加/回写的和,而不是 161002。

当前实现把这条通路拦在 CheckPromoteType 的 CanCast(self->GetDataType(), out->GetDataType()),
而 CanCast 对 浮点→整型 返回 false。161002 的 6 个子项(dtype 不在支持范围 / dim 越界 / dim 重复 /
shape 超 8 维 / dtype≠out / out shape 不匹配)也都不覆盖这个场景,调用方无法从错误码判断真实原因。

补充观察(同一处校验,另记文档缺口,不并入本单):dtype=BOOL 只有 self=bool 时可用,
int8..uint8 self + bool out 同样 161002;互转换文档只承诺「BOOL → 整型/浮点」,未承诺反向,
故那部分建议在资料里补一句约束,而不是放开实现。

环境

  • 硬件:Atlas A2 910B2(179.166.3.2),npu-smi 26.1.1
  • CANN:9.2.0(/usr/local/Ascend/cann-9.2.0,libopapi.so),openEuler,g++ 10.3.1
  • 仓与提交:cann/ops-math 1b41503011bdc5090f45da41bac786dbbec91eba(master,CheckPromoteType 未改动)
  • 复现日期:2026-10-30

复现步骤

  1. 保存下方代码为 repro_rns.cpp
  2. g++ -O2 -std=c++17 repro_rns.cpp -o repro_rns -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lascendcl -lnnopbase -lopapi_math -lopapi
  3. ./repro_rns 0(对照,dtype=ACL_FLOAT)→ 期望 rc1=0 rc2=0 out=6
  4. ./repro_rns 1(dtype=ACL_INT32)→ 实测 rc1=161002

复现脚本

#include <cstdio>
#include <cmath>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_reduce_nansum.h"

#define CK(x) do { auto r = (x); if (r != ACL_SUCCESS) { printf("  %s ret=%d\n", #x, (int)r); return (int)r; } } while (0)

static aclTensor* mk(const std::vector<int64_t>& sh, aclDataType dt, void* p) {
    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];
    return aclCreateTensor(sh.data(), sh.size(), dt, st.data(), 0, ACL_FORMAT_ND,
                           sh.data(), sh.size(), p);
}

// mode 0: [2,3] -> out fp32(对照); mode 1: [2,3] -> out int32
int main(int argc, char** argv) {
    int mode = (argc > 1) ? atoi(argv[1]) : 0;
    std::vector<int64_t> sh{2, 3};
    std::vector<float> h{1, 1, 1, 1, 1, 1};
    aclDataType odt = (mode == 1) ? ACL_INT32 : ACL_FLOAT;
    void *dp = nullptr, *op = nullptr;
    CK(aclInit(nullptr));
    int dev = 0;
    CK(aclrtSetDevice(dev));
    aclrtStream st;
    CK(aclrtCreateStream(&st));
    CK(aclrtMalloc(&dp, 24, ACL_MEM_MALLOC_HUGE_FIRST));
    CK(aclrtMemcpy(dp, 24, h.data(), 24, ACL_MEMCPY_HOST_TO_DEVICE));
    CK(aclrtMalloc(&op, 4, ACL_MEM_MALLOC_HUGE_FIRST));
    aclTensor* self = mk(sh, ACL_FLOAT, dp);
    aclTensor* out = mk(std::vector<int64_t>{}, odt, op);
    int64_t ax[1] = {0};
    aclIntArray* dim = aclCreateIntArray(ax, 0);   // dims=[] → 全归约
    uint64_t ws = 0;
    aclOpExecutor* ex = nullptr;
    int r1 = (int)aclnnReduceNansumGetWorkspaceSize(self, dim, false, odt, out, &ws, &ex);
    printf("mode=%d rc1=%d ", mode, r1);
    if (r1 != 0) { printf("\n"); return r1; }
    void* wp = nullptr;
    if (ws > 0) CK(aclrtMalloc(&wp, ws, ACL_MEM_MALLOC_HUGE_FIRST));
    int r2 = (int)aclnnReduceNansum(wp, ws, ex, st);
    CK(aclrtSynchronizeStream(st));
    if (odt == ACL_INT32) { int32_t v = 0; CK(aclrtMemcpy(&v, 4, op, 4, ACL_MEMCPY_DEVICE_TO_HOST)); printf("rc2=%d out=%d\n", r2, v); }
    else { float v = 0; CK(aclrtMemcpy(&v, 4, op, 4, ACL_MEMCPY_DEVICE_TO_HOST)); printf("rc2=%d out=%g\n", r2, v); }
    aclDestroyTensor(self); aclDestroyTensor(out); aclDestroyIntArray(dim);
    aclrtFree(dp); aclrtFree(op); aclrtDestroyStream(st); aclrtResetDevice(dev); aclFinalize();
    return 0;
}

实际输出

$ ./repro_rns 0
mode=0 rc1=0 rc2=0 out=6

$ ./repro_rns 1
mode=1 rc1=161002

影响与建议

  • 按资料写的调用(self 为 FP32 计算结果、要求整型和)会在第一段失败,且错误码 161002 的
    6 个子项都无法解释该场景,调用方会误判为「参数写错」。
  • 建议二选一:
    1. 放开实现:把 CheckPromoteType 对 浮点→整型 的判定改为按《互转换关系》放行
      (Cast 到 dtype 后再归约,ReduceSum 分支已是现成通路),并补 dtype=int8/uint8 的饱和语义用例;
    2. 收紧资料:在 dtype/参数说明里明确「浮点 self 仅支持浮点或 BOOL 出参」,
      并在 161002 错误码表补一条「self 与 dtype 的转换不满足互转换关系」子项。
likedislike
liuhanwen8liuhanwen8
18 days ago Added label: bug-reportpending-confirmation
陈思
陈思Collaborators
17 days ago Comments:

您好,感谢反馈!

问题分析

您提供的日志表明,调用在第一段的类型转换校验处返回 161002。我们核对了 cann/ops-math 提交 212ce9d5460b1be26cfbee481aff1acbf5cc5c62:A2 路径虽然分别允许这些浮点和整型类型,但随后还会执行 CheckPromoteType(self, out);这一步发生在输入 Cast 和归约之前,因此类型各自在支持列表内、dtype 与 out 一致,仍不足以通过全部参数校验。该控制流与您报告的拒绝位置一致。参数与转换校验

文档方面,算子参数表确实直接引用了《互转换关系》,而该公共说明包含浮点到整型转换;算子页也未进一步说明当前的类型组合限制,161002 表中缺少转换组合不满足校验的场景,因此这里存在文档与现有行为的对应缺口。参数说明、互转换关系、返回码说明

同时,公共说明描述的是计算结果向输出类型转换,当前算子实现会先把输入转换成 dtype 再归约,两者涉及的数值语义需要区分。例如两个 0.6 先分别转整数再求和,与先求浮点和再转整数,结果可能不同。现有分支又根据原始 self 是否为整型选择 ReduceSum,而 ReduceNansum 的 AI Core 路径只支持浮点类型,因此仅放开前置校验,还不足以形成浮点输入到整型输出的完整支持。Cast 与归约分支、ReduceNansum 类型路径

处理方向

一个可行方向是保留现有类型组合限制,在算子页明确 A2 下浮点 self 仅接受浮点 dtype/out,并补充类型组合不满足校验时返回 161002 的说明;其中 BOOL 输出也受到独立限制,不宜写成“浮点支持浮点或 BOOL 出参”。若评估扩展浮点到整型的支持,则需要同时确定转换与归约的先后顺序、NaN 置零时机、非有限值及越界值的处理,以及小整数类型的累加和回写规则,并调整相应计算分支。我们会继续确认该算子的类型契约及文档对齐方式。

likedislike
陈思陈思Collaborators
17 days ago Assign chen_0715 as responsible person
陈思陈思Collaborators
16 days ago Removed label: pending-confirmation
CchenyifengCollaborators
15 days ago Assign sakuraqqz as responsible person
CchenyifengCollaborators
15 days ago Removed responsible person chen_0715
CANN-robotCANN-robotCollaborators
1 day ago Close issue
CANN-robotCANN-robotCollaborators
1 day ago changed custom state from 进行中 to 已完成
CANN-robotCANN-robotCollaborators
1 day ago Added label: resolved