产品 |
是否支持 |
|---|---|
Ascend 950PR/Ascend 950DT |
√ |
√ |
|
√ |
|
√ |
|
√ |
|
x |
|
√ |
对所有的输入数据求和。归约指令的总体介绍请参考如何使用归约计算API。
假设源操作数为128个half类型的数据[data0,data1,data2...data127],一个repeat可以计算完,计算过程如下。
需要注意的是两两相加的计算过程中,计算结果大于65504时结果保存为65504。例如源操作数为[60000,60000,-30000,100],首先60000+60000溢出,结果为65504,第二步计算-30000+100=-29900,第四步计算65504-29900=35604。
不同硬件形态对应的ReduceSum相加方式如下:
Ascend 950PR/Ascend 950DT 采用方式一
1 2 | template <typename T, bool isSetMask = true> __aicore__ inline void ReduceSum(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& sharedTmpBuffer, const int32_t count) |
1 2 | template <typename T> __aicore__ inline void ReduceSum(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& sharedTmpBuffer, const uint64_t mask[], const int32_t repeatTime, const int32_t srcRepStride) |
1 2 | template <typename T> __aicore__ inline void ReduceSum(const LocalTensor<T>& dst, const LocalTensor<T>& src, const LocalTensor<T>& sharedTmpBuffer, const int32_t mask, const int32_t repeatTime, const int32_t srcRepStride) |
参数名 |
描述 |
|---|---|
T |
操作数数据类型。 Ascend 950PR/Ascend 950DT,支持的数据类型为:half/float/uint64_t/int64_t |
isSetMask |
是否在接口内部设置mask,默认值为true。
|
参数名称 |
输入/输出 |
含义 |
|---|---|---|
dst |
输出 |
目的操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要保证2字节对齐(针对half数据类型),4字节对齐(针对float数据类型)。 |
src |
输入 |
源操作数。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 源操作数的数据类型需要与目的操作数保持一致。 |
sharedTmpBuffer |
输入 |
指令执行期间用于存储中间结果,用于内部计算所需操作空间,具体计算方法可参考下文中sharedTmpBuffer大小计算的介绍。 类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 LocalTensor的起始地址需要32字节对齐。 数据类型需要与目的操作数保持一致。 |
count |
输入 |
参与计算的元素个数。 参数取值范围和操作数的数据类型有关,数据类型不同,能够处理的元素个数最大值不同,最大处理的数据量不能超过UB大小限制。 |
mask/mask[] |
输入 |
mask用于控制每次迭代内参与计算的元素。
|
repeatTime |
输入 |
迭代次数。保证不超过int32_t最大值的范围即可。 |
srcRepStride |
输入 |
源操作数相邻迭代间的地址步长,即源操作数每次迭代跳过的datablock数目。详细说明请参考repeatStride。 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // 先定义一个向上取整函数 int RoundUp(int a, int b) { return (a + b - 1) / b; } // 然后定义参与计算的数据类型 int typeSize = 2; // half类型为2Bytes,float类型为4Bytes,按需填入 // 再根据数据类型定义两个单位 int elementsPerBlock = 32 / typeSize; // 1个datablock存放的元素个数 int elementsPerRepeat = 256 / typeSize; // 1次repeat可以处理的元素个数 // 最后确定首次最大repeat值 int firstMaxRepeat = repeatTime; // 此处需要注意:对于tensor高维切分计算接口,firstMaxRepeat就是repeatTime;对于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; // 最终sharedTmpBuffer所需的elements空间大小就是第一轮操作产生元素做向上取整后的结果 |
无
1 2 3 | // dstLocal,srcLocal和sharedTmpBuffer均为half类型,srcLocal的计算数据量为8320,并且连续排布,使用tensor高维切分计算接口,设定repeatTime为65,mask为全部元素参与计算 int32_t mask = 128; AscendC::ReduceSum<half>(dstLocal, srcLocal, sharedTmpBuffer, mask, 65, 8); |
1 2 3 | // dstLocal,srcLocal和sharedTmpBuffer均为half类型,srcLocal的计算数据量为8320,并且连续排布,使用tensor高维切分计算接口,设定repeatTime为65,mask为全部元素参与计算 uint64_t mask[2] = { 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF }; AscendC::ReduceSum<half>(dstLocal, srcLocal, sharedTmpBuffer, mask, 65, 8); |
1 2 | // dstLocal,srcLocal和sharedTmpBuffer均为half类型,srcLocal的计算数据量为8320,并且连续排布,使用tensor前n个数据计算接口 AscendC::ReduceSum<half>(dstLocal, srcLocal, sharedTmpBuffer, 8320); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include "kernel_operator.h" int srcDataSize = 8320; int dstDataSize = 16; int mask = 128; int repStride = 8; int repeat = srcDataSize / mask; // 这里是65 // 初始化srcLocal 、dstLocal 、sharedTmpBuffer AscendC::LocalTensor<half> srcLocal = inQueueSrc.DeQue<half>(); AscendC::LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>(); AscendC::LocalTensor<half> sharedTmpBuffer = workQueue.AllocTensor<half>(); // mask为128 一次计算128个元素,65次repeat计算完8320个数 AscendC::ReduceSum<half>(dstLocal, srcLocal, sharedTmpBuffer, mask, repeat, repStride); // 释放Tensor outQueueDst.EnQue<half>(dstLocal); inQueueSrc.FreeTensor(srcLocal); workQueue.FreeTensor(sharedTmpBuffer); |
示例结果如下:
输入数据(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.]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include "kernel_operator.h" int srcDataSize = 288; // 初始化srcLocal 、dstLocal 、sharedTmpBuffer AscendC::LocalTensor<half> srcLocal = inQueueSrc.DeQue<half>(); AscendC::LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>(); AscendC::LocalTensor<half> sharedTmpBuffer = workQueue.AllocTensor<half>(); // level2接口计算前288个数,计算前288个数的和 AscendC::ReduceSum<half>(dstLocal, srcLocal, sharedTmpBuffer, srcDataSize); // 释放Tensor outQueueDst.EnQue<half>(dstLocal); inQueueSrc.FreeTensor(srcLocal); workQueue.FreeTensor(sharedTmpBuffer); |
示例结果如下:
输入数据(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.]