This document is relevant for: Trn2, Trn3
nki.isa.tensor_scalar#
- nki.isa.tensor_scalar(dst: NkiTensor, data: NkiTensor, op0, operand0: float | NkiTensor, reverse0=False, op1=None, operand1: float | NkiTensor | None = None, reverse1=False, engine=engine.unknown, name=None)[source]#
Apply up to two math operators to the input
datatile by broadcasting scalar/vector operands in the free dimension using Vector or Scalar or GpSimd Engine:(data <op0> operand0) <op1> operand1.The input
datatile can be an SBUF or PSUM tile. Bothoperand0andoperand1can be SBUF or PSUM tiles of shape(data.shape[0], 1), i.e., vectors, or compile-time constant scalars.op1andoperand1are optional, but must beNone(default values) when unused. Note, performing one operator has the same performance cost as performing two operators in the instruction.When the operators are non-commutative (e.g., subtract), we can reverse ordering of the inputs for each operator through:
reverse0 = True:tmp_res = operand0 <op0> datareverse1 = True:operand1 <op1> tmp_res
The
tensor_scalarinstruction supports two types of operators: 1) bitvec operators (e.g., bitwise_and) and 2) arithmetic operators (e.g., add). See Supported Math Operators for NKI ISA for the full list of supported operators. The two operators,op0andop1, in atensor_scalarinstruction must be of the same type (both bitvec or both arithmetic). Bitvec operators run only on Vector Engine, so setenginetonki.isa.engine.vectorornki.isa.engine.unknown.nki.isa.engine.scalarandnki.isa.engine.gpsimdare deprecated for bitvec operators: the instruction runs on Vector Engine and NKI gives aDeprecationWarning. A later release will reject them. Bitvec operators treat elements as raw bit patterns, and requiredataanddstto have the same integer dtype.If arithmetic operators are used, the
tensor_scalarinstruction can run on Vector or Scalar or GpSimd Engine. However, each engine supports limited arithmetic operators (see Supported Math Operators by NKI ISA). The Scalar Engine on trn2 only supports some operator combinations:op0=nl.multiplyandop1=nl.addop0=nl.multiplyandop1=Noneop0=nl.addandop1=None
The GpSimd Engine supports these arithmetic operators, depending on the NeuronCore version:
NeuronCore-v2+:
nl.rsqrt, only asop0withop1andoperand1set toNoneNeuronCore-v3+:
nl.addandnl.multiply, asop0orop1NeuronCore-v4+:
nl.subtract,nl.maximum,nl.minimum,nl.equal,nl.greater,nl.greater_equal,nl.less_equalandnl.less, asop0orop1
nl.rsqrtis only supported on the GpSimd Engine.Also, arithmetic operators impose no restriction on the data types of input tensor
dataand output tensordst, but the operand0 and operand1 (if used) must be float32. The compute engine automatically castsdata.dtypeto float32 and performs the operators in float32 math. The float32 computation results are cast todst.dtypeat no additional performance cost. See Type Casting for more information about type casting behavior.Integer arithmetic for int32.
Float32 math cannot represent every int32 value above
2**24in magnitude. Whendataanddstare int32 and the operands are integer scalars, these forms use 32-bit integer math on the GpSimd Engine instead:op0=nl.multiplyandop1=nl.add, orop0=nl.multiplyandop1=None, withengineset tonki.isa.engine.gpsimdornki.isa.engine.unknownop0=nl.addandop1=None, withengine=nki.isa.engine.gpsimd. This form requiresoperand0in the int32 range, at most two free dimensions, and no tensor indirection. Otherwise, it uses float32 math.
An explicit
engine=nki.isa.engine.vectororengine=nki.isa.engine.scalarkeeps float32 math.Tensor indirection.
On NeuronCore-v4 and later,
dstanddatasupport tensor indirection (gather/scatter) by passing a view created with.indirect(index).operand0andoperand1do not support tensor indirection. When tensor indirection is used, passengine=nki.isa.engine.vector,engine=nki.isa.engine.scalar, orengine=nki.isa.engine.gpsimd. Tensor indirection is not supported when the op matches the int32/uint32 address-arithmetic pattern.When operands are manually allocated, their base partitions must satisfy:
the
indexof every.indirect()view starts on a quadrant boundary (a multiple of 32);if
datauses.indirect(),datastarts on the same partition as itsindex;if
dstuses.indirect()anddatais in SBUF,dst’sindexstarts on the same partition asdata;if
dstuses.indirect()anddatais in PSUM and uses.indirect(),dst’sindexstarts on the same partition asdata’sindex.
In addition, on the Scalar engine a scattered
dstcannot be in PSUM.The
dataanddsttiles can normally have at most three free dimensions (maximum rank 4, including the partition dimension). The specialized int32/uint32 address-arithmetic patterns have a lower limit of two free dimensions (maximum rank 3).Dynamic access patterns.
dataanddstmay each carry a dynamic access pattern built withnki.language.NkiTensor.ap(), whosestrideandcountslots hold anki.isa.NkiScalarread at runtime.The partition dimension (dim 0) is the exception, and both its
strideand itscountmust be Pythonintvalues, so put the dynamic slots on a free dimension. A tileoperand0oroperand1does not support a dynamic access pattern.- Parameters:
dst – an output tile of
(data <op0> operand0) <op1> operand1computationdata – 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 inputdatatile. Must beNoneor0whenop0is a unary operator (e.g.,nl.abs).reverse0 – 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); this operator is optional
operand1 – a scalar constant or a tile of shape
(data.shape[0], 1), where data.shape[0] is the partition axis size of the inputdatatilereverse1 – reverse ordering of inputs to
op1; if false,operand1is the rhs ofop1; if true,operand1is the lhs ofop1engine – (optional) the engine to use for the operation: nki.isa.engine.vector, nki.isa.engine.scalar, nki.isa.engine.gpsimd (supports the subset of arithmetic operators listed above) or nki.isa.engine.unknown (default, let compiler select best engine based on the input tile shape).
This document is relevant for: Trn2, Trn3