BlockReduceMin

Applicability

Product

Supported/Unsupported

Atlas A3 training products / Atlas A3 inference products

Atlas A2 training products / Atlas A2 inference products

Atlas 200I/500 A2 inference products

Atlas inference product 's AI Core

Atlas inference product 's Vector Core

x

Atlas training products

Functions

Computes the minimum 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 BlockReduceMin(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 BlockReduceMin(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

Table 1 Parameters in the template

Parameter

Description

T

Operand data type.

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

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

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

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

For the Atlas training products , the supported data type is half.

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

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.

  • 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 in the computation.

    The mask is in array form. The array length and the value range of the array elements are related to the data type of the operand. When the operand is 16-bit, the array length is 2. In this case, mask[0] and mask[1] must be in the range of [0, 264 – 1] and cannot be 0 at the same time. When the operand is 32-bit, the array length is 1. In this case, mask[0] must be in the range of (0, 264 – 1]. When the operand is 64-bit, the array length is 1. In this case, mask[0] must be in the range of (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 repeat 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].

dstRepStride

Input

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

After each repeat (eight data blocks), eight elements are obtained. Therefore, when the source operand is of the half type, the unit of RepStride is 16 bytes; when the source operand is of the float type, the unit of RepStride is 32 bytes.

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

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

  • 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 minimum value of all elements in the data block is filled with inf to return. 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 minimum value in the last four data blocks. In the half scenario, 65504 is returned for the minimum value.
  • Proper use of the reduction instruction in different scenarios can improve performance. For details about the introduction, see Using the Reduction Instruction Properly in Different Scenarios. For details about examples, see ReduceCustom.

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 BlockReduceMin – high-dimensional tensor sharding computation (contiguous mask mode)
    1
    2
    3
    4
    5
    6
    int32_t mask = 256/sizeof(half);
    int repeat = 1;
    // repeatTime = 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::BlockReduceMin<half>(dstLocal, srcLocal, repeat, mask, 1, 1, 8);
    
  • Example of BlockReduceMin – 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;
    // repeatTime = 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::BlockReduceMin<half>(dstLocal, srcLocal, repeat, mask, 1, 1, 8);
    
Result example:
Input (src_gm):
[3.902, -8.719, -5.797, -7.969, -9.516, -6.457, 0.1114, 2.781,
 4.758, 0.01262, 2.367, 2.16, -4.473, -9.336, -7.375, 6.078, 
 ... 
 3.621, -5.852, 6.902, 0.332, 1.112, 2.697, -9, 9.938, 
 4.645, 0.7021, 7.598, 4.586, 0.6431, 4.781, 3.566, 4.004]  
Output (dst_gm):
[-9.516, ..., -9, 0, ..., 0]