ReduceSum
函数功能
对所有的输入数据求和。
函数原型
- tensor前n个数据计算
template <typename T> __aicore__ inline void ReduceSum(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, const LocalTensor<T>& workLocal, const int32_t count)
 - tensor高维切分计算
- mask逐bit模式
template <typename T> __aicore__ inline void ReduceSum(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, const LocalTensor<T>& workLocal, const uint64_t mask[2], const int32_t repeatTimes, const int32_t srcRepStride)
 - mask连续模式
template <typename T> __aicore__ inline void ReduceSum(const LocalTensor<T>& dstLocal, const LocalTensor<T>& srcLocal, const LocalTensor<T>& workLocal, const int32_t mask, const int32_t repeatTimes, const int32_t srcRepStride)
 
 - mask逐bit模式
 
参数说明
参数名称  | 
输入/输出  | 
含义  | 
|---|---|---|
dstLocal  | 
输出  | 
目的操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 Atlas 训练系列产品,支持的数据类型为:half Atlas推理系列产品AI Core,支持的数据类型为:half/float Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:half/float Atlas 200I/500 A2推理产品,支持的数据类型为:half/float  | 
srcLocal  | 
输入  | 
源操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 源操作数的数据类型需要与目的操作数保持一致。 Atlas 训练系列产品,支持的数据类型为:half Atlas推理系列产品AI Core,支持的数据类型为:half/float Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:half/float Atlas 200I/500 A2推理产品,支持的数据类型为:half/float  | 
workLocal  | 
输入  | 
指令执行期间用于存储中间结果,用于内部计算所需操作空间,需特别注意空间大小,参见约束说明。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 源操作数的数据类型需要与目的操作数保持一致。 Atlas 训练系列产品,支持的数据类型为:half Atlas推理系列产品AI Core,支持的数据类型为:half/float Atlas A2训练系列产品/Atlas 800I A2推理产品,支持的数据类型为:half/float Atlas 200I/500 A2推理产品,支持的数据类型为:half/float  | 
count  | 
输入  | 
输入数据元素个数。 参数取值范围和操作数的数据类型有关,数据类型不同,能够处理的元素个数最大值不同,但是最大处理的数据量不会超过UB大小限制。  | 
mask  | 
输入  | 
mask用于控制每次迭代内参与计算的元素。 
  | 
repeatTimes  | 
输入  | 
重复迭代次数。 矢量计算单元,每次读取连续的256 Bytes数据进行计算,为完成对输入数据的处理,必须通过多次迭代(repeat)才能完成所有数据的读取与计算。repeatTimes表示迭代的次数。 关于该参数的具体描述请参考重复迭代次数-Repeat times。  | 
srcRepStride  | 
输入  | 
相邻迭代间,源操作数相同block地址步长,即源操作数每次迭代跳过的block数目。详细说明请参考相邻迭代间相同datablock的地址步长。  | 
返回值
无
支持的型号
Atlas 训练系列产品
Atlas推理系列产品AI Core
Atlas A2训练系列产品/Atlas 800I A2推理产品
Atlas 200I/500 A2推理产品
约束说明
- 为了节省地址空间,开发者可以定义一个Tensor,供srcLocal、dstLocal、workLocal同时使用(即地址重叠),地址重叠的约束条件如下:
- 若源操作数与目的操作数之间存在依赖,即第N次迭代的目的操作数是第N+1次的源操作数,这种情况是不支持地址重叠的。
 - srcLocal或者dstLocal与workLocal地址重叠时,workLocal必须满足最小所需空间要求,否则不支持地址重叠。
 - 各操作数地址如重叠则要求100%完全重叠,不支持部分重叠。
 
 
- ReduceSum的相加方式分为两种:
- 方式一:同一repeat内先按照二叉树累加、不同repeat的结果也按照二叉树累加。
假设源操作数为128个float16的数据[data0,data1,data2...data127],一个repeat可以计算完,计算过程如下。
- data0和data1相加得到data00,data2和data3相加得到data01...data124和data125相加得到data62,data126和data127相加得到data63;
 - data00和data01相加得到data000,data02和data03相加得到data001...data62和data63相加得到data031;
 - 以此类推,得到目的操作数为1个float16的数据[data]。
 
