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, whose dtype matches src and 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 as src; the same restriction applies to slices and views on such tiles.

PSUM cannot be used with scalar_load either. Use nki.isa.tensor_copy() to copy a value from PSUM to SBUF before loading it.

The source tensor must use nl.int32, nl.uint32 or nl.int64 dtype. Unsigned scalars use unsigned comparisons and arithmetic modulo 2**32.

Unlike the compute instructions, scalar_load accepts nl.int64, and the value is not truncated. An int64 scalar can be written back with nki.isa.scalar_store(), and can be passed as scalar_offset to address an HBM tensor beyond the 4GB reach of a 32-bit byte offset. It cannot bound an nki.language.fori_loop(), because the loop’s trip-count comparison has no 64-bit form.

A signed scalar can be used as a scalar_offset argument to access-pattern methods for dynamic addressing, with one restriction: on an SBUF or PSUM operand the offset must be nl.int32. On-chip buffers are megabytes, so a 64-bit offset there is rejected rather than silently narrowed.

src must name exactly one element on one partition, which a 1x1 tile does, as does a 1x1 slice of a larger tile such as tile[0:1, 3:4] or an ap() whose counts are both 1. A source of several elements, or one spanning several partitions, will be rejected at this call.

A scalar_offset on src is 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. engine opts 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’s scalar_offset, stride or count.

Parameters:
  • src – An internal SBUF or HBM tile naming one element on one partition, with dtype nl.int32, nl.uint32 or nl.int64.

  • engine – the sequencer to run the load on, e.g. nisa.engine.sync. Defaults to None, meaning every engine.

Returns:

The loaded scalar, with dtype matching src.

Return type:

NkiScalar

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