Merged
feat: 9201架构新增类型转换C API接口(half2int8/half2int4x2/bfloat162exmy及asc_cast/asc_cast_merge) #6189
feat: 9201架构新增类型转换C API接口(half2int8/half2int4x2/bfloat162exmy及asc_cast/asc_cast_merge) #6189
Merged
zhangfeifan0709Add on Sep 18
zhangfeifan0709
zhangfeifan0709Collaborators
Sep 18

描述

变更概述

本PR为 9201 架构(a6) 新增 reg_compute 类型转换 C API 接口及配套指令级 UT,共引入 190 个转换接口重载(172 void + 18 return)与 1 个类型别名 vector_fp8_e6m2_t,提供「专用命名」与「泛型命名(asc_cast/asc_cast_merge)」两套语义等价的接口;全部新增声明统一纳入 ASC_DEVKIT_VERSION_NUM >= 90200000 版本宏门控,并已 rebase 至最新 master 解决合并冲突。

新增接口明细

1. 专用命名接口(104 个重载,类型与舍入/饱和模式编入函数名)

half → int8 合并写入(20 重载)

  • asc_half2int8_{rd,rn,rna,ru,rz}{,_sat}_merge(MODE_MERGING,mask=false 处保留 dst 原值)
  • 舍入 {RD,RN,RNA,RU,RZ} × 饱和 {无后缀, _sat} × 位置 {EVEN,ODD},底层指令 vcvt

half → int4x2 合并写入(40 重载)

  • asc_half2int4x2_{rd,rn,rna,ru,rz}{,_sat}_merge
  • 舍入 ×5 × 饱和 ×2 × 位置 {DISPERSE_FIRST/SECOND/THIRD/FOURTH_QUARTER},底层指令 vcvt_f162s4(PART_P0~P3)

bfloat16 → FP8 系列(44 重载)

转换方向 接口名 形态 重载数 底层指令
bf16→e4m3 asc_bfloat162e4m3_rn{,_sat} void+return × EVEN/ODD 8 vcvt
bf16→e4m3 asc_bfloat162e4m3_rn{,_sat}_merge void × EVEN/ODD 4 vcvt
bf16→e5m2 asc_bfloat162e5m2_rn{,_sat} void+return × EVEN/ODD 8 vcvt
bf16→e5m2 asc_bfloat162e5m2_rn{,_sat}_merge void × EVEN/ODD 4 vcvt
bf16→e6m2 asc_bfloat162e6m2_rn void+return × EVEN/ODD 4 vcvt_bf162e6m2(无饱和参数)
bf16→e8m0 asc_bfloat162e8m0_{ru,rz}{,_sat} void+return × EVEN/ODD 16 vcvt_bf162e8m0

其中 e6m2 为 9201 新增目标类型,配套在 include/c_api/defs/type.h 新增类型别名 vector_fp8_e6m2_t = vector_f8e6m2。

2. 泛型命名接口(86 个重载,舍入/饱和/位置通过模板参数区分)

统一签名形态:

__simd_callee__ inline void asc_cast[_merge](
    vector_<dst>& dst, vector_<src> src, vector_bool mask,
    std::integral_constant<asc_cast_round_mode, ...> round_mode,
    std::integral_constant<asc_cast_saturation_mode, ...> saturation_mode,   // e6m2 无此项
    std::integral_constant<asc_position(_quarter)_mode, ...> dst_pos);

asc_cast_merge(68 重载,全新接口族,通用层此前无任何 merge 版 cast)

转换方向 重载数 参数空间 底层指令
half→int4x2 40 舍入{RD,RN,RNA,RU,RZ} × 饱和{NO_SAT,SAT} × 位置{4×QUARTER} vcvt_f162s4
half→int8 20 舍入×5 × 饱和×2 × 位置{EVEN,ODD} vcvt
bf16→e4m3 4 RN × 饱和×2 × 位置{EVEN,ODD} vcvt
bf16→e5m2 4 RN × 饱和×2 × 位置{EVEN,ODD} vcvt

asc_cast(18 重载,既有接口族新增 9201 专属类型组合)

转换方向 重载数 参数空间 底层指令
bf16→e4m3 4 RN × 饱和×2 × EVEN/ODD vcvt
bf16→e5m2 4 RN × 饱和×2 × EVEN/ODD vcvt
bf16→e6m2 2 RN(无饱和参数)× EVEN/ODD vcvt_bf162e6m2
bf16→e8m0 8 {RU,RZ} × 饱和×2 × EVEN/ODD vcvt_bf162e8m0

