Merged
SIMT:添加整型转换函数 #5170
SIMT:添加整型转换函数 #5170
Merged
ruoshuisixueAdd on Aug 18
ruoshuisixue
ruoshuisixueCollaborators
Aug 18

描述

本 PR 为 SIMT API 补充整型相关转换函数及配套能力,完善 half、bfloat16、fp8、数学函数和存储函数的声明与实现。
主要改动:

  • 新增 half/bfloat16 与 char、uchar、short、ushort 之间的转换接口,覆盖 rn/rz/rd/ru 等舍入模式。
  • 新增 half/bfloat16 的 bit reinterpret 接口,以及 half2 相关转换接口。
  • 新增 fp8 标量 float 转换接口 __asc_cvt_float_to_fp8。
  • 补充 nanf、float 版本 abs、float 版本 min/max,以及有符号/无符号混合整型 min/max 重载。
  • 为 asc_stcg、asc_stwt 补充 char 类型支持。
  • 同步更新 SIMT API 文档、API 列表文档描述。

关联的Issue

https://gitcode.com/cann/asc-devkit/issues/1584

测试

已新增/修改 SIMT API gtest 用例覆盖本次改动:

  • test_operator_simt_c_cast.cpp:覆盖 half/bfloat16 与 int16/uint16/char/uchar 转换、边界饱和、half2 打包/解包、fp8 标量转换。
  • test_operator_simt_c_math.cpp:覆盖 abs、nanf、混合有符号/无符号 min/max。
  • test_operator_simt_c_device_functions.cpp:覆盖 asc_stcg、asc_stwt 对 plain char 的支持。
  • simt_stub.h:同步调整测试 stub 中 __half22float2 定义。

文档更新

包含文档更新:

  • 新增/更新 SIMT half、bfloat16、fp8 精度转换函数文档。
  • 新增/更新 abs、nanf、min、max 等数学函数文档。
  • 更新 asc_stcg、asc_stwt 对 char 类型的接口说明。
  • 更新 SIMT API 列表。

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for ruoshuisixue's contribution.)
ruoshuisixueruoshuisixueCollaborators
Aug 18 Created pull request, commit ea89da6a
atomgit-bot
atomgit-bot
Aug 18 Comments:

变更摘要

该 PR 为 SIMT 设备函数库补齐整型(char/short/ushort)与半精度浮点(half、bfloat16_t)之间的类型转换能力,并同步扩展了 fp8 转换、数学函数与存储指令接口。核心改动集中在 asc_fp16_impl.h、asc_bf16_impl.h、asc_fp8_impl.h、math_functions_impl.h、device_functions_impl.h 及对应头文件声明中,并配套新增单元测试。

主要改动

  • 新增 half/bfloat16_t 与整型互转函数:在 asc_fp16_impl.h 与 asc_bf16_impl.h 中新增 __half2char_rz/__bfloat162char_rz、__half2uchar_rz/__bfloat162uchar_rz、__half2short_rn/rz/rd/ru、__bfloat162short_*、__half2ushort_*、__bfloat162ushort_* 及反向的 __short2half_*、__ushort2half_*、__short2bfloat16_*、__ushort2bfloat16_*(覆盖最近偶数/截断/向下/向上四种舍入模式),并在 internal_functions_impl.h 新增带饱和处理的内部辅助函数 __internal_cvt_char、__internal_cvt_uchar、__internal_cvt_short、__internal_cvt_ushort。
  • 新增位重解释与打包接口:为 half/bfloat16_t 增加 __half_as_short、__half_as_ushort、__short_as_half、__bfloat16_as_short、__bfloat16_as_ushort、__short_as_bfloat16,并新增 __float2half2_rn、__half2half2 打包函数及 __half22float2(改为 static 声明)。
  • 新增 fp8 单元素转换接口:在 asc_fp8.h 中新增 __asc_fp8_storage_t 类型定义,并在 asc_fp8_impl.h 中实现 __asc_cvt_float_to_fp8,将单个 float 经 __asc_cvt_float2_to_fp8x2 转换后提取低 8 位返回。
  • 扩展数学函数:在 math_functions_impl.h 中实现 nanf(解析字符串 tag 生成 NaN 返回值),并为 max/min 新增有符号/无符号混合类型重载(unsigned int/int、long/unsigned long、long long/unsigned long long),同时声明 max(float, float)、min(float, float)、abs(float)。
  • 扩展存储指令与测试:为 asc_stcg/asc_stwt 新增 char*/char 重载(内部转换为 signed char 后走既有实现),并在 SIMT 用例中新增整型转换、位重解释、nanf、混合符号 max/min 及 asc_stcg/asc_stwt 的单元测试。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 18 Comments:

