This document is relevant for: Trn2, Trn3
nki.isa.scalar_alu#
- nki.isa.scalar_alu(data1: nki.isa.NkiScalar | int, data2: nki.isa.NkiScalar | int, op, name=None)[source]#
Apply a binary integer operation to two scalar values.
Each operand is a
NkiScalaror a Pythonint. Arithmetic preserves the scalar’s dtype. When both operands are Python ints, the operation is evaluated at compile time and returns a Pythonintfor arithmetic orboolfor a comparison, without emitting an instruction.Overloaded Python operators on NkiScalars are equivalent to this API and are preferred when available. For example,
a + bis equivalent tonisa.scalar_alu(a, b, op=nl.add).Supported arithmetic operations are
nl.add,nl.subtract,nl.multiply,nl.divide,nl.fmod,nl.maximumandnl.minimum.nl.divideperforms integer division rounded toward zero;nl.fmodreturns the remainder with the sign ofdata1. For example, dividing-17by4gives-4, with remainder-1.Comparison operations are
nl.less,nl.less_equal,nl.greater,nl.greater_equal,nl.equalandnl.not_equal. A comparison involving a NkiScalar returns anl.int32NkiScalar containing 1 when true or 0 when false. The result supports scalar arithmetic, stores, loop bounds and access pattern offsets. It cannot be used as a Pythoniforwhilecondition.Data types.
Scalars must have the same width and signedness.
nl.int32andnl.uint32support every operation;nl.int64supports only add, subtract and multiply. A Python int combined with a NkiScalar must fit the scalar’s dtype. Add, subtract and multiply wrap at the scalar’s width; arithmetic on two Python ints is unbounded.Constraints.
Tensor operands are not supported. Division and remainder require a nonzero divisor; signed division overflow is unsupported for NkiScalar operands. Operands are evaluated in the order
data1 op data2. This API is available with the tracer frontend andnki.simulate().Engines.
The result is bound to the sequencer an operand is bound to, if any (see
NkiScalar). Operands bound to two different sequencers are an error.- Parameters:
data1 – Left operand, a NkiScalar or Python int.
data2 – Right operand, a NkiScalar or Python int.
op – One of the supported
nki.languagebinary operations.name – Optional instruction name. Ignored when both operands are Python ints.
- Returns:
A new NkiScalar, or a Python int or bool when both operands are ints.
Examples:
value = nisa.scalar_load(buf, engine=nisa.engine.sync) # Prefer overloaded Python operators when available. incremented = value + 1 scaled = incremented * 3 # Equivalent explicit ISA calls: # incremented = nisa.scalar_alu(value, 1, op=nl.add) # scaled = nisa.scalar_alu(incremented, 3, op=nl.multiply) out_buf = nl.ndarray((1, 1), dtype=nl.int32, buffer=nl.sbuf) # scaled is bound to sync, like value. nisa.scalar_store(out_buf, scaled, engine=nisa.engine.sync)
This document is relevant for: Trn2, Trn3