news 2026/6/21 16:17:42

CANN/Ascend C浮点转BF16函数

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
CANN/Ascend C浮点转BF16函数

asc_float2bfloat16

【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit

产品支持情况

产品是否支持
Atlas A3 训练系列产品/Atlas A3 推理系列产品
Atlas A2 训练系列产品/Atlas A2 推理系列产品

功能说明

将float类型数据转换为bfloat16_t类型,并支持多种舍入模式:

  • RINT舍入模式:四舍六入五成双舍入
  • ROUND舍入模式:四舍五入舍入
  • FLOOR舍入模式:向负无穷舍入
  • CEIL舍入模式:向正无穷舍入
  • TRUNC舍入模式:向零舍入

函数原型

  • 前n个数据连续计算

    // RINT舍入模式 __aicore__ inline void asc_float2bfloat16_rn(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint32_t count) // ROUND舍入模式 __aicore__ inline void asc_float2bfloat16_rna(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint32_t count) // FLOOR舍入模式 __aicore__ inline void asc_float2bfloat16_rd(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint32_t count) // CEIL舍入模式 __aicore__ inline void asc_float2bfloat16_ru(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint32_t count) // TRUNC舍入模式 __aicore__ inline void asc_float2bfloat16_rz(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint32_t count)
  • 高维切分计算

    // RINT舍入模式 __aicore__ inline void asc_float2bfloat16_rn(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint8_t repeat, uint16_t dst_block_stride, uint16_t src_block_stride, uint16_t dst_repeat_stride, uint16_t src_repeat_stride) // ROUND舍入模式 __aicore__ inline void asc_float2bfloat16_rna(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint8_t repeat, uint16_t dst_block_stride, uint16_t src_block_stride, uint16_t dst_repeat_stride, uint16_t src_repeat_stride) // FLOOR舍入模式 __aicore__ inline void asc_float2bfloat16_rd(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint8_t repeat, uint16_t dst_block_stride, uint16_t src_block_stride, uint16_t dst_repeat_stride, uint16_t src_repeat_stride) // CEIL舍入模式 __aicore__ inline void asc_float2bfloat16_ru(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint8_t repeat, uint16_t dst_block_stride, uint16_t src_block_stride, uint16_t dst_repeat_stride, uint16_t src_repeat_stride) // TRUNC舍入模式 __aicore__ inline void asc_float2bfloat16_rz(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint8_t repeat, uint16_t dst_block_stride, uint16_t src_block_stride, uint16_t dst_repeat_stride, uint16_t src_repeat_stride)
  • 同步计算

    // RINT舍入模式 __aicore__ inline void asc_float2bfloat16_rn_sync(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint32_t count) //ROUND舍入模式 __aicore__ inline void asc_float2bfloat16_rna_sync(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint32_t count) // FLOOR舍入模式 __aicore__ inline void asc_float2bfloat16_rd_sync(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint32_t count) // CEIL舍入模式 __aicore__ inline void asc_float2bfloat16_ru_sync(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint32_t count) // TRUNC舍入模式 __aicore__ inline void asc_float2bfloat16_rz_sync(__ubuf__ bfloat16_t* dst, __ubuf__ float* src, uint32_t count)

参数说明

参数名输入/输出描述
dst输出目的操作数(矢量)的起始地址。
src输入源操作数(矢量)的起始地址。
count输入参与计算的元素个数。
repeat输入迭代次数。
dst_block_stride输入目的操作数单次迭代内不同DataBlock间地址步长。
src_block_stride输入源操作数单次迭代内不同DataBlock间地址步长。
dst_repeat_stride输入目的操作数相邻迭代间相同DataBlock的地址步长。
src_repeat_stride输入源操作数相邻迭代间相同DataBlock的地址步长。

返回值说明

流水类型

PIPE_V

约束说明

  • 操作数地址重叠约束请参考通用地址重叠约束。
  • dst、src的起始地址需要32字节对齐。

调用示例

constexpr uint32_t total_length = 256; __ubuf__ float src[total_length]; __ubuf__ bfloat16_t dst[total_length]; asc_float2bfloat16_rn(dst, src, total_length);

【免费下载链接】asc-devkit本项目是CANN 推出的昇腾AI处理器专用的算子程序开发语言,原生支持C和C++标准规范,主要由类库和语言扩展层构成,提供多层级API,满足多维场景算子开发诉求。项目地址: https://gitcode.com/cann/asc-devkit

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/19 14:58:58

LPC315x引脚复用配置详解:从原理到实践,释放MCU全部潜力

1. 项目概述:理解LPC315x的引脚复用哲学在嵌入式系统,尤其是便携式、成本敏感型设备的设计中,我们常常面临一个核心矛盾:芯片内部集成的功能模块越来越丰富,但封装引脚的数量却受到物理尺寸和成本的严格限制。这就好比…

作者头像 李华
网站建设 2026/6/19 14:55:53

2026 Google广告 vs Meta广告:出海广告投放策略有何区别?

对于出海品牌和跨境电商卖家来说,Google和Meta几乎是绕不开的两大广告平台。但进入2026年后,随着AI技术深度融入广告系统,两大广告平台都在加速向智能化、自动化方向发展,两者的投放逻辑正在发生明显变化。过去,很多运…

作者头像 李华
网站建设 2026/6/19 14:53:19

嵌入式定时器深度解析:看门狗与PIT原理、配置与实战避坑

1. 嵌入式定时器模块:系统稳定运行的守护者在嵌入式系统的世界里,代码不仅要能跑,还得跑得稳、跑得久。尤其是在那些无人值守的工业现场、飞驰的汽车电子系统,或者部署在荒野的物联网节点里,一次意外的程序“跑飞”或死…

作者头像 李华