Skip to content

asc_xor

产品支持情况

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

功能说明

头文件路径为:"c_api/reg_compute/compute/reg_logic.h"

asc_xor支持两种接口:

  • 对矢量数据寄存器操作: 根据mask对源矢量数据寄存器src0src1进行按位异或(^)操作,将结果写入目的矢量数据寄存器dst或通过函数返回值返回。未被mask筛选的位置被置为0。
  • 对掩码寄存器操作: 根据mask对两个源掩码寄存器src0src1进行按位异或(^)操作,将结果写入目的掩码数据寄存器dst或通过函数返回值返回。未被mask筛选的位置被置为0。

计算公式如下:

Python
def asc_xor(dst, src0, src1, mask):
    for i in range(len(src0)):
        dst[i] = src0[i] ^ src1[i] if mask[i] else 0

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

函数原型

矢量数据寄存器按位异或

C
// 通过函数返回值返回结果(占位符形式)
__simd_callee__ inline vector_<dtype> asc_xor(vector_<dtype> src0,
                                               vector_<dtype> src1,
                                               vector_bool mask)

// 通过引用参数输出结果(占位符形式)
__simd_callee__ inline void asc_xor(vector_<dtype>& dst,
                                    vector_<dtype> src0,
                                    vector_<dtype> src1,
                                    vector_bool mask)

dtype支持数据类型

dtype支持的数据类型:int8_tuint8_tint16_tuint16_tint32_tuint32_t

函数原型典型示例

C
// 示例:对int8_t矢量数据寄存器执行按位异或,通过函数返回值返回结果。
__simd_callee__ inline vector_int8_t asc_xor(vector_int8_t src0,
                                              vector_int8_t src1,
                                              vector_bool mask)

// 示例:对int8_t矢量数据寄存器执行按位异或。
__simd_callee__ inline void asc_xor(vector_int8_t& dst,
                                    vector_int8_t src0,
                                    vector_int8_t src1,
                                    vector_bool mask)

掩码寄存器按位异或

C
// 通过函数返回值返回结果。
__simd_callee__ inline vector_bool asc_xor(vector_bool src0,
                                           vector_bool src1,
                                           vector_bool mask)

// 通过引用参数输出结果。
__simd_callee__ inline void asc_xor(vector_bool& dst,
                                    vector_bool src0,
                                    vector_bool src1,
                                    vector_bool mask)

参数说明

表1 参数说明(操作数为矢量数据寄存器)

参数名输入/输出描述
dst输出目的矢量数据寄存器,仅无返回值类型接口包含该参数,写入按位异或结果。dtype须与src0src1一致。
src0输入源矢量数据寄存器,参与按位异或的操作数。dtype须与dst一致。
src1输入源矢量数据寄存器,参与按位异或的操作数。dtype须与dst一致。
mask输入源操作数掩码(掩码寄存器),用于指示在计算过程中哪些元素参与计算。对应位置为1时参与计算,为0时不参与计算。mask未筛选的元素在输出中置零。

表2 参数说明(操作数为掩码寄存器)

参数名输入/输出描述
dst输出目的掩码寄存器,仅无返回值类型接口包含该参数,写入按位异或结果。
src0输入源掩码寄存器,参与按位异或的操作数。
src1输入源掩码寄存器,参与按位异或的操作数。
mask输入源掩码寄存器,用于指示在计算过程中哪些元素参与计算。对应位置为1时参与计算,为0时不参与计算。mask未筛选的元素在输出中置零。

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

返回值说明

对于返回值类型接口,返回保存按位异或结果的矢量数据寄存器或掩码寄存器,数据类型与src0保持一致。

约束说明

  • 本接口非AIV调用直接返回。
  • dst中未被mask筛选的位置被置为0。

调用示例

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

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

Bash
bisheng example.asc -o main --npu-arch=dav-3510 && ./main
C++
#include <cstdint>
#include <iostream>
#include <vector>

#include "c_api/asc_simd.h"
#include "acl/acl.h"

