BlockReduceMax
产品支持情况
功能说明
对每个datablock内所有元素求最大值。归约指令的总体介绍请参考如何使用归约计算API。
函数原型
-
mask逐bit模式
template <typename T, bool isSetMask = true> __aicore__ inline void BlockReduceMax(const LocalTensor<T>& dst, const LocalTensor<T>& src,const int32_t repeatTime, const uint64_t mask[], const int32_t dstRepStride, const int32_t srcBlkStride, const int32_t srcRepStride) -
mask连续模式
template <typename T, bool isSetMask = true> __aicore__ inline void BlockReduceMax(const LocalTensor<T>& dst, const LocalTensor<T>& src,const int32_t repeatTime, const int32_t mask, const int32_t dstRepStride, const int32_t srcBlkStride, const int32_t srcRepStride)
参数说明
表 1 模板参数说明
|
Ascend 950PR/Ascend 950DT,支持的数据类型为:half/float Atlas A3 训练系列产品/Atlas A3 推理系列产品,支持的数据类型为:half/float |
|
|
表 2 参数说明
|
类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
||
|
类型为LocalTensor,支持的TPosition为VECIN/VECCALC/VECOUT。 |
||
|
||
目的操作数相邻迭代间的地址步长。以一个repeatTime归约后的长度为单位。 每个repeatTime(8个datablock)归约后,得到8个元素,所以输入类型为half类型时,RepStride单位为16Byte;输入类型为float类型时,RepStride单位为32Byte。 |
||
单次迭代内datablock的地址步长。详细说明请参考dataBlockStride。 |
||
源操作数相邻迭代间的地址步长,即源操作数每次迭代跳过的datablock数目。详细说明请参考repeatStride。 |
返回值说明
无
约束说明
-
操作数地址对齐要求请参见通用地址对齐约束。
-
为了节省地址空间,您可以定义一个Tensor,供源操作数与目的操作数同时使用(即地址重叠),需要注意计算后的目的操作数数据不能覆盖未参与计算的源操作数,需要谨慎使用。
-
针对不同场景合理使用归约指令可以带来性能提升,具体样例请参考ReduceCustom。
调用示例
本样例中只展示Compute流程中的部分代码。如果您需要运行样例代码,请将该代码段拷贝并替换样例模板中Compute函数的部分代码即可。
-
BlockReduceMax-tensor高维切分计算样例-mask连续模式
int32_t mask = 256/sizeof(half); int repeat = 1; // repeat = 1, 128 elements one repeat, 128 elements total // srcBlkStride = 1, no gap between blocks in one repeat // dstRepStride = 1, srcRepStride = 8, no gap between repeats AscendC::BlockReduceMax<half>(dstLocal, srcLocal, repeat, mask, 1, 1, 8); -
BlockReduceMax-tensor高维切分计算样例-mask逐bit模式
uint64_t mask[2] = { UINT64_MAX, UINT64_MAX }; int repeat = 1; // repeat = 1, 128 elements one repeat, 128 elements total // srcBlkStride = 1, no gap between blocks in one repeat // dstRepStride = 1, srcRepStride = 8, no gap between repeats AscendC::BlockReduceMax<half>(dstLocal, srcLocal, repeat, mask, 1, 1, 8);
结果示例如下:
输入数据src_gm:
[-8.781, 4.688, -0.09607, -5.445, 4.957, -4.832, 9.555, 8.391,
6.273, -2.412, 7.969, 3.9, -0.4238, 2.988, -6.855, -1.335,
...
9.68, -6.672, -6.488, -7.398, 8.562, 3.508, 3.135, -5.512,
-7.883, -8.594, -5.895, -8.938, -7.676, -7.867, -9.188, -5.715]
输出数据dst_gm:
[9.555, ..., 9.68, 0, ... 0]
样例模板
#include "kernel_operator.h"
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);
pipe.InitBuffer(inQueueSrc, 1, srcDataSize * sizeof(half));
pipe.InitBuffer(outQueueDst, 1, dstDataSize * sizeof(half));
}
__aicore__ inline void Process()
{
CopyIn();
Compute();
CopyOut();
}
private:
__aicore__ inline void CopyIn()
{
AscendC::LocalTensor<half> srcLocal = inQueueSrc.AllocTensor<half>();
AscendC::DataCopy(srcLocal, srcGlobal, srcDataSize);
inQueueSrc.EnQue(srcLocal);
}
__aicore__ inline void Compute()
{
AscendC::LocalTensor<half> srcLocal = inQueueSrc.DeQue<half>();
AscendC::LocalTensor<half> dstLocal = outQueueDst.AllocTensor<half>();
half zero(0);
AscendC::Duplicate(dstLocal, zero, dstDataSize);
//指令执行部分(替换成上述代码)
outQueueDst.EnQue<half>(dstLocal);
inQueueSrc.FreeTensor(srcLocal);
}
__aicore__ inline void CopyOut()
{
AscendC::LocalTensor<half> dstLocal = outQueueDst.DeQue<half>();
AscendC::DataCopy(dstGlobal, dstLocal, dstDataSize);
outQueueDst.FreeTensor(dstLocal);
}
private:
AscendC::TPipe pipe;
AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueSrc;
AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueueDst;
AscendC::GlobalTensor<half> srcGlobal, dstGlobal;
int srcDataSize = 128;
int dstDataSize = 64;
};
extern "C" __global__ __aicore__ void reduce_simple_kernel(__gm__ uint8_t* src, __gm__ uint8_t* dstGm)
{
KernelReduce op;
op.Init(src, dstGm);
op.Process();
}