Closed
[Documentation|文档反馈]: aclnnForeachCopy 的 out dtype 对应关系漏列 BFLOAT16→FLOAT32(实现支持) #7941
liuhanwen8Add on  20 days agoClosed at  16 days ago
liuhanwen8
liuhanwen8
20 days ago Create

文档位置

foreach/foreach_copy/docs/aclnnForeachCopy.md → 参数表 → out 行的「使用说明」列(第 4 列)。

原文摘录:

数据类型与入参 x 的数据类型存在一定的对应关系:
当入参 x 的数据类型为 FLOAT32 时,输出的数据类型支持 FLOAT16 或者 BFLOAT16。
当入参 x 的数据类型为 FLOAT16 时,输出的数据类型支持 FLOAT32。
当输入 x 的数据类型不为 FLOAT32 或者 FLOAT16 时,输出的数据类型与输入 x 的数据类型保持一致。

现象

上面第三句把「x 不是 FLOAT32/FLOAT16」的情况收敛成「out 与 x 同 dtype」,
按此口径 x=BFLOAT16 时 out 只允许 BFLOAT16,out=FLOAT32 应当属于非法组合(161002)。

但实现支持 BFLOAT16 → FLOAT32,且数值为精确转换:

  • 源码:foreach/foreach_copy/op_api/aclnn_foreach_copy.cpp 的 IsValidDtypeMapping()
    (master 7044c832,该函数第 81 行起)显式允许 DT_BF16 → DT_FLOAT;
  • 真机(CANN 9.2.0 / Atlas A2 910B2):x=bf16[2x3] out=fp32[2x3] 两段返回 0,
    输出 0.1328125 / 0.96875 / -0.24609375 …,即 bf16 值到 fp32 的精确表示。

因此资料的「对应关系」漏了一种实际支持的组合,读者按文档会把一个合法用法当成非法。

期望

资料应把 out 的 dtype 对应关系补全为四类(与实现一致):

  • x=FLOAT32 → out 支持 FLOAT16 / BFLOAT16;
  • x=FLOAT16 → out 支持 FLOAT32;
  • x=BFLOAT16 → out 支持 BFLOAT16 / FLOAT32(当前缺失);
  • 其余 dtype → out 与 x 一致。

若实现侧的 BFLOAT16 → FLOAT32 属非预期能力,则应改为返回 161002 并保持资料现状——两种处置均可,但资料与实现必须一致。

环境

  • 硬件:Atlas A2 训练系列(910B2),npu-smi 26.1.1
  • CANN:9.2.0,Linux aarch64
  • 仓库与提交:cann/ops-nn 7044c8328cfb3bb0a298034fd9c56f49aca0bbd7(2026-11-02 git fetch 后)
  • 核对日期:2026-11-02

复现步骤

  1. 保存下方脚本为 doc_repro.py
  2. 在仓库根目录执行:python3 doc_repro.py foreach/foreach_copy/docs/aclnnForeachCopy.md
  3. 观察输出最后一行:[DEFECT] 资料未声明 BFLOAT16 → FLOAT32 …,退出码为 1

(实现侧可选用同仓库 examples/test_aclnn_foreach_copy.cpp 改成 x=bf16 / out=fp32 复核,两段返回 0。)

复现脚本

#!/usr/bin/env python3
# 断言方向:应存在(资料该列而未列 ⇒ DEFECT)。退出码 0=符合预期 / 1=缺陷成立 / 2=无法判定
import re, sys

def cell_text(txt, param):
    for tr in re.findall(r"<tr>(.*?)</tr>", txt, re.S):
        if param not in tr:
            continue
        cells = [re.sub(r"\s+", " ", re.sub(r"<[^>]+>", "", c)).strip()
                 for c in re.findall(r"<td[^>]*>(.*?)</td>", tr, re.S)]
        if len(cells) >= 4 and cells[0].startswith(param):
            return cells[3]
    return ""

path = sys.argv[1] if len(sys.argv) > 1 else "foreach/foreach_copy/docs/aclnnForeachCopy.md"
txt = open(path, encoding="utf-8").read()
usage = cell_text(txt, "out")
if not usage:
    print("[SKIP] 未定位到 out 的使用说明单元格"); sys.exit(2)
