Fma

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product/Atlas A3 inference product

x

Atlas A2 training product/Atlas A2 inference product

x

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

Computes the result of multiplying two inputs and then adding a third input element-wise. The formula is as follows:

Prototype

  • Pass the temporary space through the sharedTmpBuffer input parameter.
    1
    2
    template <const FmaConfig& config = DEFAULT_FMA_CONFIG, typename T>
    __aicore__ inline void Fma(const LocalTensor<T>& dst, const LocalTensor<T>& src0, const LocalTensor<T>& src1,const LocalTensor<T>& src2, const LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t count)
    
  • The API framework allocates temporary space.
    1
    2
    template <const FmaConfig& config = DEFAULT_FMA_CONFIG, typename T>
    __aicore__ inline void Fma(const LocalTensor<T>& dst, const LocalTensor<T>& src0, const LocalTensor<T>& src1, const LocalTensor<T>& src2, const uint32_t count)
    

Precision conversion is involved in the internal implementation of this API. Therefore, extra temporary space is required to store intermediate variables during computation. The temporary space can be passed by developers through the sharedTmpBuffer input parameter or allocated by 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 developers to manage the sharedTmpBuffer space and reuse the buffer after calling the API, so that the buffer is not repeatedly allocated or deallocated, improving the flexibility and buffer utilization.
  • When the API framework allocates temporary space, developers do not need to allocate the space but must reserve the required size for the temporary space.

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

Parameters

Table 1 Template parameters

Parameter

Description

FmaConfig

Fma algorithm configuration. This is an optional parameter of the FmaConfig type. The code below describes the definition.

isReuseSource: whether the source operand can be modified. This parameter is reserved. Pass the default value false.

T

Operand data type.

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

1
2
3
struct FmaConfig {
    bool isReuseSource;
};
Table 2 API parameters

Parameter

Input/Output

Description

dst

Output

Destination operand.

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

src0, src1, src2

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 Fma and is provided by developers.

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

count

Input

Number of elements involved in the computation.

Returns

None

Constraints

  • The source operand address must not overlap the destination operand address.
  • The address of sharedTmpBuffer cannot overlap that of the source or destination operand.
  • For details about the operand address alignment requirements, see General Address Alignment Restrictions.

Examples

For details about a complete example, see Fma operator sample.

  • Pass the temporary space through the sharedTmpBuffer input parameter.
    1
    2
    3
    4
    5
    6
    7
    8
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECCALC, 1> tmpQue;
    pipe.InitBuffer(tmpQue, 1, bufferSize); // bufferSize is obtained through the tiling parameter on the host.
    AscendC::LocalTensor<uint8_t> sharedTmpBuffer = tmpQue.AllocTensor<uint8_t>();
    // The input tensor length is 1,024, the input data type of the operator is half, and the number of actually computed data elements is 512.
    static constexpr AscendC::FmaConfig fmaConfig = { false }; // The source operand is not modified.
    // dst, src0, src1, and src2 are LocalTensors of the half type.
    AscendC::Fma<fmaConfig, half>(dst, src0, src1, src2, sharedTmpBuffer, 512);
    
  • The API framework allocates temporary space.
    1
    2
    3
    4
    5
    6
    7
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECCALC, 1> tmpQue;
    pipe.InitBuffer(tmpQue, 1, bufferSize); // bufferSize is obtained through the tiling parameter on the host.
    // The input tensor length is 1,024, the input data type of the operator is half, and the number of actually computed data elements is 512.
    static constexpr AscendC::FmaConfig fmaConfig = { false }; // The source operand is not modified.
    // dst, src0, src1, and src2 are LocalTensors of the half type.
    AscendC::Fma<fmaConfig, half>(dst, src0, src1, src2, 512);
    
Result example:
1
2
3
4
Input (src0):[1 1 1...... 1 1 1]
Input (src1):[2 2 2 ...... 2 2 2]
Input (src2):[1 1 1 ...... 1 1 1]
Output (dst):[3 3 3 ...... 3 3 3]