Skip to content

asc_bfloat162e1m2x2

产品支持情况​

  • Ascend 950PR&950DT系列产品:支持
  • Atlas A3系列产品:不支持
  • Atlas A2系列产品:不支持
  • Atlas 200I/500 A2推理产品:不支持
  • Atlas推理系列产品AI Core:不支持
  • Atlas推理系列产品Vector Core:不支持
  • Atlas训练系列产品:不支持

功能说明​

头文件路径为:"c_api/reg_compute/reg_convert.h"。

根据mask将src中参与计算的bfloat16_t类型元素转换为fp4x2_e1m2_t类型,结果写入dst。由于源操作数与目的操作数的类型位宽比为4:1,dst中的元素按索引分为四组,参数dst_quarter_pos从dst中离散选取每隔4个元素的索引位置写入转换结果,具体为索引0、4、8、…,索引1、5、9、…,索引2、6、10、…或索引3、7、11、…,其他位置置零。

关于舍入模式和饱和/非饱和模式的详细说明,请参见舍入模式与饱和模式。

本接口为Reg矢量计算接口,仅在AIV上生效。

函数原型​

C
// 通过引用参数输出结果(占位符形式)
__simd_callee__ inline void asc_bfloat162e1m2x2_<round_mode>(vector_fp4x2_e1m2_t& dst,
                                                             vector_bfloat16_t src,
                                                             vector_bool mask,
                                                             std::integral_constant<asc_position_quarter_mode, <quarter_pos>> dst_quarter_pos)

// 通过函数返回值返回结果(占位符形式)
__simd_callee__ inline vector_fp4x2_e1m2_t asc_bfloat162e1m2x2_<round_mode>(vector_bfloat16_t src,
                                                                            vector_bool mask,
                                                                            std::integral_constant<asc_position_quarter_mode, <quarter_pos>> dst_quarter_pos)

占位符说明如下:

  • <round_mode>支持rd(FLOOR)、rn(RINT)、rna(ROUND)、ru(CEIL)和rz(TRUNC)。

<quarter_pos>支持asc_position_quarter_mode枚举类的四个取值:

C
enum class asc_position_quarter_mode {
    DISPERSE_FIRST_QUARTER = 0,
    DISPERSE_SECOND_QUARTER,
    DISPERSE_THIRD_QUARTER,
    DISPERSE_FOURTH_QUARTER
};

函数原型典型示例​

C
// 通过引用参数输出结果
__simd_callee__ inline void asc_bfloat162e1m2x2_rn(vector_fp4x2_e1m2_t& dst,
                                                   vector_bfloat16_t src,
                                                   vector_bool mask,
                                                   std::integral_constant<asc_position_quarter_mode,
                                                                          asc_position_quarter_mode::DISPERSE_FIRST_QUARTER> dst_quarter_pos)

// 通过函数返回值返回结果
__simd_callee__ inline vector_fp4x2_e1m2_t asc_bfloat162e1m2x2_rn(vector_bfloat16_t src,
                                                                  vector_bool mask,
                                                                  std::integral_constant<asc_position_quarter_mode,
                                                                                         asc_position_quarter_mode::DISPERSE_FIRST_QUARTER> dst_quarter_pos)

参数说明​

表1 参数说明

参数名输入/输出描述
dst输出目的操作数(矢量数据寄存器)。
src输入源操作数(矢量数据寄存器)。
mask输入掩码寄存器,用于控制各元素是否参与计算。mask中与元素对应的比特位为1时,该元素参与计算;为0时,该元素不参与计算。
dst_quarter_pos输入四分之一位置选择标签(编译期标签分发,通过编译期重载选择对应实现),用于选择目的操作数的写入地址,其他位置清零。可传入对应的编译期标签:ASC_DISPERSE_FIRST_QUARTER、ASC_DISPERSE_SECOND_QUARTER、ASC_DISPERSE_THIRD_QUARTER和ASC_DISPERSE_FOURTH_QUARTER。

矢量数据寄存器和掩码寄存器的详细说明请参见reg数据类型定义。

返回值说明​

  • 通过引用参数输出结果的函数原型无返回值。
  • 通过函数返回值输出结果的函数原型返回计算结果,返回值类型与对应引用输出函数原型中dst参数的类型一致(去除引用)。

约束说明​

  • Reg矢量计算C API通用约束请参见通用约束。
  • 位置选择标签参数仅能使用编译期常量,编译器据此在编译期分发至对应的重载。
  • 位置选择标签选择目的操作数的写入地址,其他位置清零。
  • mask掩码位为0时,dst对应元素置0。

调用示例​

将代码保存为example.asc后,可通过bisheng命令编译运行,其中--npu-arch参数需根据实际产品型号指定对应的NPU架构,具体产品与NPU架构的映射关系请参考NPU_ARCH。

以Ascend 950PR&950DT系列产品(对应NPU架构为dav-3510)为例,编译运行命令如下:

Bash
bisheng example.asc -o main --npu-arch=dav-3510 && ./main
C
#include <cstdint>
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "c_api/asc_simd.h"
#include "utils/std/cmath.h"