代码审查

审查结论

我已逐一审查了全部 15 个变更文件。最终报告 2 项发现(P2×1、P3×1),各文件审查情况如下:

各文件审查确认

文件 结论
impl/simt_api/asc_bf16_impl.h 新增 half/bf16↔char/short 转换与位重解释函数,逐条核对与测试断言(rn/rz/rd/ru 舍入、饱和钳位)完全一致,无问题
impl/simt_api/asc_fp16_impl.h 同上,转换函数与 __float2half2_rn/__half2half2 逻辑正确,无问题
impl/simt_api/asc_fp8_impl.h __asc_cvt_float_to_fp8 取 fp8x2 低字节,与 stub 布局及测试断言(E4M3=56/E5M2=60)一致,无问题
impl/simt_api/device_functions_impl.h asc_stcg/asc_stwt 的 char 重载按位保持正确,__gm__ 与纯 SIMT 段由 #ifndef __NPU_COMPILER_INTERNAL_PURE_SIMT__ 互斥,无问题
impl/simt_api/internal_functions_impl.h __internal_cvt_char/uchar/short/ushort 边界钳位正确(127/-128/255/32767/-32768/65535),无问题
impl/simt_api/math_functions_impl.h nanf 宏为 glibc 移植,hex/octal/decimal 解析与 64 位溢出检测正确;发现设备侧 __gm__ 变体无测试覆盖(P3)
include/simt_api/asc_bf16.h 新声明与 impl 一一对应,无问题
include/simt_api/asc_fp16.h 新声明与 impl 对应;__half22float2 静态声明遵循 __heq 等既有"编译器内建"模式,未报
include/simt_api/asc_fp8.h __asc_fp8_storage_t typedef 与声明一致,无问题
include/simt_api/device_functions.h char 重载声明与 impl 配对,无问题
include/simt_api/math_functions.h 发现 float max/min/abs 静态声明无库内定义 + 测试掩蔽(P2)
tests/.../test_operator_simt_c_cast.cpp 新增用例与实现语义逐条吻合,无问题
tests/.../test_operator_simt_c_device_functions.cpp char 存取回环测试正确,无问题
tests/.../test_operator_simt_c_math.cpp ::abs(y) 可能命中 abs(int)(归入 P2);nanf 用例仅覆盖纯 SIMT 变体(归入 P3)
tests/.../common_simt/simt_stub.h inline→static 是为匹配新增的 __half22float2 静态声明,修复正确,无问题

发现汇总

  • P2(1 项):math_functions.h 新增的 float max/min/abs 在所有分支中仅为 static 声明,SIMT 库内无任何定义,可用性完全依赖未经验证的编译器 float 内建;新增测试 ::abs(y) 在声明块未激活时静默解析到 <cstdlib> 的 abs(int),float 重载实际未被测试(激活时则因静态函数无定义而链接失败)。
  • P3(1 项):nanf 设备侧 const __gm__ char* 变体(复杂解析逻辑)无任何编译/运行覆盖,且签名与 CUDA nanf(const char*) 契约不一致,存在误用风险。

总体风险判断

本 PR 主体(整型转换函数、位重解释、混合符号 max/min、char 存取、fp8 单标量转换)逻辑正确、测试与实现语义一致,未发现会导致错误结果的确定性缺陷。主要风险集中在新增的 float max/min/abs 公共 API 缺乏库内定义与有效测试(P2),以及 nanf 设备侧变体的覆盖缺失(P3),均为"声明/契约层面"问题而非已触发的运行时错误。整体变更风险中等偏低。

类型 数量
🔴 阻塞 1
🟡 建议 0

⛔ 需要修改

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Aug 18 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Aug 18 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
docs ✅ ai_xin, louyuxuan, zhanghao_0689 (3/2) ✅ ai_xin (1/1)
impl/simt_api ✅ wangxiaozhi, zhanghao_0689, louyuxuan (3/2) ✅ wangxiaozhi (1/1)
include/simt_api ✅ louyuxuan, zhanghao_0689 (2/2) ✅ zhanghao_0689 (1/1)
repo-cann/asc-devkit ✅ wangxiaozhi, zhanghao_0689, louyuxuan (3/2) ✅ wangxiaozhi, zhanghao_0689 (2/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

ruoshuisixue, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
615 messages are collapsed here。 See more
louyuxuan
louyuxuanCollaborators
Aug 26 Comments:

/lgtm

likedislike
wangxiaozhi
wangxiaozhiCollaborators
Aug 26 Comments:

/approve

likedislike
zhanghao_0689
zhanghao_0689Collaborators
Aug 26 Comments:

/approve

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Aug 26 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Aug 26 Merged pull request