两套命名接口已程序化核验:86 个 void 重载严格一一对应,每个配对发出的底层 vcvt* 指令(含全部参数)逐字符一致;cast 族不提供返回值形态,是用户视角的唯一差异。舍入模式 → 指令参数映射:RD→ROUND_F、RN→ROUND_R、RNA→ROUND_A、RU→ROUND_C、RZ→ROUND_Z;饱和 → RS_ENABLE/RS_DISABLE;位置 → PART_EVEN/PART_ODD、PART_P0~P3。

版本宏门控与冲突解决

  • include/c_api/reg_compute/reg_convert.h 中 190 个新增声明整体置于 #if (ASC_DEVKIT_VERSION_NUM >= 90200000) 门控块,vector_fp8_e6m2_t 别名同步纳入门控(include/c_api/defs/type.h),避免影响 9.2.0 之前版本的编译兼容性
  • 分支已 rebase 至最新 master 并解决 reg_convert.h 的合并冲突(声明区块重排,接口集合不变)

配套改动

文件 变动
impl/c_api/reg_base_impl/npu_arch_9201/reg_convert_intf_impl.h 新建(1901 行),190 个重载的完整实现,仅 9201 架构
impl/c_api/reg_base_impl/reg_convert_intf_impl.h 新增 __NPU_ARCH__ == 9201 impl 分发包含;5 处过时注释清理
include/c_api/asc_simd.h、include/c_api/asc_reg_vector.h 入口条件编译 3510 扩展为 3510 || 9201,修复 9201 下 reg_compute 接口链不可达
tests/api/c_api/stub/cce_stub.h 补齐 Fp8e6m2T/vector_f8e6m2 类型与 vcvt(f8e4m3/e5m2←bf16)、vcvt_bf162e6m2、vcvt_bf162e8m0 stub(仅 UT 环境)

设计决策

  1. merging 接口仅提供 void 形态:单返回值重载中 dst 为函数内新定义的 vector,merging 模式下目标位置外的值无意义,无业务场景
  2. merging 后缀命名 _merge:语义明确,区别于 MODE_ZEROING 常规写入(mask=false 处写 0)
  3. 双套命名并存:专用命名直观易查,泛型命名适配模板/泛型调用场景,二者指令级等价

关联的Issue

无

测试

新增 tests/api/c_api/npu_arch_9201/utils/test_vcvt_instr_utils.h(指令级 UT 框架宏)与 8 个测试文件,mockcpp 指令级 mock,190 个用例与 190 个新重载 1:1 对应:

测试文件 用例 覆盖
test_asc_half2int4x2.cpp 40 10 API × 4 quarter 位置
test_asc_half2int8.cpp 20 10 API × EVEN/ODD
test_asc_bfloat162e4m3.cpp 12 rn/rn_sat(void+return)+ merge
test_asc_bfloat162e5m2.cpp 12 同上
test_asc_bfloat162e6m2.cpp 4 rn × EVEN/ODD(void+return)
test_asc_bfloat162e8m0.cpp 16 ru/rz × sat × EVEN/ODD
test_asc_cast.cpp 18 泛型 asc_cast 全部重载
test_asc_cast_merge.cpp 68 泛型 asc_cast_merge 全部重载

测试结果:

文档更新

无独立文档更新,接口以头文件声明为准。

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for zhangfeifan0709's contribution.)
zhangfeifan0709zhangfeifan0709Collaborators
Sep 18 Created pull request, commit c64755f7
CANN-robotCANN-robotCollaborators
Sep 18 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Sep 18 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 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
impl/c_api ✅ wuyang_hw, Lycheeeee (2/2) ✅ Lycheeeee (1/1)
include/c_api ✅ wuzhaolin, Lycheeeee, wuyang_hw (3/2) ✅ wuzhaolin (1/1)
include/c_api/reg_compute ✅ horming, Lycheeeee, wuyang_hw (3/2) ✅ horming (1/1)
repo-cann/asc-devkit ✅ Lycheeeee, wuyang_hw (2/2) ✅ Lycheeeee, wuyang_hw (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robotCollaborators
Sep 18 Set zengjuan,zhanghao_0689,suqwe,wulinyu,horming,kong0808,dengjing_aoe,YeZZzzz1,bluesky901,yangbinhua,zhudi22,chenyiyuan,xun_zhuge,XuebinYang,sjtulxh,bianque,houyuyang,pangfd,Lycheeeee,YANXI_ZHAO,chentianyu19,shi_nanxiang,wuyang_hw,jiangxinyu3,ascendhjh,hujiawen_kaven,wangxiaozhi,DragonBornHD84,louyuxuan,hyolee,yukangchen,wuzhaolin as approver
317 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Oct 12 Added label: lgtm
horming
hormingCollaborators
Oct 12 Comments:

/approve

likedislike
wuzhaolin
wuzhaolinCollaborators
Oct 12 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Oct 12 Added label: approved
CANN-robotCANN-robotCollaborators
Oct 12 Merged pull request