CANN/Ascend C SIMT bfloat16x2加法函数

__haddx2

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

产品支持情况

产品是否支持
Ascend 950PR/Ascend 950DT
Atlas A3 训练系列产品/Atlas A3 推理系列产品x
Atlas A2 训练系列产品/Atlas A2 推理系列产品x
Atlas 200I/500 A2 推理产品x
Atlas 推理系列产品AI Corex
Atlas 推理系列产品Vector Corex
Atlas 训练系列产品x

功能说明

计算两个bfloat16x2_t类型数据各分量的相加结果,并遵循CAST_RINT模式对结果进行舍入。

函数原型

bfloat16x2_t __haddx2(const bfloat16x2_t x, const bfloat16x2_t y)

参数说明

表 1 参数说明

参数名输入/输出描述
x输入源操作数。
y输入源操作数。

返回值说明

输入数据各分量相加的结果。相加的分量x和y满足:

  • x为有限值,y为±inf时,返回值为±inf。
  • x为±inf,y为±inf时,返回值为±inf。
  • x为±inf,y为∓inf时,返回值为nan。
  • x为±0,y为±0时,返回值为±0。
  • 对有限值x(包括±0),x=-y时,返回值为+0。
  • x,y任意一个为nan时,返回值为nan。

约束说明

需要包含的头文件

使用该接口需要包含"simt_api/asc_bf16.h"头文件。

#include "simt_api/asc_bf16.h"

调用示例

  • SIMT编程场景:

    // 使用短向量可提升数据搬运效率
    __global__ __launch_bounds__(1024) void simt_haddx2(bfloat16_t* x, bfloat16_t* y, bfloat16_t* dst, uint32_t input_total_length)
    {
        uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
        // 每个线程处理1个bfloat16x2_t类型的数据,即2个bfloat16_t类型的数据,因此idx >= input_total_length / 2的线程不处理数据
        if (idx >= input_total_length / 2) {
            return;
        }
        bfloat16x2_t* input1 = (bfloat16x2_t*)x;
        bfloat16x2_t* input2 = (bfloat16x2_t*)y;
        bfloat16x2_t* out = (bfloat16x2_t*)dst;
        out[idx] = __haddx2(input1[idx], input2[idx]);
    }
    
  • SIMD与SIMT混合编程场景:

    // 使用短向量可提升数据搬运效率
    __simt_vf__ __launch_bounds__(1024) inline void simt_haddx2(__gm__ bfloat16x2_t* x, __gm__ bfloat16x2_t* y, __gm__ bfloat16x2_t* dst, uint32_t input_total_length)
    {
        uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
        // 每个线程处理1个bfloat16x2_t类型的数据,即2个bfloat16_t类型的数据,因此idx >= input_total_length / 2的线程不处理数据
        if (idx >= input_total_length / 2) {
            return;
        }
        dst[idx] = __haddx2(x[idx], y[idx]);
    }
    
    __global__ __vector__ void compute_kernel(__gm__ bfloat16_t* x, __gm__ bfloat16_t* y, __gm__ bfloat16_t* dst, uint32_t input_total_length)
    {
        asc_vf_call<simt_haddx2>(dim3(1024), (__gm__ bfloat16x2_t*)x, (__gm__ bfloat16x2_t*)y, (__gm__ bfloat16x2_t*)dst, input_total_length);
    }
    

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

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

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值