Closed
[Bug-Report|缺陷反馈]: Host FP16 的64位整数赋值在饱和前丢弃高32位 #1416
XztxztAdd on  9 days agoClosed at  4 days ago
Xztxzt
Xztxzt
9 days ago Create

Describe the current behavior / 问题描述 (Mandatory / 必填)

Adump和Runtime两份fp16_t的int64_t/uint64_t赋值先转为32位整数,导致2^32变为0、2^32+1变为1,负数还可能丢失符号。头文件显式提供这些64位重载,既有测试只覆盖16/32等小值。

基线源码位置:

  • src/dfx/adump/adump/printf/fp16_t.cpp:519
  • src/runtime/core/src/dfx/fp16_t.cpp:473

Environment / 环境信息 (Mandatory / 必填)

仓库:cann/runtime
基线:8f16f26e162fd995ceb36c7c51d4c65414cc3063
Linux x86_64;GCC9.5.0;C++17/-O2/-fno-strict-aliasing;GoogleTest1.14.0;UBSan;clang-format16.0.0。

  • 执行了两份完整生产fp16_t.cpp和真实类头;缺失的securec位拷贝、日志/错误宏及RtPtrToPtr依赖由最小Host夹具提供。数值实现未替换;此证据不代表完整SDK库构建、设备或仿真执行。
  • Adump完整fp16_t_utest.cpp已独立编译运行;Runtime仅编译提交在rt_utest_printf.cc中的新增3项测试体,完整PrintfTest集成目标未运行。
  • 仅证明显式Host数值辅助接口错误及修复,不声称已有算子/设备路径触发此转换。完整pre-commit框架未运行。

Steps to reproduce the issue / 重现步骤 (Mandatory / 必填)

使用Adx::fp16_t或cce::runtime::fp16_t,执行value=INT64_C(4294967296)或value=UINT64_C(4294967297),检查value.val和toFloat():原版为0/1,预期编码0x7bff、数值65504;负2^32预期0xfbff、-65504。HostFp16Int64Test.SignedWideInputs与UnsignedWideInputs还覆盖INT64_MIN/MAX及UINT64_MAX,并验证构造器和赋值返回引用。

Describe the expected behavior / 预期结果 (Mandatory / 必填)

保留原输入的数值大小和符号,按本类既有规则将超范围正/负整数饱和为±65504;有限半精度范围内的舍入保持一致。

原版Adump测试58通过、2失败,Runtime新增回归1通过、2失败;修复后分别60/3项全部通过,UBSan无报告。两份实现都逐个比对[-65504,65504]内的signed64以及[0,65504]内unsigned64与既有32位整数舍入编码。clang-format16.0.0对修改范围及git diff --check通过。 已在本地运行配置的官方OAT v1.0.6 hook,变更文件通过;该结果不代表全部pre-commit、SDK构建或设备验证。

Special notes for this issue/备注 (Optional / 选填)

建议最小修复:两份实现的64位重载均委托现有double转换。double能精确表示有限FP16范围内的每个整数;超过此范围的64位值即使在double中舍入,也仍走相同的有符号饱和路径。分别扩展两组件的原生UT。

关联 PR:https://gitcode.com/cann/runtime/merge_requests/6444

likedislike
Xztxzt
Xztxzt
9 days ago Comments:

/assign @Xztxzt

likedislike
CANN-robotCANN-robotCollaborators
9 days ago Assign Xztxzt as responsible person
XztxztXztxzt
9 days ago Associated pull request
XztxztXztxzt
9 days ago Modify description of issue
yring_8
yring_8Collaborators
5 days ago Comments:

@Xztxzt 您好 感谢您的反馈。
经分析:从代码层面上确认存在64位整数赋值在饱和前丢弃高32位的风险;由于rts/adump都是纯解码,无赋值逻辑,该问题不会触发,无实际风险。
同意合入关联PR

likedislike
CANN-robotCANN-robotCollaborators
4 days ago Close issue
CANN-robotCANN-robotCollaborators
4 days ago changed custom state from 待办的 to 已完成
CANN-robotCANN-robotCollaborators
4 days ago Added label: resolved