This document is relevant for: Trn2, Trn3
nki.isa.scalar_load#
- nki.isa.scalar_load(src: NkiTensor, engine=None) NkiScalar[source]#
Load a scalar value from a 1x1 tensor.
Returns a
NkiScalar, whosedtypematchessrcand fixes the width of every value derived from it. See that class for which operations each width supports, and NKI Scalar Values for a walkthrough.Scalar loads operate on internal tiles in SBUF and HBM. You cannot use kernel input or output tensors with this API. For an input tile, use
nki.isa.dma_copy()to copy the value into an internal tile before loading it. Kernel arguments and returned tensors cannot be used directly assrc; the same restriction applies to slices and views on such tiles.PSUM cannot be used with
scalar_loadeither. Usenki.isa.tensor_copy()to copy a value from PSUM to SBUF before loading it.The source tensor must use
nl.int32,nl.uint32ornl.int64dtype. Unsigned scalars use unsigned comparisons and arithmetic modulo2**32.Unlike the compute instructions,
scalar_loadacceptsnl.int64, and the value is not truncated. An int64 scalar can be written back withnki.isa.scalar_store(), and can be passed asscalar_offsetto address an HBM tensor beyond the 4GB reach of a 32-bit byte offset. It cannot bound annki.language.fori_loop(), because the loop’s trip-count comparison has no 64-bit form.A signed scalar can be used as a
scalar_offsetargument to access-pattern methods for dynamic addressing, with one restriction: on an SBUF or PSUM operand the offset must benl.int32. On-chip buffers are megabytes, so a 64-bit offset there is rejected rather than silently narrowed.srcmust name exactly one element on one partition, which a 1x1 tile does, as does a 1x1 slice of a larger tile such astile[0:1, 3:4]or anap()whose counts are both 1. A source of several elements, or one spanning several partitions, will be rejected at this call.A
scalar_offsetonsrcis not supported, so the element to read is fixed while the kernel is built. Pass the offset to the access pattern of the instruction that consumes the scalar instead.By default the load runs on every engine and its result is available on all of them, which is what makes it the way to broadcast a value: one engine publishes with
nisa.scalar_store(..., engine=...)and every engine reads it back.engineopts out of that – the load runs on that one sequencer and the result is bound to it, so it can feed further scalar arithmetic and stores there, but not a loop bound, a loop-carried value, a condition, or an access pattern’sscalar_offset, stride or count.- Parameters:
src – An internal SBUF or HBM tile naming one element on one partition, with dtype
nl.int32,nl.uint32ornl.int64.engine – the sequencer to run the load on, e.g.
nisa.engine.sync. Defaults toNone, meaning every engine.
- Returns:
The loaded scalar, with
dtypematchingsrc.- Return type:
Example:
buf = nl.ndarray((1, 1), dtype=nl.int32, buffer=nl.sbuf) nisa.dma_copy(dst=buf, src=src) idx = nisa.scalar_load(buf) idx = idx + 1 # arithmetic on the scalar
This document is relevant for: Trn2, Trn3