需要注意的是两两相加的计算过程中,计算结果大于65504时结果保存为65504。例如源操作数为[60000,60000,-30000,100],首先60000+60000溢出,结果为65504,第二步计算-30000+100=-29900,第四步计算65504-29900=35604。
 - 方式二:同一repeat内采用二叉树累加,不同repeat的结果按顺序累加。
 
 - 方式一:同一repeat内先按照二叉树累加、不同repeat的结果也按照二叉树累加。
 - 不同硬件形态对应的ReduceSum相加方式如下:
Atlas推理系列产品AI Core采用方式一
Atlas A2训练系列产品/Atlas 800I A2推理产品tensor前n个数据计算接口采用方式二,tensor高维切分计算接口采用方式一
Atlas 200I/500 A2推理产品采用方式一
 - workLocal支持两种处理方式:
- 方式一:按照如下计算公式计算最小所需空间:
// 先定义一个向上取整函数 int RoundUp(int a, int b) { return (a + b - 1) / b; } // 然后定义参与计算的数据类型 int typeSize = 2; // half类型为2Bytes,float类型为4Bytes,按需填入 // 再根据数据类型定义两个单位 int elementsPerBlock = 32 / typeSize; // 1个block存放的元素个数 int elementsPerRepeat = 256 / typeSize; // 1次repeat可以处理的元素个数 // 最后确定首次最大repeat值 int firstMaxRepeat = repeatTimes; // 此处需要注意:对于tensor高维切分计算接口,firstMaxRepeat就是repeatTimes;对于tensor前n个数据计算接口,firstMaxRepeat为count/elementsPerRepeat,比如在half类型下firstMaxRepeat就是count/128,在float类型下为count/64,按需填入,对于count<elementsPerRepeat的场景,firstMaxRepeat就是1 int iter1OutputCount = firstMaxRepeat; // 第一轮操作产生的元素个数 int iter1AlignEnd = RoundUp(iter1OutputCount, elementsPerBlock) * elementsPerBlock; // 第一轮产生的元素个数做向上取整 int finalWorkLocalNeedSize = iter1AlignEnd; // 最终workLocal所需的elements空间大小就是第一轮操作产生元素做向上取整后的结果 - 方式二:传入任意大小的workLocal,workLocal的值不会被改变
 
 - 方式一:按照如下计算公式计算最小所需空间:
 - 不同硬件形态对应的WorkLocal处理方式如下:
Atlas推理系列产品AI Core采用方式一
Atlas A2训练系列产品/Atlas 800I A2推理产品tensor前n个数据计算接口采用方式二,tensor高维切分计算接口采用方式一
Atlas 200I/500 A2推理产品采用方式一
 
调用示例
- tensor高维切分计算样例-mask连续模式
// dstLocal,srcLocal和workLocal均为half类型,srcLocal的计算数据量为8320,并且连续排布,使用tensor高维切分计算接口,设定repeatTimes为65,mask为全部元素参与计算 uint64_t mask = 128; ReduceSum<half>(dstLocal, srcLocal, workLocal, mask, 65, 8);
 - tensor高维切分计算样例-mask逐bit模式
// dstLocal,srcLocal和workLocal均为half类型,srcLocal的计算数据量为8320,并且连续排布,使用tensor高维切分计算接口,设定repeatTimes为65,mask为全部元素参与计算 uint64_t mask[2] = { 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF }; ReduceSum<half>(dstLocal, srcLocal, workLocal, mask, 65, 8); - tensor前n个数据计算样例
// dstLocal,srcLocal和workLocal均为half类型,srcLocal的计算数据量为8320,并且连续排布,使用tensor前n个数据计算接口 ReduceSum<half>(dstLocal, srcLocal, workLocal, 8320);
 
- 完整样例如下:
 
