BlockReduceMax
Applicability
|
Product |
Supported |
|---|---|
|
|
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
√ |
Function
Computes the maximum of all elements in each data block. For details about reduction instructions, see How to Use Reduction Compute APIs.
Prototype
- Bitwise mask mode
1 2
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)
- Contiguous mask mode
1 2
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)
Parameters
|
Parameter |
Description |
|---|---|
|
T |
Operand data type. For the For the For For the For the |
|
isSetMask |
Indicates whether to set mask inside the API.
|
|
Parameter |
Input/Output |
Description |
|---|---|---|
|
dst |
Output |
Destination operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. The start address of the LocalTensor must be 16-byte aligned (for data of the half type) or 32-byte aligned (for data of the float type). |
|
src |
Input |
Source operand. The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT. The start address of the LocalTensor must be 32-byte aligned. |
|
repeatTime |
Input |
Number of iteration repeats. The value range is [0, 255]. For details about this parameter, see High-dimensional Sharding APIs. |
|
mask/mask[] |
Input |
mask is used to control the elements that participate in computation in each iteration.
|
|
dstRepStride |
Input |
Address stride between adjacent iterations of the destination operand. The unit is the length of one reduced repeat. After each repeat (eight data blocks) is reduced, eight elements are obtained. Therefore, when the input type is half, the unit of RepStride is 16 bytes. When the input type is float, the unit of RepStride is 32 bytes. Note that this parameter cannot be set to 0 for the |
|
srcBlkStride |
Input |
Address stride of data blocks in a single iteration. For details, see dataBlockStride. |
|
srcRepStride |
Input |
Address stride between adjacent iterations of the source operand, that is, the number of data blocks skipped of the source operand in each iteration. For details, see repeatStride. |
Returns
None
Restrictions
- For details about the operand address alignment requirements, see General Address Alignment Restrictions.
- To save memory space, you can define a tensor shared by the source and destination operands (by address overlapping). Note that the computed destination operand data cannot overwrite the source operands that are not involved in the computation. Exercise caution when defining the tensor.
- For the
Atlas 200I/500 A2 inference products , if mask/mask[] is configured and no element in a data block is involved in the computation, the maximum value of all elements in the data block is filled with -inf and returned. For example, in the float scenario, if mask is set to 32, that is, only the first four data blocks are computed, -inf is returned for the maximum value in the last four data blocks. In the half scenario, -65504 is returned. - Proper use of reduction instructions in different scenarios can improve performance. For details, see Selecting Low-Latency Instructions to Optimize Reduction Operation Performance. For details about examples, see ReduceCustom.
Example
This example shows only part of the code used in the computation process (Compute). To run the sample code, copy the code snippet and replace parts of code of the Compute function in Template Samples.
- Example of BlockReduceMax – high-dimensional tensor sharding computation (contiguous mask mode)
1 2 3 4 5 6
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);
- Example of BlockReduceMax – high-dimensional tensor sharding computation (bitwise mask mode)
1 2 3 4 5 6
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);
Input (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] Output (dst_gm): [9.555, ..., 9.68, 0, ... 0]
Template Sample
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#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); // Command execution part (replace with the preceding code) 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(); } |