This document is relevant for: Trn2, Trn3

nki.language.NkiStreamBuffers#

class nki.language.NkiStreamBuffers[source]#

A reusable pool of on-chip slots addressed by logical stream iteration.

Instances are created by nki.language.alloc_stream_buffers(). The factory derives one stream step from its source view, then allocates num_slots independent SBUF or PSUM work areas with equal physical capacity.

buffers[k] maps logical iteration k to physical slot k % buffers.num_slots. It returns an NkiTensor, NkiTileGrid, or NkiBlockGrid matching the source abstraction. When the source ends with a partial tensor, tile region, or block region, the final iteration returns a clamped zero-copy view over its full-capacity physical slot.

Selecting a slot allocates no storage, moves no data, and emits no instruction. Choose enough slots for the intended overlap and write operations in their logical order. The compiler enforces memory dependencies, including waits before a slot is overwritten.

Indices must be nonnegative compile-time Python integers in the logical stream domain. Indices from nl.fori_loop are compiled variables and are unsupported, even when the loop bounds are constant. Use range(source.shape[dim]) for static iteration over stream slots. Fixed indices such as buffers[0] can also be used inside compiled loops. Runtime indexing, negative indexing, implicit iteration, and sequence length are not supported.

Examples:

import nki
import nki.isa as nisa
import nki.language as nl

@nki.jit
def copy_columns_through_stream_slots(src):
    tile_shape = (128, 128)
    src_tiles = nl.tiles(src, tile_shape=tile_shape)
    dst = nl.ndarray(
        src.shape, dtype=src.dtype, buffer=nl.shared_hbm
    )
    dst_tiles = nl.tiles(dst, tile_shape=tile_shape)
    buffers = nl.alloc_stream_buffers(
        src_tiles, buffer=nl.sbuf, dim=1, num_slots=2
    )

    for j in range(src_tiles.shape[1]):
        slot = buffers[j]
        nisa.dma_copy(
            dst=slot.data,
            src=src_tiles[:, j].data,
        )
        nisa.dma_copy(
            dst=dst_tiles[:, j].data,
            src=slot.data,
        )
    return dst

See also

nki.language.alloc_stream_buffers(): Allocate and configure the

stream pool.

__getitem__(iteration_index: int) → StreamSlotT[source]#

Return the slot view for one static logical stream iteration.

The selected physical allocation is iteration_index % num_slots. Most iterations return the full-capacity slot object. When the final logical source step is partial, the method instead returns a clamped zero-copy view over that same physical allocation. The clamped result has the exact final tensor shape or grid element_shape and can be passed directly to a matching operation.

Repeated indices separated by num_slots alias the same physical storage. Place reads of the earlier value before writes for the later iteration. The compiler preserves that ordering when scheduling slot reuse.

The index is resolved while tracing. Runtime SBUF scalars, virtual registers, NumPy integer values, booleans, and negative integers are not accepted.

Parameters:

iteration_index – A nonnegative compile-time Python integer less than the logical iteration count view.shape[dim] used to create the pool.

Returns:

The exact NkiTensor, NkiTileGrid, or NkiBlockGrid view for this logical stream step.

Raises:
  • TypeError – if iteration_index is not an exact compile-time Python integer.

  • IndexError – if iteration_index is negative or outside the logical stream domain.

Examples:

import nki
import nki.isa as nisa
import nki.language as nl

@nki.jit
def copy_with_rotating_slots(src):
    tile_shape = (128, 128)
    src_tiles = nl.tiles(src, tile_shape=tile_shape)
    dst = nl.ndarray(
        src.shape, dtype=src.dtype, buffer=nl.shared_hbm
    )
    dst_tiles = nl.tiles(dst, tile_shape=tile_shape)

    for i in range(src_tiles.shape[0]):
        src_row = src_tiles[i, :]
        dst_row = dst_tiles[i, :]
        buffers = nl.alloc_stream_buffers(
            src_row, buffer=nl.sbuf, dim=0, num_slots=2
        )

        for j in range(src_row.shape[0]):
            slot = buffers[j]
            nisa.dma_copy(dst=slot.data, src=src_row[j].data)
            nisa.dma_copy(dst=dst_row[j].data, src=slot.data)
    return dst
property num_slots: int#

Return the number of independently allocated physical slots.

This is the modulus used by __getitem__(): logical iteration k selects physical slot k % num_slots. It is the configured buffering depth, not the number of logical stream iterations.

Returns:

the positive physical slot count passed to nki.language.alloc_stream_buffers().

This document is relevant for: Trn2, Trn3