BlockReduceSum
Applicability
|
Product |
Supported/Unsupported |
|---|---|
|
|
√ |
|
|
√ |
|
|
√ |
|
|
√ |
|
|
x |
|
|
√ |
Functions
Sums all elements in each data block. Source operands are added in binary tree mode. For details about reduction instructions, see How to Use Reduction Compute APIs.
To sum up 128 data elements of the half type, sum up 8 data blocks, with each data block containing 16 data elements of the half type. In each data block, data is added in binary tree mode. The following figure shows the schematic diagram of BlockReduceSum.
When being greater than 65504, the computation result is truncated to 65504. For example, the source operand is [60000, 60000, –30000, 100], 60000 + 60000 > 65504, meaning that the result overflows. In this case, the maximum value 65504 will be used as the result. Similarly, –30000 + 100 = –29900, 65504 – 29900 = 35604. The following figure shows the computation.
Prototype
- Mask bit-by-bit mode
1 2
template <typename T, bool isSetMask = true> __aicore__ inline void BlockReduceSum(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 BlockReduceSum(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 the 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 |
The mask parameter is used to control the elements involved in computation in each iteration.
|
|
dstRepStride |
Input |
Address stride between adjacent iterations of the destination operand. The stride is measured in the length of the result after one repeatTime reduction. 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 Product , if mask/mask[] is configured and no element in a data block is involved in the computation, the sum of all elements in the data block is filled with 0 to return. For example, in the float scenario, if mask is set to 32, that is, only the first four data blocks are computed, 0 is returned for the sum of the last four data blocks.
Examples
- 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 BlockReduceSum – 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::BlockReduceSum<half>(dstLocal, srcLocal, repeat, mask, 1, 1, 8);
- Example of BlockReduceSum – 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::BlockReduceSum<half>(dstLocal, srcLocal, repeat, mask, 1, 1, 8);
Result example:Input (src_gm): [-7.289, 4.48, -5.898, -6.199, 1.422, -6.168, -3.178, -1.198, 7.789, 6.754, -5.191, -0.6797, 2.883, 2.08, 8.664, -8.539, ..., -7.625, 2.529, 7.855, -2.012, -6.52, -6.652, -8.422, -9.914, -4.355, 1.849, 5.406, 1.483, -6.074, -1.897, 8.625, 1.969] Output (dst_gm): [-10.27, ..., -23.77, 0, ..., 0]
- Example of BlockReduceSum – high-dimensional tensor sharding computation (contiguous mask mode)