For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).
Mojo struct
TileTensorIter
struct TileTensorIter[mut: Bool, //, dtype: DType, LayoutType: TensorLayout, origin: Origin[mut=mut], /, *, address_space: AddressSpace = .GENERIC, circular: Bool = False, masked: Bool = False, linear_idx_type: DType = _get_index_type[LayoutType](address_space)]
Iterates over equally shaped tiles of a buffer.
Every tile shares LayoutType; the iterator advances by stride elements
from one tile to the next. A circular iterator wraps back to the start of
the buffer once the offset reaches bound.
Parameters
- mut (
Bool): Whether the iterator allows mutation of the underlying data. - dtype (
DType): The data type of the tensor elements. - LayoutType (
TensorLayout): The layout of each tile the iterator yields. - origin (
Origin[mut=mut]): The origin of the underlying pointer. - address_space (
AddressSpace): The memory address space of the buffer. - circular (
Bool): Whether iteration wraps around atbound. - masked (
Bool): Whether the tiles carry a runtime valid-row count innum_valid_rows. The iterator itself never clips the tile layout; consumers that copy a masked tile pass the count as the copy's explicit bound. - linear_idx_type (
DType): Signed integer type of offsets into the buffer.
Fields
- ptr (
Pointer[Scalar[dtype], origin, address_space=address_space]): Pointer to the start of the buffer being iterated. - tile_layout (
LayoutType): Layout shared by every tile. - offset (
Scalar[linear_idx_type]): Element offset of the current tile fromptr. - stride (
Scalar[linear_idx_type]): Element distance between consecutive tiles. - bound (
Scalar[linear_idx_type]): Buffer size in elements; the wrap-around point of a circular iterator. - num_valid_rows (
Int): Valid rows of each tile whenmasked; unused otherwise.
Implemented traits
AnyType,
Copyable,
Deinitable,
ImplicitlyCopyable,
Movable,
RegisterPassable,
TrivialRegisterPassable
comptime members
BitcastType
comptime BitcastType[target_dtype: DType] = TileTensorIter[target_dtype, LayoutType, origin, address_space=address_space, circular=circular, masked=masked, linear_idx_type=linear_idx_type]
The iterator type produced by bitcast.
Parameters
- target_dtype (
DType): The element type the buffer is viewed as.
linear_uint_type
comptime linear_uint_type = Scalar[linear_idx_type]
The integer type of offset, stride and bound.
ReshapeType
comptime ReshapeType[NewLayout: TensorLayout] = TileTensorIter[dtype, NewLayout, origin, address_space=address_space, circular=circular, masked=masked, linear_idx_type=linear_idx_type]
The iterator type produced by reshape.
Parameters
- NewLayout (
TensorLayout): The layout of each tile of the result.
TileType
comptime TileType = TileTensor[dtype, LayoutType, origin, address_space=address_space, linear_idx_type=linear_idx_type]
The TileTensor type produced by dereferencing the iterator.
Methods
__init__
def __init__(ptr: Pointer[Scalar[dtype], origin, address_space=address_space], tile_layout: LayoutType, *, bound: Int, stride: Int, offset: Int = Int(0), num_valid_rows: Int = Int(0)) -> Self
Creates an iterator over a buffer.
Args:
- ptr (
Pointer[Scalar[dtype], origin, address_space=address_space]): Pointer to the start of the buffer. - tile_layout (
LayoutType): Layout of every tile. - bound (
Int): Buffer size in elements; the wrap-around point whencircular. - stride (
Int): Element distance between consecutive tiles. - offset (
Int): Element offset of the first tile. - num_valid_rows (
Int): Valid rows of each tile; only meaningful whenmasked.
def __init__(ptr: Pointer[Scalar[dtype], origin, address_space=address_space], tile_layout: LayoutType, *, bound: Int, num_valid_rows: Int = Int(0)) -> Self where LayoutType.all_dims_known
Creates an iterator whose tiles are laid out back to back.
Args:
- ptr (
Pointer[Scalar[dtype], origin, address_space=address_space]): Pointer to the start of the buffer. - tile_layout (
LayoutType): Layout of every tile; its element count is the stride. - bound (
Int): Buffer size in elements; the wrap-around point whencircular. - num_valid_rows (
Int): Valid rows of each tile; only meaningful whenmasked.
__getitem__
def __getitem__(self) -> Self.TileType
Returns the tile at the current position.
Returns:
Self.TileType: A TileTensor view of the current tile.
__iadd__
def __iadd__(mut self, rhs: Int)
Advances by rhs tiles.
A circular iterator wraps with a modulo, so rhs may exceed the
number of tiles in the buffer.
Args:
- rhs (
Int): The number of tiles to advance.
get
def get(self) -> Self.TileType
Returns the tile at the current position.
Returns:
Self.TileType: A TileTensor view of the current tile.
next
def next(self, rhs: Int = Int(1)) -> Self
Returns an iterator rhs tiles ahead.
Args:
- rhs (
Int): The number of tiles to advance.
Returns:
Self: A copy of this iterator advanced by rhs tiles.
next_unsafe
def next_unsafe(self, rhs: Int = Int(1)) -> Self
Returns an iterator rhs tiles ahead, wrapping at most once.
Cheaper than next for a circular iterator: the wrap is a single
conditional subtract, so rhs * stride must be less than bound.
Args:
- rhs (
Int): The number of tiles to advance.
Returns:
Self: A copy of this iterator advanced by rhs tiles.
bitcast
def bitcast[target_dtype: DType](self) -> TileTensorIter[target_dtype, LayoutType, origin, address_space=address_space, circular=circular, masked=masked, linear_idx_type=linear_idx_type]
Reinterprets the buffer as target_dtype, keeping the tile layout, offset, stride and bound.
Parameters:
- target_dtype (
DType): The element type to view the buffer as.
Returns:
TileTensorIter[target_dtype, LayoutType, origin, address_space=address_space, circular=circular, masked=masked, linear_idx_type=linear_idx_type]: An iterator over the same memory and position.
reshape
def reshape[NewLayout: TensorLayout](self, new_layout: NewLayout) -> TileTensorIter[dtype, NewLayout, origin, address_space=address_space, circular=circular, masked=masked, linear_idx_type=linear_idx_type]
Views each tile under new_layout, keeping the offset, stride and bound.
Parameters:
- NewLayout (
TensorLayout): The layout of each tile of the result.
Args:
- new_layout (
NewLayout): The layout of each tile of the result.
Returns:
TileTensorIter[dtype, NewLayout, origin, address_space=address_space, circular=circular, masked=masked, linear_idx_type=linear_idx_type]: An iterator over the same memory and position.