WholeReduceMin

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

Computes the minimum value and index of all data in each repeat. The returned index value is the internal index of 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 WholeReduceMin(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, ReduceOrder order = ReduceOrder::ORDER_VALUE_INDEX)
    
  • Contiguous mask mode:
    1
    2
    template <typename T, bool isSetMask = true>
    __aicore__ inline void WholeReduceMin(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, ReduceOrder order = ReduceOrder::ORDER_VALUE_INDEX)
    

Parameters

Table 1 Parameters in the template

Parameter

Description

T

Operand data type.

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 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

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 4-byte aligned (for data of the half type) or 8-byte aligned (for data of the float type).

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.

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.

When the index and the maximum/minimum value are returned, the unit is twice the length of the data type of dst. For example, when dst is of the half type, the unit is 4 bytes.

When only the maximum or minimum value is returned, the unit is the length of the data type of dst.

When only the index is returned, the unit is the length of the data type of uint32_t.

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.

order

Input

Storage order of the value and index in dst and the result return behavior. The value is of the ReduceOrder type and defaults to ORDER_VALUE_INDEX. The values are as follows:

  • ORDER_VALUE_INDEX: indicates that the value is in the lower half. The storage sequence of returns is [value, index].
  • ORDER_INDEX_VALUE: indicates that the index is in the lower half. The storage sequence of returns is [index, value].
  • ORDER_ONLY_VALUE: indicates that only the maximum value is returned. The storage sequence of returns is [value].
  • ORDER_ONLY_INDEX: indicates that only the index of the maximum value is returned. The storage sequence of returns is [value].

For the Atlas 350 Accelerator Card, ORDER_VALUE_INDEX, ORDER_INDEX_VALUE, ORDER_ONLY_VALUE, and ORDER_ONLY_INDEX are supported.

For the Atlas A3 training product / Atlas A3 inference product , ORDER_VALUE_INDEX, ORDER_INDEX_VALUE, ORDER_ONLY_VALUE, and ORDER_ONLY_INDEX are supported.

For the Atlas A2 training product / Atlas A2 inference product , ORDER_VALUE_INDEX, ORDER_INDEX_VALUE, ORDER_ONLY_VALUE, and ORDER_ONLY_INDEX are supported.

For the Atlas 200I/500 A2 inference product , ORDER_VALUE_INDEX and ORDER_ONLY_VALUE are supported.

For the Atlas inference product AI Core, ORDER_VALUE_INDEX and ORDER_INDEX_VALUE are supported.

For the Atlas training product , ORDER_VALUE_INDEX is supported.

Returns

None

Restrictions

  • The storage order in dst is determined by the order parameter and defaults to the maximum or minimum value followed by its index. In the returned result, the index is stored using the data type specified by dst. For example, if dst is of type half, the index is stored as half and must be converted to an integer using reinterpret_cast when it is read. If the input type is half, reinterpret_cast<uint16_t*> is required. If the input type is float, reinterpret_cast<uint32_t*> is required. In the example, the first two computation results are [9.980e-01 5.364e-06], the reinterpret_cast method is called to convert 5.364e-06 to the index value 90. For Atlas A2 training product / Atlas A2 inference product and Atlas A3 training product / Atlas A3 inference product , when ORDER_ONLY_INDEX (only the index of the maximum or minimum value is returned) is used, reinterpret_cast<uint32_t*> must be used to read the index.For the Atlas 350 Accelerator Card, when ORDER_ONLY_INDEX is used and the operand data type is uint16_t, int16_t, or half, reinterpret_cast<uint32_t*> must be used to read the index.
  • 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.