namespace {
template <typename T>
void print_data(const char* label, const std::vector<T>& values)
{
    std::cout << label << ":";
    const size_t count = values.size() < 8 ? values.size() : 8;
    for (size_t i = 0; i < count; ++i) std::cout << ' ' << +values[i];
    if (values.size() > count) std::cout << " ...";
    std::cout << std::endl;
}

template <typename T>
bool compare_data(const std::vector<T>& actual, const std::vector<T>& expected, double tolerance = 0.0)
{
    if (actual.size() != expected.size()) return false;
    for (size_t i = 0; i < actual.size(); ++i) {
        if (actual[i] == expected[i]) continue;
        const double diff = static_cast<double>(actual[i]) - static_cast<double>(expected[i]);
        if (diff > tolerance || diff < -tolerance) return false;
    }
    return true;
}

constexpr uint32_t ELEMENT_COUNT = 64;

__simd_vf__ inline void compute(__ubuf__ int32_t* dst, __ubuf__ int32_t* src0, __ubuf__ int32_t* src1)
{
    vector_int32_t dst_reg;
    vector_int32_t src0_reg;
    vector_int32_t src1_reg;
    uint32_t count = ELEMENT_COUNT;
    vector_bool mask = asc_update_mask_b32(count);
    asc_loadalign(src0_reg, src0);
    asc_loadalign(src1_reg, src1);
    asc_xor(dst_reg, src0_reg, src1_reg, mask);
    asc_storealign(dst, dst_reg, mask);
}

__global__ __vector__ void asc_xor_kernel(__gm__ int32_t* dst, __gm__ int32_t* src0, __gm__ int32_t* src1)
{
    asc_init();
    __ubuf__ int32_t dst_local[ELEMENT_COUNT];
    __ubuf__ int32_t src0_local[ELEMENT_COUNT];
    __ubuf__ int32_t src1_local[ELEMENT_COUNT];
    asc_copy_gm2ub_align(src0_local, src0, ELEMENT_COUNT * sizeof(int32_t));
    asc_copy_gm2ub_align(src1_local, src1, ELEMENT_COUNT * sizeof(int32_t));
    asc_sync_notify(PIPE_MTE2, PIPE_V, EVENT_ID0);
    asc_sync_wait(PIPE_MTE2, PIPE_V, EVENT_ID0);
    compute(dst_local, src0_local, src1_local);
    asc_sync_notify(PIPE_V, PIPE_MTE3, EVENT_ID0);
    asc_sync_wait(PIPE_V, PIPE_MTE3, EVENT_ID0);
    asc_copy_ub2gm_align(dst, dst_local, ELEMENT_COUNT * sizeof(int32_t));
    asc_sync();
}
} // namespace

int main()
{
    std::vector<int32_t> src0(ELEMENT_COUNT);
    std::vector<int32_t> src1(ELEMENT_COUNT);
    std::vector<int32_t> output(ELEMENT_COUNT, 0);
    std::vector<int32_t> golden(ELEMENT_COUNT);
    for (uint32_t i = 0; i < ELEMENT_COUNT; ++i) {
        src0[i] = static_cast<int32_t>(0x101010U + i);
        src1[i] = static_cast<int32_t>(0x0f0f0000U + i);
        golden[i] = src0[i] ^ src1[i];
    }

    aclInit(nullptr);
    aclrtSetDevice(0);
    int32_t* src0_device = nullptr;
    aclrtMalloc(reinterpret_cast<void**>(&src0_device), (ELEMENT_COUNT) * sizeof(int32_t),
        ACL_MEM_MALLOC_HUGE_FIRST);
    int32_t* src1_device = nullptr;
    aclrtMalloc(reinterpret_cast<void**>(&src1_device), (ELEMENT_COUNT) * sizeof(int32_t),
        ACL_MEM_MALLOC_HUGE_FIRST);
    int32_t* dst_device = nullptr;
    aclrtMalloc(reinterpret_cast<void**>(&dst_device), (ELEMENT_COUNT) * sizeof(int32_t),
        ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMemcpy(src0_device, src0.size() * sizeof(int32_t), src0.data(), src0.size() * sizeof(int32_t),
        ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(src1_device, src1.size() * sizeof(int32_t), src1.data(), src1.size() * sizeof(int32_t),
        ACL_MEMCPY_HOST_TO_DEVICE);

    asc_xor_kernel<<<1, 0>>>(dst_device, src0_device, src1_device);
    aclrtSynchronizeDevice();
    aclrtMemcpy(output.data(), output.size() * sizeof(int32_t), dst_device, output.size() * sizeof(int32_t),
        ACL_MEMCPY_DEVICE_TO_HOST);
    print_data("Input src0", src0);
    print_data("Input src1", src1);
    print_data("Output", output);
    print_data("Golden", golden);
    const bool passed = compare_data(output, golden);
    std::cout << (passed ? "[Success] asc_xor passed." : "[Failed] asc_xor failed.") << std::endl;
    aclrtFree(dst_device);
    aclrtFree(src0_device);
    aclrtFree(src1_device);
    aclrtResetDevice(0);
    aclFinalize();
    return passed ? 0 : 1;
}

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