Closed
[Bug-Report|缺陷反馈]: aclnnXLogYScalarSelf 声明 self 支持 DOUBLE,实现强降 fp32:|self|>fp32_max 时 1e300*log(1) 返 nan(真值 0) #4296
liuhanwen8Add on  18 days agoClosed at  18 hours ago
liuhanwen8
liuhanwen8
18 days ago Create

现象

资料 math/xlogy/docs/aclnnXLogYScalarSelf.md:48 声明 self 支持 DOUBLE;实测 aclScalar 取 ACL_DOUBLE 时入参被接受(ret=0),但实现把推导类型从 DOUBLE 强降为 FLOAT,self 先按 fp32 转换,超出 fp32 表示范围即溢出为 ±inf,再与 log(other) 相乘得出 nan / ±inf。

self(ACL_DOUBLE) other 实测 out[0] 真值(fp64 算完落 fp32)
3.4028e38(fp32 max) 1.0 0.0 0.0
3.5e38 1.0 nan 0.0
1e40 1.0 nan 0.0
1e300 1.0 nan 0.0
-1e300 1.0 nan -0.0
1e300 2.0 inf inf(fp32 溢出,符合预期)

分歧只出现在 other == 1(真值精确为 0)这类乘数为 0 的位置:inf * 0 被判为 nan。

根因(math/xlogy/op_api/aclnn_x_log_y_scalar_self.cpp):

  • L130-133:promoteType = IsFloatingType(promoteType) ? promoteType : DT_FLOAT; 之后紧跟 promoteType = (promoteType == DT_DOUBLE) ? DT_FLOAT : promoteType; —— DOUBLE 被无条件降为 FLOAT;
  • L176:ConvertToTensor(self, promoteType) —— self 先转 fp32,1e300 即 +inf。

同族 aclnnXLogYScalarOther 的标量白名单同样含 DOUBLE,代码路径一致。

期望

两种修法任选其一,都要求「声明与行为一致」:

  1. 实现侧:DOUBLE 参与推导时保留 DOUBLE 计算域(至少 self 不降精度),使 1e300 * log(1.0) 返回 0;
  2. 资料侧:若不打算在 fp64 域计算,则 self 的 dtype 列表应删去 DOUBLE,或明确写「DOUBLE 会先转换为 FLOAT 计算」。

环境

  • 硬件:Atlas A2 训练系列(Ascend 910B2,单 die)
  • CANN:9.2.0(npu-smi 26.1.1),ASCEND_HOME_PATH=<CANN 安装根>/cann-9.2.0
  • 编译器:g++ 12.3(-std=c++17)
  • 仓:cann/ops-math master,sha 584b2ea6cbaeba684a51a74296d82d4d50ab3b0f(2026-10-24 提交,本地 clone 未改动);目标文件 math/xlogy/

复现步骤

  1. 保存下方代码为 repro.cpp;
  2. 编译:g++ -std=c++17 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -I$ASCEND_HOME_PATH/include/aclnnop -L$ASCEND_HOME_PATH/lib64 -lascendcl -lnnopbase -lopapi -Wl,-rpath,$ASCEND_HOME_PATH/lib64
  3. 执行:./repro
  4. 期望输出 out[0]=0.000000;实测输出 out[0]=nan

复现脚本

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