Examples

  • Example of high-dimensional tensor sharding computation (contiguous mask mode)
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    // 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.
    // Based on the preceding information, repeatTime is 4, dstRepStride is 1, srcBlkStride is 1, and srcRepStride is 8.
    // If the minimum value and index need to be computed and the result in format of [value, index] needs to be stored, the default order can be used. An example API is AscendC::WholeReduceMin<half>(dstLocal, srcLocal, 128, 4, 1, 1, 8).
    
    // To obtain the minimum value and index that is stored in the format of [index, value], you can use the following example API:
    AscendC::WholeReduceMin<half>(dstLocal, srcLocal, 128, 4, 1, 1, 8, AscendC::ReduceOrder::ORDER_INDEX_VALUE);
    
    // To obtain only the minimum value that is stored in the format of [value], you can use the following example API:
    AscendC::WholeReduceMin<half>(dstLocal, srcLocal, 128, 4, 1, 1, 8, AscendC::ReduceOrder::ORDER_ONLY_VALUE);
    
    // To obtain only the index that is stored in the format of [index], you can use the following example API:
    AscendC::WholeReduceMin<half>(dstLocal, srcLocal, 128, 4, 1, 1, 8, AscendC::ReduceOrder::ORDER_ONLY_INDEX);
    
  • Example of high-dimensional tensor sharding computation (bitwise mask mode)
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    // 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.
    // To obtain the minimum value and index that is stored in the format of [value, index], you can use the default order. The following is an example:
    AscendC::WholeReduceMin<half>(dstLocal, srcLocal, mask, 4, 1, 1, 8);
    
    // To obtain the minimum value and index that is stored in the format of [index, value], you can use the following example API:
    AscendC::WholeReduceMin<half>(dstLocal, srcLocal, mask, 4, 1, 1, 8, AscendC::ReduceOrder::ORDER_INDEX_VALUE);
    
    // To obtain only the minimum value that is stored in the format of [value], you can use the following example API:
    AscendC::WholeReduceMin<half>(dstLocal, srcLocal, mask, 4, 1, 1, 8, AscendC::ReduceOrder::ORDER_ONLY_VALUE);
    
    // To obtain only the index that is stored in the format of [index], you can use the following example API:
    AscendC::WholeReduceMin<half>(dstLocal, srcLocal, mask, 4, 1, 1, 8, AscendC::ReduceOrder::ORDER_ONLY_INDEX);
    

The following is an example:

Input (src_gm):
[10   10   10   10   10   10   10   10   10   10   10   10   10   10   10   10
 10   10   10   10   10   10   10   10   10   10   10   10   10   10   10   10
 10   10   10   10   10   10   10   10   10   10   10   10   10   10   10   10
 10   10   10   10   1   10   10   10   10   10   10   10   10   10   10   10
 10   10   10   10   10   10   10   10   10   10   10   10   10   10   10   10
 10   10   10   10   10   10   10   10   10   10   10   10   10   10   10   10
 10   10   10   10   10   10   10   10   10   10   10   10   10   10   10   10
 10   10   10   10   10   10   10   10   10   10   10   10   10   10   10   10
 20   20   20   20   20   20   20   20   20   20   20   20   20   20   20   20
 20   20   20   20   20   20   20   20   20   20   20   20   20   20   20   20
 20   20   20   20   20   20   20   20   20   20   20   20   20   20   20   20
 20   20   20   20   20   20   20   20   20   20   20   20   20   20   20   20
 20   20   20   20   20   20   20   20   20   20   20   20   20   20   20   20
 20   20   20   20   20   20   20   20   20   20   20   20   20   20   20   20
 20   20   20   20   2   20   20   20   20   20   20   20   20   20   20   20
 20   20   20   20   20   20   20   20   20   20   20   20   20   20   20   20
 ...
 30   30   30   30   30   30   30   30   30   30   30   30   30   30   30   30
 30   30   30   3   30   30   30   30   30   30   30   30   30   30   30   30
 30   30   30   30   30   30   30   30   30   30   30   30   30   30   30   30
 30   30   30   30   30   30   30   30   30   30   30   30   30   30   30   30
 30   30   30   30   30   30   30   30   30   30   30   30   30   30   30   30
 30   30   30   30   30   30   30   30   30   30   30   30   30   30   30   30
 30   30   30   30   30   30   30   30   30   30   30   30   30   30   30   30
 30   30   30   30   30   30   30   30   30   30   30   30   30   30   30   30]

If the ReduceOrder type is ORDER_VALUE_INDEX or the default value, output the data to dst_gm.
[1 3.09944e-06 2 5.96046e-06 ... 3 1.13249e-06]
If the ReduceOrder type is ORDER_INDEX_VALUE, output the data to dst_gm.
[3.09944e-06 1 5.96046e-06 2 ... 1.13249e-06 3]
If the ReduceOrder type is ORDER_ONLY_VALUE, output the data to dst_gm.
[1 2 ... 3 0 0 0 ...]
If the ReduceOrder type is ORDER_ONLY_VALUE, output the data to dst_gm.
[3.09944e-06 0 5.96046e-06 0 ... 1.13249e-06 0]

The value of index represents the binary representation of an integer value expressed in the half format. Taking the above result as an example:
Among the first 128 numbers, the position of 11 in the corresponding repeat is 52. The hexadecimal value is 0x3400, corresponding to a half-precision value of 3.09944e-06.
Among the second 128 numbers, the position of 12 in the corresponding repeat is 100. The hexadecimal value is 0x6400, corresponding to a half-precision value of 5.96046e-06.
In the last 128 numbers, the position of 13 in the corresponding repeat is 19. The hexadecimal value is 0x1300, corresponding to a half-precision value of 1.13249e-06.