This document is relevant for: Trn2, Trn3
nki.language.alloc_blocks#
- nki.language.alloc_blocks(*, tile_shape: tuple[int, ...], block_shape: tuple[int, ...], element_shape: tuple[int, ...], buffer: MemoryRegion, dtype: str, address: Optional[tuple] = None, name: str = '') NkiBlockGrid[source]#
Allocate new storage organized as a grid of tile blocks.
alloc_blockscreates the same tile geometry asnki.language.alloc_tiles(), then adds a block-coordinate level above it.tile_shapedefines the compute grain,block_shapedefines the number of tiles in one transfer or reuse grain, andelement_shaperemains the exact logical tensor extent.Block grouping does not change physical tile order or add per-block allocations. HBM uses the compact logical extent as backing storage; SBUF and PSUM use the same dense rank-two capacity layout as
nki.language.alloc_tiles().The returned
NkiBlockGridowns one independent uninitialized allocation.- Parameters:
tile_shape – The positive source-element shape of one compute tile. It must have at least two dimensions and applies to the trailing dimensions of
element_shape.block_shape – The positive tile counts grouped along each block dimension. Its rank must equal
tile_shape.element_shape – The exact positive logical element extent. Any dimensions preceding
tile_shaperemain untiled.buffer – Destination
MemoryRegion, such asnl.shared_hbm,nl.sbuf, ornl.psum.dtype – Destination NKI element dtype.
address – Optional manual address
(partition_offset, free_offset)with the same contract asnki.language.ndarray().name – the name of the tensor, used in scheduling.
- Returns:
A new
NkiBlockGridover independent, uninitialized storage. Partial boundary blocks and tiles retain exact logical extents; aggregatedataaccess requires one exact native access pattern.- Raises:
TypeError – if a shape is not a tuple of exact Python integers or
bufferis not an NKIMemoryRegion.ValueError – if a shape entry is nonpositive,
tile_shapehas fewer than two dimensions or greater rank thanelement_shape,block_shapeandtile_shaperanks differ, orbufferis unsupported.AssertionError – if the native allocation violates a
nki.language.ndarray()constraint, including PSUM address alignment.
Examples:
import nki import nki.isa as nisa import nki.language as nl @nki.jit def initialize_block_grid(): staging = nl.alloc_blocks( tile_shape=(128, 128), block_shape=(2, 2), element_shape=(512, 512), buffer=nl.sbuf, dtype=nl.bfloat16, ) dst = nl.ndarray( (512, 512), dtype=nl.bfloat16, buffer=nl.shared_hbm ) dst_blocks = nl.blocks( dst, tile_shape=(128, 128), block_shape=(2, 2), ) nisa.memset(dst=staging.data, value=0.0) nisa.dma_copy(dst=dst_blocks.data, src=staging.data) return dst
See also
nki.language.blocks(): Group tiles in existing storage.nki.language.alloc_tiles(): Allocate a tile grid without blocks.
This document is relevant for: Trn2, Trn3