namespace {
constexpr uint8_t MUTEX_ID = 0;

constexpr uint32_t ELEMENTS_PER_REGISTER = 256;
constexpr uint32_t DATA_BYTES = 260;
// 统一记录实际处理长度,用于计算迭代次数并限制尾轮的有效范围。
constexpr uint32_t TOTAL_LENGTH = DATA_BYTES / sizeof(uint8_t);
// 按单轮处理能力向上取整,确保非整寄存器长度的尾块也被处理。
constexpr uint32_t REPEAT_COUNT =
    (TOTAL_LENGTH + ELEMENTS_PER_REGISTER - 1) / ELEMENTS_PER_REGISTER;
constexpr uint32_t PADDED_BUFFER_BYTES = REPEAT_COUNT * ELEMENTS_PER_REGISTER;

__simd_vf__ inline void convert(__ubuf__ uint8_t* output, __ubuf__ uint8_t* input, uint32_t one_repeat_size, uint16_t repeat_time)
{
    // 保留剩余有效元素数,供更新掩码处理尾块。
    uint32_t remaining_length = TOTAL_LENGTH / sizeof(uint16_t);
    for (uint16_t i = 0; i < repeat_time; ++i) {
        const uint32_t offset = i * one_repeat_size;
        vector_bool mask = asc_update_mask_b16(remaining_length);
        vector_fp4x2_e1m2_t dst;
        vector_bfloat16_t src;
        asc_loadalign(dst, reinterpret_cast<__ubuf__ fp4x2_e1m2_t*>(output + offset));
        asc_loadalign(src, reinterpret_cast<__ubuf__ bfloat16_t*>(input + offset));
        asc_bfloat162e1m2x2_rn(dst, src, mask, ASC_DISPERSE_FIRST_QUARTER);
        asc_storealign(reinterpret_cast<__ubuf__ fp4x2_e1m2_t*>(output + offset), dst, mask);
    }
}

__global__ __vector__ void asc_bfloat162e1m2x2_kernel(__gm__ uint8_t* output, __gm__ uint8_t* input)
{
    asc_init();
    __ubuf__ uint8_t output_local[PADDED_BUFFER_BYTES];
    __ubuf__ uint8_t input_local[PADDED_BUFFER_BYTES];
    asc_lock(PIPE_MTE2, MUTEX_ID);
    asc_copy_gm2ub_align(input_local, input, PADDED_BUFFER_BYTES);
    asc_copy_gm2ub_align(output_local, input, PADDED_BUFFER_BYTES);
    asc_unlock(PIPE_MTE2, MUTEX_ID);

    uint32_t one_repeat_size = static_cast<uint32_t>(asc_get_vf_len() / sizeof(uint8_t));
    uint16_t repeat_time = static_cast<uint16_t>(AscendC::Std::ceil_div(TOTAL_LENGTH, one_repeat_size));
    asc_lock(PIPE_V, MUTEX_ID);
    asc_vf_call<convert>(output_local, input_local, one_repeat_size, repeat_time);
    asc_unlock(PIPE_V, MUTEX_ID);

    asc_lock(PIPE_MTE3, MUTEX_ID);
    asc_copy_ub2gm_align(output, output_local, PADDED_BUFFER_BYTES);
    asc_unlock(PIPE_MTE3, MUTEX_ID);
    asc_sync();
}
} // namespace

int main()
{
    std::vector<uint8_t> input(PADDED_BUFFER_BYTES, 0);
    std::vector<uint8_t> output(PADDED_BUFFER_BYTES, 0xff);
    const std::vector<uint8_t> golden(PADDED_BUFFER_BYTES, 0);
    aclInit(nullptr);
    aclrtSetDevice(0);
    uint8_t* input_device = nullptr;
    uint8_t* output_device = nullptr;
    aclrtMalloc(reinterpret_cast<void**>(&input_device), PADDED_BUFFER_BYTES, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc(reinterpret_cast<void**>(&output_device), PADDED_BUFFER_BYTES, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(input_device, PADDED_BUFFER_BYTES, input.data(), PADDED_BUFFER_BYTES, ACL_MEMCPY_HOST_TO_DEVICE);
    asc_bfloat162e1m2x2_kernel<<<1, 0>>>(output_device, input_device);
    aclrtSynchronizeDevice();
    aclrtMemcpy(output.data(), PADDED_BUFFER_BYTES, output_device, PADDED_BUFFER_BYTES, ACL_MEMCPY_DEVICE_TO_HOST);
    const bool passed = output == golden;
    std::cout << (passed ? "[Success] asc_bfloat162e1m2x2_rn passed." : "[Failed] asc_bfloat162e1m2x2_rn failed.") << std::endl;
    aclrtFree(input_device);
    aclrtFree(output_device);
    aclrtResetDevice(0);
    aclFinalize();
    return passed ? 0 : 1;
}

免责声明:本站内容由 asc-devkit 仓 master 分支自动编译生成,属于持续开发版本,可能存在缺陷,仅供预览与参考。如需稳定及商用资料,请查阅官方 昇腾社区。