Closed
[Documentation|文档反馈]: aclnnForeachRoundOffNumber/V2 资料未声明 roundMode=7(实测=取小数部分,资料按「其他值→RNE」理解会算错) #8008
liuhanwen8Add on  19 days agoClosed at  10 days ago
liuhanwen8
liuhanwen8
19 days ago Create

现象

aclnnForeachRoundOffNumber / aclnnForeachRoundOffNumberV2 的资料在 roundMode 取值说明里只列了 1~6,并写「当 roundMode 为其他时,如果精度损失会进行四舍六入五成双舍入操作,不涉及精度损失时则不进行舍入操作」。

但实现(kernel 与 aclnn 两侧一致)把 7 定义为「取小数部分」,这是一个独立语义,不是「其他值走 RNE」。按资料理解 roundMode=7 会得到完全不同的结果。

同机同构造实测(输入 x = {0.5, 1.5, 2.5}):

roundMode 资料语义 实测输出 说明
1 四舍六入五成双(RNE) {0, 2, 2} 一致
2 向负无穷 {0, 1, 2} 一致
3 向正无穷 {1, 2, 3} 一致
4 四舍五入 {1, 2, 3} 一致
5 向零 {0, 1, 2} 一致
7 「其他值」→ 有精度损失时按 RNE,应得 {0, 2, 2} {0.5, 0.5, 0.5} 不符:实际返回小数部分 x - trunc(x)

根因定位(仓内源码,逐行)

  • foreach/foreach_round_off_number/op_kernel/foreach_round_off_number.h:33

    constexpr int8_t CAST_FRAC = 7;
    
  • 同文件 :106-111,ComputerPerCast 对 7 走独立分支:

    if (roundModeValue == CAST_FRAC) {
        Cast(float32Tensor[maxCastDataCount], float32Tensor, RoundMode::CAST_TRUNC, dataCount);
        PipeBarrier<PIPE_V>();
        Sub(float32Tensor[maxCastDataCount], float32Tensor, float32Tensor[maxCastDataCount], dataCount);
        PipeBarrier<PIPE_V>();
    }
    

    即先 trunc 再相减 = 取小数部分,与 RNE 无关。

  • arch35 的 SIMT 实现同样如此,arch35/foreach_round_off_number_simt.h:76-77:

    case CAST_FRAC:
        return value - truncf(value);
    

两侧实现语义一致,说明「7 = 取小数部分」是有意设计的扩展能力,只是资料没写。

期望

二选一:

  1. 补文档(推荐):在 aclnnForeachRoundOffNumber.md / aclnnForeachRoundOffNumberV2.md 的 roundMode 取值列表里补一条
    「当 roundMode=7,表示取输入的小数部分(x - trunc(x))」,并明确它与「其他值走 RNE」的边界;
  2. 或若 7 不应公开,则实现侧把 7 归入 default(RNE)并与资料对齐。

附带的同类缺口(同一接口,一并核对)

同一份资料对 x 与 out 的 dtype 列表内一致性一项,把错误码归在 561002 下:

ACLNN_ERR_INNER_TILING_ERROR | 561002 | x或out中的Tensor的数据类型不一致。

而 V2 把同样的四项(shape 约束 / 列表内 dtype / 超 8 维 / roundMode 元素数)归到 161002:

ACLNN_ERR_PARAM_INVALID | 161002 | x或out中的Tensor的数据类型不一致。

