Pull Request has been successfully merged by CANN-robot.
(Thank you for ruoshuisixue's contribution.)变更摘要
该 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的单元测试。


代码审查
审查结论
我已逐一审查了全部 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*变体(复杂解析逻辑)无任何编译/运行覆盖,且签名与 CUDAnanf(const char*)契约不一致,存在误用风险。
总体风险判断
本 PR 主体(整型转换函数、位重解释、混合符号 max/min、char 存取、fp8 单标量转换)逻辑正确、测试与实现语义一致,未发现会导致错误结果的确定性缺陷。主要风险集中在新增的 float max/min/abs 公共 API 缺乏库内定义与有效测试(P2),以及 nanf 设备侧变体的覆盖缺失(P3),均为"声明/契约层面"问题而非已触发的运行时错误。整体变更风险中等偏低。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 1 |
| 🟡 建议 | 0 |
⛔ 需要修改


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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


/lgtm


/approve


/approve
/lgtm


描述
本 PR 为 SIMT API 补充整型相关转换函数及配套能力,完善 half、bfloat16、fp8、数学函数和存储函数的声明与实现。
主要改动:
__asc_cvt_float_to_fp8。nanf、float 版本abs、float 版本min/max,以及有符号/无符号混合整型min/max重载。asc_stcg、asc_stwt补充 char 类型支持。关联的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定义。文档更新
包含文档更新:
abs、nanf、min、max等数学函数文档。asc_stcg、asc_stwt对 char 类型的接口说明。类型标签