print("out 使用说明:" + usage[:400])
has_bf16_to_f32 = bool(re.search(r"BFLOAT16[^。;]{0,40}FLOAT32", usage))
print("已列 BFLOAT16 → FLOAT32:%s" % has_bf16_to_f32)
if has_bf16_to_f32:
    print("[OK] 资料已声明,本条不成立"); sys.exit(0)
print("[DEFECT] 资料未声明 BFLOAT16 → FLOAT32,而实现允许该转换"
      "(aclnn_foreach_copy.cpp IsValidDtypeMapping 允许 DT_BF16 → DT_FLOAT,实测 rc=0)")
sys.exit(1)

实测输出:

out 使用说明:支持空Tensor。该参数中所有Tensor的数据类型保持一致。……当输入`x`的数据类型不为FLOAT32或者FLOAT16时,输出的数据类型与输入`x`的数据类型保持一致。
已列 BFLOAT16 → FLOAT32:False
[DEFECT] 资料未声明 BFLOAT16 → FLOAT32,而实现允许该转换(aclnn_foreach_copy.cpp IsValidDtypeMapping 允许 DT_BF16 → DT_FLOAT,实测 rc=0)

影响

按现资料阅读,x=BFLOAT16 的用户会认为 out 只能是 BFLOAT16,把 out=FLOAT32 当成非法组合而放弃一条本就可用(且数值精确)的路径;反之,若有人照资料写用例,会把这段合法用法判成缺陷。资料与实际能力不一致,会同时误导使用方与测试方。

建议

把 out 参数的「对应关系」补成三类(或四类):

  • x=FLOAT32 → out 支持 FLOAT16 / BFLOAT16;
  • x=FLOAT16 → out 支持 FLOAT32;
  • x=BFLOAT16 → out 支持 BFLOAT16 / FLOAT32;
  • 其余 dtype(INT8/UINT8/INT16/UINT16/INT32/UINT32/INT64/DOUBLE/BOOL)→ out 与 x 一致。

若实现侧的 BFLOAT16 → FLOAT32 属于非预期能力,则请改为返回 161002,并保持资料现状——两种处置都可,但资料与实现需要一致。

likedislike
liuhanwen8liuhanwen8
20 days ago Added label: documentation
east_yang
east_yangCollaborators
20 days ago Comments:

/assign @Tian_1122

likedislike
CANN-robotCANN-robotCollaborators
20 days ago Assign Tian_1122 as responsible person
east_yangeast_yangCollaborators
20 days ago Assign east_yang as responsible person
east_yangeast_yangCollaborators
20 days ago Removed responsible person Tian_1122
east_yangeast_yangCollaborators
19 days ago Assign fengying555682 as responsible person
yuning_chen
yuning_chenCollaborators
19 days ago Comments:

初步分析

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

现象理解:缺陷反馈;aclnnForeachCopy 的 out dtype 对应关系漏列 BFLOAT16→FLOAT32(实现支持)。
当前判断:从描述看这是缺陷反馈「aclnnForeachCopy 的 out dtype 对应关系漏列 BFLOAT16→FLOAT32(实现支持)」。目前只能根据现象做文字核对,还没有上板复现。以上为初步判断,待复现确认,不能当成已确认根因。
涉及范围:aclnnForeachCopy、aclnn_foreach_copy、test_aclnn_foreach_copy、has_bf16_to_f32、BFLOAT16、FLOAT32
下一步:我们会继续对照代码/文档定位。为便于继续定位,请尽量补充:关键报错或日志片段。

likedislike
yuning_chen
yuning_chenCollaborators
19 days ago Comments:

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

likedislike
fengying555682fengying555682Collaborators
19 days ago Assign gitee-duhuiping as responsible person
fengying555682fengying555682Collaborators
19 days ago Removed responsible person fengying555682
CANN-robotCANN-robotCollaborators
16 days ago Close issue
CANN-robotCANN-robotCollaborators
16 days ago Added label: resolved