Closed
[Documentation|文档反馈]: [aclnnIsPosInf] 官方示例用 %f 打印 BOOL 出参(未定义行为,实测恒输出 0.000000) #4355
liuhanwen8Add on  18 days agoClosed at  18 hours ago
liuhanwen8
liuhanwen8
18 days ago Create

现象

一句话结论:math/is_pos_inf/examples/test_aclnn_isposinf.cpp:143 用 %f 打印 1 字节的 BOOL 出参
(std::vector<char> resultData),属未定义行为,实测恒打印 0.000000,与线上真实值无关。

同一字节两种印法(Ascend 910B3 / CANN 9.2.0,输入 {+inf, 1, -inf, 0, 3, -4, 5, 7}):

元素 线上字节 %f 打印 %d 打印
+inf 1(True) 0.000000 1
-inf 0(False) 0.000000 0

即示例跑完看到的是一列 0.000000,即使算出了 True 也看不出来。

期望

示例的打印格式应与出参类型一致:BOOL 是 1 字节整数,应当用 %d(或转成 true/false 文本)输出。
资料 math/is_pos_inf/docs/aclnnIsPosInf.md 内嵌的示例代码块用的正是 %d:

「LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]);」 —— 仓内 examples/ 这份与资料不一致。

环境

  • 硬件:Atlas A2(Ascend 910B3),npu-smi 26.1.1
  • CANN:9.2.0,Linux aarch64,g++ 11.4.0
  • 仓库与提交:cann/ops-math 365f17f(master,稀疏检出 math/is_pos_inf)
  • 复现日期:2026-10-31

复现步骤

  1. 保存下方脚本为 repro.py
  2. 在 cann/ops-math 仓根执行 python3 repro.py --repo-root .
  3. 观察输出:应打印 1 行 [DEFECT],退出码 1

编译期同一问题也可直接看到:g++ -std=c++17 -Wall -Wformat -c math/is_pos_inf/examples/test_aclnn_isposinf.cpp -I$ASCEND_HOME_PATH/include
→ warning: format '%f' expects argument of type 'double', but argument 3 has type 'int' [-Wformat=](第 143 行)。

复现脚本

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""aclnnIsPosInf 官方示例用 %f 打印 BOOL 结果(条目 2.2-10)。"""
import glob
import os
import re
import sys

FLOAT_FMT = ("%f", "%g", "%e", "%lf")


def main():
    root = sys.argv[sys.argv.index("--repo-root") + 1] if "--repo-root" in sys.argv else "."
    files = sorted(set(glob.glob(os.path.join(root, "math/is_pos_inf/examples/*.cpp"))))
    if not files:
        print("[SKIP] 找不到 examples/*.cpp")
        return 2
    bad = 0
    checked = 0
    for path in files:
        src = open(path, encoding="utf-8", errors="replace").read()
        rel = os.path.relpath(path, root)
        # 出参缓冲:1 字节类型(ACL_BOOL 的载体)
        bufs = {m.group(2) for m in
                re.finditer(r"std::vector<(char|uint8_t|unsigned char)>\s+(\w+)", src)}
        if not bufs:
            continue
        for m in re.finditer(r'LOG_PRINT\(\s*"([^"]*)"([^;]*);', src):
            fmt, args = m.group(1), m.group(2)
            hit = [b for b in bufs if b in args]
            if not hit:
                continue
            checked += 1
            line = src[:m.start()].count("\n") + 1
            if any(t in fmt for t in FLOAT_FMT):
                print("[DEFECT] %s:%d 用 %r 打印 1 字节 BOOL 缓冲 %s —— "
                      "char 提升为 int 而 %%f 按 double 取,属未定义行为;"
                      "实测恒输出 0.000000,读者无法区分 True/False"
                      % (rel, line, fmt.strip(), ",".join(sorted(hit))))
                bad += 1
    if not checked:
        print("[SKIP] 未找到打印 1 字节出参缓冲的语句(无法判定)")
        return 2
    if not bad:
        print("[OK] 示例用 %d/%%u 打印 BOOL 出参,格式与类型一致")
        return 0
    return 1


if __name__ == "__main__":
    sys.exit(main())

实测输出(退出码 1):

[DEFECT] math/is_pos_inf/examples/test_aclnn_isposinf.cpp:143 用 'result[%ld] is: %f\n' 打印 1 字节 BOOL 缓冲 resultData —— char 提升为 int 而 %f 按 double 取,属未定义行为;实测恒输出 0.000000,读者无法区分 True/False

原文片段(math/is_pos_inf/examples/test_aclnn_isposinf.cpp:143,逐字复制):

        LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);

影响

  • 按示例学接口的人会把它当成"bool 结果按浮点打印",照抄到自己的代码里,输出永远看不出 True。
  • 未定义行为的实际表现依赖编译器与寄存器分配:本机 aarch64 + g++ 11.4 恒得 0.000000;
    同一份代码换编译器或优化级别后输出不可预期,示例的"预期输出"无法复现。
  • 不影响算子本身:两段返回码与线上 BOOL 数据都正确(本轮 777 条精度用例全 PASS)。

建议

把 math/is_pos_inf/examples/test_aclnn_isposinf.cpp:143 改成
LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]),
与资料 docs/aclnnIsPosInf.md 的示例代码块保持一致;要更可读可改为
resultData[i] ? "true" : "false"。
仓内同类示例(输出 BOOL 的算子)建议一并扫描一遍 LOG_PRINT(... %f ...)。

likedislike
liuhanwen8liuhanwen8
18 days ago Added label: documentation
陈思
陈思Collaborators
17 days ago Comments:

您好,感谢反馈!该示例确实存在打印格式与结果类型不匹配的问题。

核对 cann/ops-math 提交 212ce9d5460b1be26cfbee481aff1acbf5cc5c62 后,独立示例仍用 %f 打印 std::vector<char> 中的 BOOL 结果;这部分代码与您报告的 365f17f 一致。LOG_PRINT 直接调用 printf,其中 char 实参经整数提升后为 int,而 %f 要求 double,因此属于未定义行为。您观察到的 0.000000 是该环境下的表现,其他编译器或优化设置下也可能出现不同输出,不能据此判断 BOOL 结果为 false。

一个可行的最小修订是改用 %d,与文档内嵌示例保持一致,例如:

LOG_PRINT("result[%ld] is: %d\n", i, static_cast<int>(resultData[i]));

若希望显示 true/false,则可配合 %s 使用 resultData[i] ? "true" : "false"。这项调整只需修改结果展示,保留现有 BOOL 缓冲及拷贝方式。

我们也核对了 IsNegInf、IsInf、IsFinite 的 aclnn 示例,其结果打印均使用 %d。本轮通过独立的主机编译片段确认了 %f 的参数类型诊断,以及上述两种写法的格式匹配;未重新运行您报告的 NPU 精度用例。我们会继续跟踪该示例与文档的一致性问题。

likedislike
陈思陈思Collaborators
17 days ago Assign chen_0715 as responsible person
陈思陈思Collaborators
17 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