TAXPY

Introduction

Performs an in-place scaled accumulation (AXPY, a⋅x+ya \cdot x + y) on a tile: scales src0 by scalar and accumulates the result onto dst.

dsti,j←scalar⋅src0i,j+dsti,j\mathrm{dst}_{i,j} \leftarrow \mathrm{scalar} \cdot \mathrm{src0}_{i,j} + \mathrm{dst}_{i,j}

dst serves as both the accumulation input (yy) and the output, and must be initialized before the call; src0 (xx) is read-only; scalar (aa) is a scalar.

Mathematical Semantics

For each element (i, j) in the valid region:

dsti,jnew=scalar⋅src0i,j+dsti,jold\mathrm{dst}_{i,j}^{\text{new}} = \mathrm{scalar} \cdot \mathrm{src0}_{i,j} + \mathrm{dst}_{i,j}^{\text{old}}

  • dst: read-modify-write (RMW). Reads the old value as the accumulation base yy, and writes back scalar⋅x+y\mathrm{scalar} \cdot x + y.
  • src0: read-only. Participates in the operation element by element (xx).
  • scalar: scalar scaling factor (aa), of type TileDataSrc::DType.

Unless otherwise specified, the semantics are defined within the valid region, and target-dependent behavior is marked as implementation-defined.

C++ Built-in APIs

Declared in include/pto/common/pto_instr.hpp.

The public include header is <pto/pto-inst.hpp>, and the internal declaration is located in pto/common/pto_instr.hpp.

template <typename TileDataDst, typename TileDataSrc, typename... WaitEvents>
PTO_INST RecordEvent TAXPY(TileDataDst &dst, TileDataSrc &src0, typename TileDataSrc::DType scalar,
                           WaitEvents &...events);
ParameterDirectionMeaning
dstInput/OutputAccumulation base and result tile (yy), read-modify-write, Vec
src0InputScaling source tile (xx), read-only, Vec, with the same valid shape as dst
scalarInputScalar scaling coefficient (aa), of type TileDataSrc::DType
events...InputWait events (WaitEvents), implicit TSYNC before the instruction

Tile Size and Data Type

For a valid shape of M×NM \times N:

TileDtypeValid ShapeTileTypeDescription
dsthalf or floatM×NM \times NVec (UB)Accumulation base + result (RMW)
src0half or floatM×NM \times NVec (UB)Scaling source, element-wise

The valid rows and valid columns of dst and src0 must be exactly the same.

Supported Input Dtypes

dst Dtypesrc0 Dtypescalar DtypeDescription
halfhalfhalfSame-type path, directly vaxpy
floatfloatfloatSame-type path, directly vaxpy
floathalfhalfDifference path: src0 is widened to FP32 before accumulation

dst and src0 must have the same dtype, or dst is float and src0 is half (allowing half-to-float widened accumulation). The combination where dst is half and src0 is float is invalid (blocked by static_assert in the implementation).

Implementation Notes

TAXPY executes on the vector pipe (PIPE_V) using the built-in vaxpy (a⋅x+ya \cdot x + y) vector:

  1. Same type (dst and src0 have the same dtype): loads src0 and dst repeat by repeat, executes vaxpy(dst, src0, scalar), and writes back to dst; the columns in the tail that are less than one repeat are masked by the predicate mask.
  2. Different type (dst=float, src0=half): the half data of src0 is widened to FP32 before participating in the accumulation (on Ascend 950PR/Ascend 950DT, it is unpacked via UNPK_B16 and converted via vcvt; on Atlas A2/A3 training products/Atlas A2/A3 inference products, it is natively handled by vaxpy as 4-block src / 8-block dst).
  3. On Atlas A2/A3 training products/Atlas A2/A3 inference products, the implementation selects between count mode and norm mode based on whether the repeat-stride overflows and on the relationship between the number of columns and the number of rows, to cover any valid shape.

Constraints

ConstraintScopeReason
dst and src0 must be TileType::VecAll targetsExecutes on UB (vector pipeline)
dst and src0 have the same valid shape (M×NM \times N)All targetsElement-wise one-to-one correspondence
dst dtype ∈ {half, float}All targetsFloating-point word width supported by vaxpy
dst/src0 have the same dtype, or (float,half)All targetsOnly half→float widened accumulation is allowed
dst must be initialized before the callAll targetsdst is read as the accumulation base yy

Examples

// dst must be initialized first (as the accumulation base y); result: dst = scalar * src0 + dst
TAXPY(dstTile, srcTile, scalar);

For complete ST examples, see tests/npu/a5/src/st/testcase/taxpy/ (A5), tests/npu/a2a3/src/st/testcase/taxpy/ (A2/A3), tests/npu/kirin9030/src/st/testcase/taxpy/ (Kirin9030), and tests/cpu/st/testcase/taxpy/ (CPU reference implementation).