Axpy

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

x

Atlas inference product's AI Core

Atlas inference product's Vector Core

x

Atlas training products

x

Function

Adds the product of each element of the source operand (srcTensor) and a scalar to the corresponding element in the destination operand (dstTensor). The formula is as follows.

This API has the same function as the basic API Axpy. The difference is that this API combines Muls and Add to provide better precision.

Prototype

1
2
template <typename T, typename U, bool isReuseSource = false>
__aicore__ inline void Axpy(const LocalTensor<T>& dstTensor, const LocalTensor<U>& srcTensor, const U scalarValue, const LocalTensor<uint8_t>& sharedTmpBuffer, const uint32_t calCount)

Parameters

Table 1 Template parameters

Parameter

Description

T

Data type of the destination operand.

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 inference product's AI Core, the supported data types are half and float.

U

Data type of the source operand.

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 inference product's AI Core, the supported data types are half and float.

isReuseSource

Whether the source operand can be modified. This parameter is reserved. Pass the default value false.

Table 2 API parameters

Parameter

Input/Output

Description

dstTensor

Output

Destination operand.

The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT.

srcTensor

Input

Source operand.

The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT.

scalarValue

Input

Scalar. The supported data types are half and float. The type of the scalar operand must be the same as that of srcTensor.

sharedTmpBuffer

Input

Temporary buffer.

The type is LocalTensor, and the supported TPosition is VECIN, VECCALC, or VECOUT.

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 needs to be passed through the sharedTmpBuffer input parameter by developers. For details about how to obtain the temporary space size (BufferSize), see GetAxpyMaxMinTmpSize.

calCount

Input

Number of elements involved in the computation.

Returns

None

Restrictions

  • The source operand address must not overlap the destination operand address.
  • sharedTmpBuffer must not overlap the addresses of the source operand and destination operand.
  • For details about the operand address alignment requirements, see General Address Alignment Restrictions.
  • The supported precision combinations are as follows.
    • half type: srcLocal data type = half; scalar data type = half; dstLocal data type = half; PAR=128
    • float type: srcLocal data type = float; scalar data type = float; dstLocal data type = float; PAR = 64
    • mix type: srcLocal data type = half; scalar data type = half; dstLocal data type = float; PAR = 64

Example

For details about the complete call example, see More Examples.

1
2
3
4
5
6
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 1024, the input data type of the operator is half, and the number of actually computed data elements is 512.
AscendC::Axpy(dstLocal, srcLocal, static_cast<half>(3.0), sharedTmpBuffer, 512);
Result example:
1
2
Input data (srcLocal): [104.875 107.4375 -62.59375 ...  -242.875 15.8828125]
Output data (dstLocal): [316.5 324.2 185.8  ...  -726.5 49.66]