Closed
[Documentation|文档反馈]: [aclnnIsPosInf] 官方示例用 %f 打印 BOOL 出参(未定义行为,实测恒输出 0.000000) #4355
liuhanwen8Add on 18 days agoClosed at 18 hours ago
18 days ago Added label: documentation
陈思
17 days ago Comments:
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 精度用例。我们会继续跟踪该示例与文档的一致性问题。


17 days ago Assign chen_0715 as responsible person
17 days ago Added label: pending-confirmation
15 days ago Removed label: pending-confirmation
18 hours ago Close issue
18 hours ago changed custom state from 进行中 to 已完成
16 hours ago Added label: resolved
现象
一句话结论:
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-inf即示例跑完看到的是一列
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/这份与资料不一致。环境
npu-smi26.1.1cann/ops-math365f17f(master,稀疏检出math/is_pos_inf)复现步骤
repro.pycann/ops-math仓根执行python3 repro.py --repo-root .[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):
原文片段(
math/is_pos_inf/examples/test_aclnn_isposinf.cpp:143,逐字复制):影响
0.000000;同一份代码换编译器或优化级别后输出不可预期,示例的"预期输出"无法复现。
建议
把
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 ...)。