ReduceMax

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

x

Atlas inference product AI Core

x

Atlas inference product Vector Core

x

Atlas training product

x

Function Usage

Returns the maximum value of a multi-dimensional vector in a specified dimension.

The specified dimension (Reduce axis) is defined as the R axis, and the non-specified dimension (Normal axis) is defined as the A axis. As shown in the following figure, the operation is performed on a two-dimensional matrix with shape (2, 3). If the maximum value is computed in the first dimension, the output result is [4, 5, 6]. If the maximum value is computed in the second dimension, the output result is [3, 6]. The first dimension is the outer axis, and the second dimension is the inner axis.

Figure 1 Example of ReduceMax computed in the first dimension
Figure 2 Example of ReduceMax computed in the last dimension

Prototype

  • Pass the temporary space through the sharedTmpBuffer input parameter.
    1
    2
    template <class T, class pattern, bool isReuseSource = false>
    __aicore__ inline void ReduceMax(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, const LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t srcShape[], bool srcInnerPad)
    
  • Allocate the temporary space through the API framework.
    1
    2
    template <class T, class pattern, bool isReuseSource = false>
    __aicore__ inline void ReduceMax(const LocalTensor<T>& dstTensor, const LocalTensor<T>& srcTensor, const uint32_t srcShape[], bool srcInnerPad)
    

Due to the complex mathematical computation involved in the internal implementation of this API, additional temporary space is required to store intermediate variables generated during computation. The temporary space can be passed through the sharedTmpBuffer input parameter or allocated through the API framework.

  • When the sharedTmpBuffer input parameter is used for passing the temporary space, the tensor serves as the temporary space. In this case, the API framework is not required for temporary space allocation. This enables you to manage the sharedTmpBuffer space and reuse the buffer after calling the API, so that the buffer is not repeatedly allocated and deallocated, improving the flexibility and buffer utilization.
  • When the API framework is used for temporary space allocation, you do not need to allocate the space, but must reserve the required size for the temporary space.

If sharedTmpBuffer is used, you must allocate the tensor space. If the API framework is used, you must reserve the temporary space. To obtain the size of the temporary space (BufferSize) to be reserved, use the API provided in GetReduceMaxMaxMinTmpSize.

Parameters

Table 1 Template parameters

Parameter

Description

T

Operand data type.

For the Atlas 350 Accelerator Card, the supported data types are int8_t, uint8_t, int16_t, uint16_t, half, bfloat16_t, int32_t, uint32_t, float, int64_t and uint64_t.

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.

pattern

ReduceMax computation axes, including the Reduce axis and Normal axis. pattern is a string composed of letters A (standing for Normal axis) and R (standing for Reduce axis), with the number of letters equal to the number of dimensions in the vector. For example, AR indicates performing a ReduceMax operation on a 2D vector: The first dimension is the Normal axis, and the second dimension is the Reduce axis, meaning that the maximum value of the second dimension is obtained.

pattern is a struct defined in the AscendC::Pattern::Reduce namespace. You can ignore its member variables.

Currently, the value of pattern can only be AR or RA.

isReuseSource

Whether the source operand can be modified. The default value is false. If you allow the source operand to be modified, enable this parameter to reduce memory space usage.

If this parameter is set to true, the src memory space is reused during internal computation of this API to reduce memory space usage. If this parameter is set to false, the src memory space is not reused during internal computation of this API.

For details about how to use isReuseSource, see More Examples.

Table 2 API parameters

Parameter

Input/Output

Description

dstTensor

Output

Destination operand.

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

srcTensor

Input

Source operand.

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

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

sharedTmpBuffer

Input

Temporary buffer.

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

This parameter is used to store intermediate variables during complex computation in ReduceMax and is provided by developers.

For details about how to obtain the temporary space size (BufferSize), see GetReduceMaxMaxMinTmpSize.

srcShape

Input

An array of the uint32_t type, indicating the shape information of the source operand. The dimension of the shape must be consistent with that of the template parameter pattern. For example, if pattern is AR, the shape dimension must be two-dimensional.

For the Atlas 350 Accelerator Card, only two-dimensional shapes are supported.

For the Atlas A3 training product/Atlas A3 inference product, only two-dimensional shapes are supported.

For the Atlas A2 training product/Atlas A2 inference product, only two-dimensional shapes are supported.

srcInnerPad

Input

Whether the innermost axis data to be computed is 32-byte aligned.

For the Atlas 350 Accelerator Card, this parameter is reserved. In the API, the srcShape and pattern parameters are used to calculate whether the innermost axis data is 32-byte aligned.

For the Atlas A3 training product/Atlas A3 inference product, only true is supported.

For the Atlas A2 training product/Atlas A2 inference product, only true is supported.

Returns

None

Constraints

  • For details about the operand address alignment requirements, see General Address Alignment Restrictions.
  • When srcInnerPad is set to True, the inner axis of the input vector must be a multiple of 32 bytes. If the byte length of the original inner axis n is not a multiple of 32, pad it up to the nearest multiple of 32. The resulting inner axis size after padding is: inner = (n * sizeof(T) + 32 - 1) / 32 * 32 / sizeof(T)
  • The source operand address must not overlap the destination operand address.
  • The address of sharedTmpBuffer cannot overlap that of the source or destination operand.

Examples

For a complete operator sample, see ReduceMax operator sample.

  • 32-byte alignment of inner axis data
    1
    2
    3
    uint32_t shape[] = { 2, 8 };
    constexpr bool isReuse = true;
    AscendC::ReduceMax<float, AscendC::Pattern::Reduce::AR, isReuse>(dstLocal, srcLocal, tmp, shape, true); // tmp indicates the size of the input temporary space, shape indicates the input shape of srcLocal, and true indicates whether the address is 32-byte aligned.
    

    Result example:

    1
    2
    3
    4
    5
    6
    7
    The input and output data type is float.
    Input data (src):
    [[ 0.0 4.0 2.0 0.0 -1.0 2.0 -1.0 7.0],
     [ 0.0 1.0 -9.0 2.0 2.0 2.0 8.0 3.0]]
    Input pattern: AR
    Input shape: (2,8)
    Output data (dst): [7.0 8.0]
    
  • Non-32-byte alignment of axis data
    The data type is half, the input shape is [2,5], and the input pattern is AR.
    1
    2
    uint32_t shape[2] = {2, 5};
    ReduceMax<T, AscendC::Pattern::Reduce::AR, false>(dstLocal, srcLocal, tmp, shape, true);
    

    Result example:

    1
    2
    3
    4
    5
    6
    7
    The original input and output data types are half.
    Original input data (src):
    [[ 0.0 4.0 2.0 0.0 -1.0],
     [ 0.0 1.0 -9.0 2.0 2.0]]
    Input pattern: AR
    Original input shape: (2,5)
    Output data (dst): [4.0 2.0]