This document is relevant for: Trn2, Trn3
nki.language.NkiGrid#
- class nki.language.NkiGrid[source]#
Base class for tensor views organized into tiles or blocks.
NkiGridis the common interface forNkiTileGridandNkiBlockGrid. A grid describes an existing tensor region through logical tile or block coordinates while retaining its dtype, buffer, and underlying storage.Users do not construct
NkiGriddirectly. Grid views are created vianki.language.tiles()andnki.language.blocks():tile_grid = nl.tiles(src, tile_shape=(128, 256)) block_grid = nl.blocks( src, tile_shape=(128, 256), block_shape=(2, 2), )
nki.language.tiles()returns anNkiTileGrid, andnki.language.blocks()returns anNkiBlockGrid. Both concrete objects are instances ofNkiGrid. Refer to those factory functions for the tiling, block-grouping, and boundary behavior of the returned view.See also
nki.language.tiles(): Create a tile-coordinate view.nki.language.blocks(): Group tiles into block coordinates.- __getitem__(key: GridKey) Self[source]#
Select tile or block coordinates while preserving the grid type.
Supported key forms:
An
intselects and consumes one grid coordinate. Negative coordinates are resolved relative to the current grid extent.A
sliceselects a static positive-step coordinate range and preserves that navigation level with its selected count.An
int32oruint32SBUF scalar, annki.isa.NkiScalar, or anki.isa.VirtualRegisterselects and consumes one grid coordinate at runtime. Its value is applied whendatais requested.A
(K, 1)int32oruint32SBUF tensor withK > 1selectsKsource rows from the first remaining HBM tile dimension. The result’s partition extent becomesK; a(1, 1)tensor remains a scalar grid-coordinate key.A
GridElementOffsetselects and consumes one coordinate using a source-element offset relative to the current view.A tuple applies one key to each successive grid dimension. Omitted trailing dimensions remain available for later indexing, and at most one tuple entry may be a runtime key.
- Parameters:
key – One supported per-dimension key or a tuple of such keys.
- Returns:
The same concrete grid type as
self, sharing the source storage.- Raises:
TypeError – if a key or slice boundary has an unsupported type, or a runtime index has an unsupported buffer or dtype.
IndexError – if a static integer or element offset is outside the selected grid extent, too many keys are supplied, or the grid has no remaining coordinates at its current level.
ValueError – if a slice is empty, has a nonpositive step, or uses a non-static boundary; an element offset is misaligned; runtime selection would have a nonuniform result shape; a vector index has more rows than the selected tile’s partition extent; or an indirect-addressing constraint is violated.
- property buffer: MemoryRegion#
Return the memory region containing the backing tensor.
- Returns:
The
MemoryRegionbacking the view.
- property data: NkiTensor#
Expose the complete grid selection as one native tensor operand.
Grid navigation retains tile or block coordinates.
datais the explicit transition to theNkiTensoraccepted by NKI instructions. The returned tensor preserves the selection’s offsets, strides, physical gaps, and supported runtime index.- Returns:
An exact native tensor operand over the current grid region.
- Raises:
ValueError – if one native access pattern cannot represent the selected region, for example across an unsupported nonuniform partition remainder or an overly complex access pattern. Split the grid into representable regions in that case. Use
is_data_representableto test this before accessingdata.
Note
data.shapedescribes the native access-pattern traversal. It may differ fromelement_shapeand from the private backing allocation shape. For example, a complete two-dimensional tile grid can expose(P, tile_rows, tile_columns, F), while a contiguous free-dimension tail can compact to(P, valid_free_elements). Capacity reserved behind an SBUF or PSUM remainder is never exposed.When the selection covers exactly one tile (every remaining tile and block count is one, and no untiled leading HBM dimension remains),
data.shape == element_shape, including axes of length one. For example, a(128, 1, 4)cell hasdata.shape == (128, 1, 4). Tiles of rank five or more keep the coalesced traversal shape, and so does a selection that retains a leading dimension, becausedataplaces the partition axis first.Examples:
import nki import nki.isa as nisa import nki.language as nl @nki.jit def copy_grid_as_one_operand(src): # src is tile-aligned 2D, for example (256, 512). src_tiles = nl.tiles(src, tile_shape=(128, 128)) staging_tiles = nl.tiles( nl.ndarray( src_tiles.element_shape, dtype=src.dtype, buffer=nl.sbuf, ), tile_shape=src_tiles.tile_shape, ) dst = nl.ndarray( src.shape, dtype=src.dtype, buffer=nl.shared_hbm ) dst_tiles = nl.tiles(dst, tile_shape=(128, 128)) nisa.dma_copy(dst=staging_tiles.data, src=src_tiles.data) nisa.dma_copy(dst=dst_tiles.data, src=staging_tiles.data) return dst
- property dtype: str#
Return the element dtype of the backing tensor.
- Returns:
The grid’s NKI element dtype.
- property element_shape: tuple[int, ...]#
Return the number of selected elements along each source dimension.
- Returns:
The selected element counts in source-dimension order.
- property index_step_elements: tuple[int, ...]#
Return each grid-coordinate step in source-element units.
Use
nki.language.grid_element_offset()when an index is already expressed in these units.- Returns:
The source-element stride of each current grid coordinate.
- property is_data_representable: bool#
Return whether
datacan represent this selection exactly.- Returns:
Truewhen one native tensor operand can describe the complete selection.
- property is_remainder: bool#
Return whether this selection contains a partial boundary tile.
- Returns:
Truewhen at least one selected tile extent is partial.
- property shape: tuple[int, ...]#
Return the extents of the grid coordinates available for indexing.
Tile-grid dimensions count tiles and block-grid dimensions count blocks. Untiled leading HBM dimensions appear first. Counts include partial boundary cells. Integer indexing consumes a coordinate level; slicing preserves it with the selected count.
- Returns:
One positive count per currently indexable grid dimension.
This document is relevant for: Trn2, Trn3