int main() {
    if (aclInit(nullptr) != ACL_SUCCESS) { printf("aclInit failed\n"); return 1; }
    if (aclrtSetDevice(0) != ACL_SUCCESS) { printf("setDevice failed\n"); return 1; }
    aclrtStream stream;
    aclrtCreateStream(&stream);

    const std::vector<int64_t> shape = {2};
    std::vector<float> otherHost = {1.0f, 2.0f};
    std::vector<float> outHost(2, 0.0f);
    void *otherDev = nullptr, *outDev = nullptr;
    aclrtMalloc(&otherDev, 2 * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(&outDev, 2 * sizeof(float), ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(otherDev, 2 * sizeof(float), otherHost.data(), 2 * sizeof(float),
                ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(outDev, 2 * sizeof(float), outHost.data(), 2 * sizeof(float),
                ACL_MEMCPY_HOST_TO_DEVICE);
    std::vector<int64_t> strides = {1};
    aclTensor* other = aclCreateTensor(shape.data(), 1, ACL_FLOAT, strides.data(), 0,
                                       ACL_FORMAT_ND, shape.data(), 1, otherDev);
    aclTensor* out = aclCreateTensor(shape.data(), 1, ACL_FLOAT, strides.data(), 0,
                                     ACL_FORMAT_ND, shape.data(), 1, outDev);
    double selfValue = 1e300;                      // self 的 dtype 是 ACL_DOUBLE
    aclScalar* self = aclCreateScalar(&selfValue, ACL_DOUBLE);

    uint64_t ws = 0;
    aclOpExecutor* exec = nullptr;
    auto ret1 = aclnnXLogYScalarSelfGetWorkspaceSize(self, other, out, &ws, &exec);
    int ret2 = -1;
    if (ret1 == 0) {
        void* wsp = nullptr;
        if (ws > 0) aclrtMalloc(&wsp, ws, ACL_MEM_MALLOC_HUGE_FIRST);
        ret2 = (int)aclnnXLogYScalarSelf(wsp, ws, exec, stream);
        aclrtSynchronizeStream(stream);
        if (wsp) aclrtFree(wsp);
    }
    aclrtMemcpy(outHost.data(), 2 * sizeof(float), outDev, 2 * sizeof(float),
                ACL_MEMCPY_DEVICE_TO_HOST);
    printf("ret1=%d ret2=%d workspaceSize=%llu\n", (int)ret1, ret2,
           (unsigned long long)ws);
    printf("out[0]=%f (expect 0)\n", outHost[0]);
    printf("out[1]=%f (expect inf: 6.9e299 overflows fp32)\n", outHost[1]);

    aclDestroyScalar(self);
    aclDestroyTensor(other);
    aclDestroyTensor(out);
    aclrtFree(otherDev);
    aclrtFree(outDev);
    aclrtDestroyStream(stream);
    aclrtResetDevice(0);
    aclFinalize();
    return 0;
}

实测输出:

ret1=0 ret2=0 workspaceSize=0
out[0]=nan (expect 0)
out[1]=inf (expect inf: 6.9e299 overflows fp32)

影响

资料声明 DOUBLE 的意义完全落在 fp32 表示不了的区间(|self| > 3.4028e38)——该区间内的 DOUBLE 与 FLOAT 逐位等价,声明与否无差别;而正是这一段给出了错误结果:x * log(1) 这类数学上精确为 0 的表达式返回 nan,x * log(2) 类提前溢出为 ±inf。使用 DOUBLE 标量做缩放(如 1e39 * log(y))的调用方会拿到全 nan 输出,且没有任何错误码提示。

建议

优先按实现侧修(保留 DOUBLE 计算域);若确定只在 fp32 域计算,请同步把 self 的 dtype 列表中的 DOUBLE 去掉,或在 self 使用说明里补一句「DOUBLE 会转换为 FLOAT 参与计算」。同族 aclnnXLogYScalarOther / aclnnXLogYTensor 有相同代码形态,一并核对。

likedislike
liuhanwen8liuhanwen8
18 days ago Added label: bug-report
陈思
陈思Collaborators
18 days ago Comments:

您好,感谢反馈!

核查代码基线为 cann/ops-math 8c1f633e3679f83b55a237c552f842cdcf80462f;设备现象依据您提供的实测。

问题分析

核查 ScalarSelf实现,self白名单包含DOUBLE,但 CheckPromoteType 将推导出的DOUBLE计算类型转换为FLOAT。随后self经 ConvertToTensor、other经Cast进入该类型,执行XLogY后再转换到out类型。

因此这条路径没有保留DOUBLE计算域。self=1e300转换为fp32时超出有限表示范围;即使other=1使log项为0,也存在 inf*0 产生NaN的路径,这能解释您报告的两段均成功而输出NaN。最终out使用DOUBLE也无法恢复先前转换损失的信息。

处理方向

若保留DOUBLE计算语义,需要核对底层计算支持、类型推导和结果转换,不能只删除一处DOUBLE→FLOAT;若接口定位为FLOAT计算,则应明确声明DOUBLE标量的转换方式及超范围处理约束。直接删除DOUBLE输入支持会影响已有调用,也需评估兼容性。当前建议避免依赖超过fp32有限范围的标量执行此路径。我们会继续分析输入声明与计算域如何保持一致。

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