文档
注册

CastDeq

函数功能

当isVecDeq=false时,根据SetDeqScale设置的scale、offset、signMode,对int16_t类型的输入做量化并进行精度转换,得到int8_t/uint8_t类型的数据。当需要返回有符号数时,signMode = true;返回无符号数时,signMode = false。计算公式如下:

当isVecDeq=true时,根据SetDeqScale设置的一段128B的UB上的16组量化参数scale0-scale15、offset0-offset15、signMode0-signMode15,以循环的方式对int16_t类型的输入做量化并进行精度转换,得到int8_t/uint8_t类型的数据。当需要返回有符号数时,signMode = true;返回无符号数时,signMode = false。计算公式如下:

函数原型

  • tensor前n个数据计算

    template <typename T1, typename T2, bool isVecDeq = false, bool halfBlock = true> __aicore__ inline void CastDeq(const LocalTensor<T1>& dstLocal, const LocalTensor<T2>& srcLocal, uint32_t calCount);

  • tensor高维切分计算
    • mask逐bit模式

      template <typename T1, typename T2, bool isSetMask = true, bool isVecDeq = true, bool halfBlock = true> __aicore__ inline void CastDeq(const LocalTensor<T1>& dstLocal, const LocalTensor<T2>& srcLocal, uint64_t mask[2], const uint8_t repeatTimes, const UnaryRepeatParams& repeatParams);

    • mask连续模式

      template <typename T1, typename T2, bool isSetMask = true, bool isVecDeq = true, bool halfBlock = true> __aicore__ inline void CastDeq(const LocalTensor<T1>& dstLocal, const LocalTensor<T2>& srcLocal, uint64_t mask, const uint8_t repeatTimes, const UnaryRepeatParams& repeatParams);

参数说明

图1 halfBlock说明
表1 模板参数说明

halfBlock

用于指示输出元素存放在上半还是下半Block。halfBlock=true时,结果存放在下半Block;halfBlock=false时,结果存放在上半Block,如图图1

T1

输出Tensor的数据类型,支持int8_t/uint8_t。

和SetDeqScale接口的signMode入参配合使用,当signMode=true时输出数据类型int8_t;signMode=false时输出数据类型uint8_t。

T2

输入Tensor的数据类型,支持int16_t。

isVecDeq

和SetDeqScale(const LocalTensor<T>& src)接口配合使用,当SetDeqScale接口传入Tensor时,isVecDeq必须为true。

isSetMask

是否在接口内部设置mask。

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

参数名

输入/输出

描述

dstLocal

输出

目的操作数。

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

Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型int8_t/uint8_t

Atlas推理系列产品(Ascend 310P处理器)AI Core,支持的数据类型为:int8_t/uint8_t/

srcLocal

输入

源操作数。

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

Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型int16_t

Atlas推理系列产品(Ascend 310P处理器)AI Core,支持的数据类型为:int16_t

mask

输入

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

  • 连续模式:表示前面连续的多少个元素参与计算。数据类型为uint64。取值范围和操作数的数据类型有关,数据类型不同,每次迭代内能够处理的元素个数最大值不同。当操作数为16位时,mask∈[1, 128];当操作数为32位时,mask∈[1, 64]。
  • 逐bit模式:可以按位控制哪些元素参与计算,bit位的值为1表示参与计算,0表示不参与。参数类型为长度为2的uint64_t类型数组。

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

    参数取值范围和操作数的数据类型有关,数据类型不同,每次迭代内能够处理的元素个数最大值不同。当操作数为16位时,mask[0]、mask[1]∈[0, 264-1]并且不同时为0;当操作数为32位时,mask[1]为0,mask[0]∈(0, 264-1];当操作数为64位时,mask[1]为0,mask[0]∈(0, 232-1]。

当源操作数和目的操作数位数不同时,以数据类型的字节较大的为准。例如,源操作数为half类型,目的操作数为int32_t类型,计算mask时以int32_t为准。

repeatTimes

输入

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

关于该参数的具体描述请参考通用参数说明

UnaryRepeatParams

输入

控制操作数地址步长的数据结构。结构体内包含操作数相邻迭代间相同block的地址步长,操作数同一迭代内不同block的地址步长等参数。

该数据结构的定义请参考UnaryRepeatParams

相邻迭代间的地址步长参数说明请参考Repeat stride(相邻迭代间相同datablock的地址步长);同一迭代内datablock的地址步长参数说明请参考Block stride(同一迭代内不同datablock的地址步长)

calCount

输入

输入数据元素个数。

返回值

支持的型号

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

Atlas推理系列产品(Ascend 310P处理器)AI Core

