Div

产品支持情况

产品

是否支持

Ascend 950PR/Ascend 950DT

Atlas A3 训练系列产品/Atlas A3 推理系列产品

Atlas A2 训练系列产品/Atlas A2 推理系列产品

Kirin X90

Kirin 9030

功能说明

按元素求商,公式表达如下:

函数原型

  • 整个tensor参与计算

    dst = src0 / src1;
    
  • tensor前n个数据计算

    template <typename T, const DivConfig& config = DEFAULT_DIV_CONFIG>
    __aicore__ inline void Div(const LocalTensor<T>& dst, const LocalTensor<T>& src0, const LocalTensor<T>& src1, const int32_t& count)
    
  • tensor高维切分计算

    • mask逐bit模式

      template <typename T, bool isSetMask = true, const DivConfig& config = DEFAULT_DIV_CONFIG>
      __aicore__ inline void Div(const LocalTensor<T>& dst, const LocalTensor<T>& src0, const LocalTensor<T>& src1, uint64_t mask[], const uint8_t repeatTime, const BinaryRepeatParams& repeatParams)
      
    • mask连续模式

      template <typename T, bool isSetMask = true, const DivConfig& config = DEFAULT_DIV_CONFIG>
      __aicore__ inline void Div(const LocalTensor<T>& dst, const LocalTensor<T>& src0, const LocalTensor<T>& src1, uint64_t mask, const uint8_t repeatTime, const BinaryRepeatParams& repeatParams)
      

参数说明

表 1 模板参数说明

参数名

描述

T

操作数数据类型。

Ascend 950PR/Ascend 950DT,支持的数据类型为:int16_t、uint16_t、half、int32_t、uint32_t、float、complex32、int64_t、uint64_t、complex64。

Atlas A3 训练系列产品/Atlas A3 推理系列产品,支持的数据类型为:half、float。

Atlas A2 训练系列产品/Atlas A2 推理系列产品,支持的数据类型为:half、float。

Kirin X90,支持的数据类型为:half、float。

Kirin 9030,支持的数据类型为:half、float。

isSetMask

是否在接口内部设置mask。

  • true,表示在接口内部设置mask。
  • false,表示在接口外部设置mask,开发者需要使用SetVectorMask接口设置mask值。这种模式下,本接口入参中的mask值必须设置为占位符MASK_PLACEHOLDER。

config

用于配置精度计算模式,DivConfig类型,定义如下:

enum class DivAlgo {
    INTRINSIC = 0,
    DIFF_COMPENSATION,
    PRECISION_1ULP_FTZ_TRUE,
    PRECISION_0ULP_FTZ_TRUE,
    PRECISION_0ULP_FTZ_FALSE,
    PRECISION_1ULP_FTZ_FALSE
};
struct DivConfig {
    DivAlgo algo = DivAlgo::INTRINSIC;
};

通过DivConfig结构体的参数algo来配置精度计算模式。algo取值如下:

  • DivAlgo::INTRINSIC、DivAlgo::PRECISION_1ULP_FTZ_TRUE,使用单指令计算得出结果,最大精度误差为1 ulp。
  • DivAlgo::DIFF_COMPENSATION、DivAlgo::PRECISION_0ULP_FTZ_TRUE,使用差值补偿算法得出结果,最大精度误差为0 ulp。目前,该算法支持float数据类型。
  • DivAlgo::PRECISION_0ULP_FTZ_FALSE,支持Subnormal数据计算,使用差值补偿算法得出结果,最大精度误差为0 ulp。目前,该算法支持float数据类型。
  • DivAlgo::PRECISION_1ULP_FTZ_FALSE,支持Subnormal数据计算,使用单指令计算得出结果,最大精度误差为1 ulp。

该参数的默认值DEFAULT_DIV_CONFIG的取值如下:

constexpr DivConfig DEFAULT_DIV_CONFIG = { DivAlgo::INTRINSIC };

表 2 参数说明

参数名

输入/输出

描述

dst

输出

目的操作数。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

LocalTensor的起始地址需要32字节对齐。

src0、src1

输入

源操作数。

类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。

LocalTensor的起始地址需要32字节对齐。

两个源操作数的数据类型需要与目的操作数保持一致。

count

输入

参与计算的元素个数。

mask[]/mask

输入

mask用于控制每次迭代内参与计算的元素。

  • 逐bit模式:可以按位控制哪些元素参与计算,bit位的值为1表示参与计算,0表示不参与。

    mask为数组形式,数组长度和数组元素的取值范围和操作数的数据类型有关。当操作数为16位时,数组长度为2,mask[0]、mask[1]∈[0, 264-1]并且不同时为0;当操作数为32位时,数组长度为1,mask[0]∈(0, 264-1];当操作数为64位时,数组长度为1,mask[0]∈(0, 232-1]。

    例如,mask=[8, 0],8=0b1000,表示仅第4个元素参与计算。

  • 连续模式:表示前面连续的多少个元素参与计算。取值范围和操作数的数据类型有关,数据类型不同,每次迭代内能够处理的元素个数最大值不同。当操作数为16位时,mask∈[1, 128];当操作数为32位时,mask∈[1, 64];当操作数为64位时,mask∈[1, 32]。

repeatTime

输入

