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_buffers derives the shape of one logical step through view and allocates num_slots independent SBUF or PSUM destinations of that capacity.

dim identifies the coordinate in view.shape that advances between stream iterations. Integer-indexing that coordinate while retaining every other coordinate defines one step. For a tile grid with shape == (4, 6) and dim=1, each step represents one four-tile column:

Six stream iterations reusing two physical slots by modulo indexing.

Fig. 155 Logical iterations rotate through a fixed physical slot pool.#

Accessing buffers[k] selects physical slot k % num_slots and returns the same abstraction as the logical step: an NkiTensor, NkiTileGrid, or NkiBlockGrid.

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. k must 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_axes gives every slot the transpose-destination layout described by nki.language.empty_like(). address is 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 as nki.language.ndarray().

Parameters:
  • view – An NkiTensor, NkiTileGrid, or NkiBlockGrid. One integer step along dim defines a slot’s logical shape and abstraction type.

  • buffer – Destination on-chip MemoryRegion. It must be nl.sbuf or nl.psum.

  • dim – Zero-based dimension in view.shape to 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 NkiStreamBuffers pool with num_slots physical allocations and view.shape[dim] valid logical indices.

Raises:
  • TypeError – if view is unsupported, buffer is not an NKI MemoryRegion, dim or num_slots is not an exact Python integer, transpose_axes is not a tuple, or address is malformed.

  • ValueError – if buffer is not SBUF or PSUM, dim is outside view.shape, num_slots is 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