WholeReduceSum

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product / Atlas A3 inference product

Atlas A2 training product / Atlas A2 inference product

Atlas 200I/500 A2 inference product

Atlas inference product AI Core

Atlas inference product Vector Core

x

Atlas training product

Function Usage

Sums all data in each repeat. 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 WholeReduceSum(const LocalTensor<T>& dst, const LocalTensor<T>& src, const uint64_t mask[], const int32_t repeatTime, 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 WholeReduceSum(const LocalTensor<T>& dst, const LocalTensor<T>& src, const int32_t mask, const int32_t repeatTime, const int32_t dstRepStride, const int32_t srcBlkStride, const int32_t srcRepStride)
    
  • Bitwise mask mode
    1
    2
    template <typename T, bool isSetMask = true, typename U = T>
    __aicore__ inline void WholeReduceSum(const LocalTensor<U>& dst, const LocalTensor<T>& src, const uint64_t mask[], const int32_t repeatTime, const int32_t dstRepStride, const int32_t srcBlkStride, const int32_t srcRepStride)
    
  • Contiguous mask mode
    1
    2
    template <typename T, bool isSetMask = true, typename U = T>
    __aicore__ inline void WholeReduceSum(const LocalTensor<U>& dst, const LocalTensor<T>& src, const int32_t mask, const int32_t repeatTime, const int32_t dstRepStride, const int32_t srcBlkStride, const int32_t srcRepStride)
    

Parameters

Table 1 Parameters in the template

Parameter

Description

T

Data type of the source operand.

U

Data type of the destination operand.

isSetMask

Indicates whether to set mask inside the API.

  • true: sets mask inside the API.
  • false: sets mask outside the API. Developers need to use the SetVectorMask API to set the mask value. In this mode, the mask value in the input parameter of this API must be set to the placeholder MASK_PLACEHOLDER.
Table 2 Parameters

Parameter

Input/Output

Meaning

dst

Output

Destination operand.

The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT.

The start address of the LocalTensor must be 2-byte aligned (for data of the half type) or 4-byte aligned (for data of the float type).

For the Atlas 350 Accelerator Card, the supported data types are uint32_t, int32_t, half, and float.

For the Atlas A3 training product / Atlas A3 inference product , the supported data types are half and float.

For the Atlas A2 training product / Atlas A2 inference product , the supported data types are half and float.

For the Atlas 200I/500 A2 inference product , the supported data types are half and float.

For the Atlas inference product AI Core, the supported data types are half and float.

For the Atlas training product , the supported data types are half and float.

src

Input

Source operand.

The type is LocalTensor, and TPosition can be VECIN, VECCALC, or VECOUT.

The start address of LocalTensor must be 32-byte aligned.

The source operand must have the same data type as the destination operand.

For the Atlas 350 Accelerator Card, when the data type of src is uint16_t or int16_t, the data type of dst is uint32_t or int32_t. In other cases, the data type of dst is the same as that of src.

For the Atlas 350 Accelerator Card, the supported data types are uint16_t, int16_t, uint32_t, int32_t, half, and float.

For the Atlas A3 training product / Atlas A3 inference product , the supported data types are half and float.

For the Atlas A2 training product / Atlas A2 inference product , the supported data types are half and float.

For the Atlas 200I/500 A2 inference product , the supported data types are half and float.

For the Atlas inference product AI Core, the supported data types are half and float.

For the Atlas training product , the supported data types are half and float.

mask/mask[]

Input

mask controls the elements that participate in computation in each iteration.

  • Bitwise mode: controls the elements that participate in computation by bit. If a bit is set to 1, the corresponding element participates in the computation. If a bit is set to 0, the corresponding element is masked from the computation.

    The mask value is an array. The array length and the value range of the array elements are related to the operand data type. When the operand is 16-bit, the array length is 2, with mask[0] and mask[1] each in the range [0, 264 – 1], and they cannot both be 0 at the same time. When the operand is 32-bit, the array length is 1, with mask[0] in the range (0, 264 – 1]. When the operand is 64-bit, the array length is 1, with mask[0] in the range (0, 232 – 1].

    For example, if mask = [0, 8] and 8 = 0b1000, only the fourth element participates in computation.

  • Contiguous mode: indicates the number of contiguous elements that participate in computation. The value range is related to the operand data type. The maximum number of elements that can be processed in each iteration varies according to the data type. When the operand is 16-bit, mask ∈ [1, 128]. When the operand is 32-bit, mask ∈ [1, 64]. When the operand is 64-bit, mask ∈ [1, 32].

repeatTime

Input

Number of iteration repeats. The value range is [0, 255].

For details about this parameter, see High-dimensional Sharding APIs.

dstRepStride

Input

Address stride between adjacent iterations of the destination operand. The unit is the length after reduction of a repeat.

The unit is the byte length of the dst data type. For example, when dst is of the half type, the unit is 2 bytes.

Note that this parameter cannot be set to 0 for the Atlas training product .

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 from the source operand in each iteration. For details, see repeatStride.

Returns

None

Restrictions

  • WholeReduceSum uses the binary tree mode to implement internal adding.

    Assume that the source operand is 128 data elements of the half type [data0, data1, data2, ..., data127], the computation can be completed in one repeat. The computation process is as follows:

    1. Add data0 and data1 to obtain data00, add data2 and data3 to obtain data01, ..., add data124 and data125 to obtain data62, and add data126 and data127 to obtain data63.
    2. Add data00 and data01 to obtain data000, add data02 and data03 to obtain data001, ..., and add data62 and data63 to obtain data031.
    3. By analogy, the destination operand is one data element of the half type ([data]).

    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.

Examples

  • Example of high-dimensional tensor sharding computation (contiguous mask mode)
    1
    2
    3
    // Both dstLocal and srcLocal are of the half type. The computation data volume of srcLocal is set to 512. The data is continuously arranged, and so is the computation result. The high-dimensional tensor sharding computation API is used. mask indicates that a maximum of 128 elements are involved for computation.
    // Based on the preceding information, repeatTime is 4, dstRepStride is 1, srcBlkStride is 1, and srcRepStride is 8.
    AscendC::WholeReduceSum<half>(dstLocal, srcLocal, 128, 4, 1, 1, 8); 
    
  • Example of high-dimensional tensor sharding computation (bitwise mask mode)
    1
    2
    3
    4
    5
    // Both dstLocal and srcLocal are of the half type. For srcLocal, the computation data is of size 512 and is continuously arranged. Its computation result is also continuously arranged. It uses the high-dimensional tensor sharding computation API. mask is set to 128, indicating that all elements are involved in the computation.
    uint64_t mask[2] = { 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF };
    
    // Based on the preceding information, repeatTime is 4, dstRepStride is 1, srcBlkStride is 1, and srcRepStride is 8.
    AscendC::WholeReduceSum<half>(dstLocal, srcLocal, mask, 4, 1, 1, 8);
    
  • Proper use of the reduction instruction in different scenarios can improve performance. For details about the introduction, see Selecting Low-Latency Instructions to Optimize Reduction Operation Performance. For details about examples, see ReduceCustom.