Skip to content

Duplicate

产品支持情况

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

功能说明

头文件路径为:"basic_api/kernel_operator_vec_duplicate_intf.h"

Duplicate接口将一个变量或立即数复制多次并填充到向量中。

接口支持使用前n个数据计算和高维切分计算的方式,前n个数据计算原理和参考伪代码如下:

Python
import numpy as np

def Duplicate(scalarValue, dst, count):
    for i in range(count):     
        dst[i] = scalarValue

针对Ascend 950PR/Ascend 950DT,为方便开发者使用,tensor前n个数据计算接口同时也支持直接传入LocalTensor,此时会将LocalTensor的第一个元素复制多次并填充到向量中。

函数原型

  • tensor前n个数据计算,源操作数为标量

    C++
    template <typename T>
    __aicore__ inline void Duplicate(const LocalTensor<T>& dst, const T& scalarValue, const int32_t& count)
    
  • tensor前n个数据计算,源操作数为LocalTensor

    C++
    // 该函数原型,仅支持Ascend 950PR/Ascend 950DT。
    template <typename T>
    __aicore__ inline void Duplicate(const LocalTensor<T>& dst, const LocalTensor<T>& src, const int32_t& count)
    
  • tensor高维切分计算,mask逐比特模式

    C++
    template <typename T, bool isSetMask = true>
    __aicore__ inline void Duplicate(const LocalTensor<T>& dst, const T& scalarValue, uint64_t mask[], const uint8_t repeatTime, const uint16_t dstBlockStride, const uint8_t dstRepeatStride)
    
  • tensor高维切分计算,mask连续模式

    C++
    template <typename T, bool isSetMask = true>
    __aicore__ inline void Duplicate(const LocalTensor<T>& dst, const T& scalarValue, uint64_t mask, const uint8_t repeatTime, const uint16_t dstBlockStride, const uint8_t dstRepeatStride)
    

参数说明

表1 模板参数说明

参数说明描述
T操作数数据类型。
isSetMask是否在接口内部设置mask。
• true,表示在接口内部设置mask。
• false,表示在接口外部设置mask,开发者需要使用SetVectorMask接口设置mask值。这种模式下,接口入参中的mask值设置为占位符MASK_PLACEHOLDER,用于占位,无实际含义。

表2 接口参数说明

参数说明输入/输出含义
dst输出目的操作数,类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。LocalTensor的起始地址需要按照32字节对齐。
scalarValue输入被复制的源操作数,数据类型需与dst中元素的数据类型保持一致。
src输入源操作数,类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。
数据类型需与dst中元素的数据类型保持一致。
当传入该参数时,会将src[0]复制多次并填充到向量中。
count输入参与计算的元素个数。关于该参数的具体说明请参考连续计算
mask/mask[]输入mask用于控制每次迭代内参与计算的元素。
设置详见掩码操作
repeatTime输入指令迭代次数,每次迭代完成8个DataBlock的数据收集,取值范围:repeatTime∈[0,255]。
dstBlockStride输入单次迭代内,矢量目的操作数不同datablock间地址步长,单位为DataBlock。
dstRepeatStride输入相邻迭代间,矢量目的操作数相同datablock地址步长,单位为DataBlock。

数据类型

  • 针对Ascend 950PR/Ascend 950DT,T支持的数据类型为:bool、int8_t、uint8_t、fp4x2_e2m1_t、fp4x2_e1m2_t、hifloat8_t、fp8_e8m0_t、fp8_e5m2_t、fp8_e4m3fn_t、int16_t、uint16_t、half、bfloat16_t、int32_t、uint32_t、float、complex32、int64_t、uint64_t、complex64。其中,bool、int8_t、uint8_t、fp4x2_e2m1_t、fp4x2_e1m2_t、hifloat8_t、fp8_e5m2_t、fp8_e4m3fn_t、fp8_e8m0_t、complex32、int64_t、uint64_t、complex64数据类型仅支持tensor前n个数据计算接口。
  • 针对Atlas A3 训练系列产品/Atlas A3 推理系列产品,T支持的数据类型为:int16_t、uint16_t、half、bfloat16_t、int32_t、uint32_t、float。
  • 针对Atlas A2 训练系列产品/Atlas A2 推理系列产品,T支持的数据类型为:int16_t、uint16_t、half、bfloat16_t、int32_t、uint32_t、float。
  • 针对Atlas 200I/500 A2 推理产品,T支持的数据类型为:int16_t、uint16_t、half、bfloat16_t、int32_t、uint32_t、float。
  • 针对Atlas 推理系列产品AI Core,T支持的数据类型为:int16_t、uint16_t、half、bfloat16_t、int32_t、uint32_t、float。
  • 针对Atlas 训练系列产品,T支持的数据类型为:int16_t、uint16_t、half、bfloat16_t、int32_t、uint32_t、float。
  • 针对Kirin X90,T支持的数据类型为:half。
  • 针对Kirin 9030,T支持的数据类型为:half。

返回值说明

约束说明

  • 当参数count或repeatTime取值为0时,该接口的行为如下:
    • 针对如下型号,该接口不会执行计算操作,不会对目的操作数进行写入,该接口将被视为NOP(空操作)。
      • Atlas A3 训练系列产品/Atlas A3 推理系列产品
      • Atlas A2 训练系列产品/Atlas A2 推理系列产品
    • 针对Ascend 950PR/Ascend 950DT,该接口通过VF调用Reg矢量计算API实现兼容,当参数count或repeatTime取值为0时,不保证该接口将被视为NOP(空操作)。

调用示例

本调用示例中只展示完整样例中的接口部分代码。如果您需要运行示例代码,请直接参考完整样例Duplicate样例进行编译执行。

  • tensor高维切分计算样例,mask连续模式

    C++
    uint64_t mask = 128;
    half scalar = 18.0;
    // repeatTime = 2, 128 elements one repeat, 256 elements total
    // dstBlkStride = 1, no gap between blocks in one repeat
    // dstRepStride = 8, no gap between repeats
    AscendC::Duplicate(dstLocal, scalar, mask, 2, 1, 8 );
    
  • tensor高维切分计算样例,mask逐bit模式

    C++
    uint64_t mask[2] = { UINT64_MAX, UINT64_MAX };
    half scalar = 18.0;
    // repeatTime = 2, 128 elements one repeat, 256 elements total
    // dstBlkStride = 1, no gap between blocks in one repeat
    // dstRepStride = 8, no gap between repeats
    AscendC::Duplicate(dstLocal, scalar, mask, 2, 1, 8 );
    
  • tensor前n个数据计算样例,源操作数为标量

    C++
    half inputVal(18.0);
    int32_t srcDataSize = 256; // 参与计算的元素个数
    AscendC::Duplicate<half>(dstLocal, inputVal, srcDataSize);
    
  • tensor前n个数据计算样例,源操作数为Tensor
    C++
    AscendC::Duplicate<half>(dstLocal, srcLocal, srcDataSize);
    

结果示例如下:

scalar: 18.0
srcLocal: [18.0 1.0 2.0 ... 254.0 255.0]
dstLocal: [18.0 18.0 18.0 ... 18.0 18.0]

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