注意事项

  • repeatTimes∈[0,255]。
  • 每个repeat的并行度取决于数据精度、AI处理器型号,如s16->s8/u8转换每次迭代操作128个源/目的元素。
  • 为了节省地址空间,开发者可以定义一个Tensor,供源操作数与目的操作数同时使用(即地址重叠),相关约束如下 :
    • 对于单次repeat(repeatTimes=1),且源操作数与目的操作数之间要求100%完全重叠,不支持部分重叠。
    • 对于多次repeat(repeatTimes>1),若源操作数与目的操作数之间存在依赖,即第N次迭代的目的操作数是第N+1次的源操作数,这种情况是不支持地址重叠的。

调用示例

如果您需要运行样例代码,请将该代码段拷贝并替换样例模板中Compute函数的部分代码即可。

  • 高维切分计算接口样例-mask连续模式
    uint64_t mask = 256 / sizeof(int16_t);
    // repeatTimes = 2, 128 elements one repeat, 256 elements total
    // dstBlkStride, srcBlkStride = 1, no gap between blocks in one repeat
    // dstRepStride, srcRepStride = 8, no gap between repeats
    CastDeq<uint8_t, int16_t, true, true, true>(dstLocal, srcLocal, mask, 2, { 1, 1, 8, 8 });
  • 高维切分计算接口样例-mask逐bit模式
    uint64_t mask[2] = { UINT64_MAX, UINT64_MAX };
    // repeatTimes = 2, 128 elements one repeat, 256 elements total
    // dstBlkStride, srcBlkStride = 1, no gap between blocks in one repeat
    // dstRepStride, srcRepStride = 8, no gap between repeats
    CastDeq<uint8_t, int16_t, true, true, true>(dstLocal, srcLocal, mask, 8, { 1, 1, 8, 8 });
  • 前n个数计算接口样例
    CastDeq<uint8_t, int16_t, true, true>(dstLocal, srcLocal, 256);

结果示例如下:

输入数据(srcLocal): 
[20 53 26 12 36  6 20 93 66 30 56 99 59 92  7 37 22 47 98 10 85 29 14 46
 17 34 45 17 25 45 82 17 66 94 68 23 67  8 89  8 92  6 10 80 87 20  9 81
 70 62 11 58 38 83 32 14 38 47 41 63 94 26 96 89 88 35 86 55 60 82 15 65
 92 67 83 23 63 25 85 93 50 91 75 60 80 10 55 20 71 14 67 23 31 63  7 93
 69 45 61 23 43 86 11 81 81 36 76 58 53 25 23 51 59 78 82 10 39 40 24 50
 68 49 79 40  4 53 22 38 45 17 29 54  9 66 98 47 12 47 47 20 98  0 59 77
  1 21 39 70 66 20 68  8 77 77 54  0  3 33 37 37 48 60 83 88 27 70 31 49
 75 21 59  3 99 84 92 84 14 44 26 56 72 56 37 52 39 11  2 59 59 65 71 64
 10 65 62 48 42 79 69 69 27 99  8 38 36 77 34 34 60 50 52 50 41 31 95 68
 27 16 42 64 19 47  0 10 36 36 33 62 98 64 32 81 49 53 27 70 35  9 63  7
 10 89  3 39 94 23 89 16 23 60 71 42 46 58 65 90]
输出数据(dstLocal): 
[ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 20 53 26 12 36  6 20 93
 66 30 56 99 59 92  7 37  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 22 47 98 10 85 29 14 46 17 34 45 17 25 45 82 17  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0 66 94 68 23 67  8 89  8 92  6 10 80 87 20  9 81
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 70 62 11 58 38 83 32 14
 38 47 41 63 94 26 96 89  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 88 35 86 55 60 82 15 65 92 67 83 23 63 25 85 93  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0 50 91 75 60 80 10 55 20 71 14 67 23 31 63  7 93
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 69 45 61 23 43 86 11 81
 81 36 76 58 53 25 23 51  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 59 78 82 10 39 40 24 50 68 49 79 40  4 53 22 38  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0 45 17 29 54  9 66 98 47 12 47 47 20 98  0 59 77
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1 21 39 70 66 20 68  8
 77 77 54  0  3 33 37 37  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 48 60 83 88 27 70 31 49 75 21 59  3 99 84 92 84  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0 14 44 26 56 72 56 37 52 39 11  2 59 59 65 71 64
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 10 65 62 48 42 79 69 69
 27 99  8 38 36 77 34 34  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
 60 50 52 50 41 31 95 68 27 16 42 64 19 47  0 10  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0 36 36 33 62 98 64 32 81 49 53 27 70 35  9 63  7
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0 10 89  3 39 94 23 89 16
 23 60 71 42 46 58 65 90]

样例模板

为了方便您快速运行指令中的参考样例,本章节提供样例模板。

您可以将以下样例模板作为代码框架,只需将具体指令中的样例片段拷贝替换下文代码段中的加粗内容即可。
#include "kernel_operator.h"
using namespace AscendC;

