This document is relevant for: Trn2, Trn3
nki.language.NkiGrid#
- class nki.language.NkiGrid(**kwargs: Any)[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.Methods
Attributes
Return the memory region containing the backing tensor.
Expose the complete grid selection as one native tensor operand.
Return the element dtype of the backing tensor.
Return the number of selected elements along each source dimension.
Return each grid-coordinate step in source-element units.
Return whether
datacan represent this selection exactly.Return whether this selection contains a partial boundary tile.
Return the extents of the grid coordinates available for indexing.
Return the nominal source-element shape of one tile.
- 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, and physical gaps.- 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.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.
- 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