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
RuntimeLayout
struct RuntimeLayout[layout: Layout, /, *, element_type: DType = DType.int64, linear_idx_type: DType = DType.int64]
A runtime-configurable layout that uses RuntimeTuple for storage.
This struct provides a layout implementation that can be modified at runtime,
unlike the static Layout type. It
uses RuntimeTuple for
shape and stride storage.
The layout must have statically known dimensions at compile time, but the actual shape and stride values can be modified during execution.
Parametersβ
- βlayout (
Layout): The staticLayouttype to base this runtime layout on. - βelement_type (
DType): The integer type of the each dimension element. Must be signed. - βlinear_idx_type (
DType): The integer type of the linear index into memory returned bycrd2idx. Must be signed.
Fieldsβ
- βshape (
RuntimeLayout[layout, element_type=element_type, linear_idx_type=linear_idx_type].ShapeType): The shape of the layout as a runtime tuple. Stores the size of each dimension. Uses the specified bitwidth and is unsigned. Must match the static layout's shape dimensions. - βstride (
RuntimeLayout[layout, element_type=element_type, linear_idx_type=linear_idx_type].StrideType): The stride of the layout as a runtime tuple. Stores the stride (step size) for each dimension. Uses 64-bit unsigned integers since strides can be large values. Must match the static layout's stride dimensions.
Implemented traitsβ
AnyType,
Copyable,
Defaultable,
ImplicitlyCopyable,
ImplicitlyDeletable,
Movable,
RegisterPassable,
TrivialRegisterPassable,
Writable
comptime membersβ
ShapeTypeβ
comptime ShapeType = RuntimeTuple[layout.shape, element_type=element_type]
Type alias for the runtime shape tuple.
StrideTypeβ
comptime StrideType = RuntimeTuple[layout.stride, element_type=linear_idx_type]
Type alias for the runtime stride tuple.
Methodsβ
__init__β
def __init__() -> Self
Initialize a RuntimeLayout with default values.
Creates a new RuntimeLayout instance with default shape and stride
values. Requires that the static layout has known dimensions at compile
time.
Constraints:
The static layout that this runtime layout is based on must have all dimensions known.
def __init__(shape: RuntimeTuple[layout.shape, element_type=element_type], stride: RuntimeTuple[layout.stride, element_type=linear_idx_type]) -> Self
Initialize a RuntimeLayout with specified shape and stride.
Args:
- βshape (
RuntimeTuple[layout.shape, element_type=element_type]): ARuntimeTuplecontaining the dimensions of each axis. - βstride (
RuntimeTuple[layout.stride, element_type=linear_idx_type]): ARuntimeTuplecontaining the stride values for each axis.
__call__β
def __call__(self, idx: Int) -> Scalar[linear_idx_type]
Convert a single index to a flat linear index.
Args:
- βidx (
Int): The one-dimensional index to convert.
Returns:
Scalar[linear_idx_type]: The corresponding flat linear index in the layout.
def __call__[t: IntTuple](self, idx: RuntimeTuple[t, element_type=idx.element_type]) -> Scalar[linear_idx_type]
Convert a multi-dimensional index to a flat linear index.
Parameters:
- βt (
IntTuple): TheIntTupletype for the index.
Args:
- βidx (
RuntimeTuple[t, element_type=idx.element_type]): ARuntimeTuplecontaining the multi-dimensional coordinates.
Returns:
Scalar[linear_idx_type]: The corresponding flat linear index in the layout.
idx2crdβ
def idx2crd[t: IntTuple](self, idx: RuntimeTuple[t, element_type=idx.element_type]) -> RuntimeTuple[idx2crd(t, layout.shape, layout.stride), element_type=element_type]
Converts a linear index to logical coordinates.
This is the inverse operation of the call method, mapping from a memory index back to the corresponding logical coordinates.
Parameters:
- βt (
IntTuple): TheIntTupletype for the index.
Args:
- βidx (
RuntimeTuple[t, element_type=idx.element_type]): The linear index to convert.
Returns:
RuntimeTuple[idx2crd(t, layout.shape, layout.stride), element_type=element_type]: The logical coordinates corresponding to the given index.
sizeβ
def size(self) -> Int
Calculate the total number of elements in the layout.
Returns:
Int: The product of all dimensions in the shape, representing the total
number of elements that can be addressed by this layout.
bound_check_requiredβ
def bound_check_required(self) -> Bool
Determine if bounds checking is required for this layout.
Returns:
Bool: True if any dimension in the shape differs from the static layout's
shape, False otherwise.
castβ
def cast[_element_type: DType, /, *, target_linear_idx_type: DType = linear_idx_type](self) -> RuntimeLayout[layout, element_type=_element_type, linear_idx_type=target_linear_idx_type]
Cast the layout to use a different element bitwidth.
Parameters:
- β_element_type (
DType): The target data type. - βtarget_linear_idx_type (
DType): The target linear idx type.
Returns:
RuntimeLayout[layout, element_type=_element_type, linear_idx_type=target_linear_idx_type]: A new RuntimeLayout with the shape cast to the specified type.
row_majorβ
static def row_major[rank: Int, //](shape: IndexList[rank, element_type=shape.element_type]) -> Self
Create a row-major layout from the given shape.
In row-major layout, elements with adjacent rightmost indices are adjacent in memory.
Parameters:
- βrank (
Int): The number of dimensions in the layout.
Args:
- βshape (
IndexList[rank, element_type=shape.element_type]): AnIndexListcontaining the dimensions of each axis.
Returns:
Self: A RuntimeLayout with row-major stride ordering.
col_majorβ
static def col_major[rank: Int, //](shape: IndexList[rank, element_type=shape.element_type]) -> Self
Create a column-major layout from the given shape.
In column-major layout, elements with adjacent leftmost indices are adjacent in memory.
Parameters:
- βrank (
Int): The number of dimensions in the layout.
Args:
- βshape (
IndexList[rank, element_type=shape.element_type]): AnIndexListcontaining the dimensions of each axis.
Returns:
Self: A RuntimeLayout with column-major stride ordering.
write_toβ
def write_to(self, mut writer: T)
Write a string representation of the layout to a writer.
Args:
- βwriter (
T): TheWriterobject to write the layout representation to.
sublayoutβ
def sublayout[i: Int](self) -> RuntimeLayout[layout[i], element_type=element_type, linear_idx_type=linear_idx_type]
Extract a nested sublayout at the specified index.
Parameters:
- βi (
Int): The index of the nested layout to extract.
Returns:
RuntimeLayout[layout[i], element_type=element_type, linear_idx_type=linear_idx_type]: A RuntimeLayout representing the nested layout at index i.
dimβ
def dim(self, i: Int) -> Int
Get the size of the dimension at the specified index.
Args:
- βi (
Int): The index of the dimension to retrieve.
Returns:
Int: The size of the dimension at index i.
__len__β
static def __len__() -> Int
Get the number of dimensions in the layout.
Returns:
Int: The number of dimensions (rank) of the layout.