template <typename srcType, typename dstType>
class KernelCastDeq {
public:
    __aicore__ inline KernelCastDeq() {}
    __aicore__ inline void Init(GM_ADDR src_gm, GM_ADDR dst_gm, uint32_t inputSize, bool halfBlock, bool isVecDeq)
    {
        srcSize = inputSize;
        dstSize = inputSize * 2;
        this->halfBlock = halfBlock;
        this->isVecDeq = isVecDeq;
        src_global.SetGlobalBuffer(reinterpret_cast<__gm__ srcType*>(src_gm), srcSize);
        dst_global.SetGlobalBuffer(reinterpret_cast<__gm__ dstType*>(dst_gm), dstSize);
        pipe.InitBuffer(inQueueX, 1, srcSize * sizeof(srcType));
        pipe.InitBuffer(outQueue, 1, dstSize * sizeof(dstType));
        pipe.InitBuffer(tmpQueue, 1, 128);
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }
private:
    __aicore__ inline void CopyIn()
    {
        LocalTensor<srcType> srcLocal = inQueueX.AllocTensor<srcType>();
        DataCopy(srcLocal, src_global, srcSize);
        inQueueX.EnQue(srcLocal);
    }
    __aicore__ inline void Compute()
    {
        LocalTensor<dstType> dstLocal = outQueue.AllocTensor<dstType>();
        LocalTensor<uint64_t> tmpBuffer = tmpQueue.AllocTensor<uint64_t>();
        Duplicate(tmpBuffer.ReinterpretCast<int32_t>(), static_cast<int32_t>(0), 32);
        PipeBarrier<PIPE_V>();
        Duplicate<int32_t>(dstLocal.template ReinterpretCast<int32_t>(), static_cast<int32_t>(0), dstSize / sizeof(int32_t));
        PipeBarrier<PIPE_ALL>();
        bool signMode = false;
        if constexpr (IsSameType<dstType, int8_t>::value) {
            signMode = true;
        }
        LocalTensor<srcType> srcLocal = inQueueX.DeQue<srcType>();
        if (halfBlock) {
            if (isVecDeq) {
                float vdeqScale[16] = { 1.0 };
                int16_t vdeqOffset[16] = { 0 };
                bool vdeqSignMode[16] = { signMode };
                VdeqInfo vdeqInfo(vdeqScale, vdeqOffset, vdeqSignMode);
                SetDeqScale(tmpBuffer, vdeqInfo);
                CastDeq<dstType, srcType, true, true>(dstLocal, srcLocal, srcSize);
            } else {
                float scale = 1.0;
                int16_t offset = 0;
                SetDeqScale(scale, offset, signMode);
                CastDeq<dstType, srcType, false, true>(dstLocal, srcLocal, srcSize);
            }
        } else {
            if (isVecDeq) {
                float vdeqScale[16] = { 1.0 };
                int16_t vdeqOffset[16] = { 0 };
                bool vdeqSignMode[16] = { signMode };
                VdeqInfo vdeqInfo(vdeqScale, vdeqOffset, vdeqSignMode);
                SetDeqScale(tmpBuffer, vdeqInfo);
                CastDeq<dstType, srcType, true, false>(dstLocal, srcLocal, srcSize);
            } else {
                float scale = 1.0;
                int16_t offset = 0;
                SetDeqScale(scale, offset, signMode);
                CastDeq<dstType, srcType, false, false>(dstLocal, srcLocal, srcSize);
            }
        }
        outQueue.EnQue<dstType>(dstLocal);
        tmpQueue.FreeTensor(tmpBuffer);
        inQueueX.FreeTensor(srcLocal);
    }
    __aicore__ inline void CopyOut()
    {
        LocalTensor<dstType> dstLocal = outQueue.DeQue<dstType>();
        DataCopy(dst_global, dstLocal, dstSize);
        outQueue.FreeTensor(dstLocal);
    }
private:
    GlobalTensor<srcType> src_global;
    GlobalTensor<dstType> dst_global;
    TPipe pipe;
    TQue<QuePosition::VECIN, 1> inQueueX;
    TQue<QuePosition::VECIN, 1> tmpQueue;
    TQue<QuePosition::VECOUT, 1> outQueue;
    bool halfBlock = false;
    bool isVecDeq = false;
    uint32_t srcSize = 0;
    uint32_t dstSize = 0;
};
template <typename srcType, typename dstType>
__aicore__ void kernel_cast_deqscale_operator(GM_ADDR src_gm, GM_ADDR dst_gm, uint32_t dataSize, bool halfBlock, bool isVecDeq)
{
    KernelCastDeq<srcType, dstType> op;
    op.Init(src_gm, dst_gm, dataSize, halfBlock, isVecDeq);
    op.Process();
}
搜索结果
找到“0”个结果

当前产品无相关内容

未找到相关内容,请尝试其他搜索词