tensor高维切分计算接口完整调用示例一:
#include "kernel_operator.h"
namespace AscendC {
class KernelReduce {
public:
    __aicore__ inline KernelReduce() {}
    __aicore__ inline void Init(__gm__ uint8_t* src, __gm__ uint8_t* dstGm)
    {
        srcGlobal.SetGlobalBuffer((__gm__ half*)src);
        dstGlobal.SetGlobalBuffer((__gm__ half*)dstGm);
        repeat = srcDataSize / mask;
        pipe.InitBuffer(inQueueSrc, 1, srcDataSize * sizeof(half));
        pipe.InitBuffer(workQueue, 1, 80 * sizeof(half)); // 此处按照公式计算所需的最小work空间为80,也就是160Bytes
        pipe.InitBuffer(outQueueDst, 1, dstDataSize * sizeof(half));
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }
private:
    __aicore__ inline void CopyIn()
    {
        LocalTensor<half> srcLocal = inQueueSrc.AllocTensor<half>();
        DataCopy(srcLocal, srcGlobal, srcDataSize);
        inQueueSrc.EnQue(srcLocal);
    }
    __aicore__ inline void Compute()
    {
        LocalTensor<half> srcLocal = inQueueSrc.DeQue<half>();
        LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>();
        LocalTensor<half> workLocal = workQueue.AllocTensor<half>();
        // level0
        ReduceSum<half>(dstLocal, srcLocal, workLocal, mask, repeat, repStride);
        outQueueDst.EnQue<half>(dstLocal);
        inQueueSrc.FreeTensor(srcLocal);
        workQueue.FreeTensor(workLocal);
    }
    __aicore__ inline void CopyOut()
    {
        LocalTensor<half> dstLocal = outQueueDst.DeQue<half>();
        DataCopy(dstGlobal, dstLocal, dstDataSize);
        outQueueDst.FreeTensor(dstLocal);
    }
private:
    TPipe pipe;
    TQue<QuePosition::VECIN, 1> inQueueSrc;
    TQue<QuePosition::VECOUT, 1> workQueue;
    TQue<QuePosition::VECOUT, 1> outQueueDst;
    GlobalTensor<half> srcGlobal, dstGlobal;
    int srcDataSize = 8320;
    int dstDataSize = 16;
    int mask = 128;
    int repStride = 8;
    int repeat = 0;
};
} // namespace AscendC
示例结果 
输入数据(src_gm):
[1. 1. 1. ... 1. 1. 1.]
输出数据(dst_gm):
[8320.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
    0.    0.    0.    0.]
完整调用示例二:
#include "kernel_operator.h"
namespace AscendC {
class KernelReduce {
public:
    __aicore__ inline KernelReduce() {}
    __aicore__ inline void Init(__gm__ uint8_t* src, __gm__ uint8_t* dstGm)
    {
        srcGlobal.SetGlobalBuffer((__gm__ half*)src);
        dstGlobal.SetGlobalBuffer((__gm__ half*)dstGm);
        repeat = srcDataSize / mask;
        pipe.InitBuffer(inQueueSrc, 1, srcDataSize * sizeof(half));
        pipe.InitBuffer(workQueue, 1, 16 * sizeof(half)); // 此处按照公式计算所需的最小work空间为16,也就是32Bytes
        pipe.InitBuffer(outQueueDst, 1, dstDataSize * sizeof(half));
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }
private:
    __aicore__ inline void CopyIn()
    {
        LocalTensor<half> srcLocal = inQueueSrc.AllocTensor<half>();
        DataCopy(srcLocal, srcGlobal, srcDataSize);
        inQueueSrc.EnQue(srcLocal);
    }
    __aicore__ inline void Compute()
    {
        LocalTensor<half> srcLocal = inQueueSrc.DeQue<half>();
        LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>();
        LocalTensor<half> workLocal = workQueue.AllocTensor<half>();
        // level2
        ReduceSum<half>(dstLocal, srcLocal, workLocal, srcDataSize);
        outQueueDst.EnQue<half>(dstLocal);
        inQueueSrc.FreeTensor(srcLocal);
        workQueue.FreeTensor(workLocal);
    }
    __aicore__ inline void CopyOut()
    {
        LocalTensor<half> dstLocal = outQueueDst.DeQue<half>();
        DataCopy(dstGlobal, dstLocal, dstDataSize);
        outQueueDst.FreeTensor(dstLocal);
    }
private:
    TPipe pipe;
    TQue<QuePosition::VECIN, 1> inQueueSrc;
    TQue<QuePosition::VECOUT, 1> workQueue;
    TQue<QuePosition::VECOUT, 1> outQueueDst;
    GlobalTensor<half> srcGlobal, dstGlobal;
    int srcDataSize = 288;
    int dstDataSize = 16;
    int mask = 128;
    int repStride = 8;
    int repeat = 0;
};
} // namespace AscendC
输入数据(src_gm):
[1. 1. 1. ... 1. 1. 1.]
输出数据(dst_gm):
[288.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
   0.   0.]