This document is relevant for: Trn2, Trn3
nki.language.alloc_stream_buffers#
- nki.language.alloc_stream_buffers(view: nki.language.NkiTensor | nki.language.NkiTileGrid | nki.language.NkiBlockGrid, *, buffer: MemoryRegion, dim: int, num_slots: int = 2, dtype: Optional[str] = None, transpose_axes: Optional[tuple[int, ...]] = None, address: Optional[tuple] = None, name: str = '') NkiStreamBuffers[source]#
Allocate reusable on-chip slots while advancing through one view dimension.
Streaming kernels commonly walk a large HBM view while keeping only a few work areas on chip.
alloc_stream_buffersderives the shape of one logical step throughviewand allocatesnum_slotsindependent SBUF or PSUM destinations of that capacity.dimidentifies the coordinate inview.shapethat advances between stream iterations. Integer-indexing that coordinate while retaining every other coordinate defines one step. For a tile grid withshape == (4, 6)anddim=1, each step represents one four-tile column:Fig. 155 Logical iterations rotate through a fixed physical slot pool.#
Accessing
buffers[k]selects physical slotk % num_slotsand returns the same abstraction as the logical step: anNkiTensor,NkiTileGrid, orNkiBlockGrid.The kernel author chooses the buffer depth and writes pipeline stages in their logical order. The compiler enforces dependencies between operations that use the same storage.
kmust be a nonnegative compile-time Python integer from a static or statically unrolled loop; runtime slot selection is not supported.Every physical slot has the capacity of the first logical step. If the final step is a partial tensor, tile region, or block region,
buffers[k]returns a clamped view over the full-capacity slot.Creating the pool allocates uninitialized storage.
transpose_axesgives every slot the transpose-destination layout described bynki.language.empty_like().addressis the base address for the pool: the first slot uses it, and later slots advance its free offset by one aligned native backing extent using the same units asnki.language.ndarray().- Parameters:
view – An
NkiTensor,NkiTileGrid, orNkiBlockGrid. One integer step alongdimdefines a slot’s logical shape and abstraction type.buffer – Destination on-chip
MemoryRegion. It must benl.sbufornl.psum.dim – Zero-based dimension in
view.shapeto advance for each logical stream iteration. This argument is mandatory.num_slots – Positive number of separately allocated physical slots. Defaults to two.
dtype – Destination dtype for every slot. Defaults to
view.dtype.transpose_axes – Optional rank-specific transpose destination order; see
nki.language.empty_like().address – Optional base manual address
(partition_offset, free_offset)for the slot pool.name – Base tensor name. Nonempty names produce physical slot names suffixed with
_slot_0,_slot_1, and so on.
- Returns:
An
NkiStreamBufferspool withnum_slotsphysical allocations andview.shape[dim]valid logical indices.- Raises:
TypeError – if
viewis unsupported,bufferis not an NKIMemoryRegion,dimornum_slotsis not an exact Python integer,transpose_axesis not a tuple, oraddressis malformed.ValueError – if
bufferis not SBUF or PSUM,dimis outsideview.shape,num_slotsis not positive, a later step exceeds the first step’s capacity, or the requested allocation or transpose layout is invalid.AssertionError – if a native allocation fails manual address validation, including PSUM bank alignment.
Examples:
import nki import nki.isa as nisa import nki.language as nl @nki.jit def scale_with_three_stage_pipeline(src): # src.shape == (128, num_steps, free_size) num_steps = src.shape[1] assert num_steps >= 2 buffers = nl.alloc_stream_buffers( src, buffer=nl.sbuf, dim=1, num_slots=3, ) dst = nl.ndarray( src.shape, dtype=src.dtype, buffer=nl.shared_hbm ) # Prologue: prime the load and compute stages. nisa.dma_copy(dst=buffers[0], src=src[:, 0, :]) nisa.dma_copy(dst=buffers[1], src=src[:, 1, :]) nisa.tensor_scalar( dst=buffers[0], data=buffers[0], op0=nl.multiply, operand0=2.0, ) # Steady state: load k, compute k - 1, and store k - 2. for k in range(2, num_steps): nisa.dma_copy( dst=buffers[k], src=src[:, k, :], ) nisa.tensor_scalar( dst=buffers[k - 1], data=buffers[k - 1], op0=nl.multiply, operand0=2.0, ) nisa.dma_copy( dst=dst[:, k - 2, :], src=buffers[k - 2], ) # Epilogue: finish the final compute and drain both pending stores. nisa.tensor_scalar( dst=buffers[num_steps - 1], data=buffers[num_steps - 1], op0=nl.multiply, operand0=2.0, ) nisa.dma_copy( dst=dst[:, num_steps - 2, :], src=buffers[num_steps - 2], ) nisa.dma_copy( dst=dst[:, num_steps - 1, :], src=buffers[num_steps - 1], ) return dst
See also
NkiStreamBuffers: Static slot selection and remainder behavior.nki.language.empty_like(): Per-slot allocation semantics.
This document is relevant for: Trn2, Trn3