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 trait
TensorLayout
Trait defining the interface for mixed compile-time/runtime layouts.
Implementors map logical multi-dimensional coordinates to linear memory indices, with support for dimensions that are known at compile time or determined at runtime.
Implemented traitsβ
AnyType,
Copyable,
ImplicitlyCopyable,
ImplicitlyDeletable,
Movable,
RegisterPassable,
TrivialRegisterPassable
comptime membersβ
all_dims_knownβ
comptime all_dims_known = _Self.shape_known and _Self.stride_known
Whether all shape and stride dimensions are known at compile time.
flat_rankβ
comptime flat_rank
The number of dimensions after flattening nested coordinates.
rankβ
comptime rank
The number of dimensions in the layout.
shape_knownβ
comptime shape_known
Whether all shape dimensions are known at compile time.
static_cosizeβ
comptime static_cosize
The compile-time size of the memory region spanned by the layout.
static_productβ
comptime static_product
The compile-time product of all shape dimensions.
static_shapeβ
comptime static_shape[i: Int]
Returns the compile-time value of the i-th shape dimension.
Parametersβ
- βi (
Int): The dimension index.
static_strideβ
comptime static_stride[i: Int]
Returns the compile-time value of the i-th stride dimension.
Parametersβ
- βi (
Int): The dimension index.
stride_knownβ
comptime stride_known
Whether all stride dimensions are known at compile time.
Required methodsβ
__init__β
def __init__(out self, *, copy: Self)
Create a new instance of the value by copying an existing one.
Args:
- βcopy (
_Self): The value to copy.
Returns:
_Self
def __init__(out self, *, deinit move: Self)
Create a new instance of the value by moving the value of another.
Args:
- βmove (
_Self): The value to move.
Returns:
_Self
shapeβ
def shape[i: Int](self) -> _Self.__shape_types[SIMDLength(i)]
Returns the i-th shape dimension.
Parameters:
- βi (
Int): The dimension index.
Returns:
_Self.__shape_types[SIMDLength(i)]: The shape value for dimension i.
strideβ
def stride[i: Int](self) -> _Self.__stride_types[SIMDLength(i)]
Returns the i-th stride dimension.
Parameters:
- βi (
Int): The dimension index.
Returns:
_Self.__stride_types[SIMDLength(i)]: The stride value for dimension i.
productβ
def product(self) -> Int
Returns the total number of elements in the layout's domain.
Returns:
Int: The product of all shape dimensions.
sizeβ
def size(self) -> Int
Returns the total number of elements. Alias for product().
Returns:
Int: The product of all shape dimensions.
__call__β
def __call__[index_type: CoordLike, *, linear_idx_type: DType = DType.int64](self, index: index_type) -> Scalar[linear_idx_type]
Maps a logical coordinate to a linear memory index.
Parameters:
- βindex_type (
CoordLike): The coordinate type. - βlinear_idx_type (
DType): The data type for the returned linear index.
Args:
- βindex (
index_type): The logical coordinates to map.
Returns:
Scalar[linear_idx_type]: The linear memory index corresponding to the given coordinates.
idx2crdβ
def idx2crd[*, out_dtype: DType = DType.int64](self, idx: Int) -> Coord[*#kgen.param_list.tabulate(len(_Self.__shape_types), [idx: __mlir_type.index] Coord[*#kgen.param_list.tabulate(len(_Self.__shape_types[SIMDLength(Int(idx))]._ParamListType), [idx: __mlir_type.index] Scalar[out_dtype])] if _Self.__shape_types[SIMDLength(Int(idx))].is_tuple else Scalar[out_dtype])]
Maps a linear memory index back to logical coordinates.
This is the inverse of __call__ (crd2idx). Given a linear index,
it computes the corresponding multi-dimensional coordinates.
For hierarchical layouts (e.g. from zipped_divide), the result
preserves the nested coordinate structure.
Examples: For a layout with shape (3, 4) and row-major strides:
- layout.idx2crd(0) returns (0, 0).
- layout.idx2crd(5) returns (1, 1).
- layout.idx2crd(11) returns (2, 3).
Parameters:
- βout_dtype (
DType): The data type for the output coordinate values.
Args:
- βidx (
Int): The linear memory index to convert to coordinates.
Returns:
Coord[*#kgen.param_list.tabulate(len(_Self.__shape_types), [idx: __mlir_type.index] Coord[*#kgen.param_list.tabulate(len(_Self.__shape_types[SIMDLength(Int(idx))]._ParamListType), [idx: __mlir_type.index] Scalar[out_dtype])] if _Self.__shape_types[SIMDLength(Int(idx))].is_tuple else Scalar[out_dtype])]: A Coord containing the logical coordinates corresponding to
the linear index. For nested layouts, the result mirrors the
shape nesting with Scalar leaves.
shape_coordβ
def shape_coord(self) -> Coord[_Self._shape_types]
Returns the full shape as a Coord.
Returns:
Coord[_Self._shape_types]: A Coord containing all shape dimensions.
stride_coordβ
def stride_coord(self) -> Coord[_Self._stride_types]
Returns the full stride as a Coord.
Returns:
Coord[_Self._stride_types]: A Coord containing all stride dimensions.
transposeβ
def transpose(self) -> Layout[*?, *?]
Transposes the layout by reversing the order of dimensions.
For an n-dimensional layout, this reverses the order of both shapes and strides. For 2D layouts, this swaps rows and columns.
Returns:
Layout[*?, *?]: A new Layout with transposed dimensions.
make_dynamicβ
def make_dynamic[dtype: DType](self) -> Layout[*?, *?]
Converts all dimensions to runtime values of the given dtype.
Parameters:
- βdtype (
DType): The data type for the resultingScalarvalues.
Returns:
Layout[*?, *?]: A new Layout with all dimensions as Scalar[dtype].
Provided methodsβ
copyβ
def copy(self) -> Self
Explicitly construct a copy of self, a convenience method for Self(copy=self) when the type is inconvenient to write out.
Overriding this method is not allowed.
Returns:
_Self: A copy of this value.