实测(同机同构造 x=fp32[2x3]ND|x_1=fp16[2x3]ND|out=fp32[2x3]ND):V1 返 561002、V2 返 161002,各自与自己的资料一致,但 V1/V2 这两份资料的归属互相矛盾 —— 同一个 l0 kernel,两个入口的文档给了两种错误码。建议一并统一(同族 aclnnForeachAsin/Atan 的同类问题见 #7945 / #7946)。

环境

  • 硬件:Ascend 910B2(Atlas A2)单卡,Health=OK
  • CANN:9.2.0(opp/version.info → Version=9.2.0,文件时间 2026-10-20)
  • 驱动:npu-smi 26.1.1
  • 仓库:cann/ops-nn master 12ad726c(2026-11-03 fetch)
  • OS:Linux aarch64;g++ -std=c++17

复现步骤

  1. 把下方代码存成 probe.cpp。
  2. 编译:
    g++ -O1 -std=c++17 -o probe probe.cpp \
        -I${ASCEND_HOME_PATH}/include -I${ASCEND_HOME_PATH}/include/aclnnop \
        -L${ASCEND_HOME_PATH}/lib64 -lascendcl -lnnopbase -lopapi -lpthread
    
  3. 跑:./probe <device_id> 7
  4. 输出 out = 0.5 0.5 0.5,而资料「其他值」口径应为 0 2 2。

复现脚本

实测输出(A2 / CANN 9.2.0 / 12ad726c,device 7):

mode=7 n= 3  RET_GWS=0  ws=32
RET_EXEC=0
out = 0.5 0.5 0.5

对照组(同二进制、同输入、只改 roundMode):

mode=1 n= 3  RET_GWS=0  ws=32
RET_EXEC=0
out = 0 2 2          ← 与 RNE 一致
// probe.cpp —— aclnnForeachRoundOffNumber:roundMode=7 的实际语义
// 用法: ./probe <device_id> <roundMode> [n]
#include <acl/acl.h>
#include <aclnnop/aclnn_foreach_round_off_number.h>
#include <cstdio>
#include <cstdlib>
#include <vector>

int main(int argc, char **argv)
{
    int dev = (argc > 1) ? atoi(argv[1]) : 0;
    int mode = (argc > 2) ? atoi(argv[2]) : 7;
    int n = (argc > 3) ? atoi(argv[3]) : 3;

    aclInit(nullptr);
    aclrtSetDevice(dev);
    aclrtStream stream = nullptr;
    aclrtCreateStream(&stream);

    // 输入全是平局点:0.5, 1.5, 2.5, ...
    std::vector<float> hostIn(n);
    for (int i = 0; i < n; ++i) { hostIn[i] = 0.5f + static_cast<float>(i); }

    int64_t dims[1] = {n};
    const size_t bytes = n * sizeof(float);
    void *inDev = nullptr, *outDev = nullptr;
    aclrtMalloc(&inDev, bytes, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(&outDev, bytes, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(inDev, bytes, hostIn.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE);

    aclTensor *xT = aclCreateTensor(dims, 1, ACL_FLOAT, nullptr, 0, ACL_FORMAT_ND, dims, 1, inDev);
    aclTensor *outT = aclCreateTensor(dims, 1, ACL_FLOAT, nullptr, 0, ACL_FORMAT_ND, dims, 1, outDev);
    aclTensorList *xList = aclCreateTensorList(&xT, 1);
    aclTensorList *outList = aclCreateTensorList(&outT, 1);

    int8_t modeI8 = static_cast<int8_t>(mode);
    int64_t rmDims[1] = {1};
    void *rmDev = nullptr;
    aclrtMalloc(&rmDev, sizeof(int8_t), ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(rmDev, sizeof(int8_t), &modeI8, sizeof(int8_t), ACL_MEMCPY_HOST_TO_DEVICE);
    aclTensor *rmT = aclCreateTensor(rmDims, 1, ACL_INT8, nullptr, 0, ACL_FORMAT_ND, rmDims, 1, rmDev);

    uint64_t wsSize = 0;
    aclOpExecutor *executor = nullptr;
    int ret = aclnnForeachRoundOffNumberGetWorkspaceSize(xList, rmT, outList, &wsSize, &executor);
    printf("mode=%d n=%2d  RET_GWS=%d  ws=%llu\n", mode, n, ret, static_cast<unsigned long long>(wsSize));
    if (ret != 0) { return ret; }

    void *ws = nullptr;
    if (wsSize > 0) { aclrtMalloc(&ws, wsSize, ACL_MEM_MALLOC_HUGE_FIRST); }
    ret = aclnnForeachRoundOffNumber(ws, wsSize, executor, stream);
    printf("RET_EXEC=%d\n", ret);
    aclrtSynchronizeStream(stream);
    aclrtMemcpy(hostIn.data(), bytes, outDev, bytes, ACL_MEMCPY_DEVICE_TO_HOST);

    printf("out =");
    for (int i = 0; i < n; ++i) { printf(" %.6g", hostIn[i]); }
    printf("\n");

    aclDestroyTensorList(xList);
    aclDestroyTensorList(outList);
    aclrtFree(inDev); aclrtFree(outDev); aclrtFree(rmDev); aclrtFree(ws);
    aclrtDestroyStream(stream);
    aclrtResetDevice(dev);
    aclFinalize();
    return 0;
}
likedislike
liuhanwen8liuhanwen8
19 days ago Added label: documentation
yuning_chenyuning_chenCollaborators
18 days ago Assign tramp-ll as responsible person
yuning_chen
yuning_chenCollaborators
18 days ago Comments:

初步分析

感谢反馈,我们已开始核对这个问题。

现象理解:aclnnForeachRoundOffNumber/V2 的资料未声明 roundMode=7 的语义,实测为取小数部分;按资料「其他值按 RNE 处理」的描述理解会算错。

当前判断:初步判断是资料对 roundMode 取值表的覆盖不全,需要对照 kernel 实现确认 7 的实际语义是否为对外承诺行为,待确认后再定是补资料还是收窄取值。

涉及范围:foreach/foreach_round_off_number 的 kernel roundMode 分支(含 arch35 SIMT 实现)与 aclnnForeachRoundOffNumber/V2 资料。

下一步:我们会对照实现列出 roundMode 全部取值的实际语义,再补齐资料或增加取值校验。

likedislike
yuning_chen
yuning_chenCollaborators
18 days ago Comments:

您好,该问题已收到,当前问题 @tramp-ll 正在处理中

likedislike
yuning_chenyuning_chenCollaborators
11 days ago Assign east_yang as responsible person, remove original responsible person: tramp-ll
east_yangeast_yangCollaborators
11 days ago Assign Tian_1122 as responsible person
east_yangeast_yangCollaborators
11 days ago Removed responsible person east_yang
tramp-lltramp-llCollaborators
10 days ago Assign tramp-ll as responsible person
tramp-ll
tramp-llCollaborators
10 days ago Comments:

最新文档资料已经更新roundMode=7说明

likedislike
tramp-lltramp-llCollaborators
10 days ago changed custom state from 进行中 to 已完成
tramp-lltramp-llCollaborators
10 days ago Close issue
CANN-robotCANN-robotCollaborators
8 days ago Added label: resolved