重复迭代次数。矢量计算单元,每次读取连续的256Bytes数据进行计算,为完成对输入数据的处理,必须通过多次迭代(repeat)才能完成所有数据的读取与计算。repeatTime表示迭代的次数。

repeatParams

输入

控制操作数地址步长的参数。BinaryRepeatParams类型,包含操作数相邻迭代间相同datablock的地址步长,操作数同一迭代内不同datablock的地址步长等参数。

返回值说明

约束说明

  • 操作数地址对齐要求请参见通用地址对齐约束

  • 操作数地址重叠约束请参考通用地址重叠约束

  • 使用整个tensor参与计算接口符号重载时,运算量为目的LocalTensor的总长度。

  • 注意除零错误。

  • 针对Ascend 950PR/Ascend 950DT,uint64_t/int64_t/complex32/complex64数据类型仅支持tensor前n个数据计算接口和整个tensor参与计算的运算符重载。

调用示例

更多样例可参考LINK

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

    #include "kernel_operator.h"
     
    class KernelDiv {
    public:
        __aicore__ inline KernelDiv() {}
        __aicore__ inline void Init(__gm__ uint8_t* src0Gm, __gm__ uint8_t* src1Gm, __gm__ uint8_t* dstGm)
        {
            src0Global.SetGlobalBuffer((__gm__ half*)src0Gm);
            src1Global.SetGlobalBuffer((__gm__ half*)src1Gm);
            dstGlobal.SetGlobalBuffer((__gm__ half*)dstGm);
            pipe.InitBuffer(inQueueSrc0, 1, 512 * sizeof(half));
            pipe.InitBuffer(inQueueSrc1, 1, 512 * sizeof(half));
            pipe.InitBuffer(outQueueDst, 1, 512 * sizeof(half));
        }
        __aicore__ inline void Process()
        {
            CopyIn();
            Compute();
            CopyOut();
        }
    private:
        __aicore__ inline void CopyIn()
        {
            AscendC::LocalTensor<half> src0Local = inQueueSrc0.AllocTensor<half>();
            AscendC::LocalTensor<half> src1Local = inQueueSrc1.AllocTensor<half>();
            AscendC::DataCopy(src0Local, src0Global, 512);
            AscendC::DataCopy(src1Local, src1Global, 512);
            inQueueSrc0.EnQue(src0Local);
            inQueueSrc1.EnQue(src1Local);
        }
        __aicore__ inline void Compute()
        {
            AscendC::LocalTensor<half> src0Local = inQueueSrc0.DeQue<half>();
            AscendC::LocalTensor<half> src1Local = inQueueSrc1.DeQue<half>();
            AscendC::LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>();
            
            uint64_t mask = 128;
            AscendC::Div(dstLocal, src0Local, src1Local, mask, 4, { 1, 1, 1, 8, 8, 8 });
    
     
            outQueueDst.EnQue<half>(dstLocal);
            inQueueSrc0.FreeTensor(src0Local);
            inQueueSrc1.FreeTensor(src1Local);
        }
        __aicore__ inline void CopyOut()
        {
            AscendC::LocalTensor<half> dstLocal = outQueueDst.DeQue<half>();
            AscendC::DataCopy(dstGlobal, dstLocal, 512);
            outQueueDst.FreeTensor(dstLocal);
        }
    private:
        AscendC::TPipe pipe;
        AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueSrc0, inQueueSrc1;
        AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueueDst;
        AscendC::GlobalTensor<half> src0Global, src1Global, dstGlobal;
    };
     
    extern "C" __global__ __aicore__ void div_simple_kernel(__gm__ uint8_t* src0Gm, __gm__ uint8_t* src1Gm,
        __gm__ uint8_t* dstGm)
    {
        KernelDiv op;
        op.Init(src0Gm, src1Gm, dstGm);
        op.Process();
    }
    
  • tensor高维切分计算样例-mask逐bit模式

    uint64_t mask[2] = { UINT64_MAX, UINT64_MAX };
    // repeatTime = 4,一次迭代计算128个数,共计算512个数
    // dstBlkStride, src0BlkStride, src1BlkStride = 1,单次迭代内数据连续读取和写入
    // dstRepStride, src0RepStride, src1RepStride = 8,相邻迭代间数据连续读取和写入
    AscendC::Div(dstLocal, src0Local, src1Local, mask, 4, { 1, 1, 1, 8, 8, 8 });
    
  • tensor前n个数据计算样例

    AscendC::Div(dstLocal, src0Local, src1Local, 512);
    // Div 0ulp
    static constexpr DivConfig config = { DivAlgo::DIFF_COMPENSATION };
    Div<T, config>(dstLocalX, srcLocalX, srcLocalY, calCount);
    // Div Subnormal
    static constexpr DivConfig config = { DivAlgo::PRECISION_0ULP_FTZ_FALSE };
    Div<T, config>(dstLocalX, srcLocalX, srcLocalY, calCount);
    
  • 整个tensor参与计算样例

    dstLocal = src0Local / src1Local;
    

结果示例如下:

输入数据src0Local:[1.0 2.0 3.0 ... 512.0]
输入数据src1Local:[2.0 2.0 2.0 ... 2.0]
输出数据dstLocal:[0.5 1.0 1.5 ... 256.0]