This document is relevant for: Trn2, Trn3
nki.isa.scalar_tensor_tensor#
- nki.isa.scalar_tensor_tensor(dst: NkiTensor, data: NkiTensor, op0, operand0: float | NkiTensor, op1, operand1: NkiTensor, reverse0=False, reverse1=False, reduce_cmd: reduce_cmd = reduce_cmd.reset_reduce, reduce_op=Ellipsis, reduce_res: NkiTensor | None = None, name=None)[source]#
Apply two math operators in sequence using Vector Engine:
(data <op0> operand0) <op1> operand1.This instruction is equivalent to running two operations back-to-back: 1.
temp_result = tensor_scalar(data, op0, operand0)- broadcastoperand0and applyop02.dst = tensor_tensor(temp_result, op1, operand1)- element-wise operation withoperand1The
operand0can be either a compile-time constant scalar for broadcast across all elements ofdataor a tile of shape(data.shape[0], 1)for broadcast along the free dimension. Theoperand1tile must have the same shape asdatafor element-wise operation.The scalar broadcasting in the first operation is performed at no additional performance cost, making this instruction have approximately the same latency as a regular
tensor_tensorinstruction.Both
op0andop1must be arithmetic operators (see Supported Math Operators for NKI ISA for supported operators). Bitvec operators are not supported. When the operators are non-commutative (e.g., subtract), operand ordering can be reversed usingreverse0andreverse1flags.Memory types.
The input
datatile can be an SBUF or PSUM tile. Theoperand0can be an SBUF or PSUM tile or a compile-time constant scalar. Theoperand1must be an SBUF or PSUM tile. However,dataandoperand1cannot both reside in PSUM. The outputdsttile can be written to either SBUF or PSUM.Data types.
All input tiles can be any supported NKI data type (see Supported Data Types for more information). The Vector Engine automatically casts input data types to float32 and performs all computations in float32 math. The float32 results are cast to the data type of output
dst. See nki-type-casting for more information about type casting behavior.Layout.
The parallel computation dimension of
nisa.scalar_tensor_tensoris along the partition dimension.Tile size.
The partition dimension size of input
data,operand1, and outputdsttiles must be the same and must not exceed 128. The total number of elements per partition of inputdata,operand1, and outputdsttiles must be the same and must not exceed the physical size of each SBUF partition. If operand0 is not a scalar, the partition dimension size ofoperand0must be the same as that ofdataand the number of elements per partition ofoperand0must be 1. Thedata,operand1, anddsttiles can have at most two free dimensions (maximum rank 3, including the partition dimension).In addition to the elementwise computation above, this API can optionally perform a reduction along the free dimension(s) of the elementwise result using Vector Engine, at a small additional performance cost. The reduction result is written into
reduce_res, which must be a SBUF/PSUM tile with the same partition axis size as the input tiledataand one element per partition.reduce_opmust benl.add; bitvec operators do not support accumulation.\[\begin{split}dst = (data <op0> operand0) <op1> operand1 \\ reduce\_res = reduce\_op(dst, axis=<FreeAxis>)\end{split}\]Accumulator behavior:
The Vector Engine maintains internal accumulator registers that can be controlled via the
reduce_cmdparameter:reduce_cmd.reset_reduce: (default) Reset accumulators to 0, then accumulate the current results.reduce_cmd.reduce: Continue accumulating without resetting.
Note
The accumulator registers are shared across Vector Engine accumulation instructions including
exponential(),range_select(),scalar_tensor_tensor(),select_reduce(),tensor_scalar_cumulative(), andtensor_scalar_reduce().- Parameters:
dst – the output tile
data – the input tile
op0 – the first math operator used with operand0 (see Supported Math Operators for NKI ISA for supported operators)
operand0 – a scalar constant or a tile of shape
(data.shape[0], 1), where data.shape[0] is the partition axis size of the inputdatatilereverse0 – reverse ordering of inputs to
op0; if false,operand0is the rhs ofop0; if true,operand0is the lhs ofop0op1 – the second math operator used with operand1 (see Supported Math Operators for NKI ISA for supported operators)
operand1 – a tile with the same size as
datafor element-wise operationreverse1 – reverse ordering of inputs to
op1; if false,operand1is the rhs ofop1; if true,operand1is the lhs ofop1reduce_cmd – (optional) Controls the accumulator behavior, supports:
reset_reduce(default) andreduce.reduce_op – (optional) Reduction operator to apply (only
nl.addis supported)reduce_res – (optional)
(data.shape[0], 1)SBUF/PSUM tile that receives the DVE accumulator value afterreduce_cmdis applied, via a fused non-destructive read out (the accumulator is preserved, so a following DVE reduction continues to accumulate into it). PassNoneto skip the read out when chaining multiple instructions.
This document is relevant for: Trn2, Trn3