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
OwnedTensor
struct OwnedTensor[dtype: DType, address_space: AddressSpace, LayoutType: TensorLayout, *, Engine: OwnedTensorEngine]
Owns the storage for a tensor whose layout is known at compile time.
Construction allocates storage through Engine, and access goes through a
TileTensor built from the tensor, whose origin is tied to it. The tensor
is Movable exactly when its storage is.
Examples:
from layout import TileTensor, row_major
from layout.owned_tensor import OwnedGPUTensor
def kernel():
var owned = OwnedGPUTensor[.float32, .SHARED](row_major[4, 8]())
var tile = TileTensor(owned)
tile[1, 2] = 7.0Parameters
- dtype (
DType): The element data type of the tensor. - address_space (
AddressSpace): The address space the storage resides in. - LayoutType (
TensorLayout): The layout of the tensor. Constrained to layouts whose dimensions are all known at compile time. - Engine (
OwnedTensorEngine): The engine that supplies the storage.
Fields
- layout (
LayoutType): The layout that maps coordinates to offsets in the storage.
Implemented traits
AnyType,
Deinitable,
Movable (where conforms_to(Engine.StorageType[dtype, address_space, LayoutType], Movable))
comptime members
Storage
comptime Storage = Engine.StorageType[dtype, address_space, LayoutType]
The owning storage Engine supplies for this tensor.
Methods
__init__
def __init__(out self, layout: LayoutType)
Allocates storage for a tensor with the given layout.
The elements are not initialized.
Constraints:
Every dimension of LayoutType must be known at compile time.
Args:
- layout (
LayoutType): The layout of the tensor.
__getitem__
def __getitem__(ref self, a0: _IndexOrSlice) -> TileTensor[dtype, Layout[TypeList[#kgen.param_list.concat(ComptimeInt[Int((add (mul a0.start_index, -1), a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)]))] if (xor identical(a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)], -1), True) if (a0.start_index != Int(-1)) else (a0.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a0.kept else )](), TypeList[#kgen.param_list.concat(#kgen.param_list.tabulate(len(LayoutType.__stride_types), [idx: __mlir_type.index] LayoutType.__stride_types[idx] if a0.kept else ))]()], origin_of(self), Engine=Engine.ViewEngine.OffsetResultType[TypeList[ComptimeInt[_subscript_static_offset[TypeList[_IndexOrSlice[a0.static, a0.kept, a0.start_index, a0.end_index]](), LayoutType]()], Scalar[_get_index_type[LayoutType](address_space)]]()], address_space=address_space]
Fixes or narrows each dimension, returning a TileTensor view.
This is TileTensor(self)[...]: the view borrows from self, and
each argument is an index, which drops its dimension, or a slice,
which keeps it. Integer literals, Idx values and : are
compile-time, so owned[0, 2:4, :] keeps static extents. See
TileTensor.__getitem__ for the argument forms.
Example:
from layout import row_major
from layout.owned_tensor import OwnedGPUTensor
def kernel():
var owned = OwnedGPUTensor[.float32, .SHARED](row_major[4, 8]())
var row = owned[1:2, :]
row[0, 3] = 7.0Args:
- a0 (
_IndexOrSlice): The index or slice for dimension 0.
Returns:
def __getitem__(ref self, a0: _IndexOrSlice, a1: _IndexOrSlice) -> TileTensor[dtype, Layout[TypeList[#kgen.param_list.concat(ComptimeInt[Int((add (mul a0.start_index, -1), a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)]))] if (xor identical(a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)], -1), True) if (a0.start_index != Int(-1)) else (a0.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a0.kept else , ComptimeInt[Int((add (mul a1.start_index, -1), a1.end_index if (xor identical(a1.end_index, -1), True) else LayoutType.static_shape[Int(1)]))] if (xor identical(a1.end_index if (xor identical(a1.end_index, -1), True) else LayoutType.static_shape[Int(1)], -1), True) if (a1.start_index != Int(-1)) else (a1.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a1.kept else )](), TypeList[#kgen.param_list.concat(#kgen.param_list.tabulate(len(LayoutType.__stride_types), [idx: __mlir_type.index] LayoutType.__stride_types[idx] if _IndexOrSlice[a0.static, a0.kept, a0.start_index, a0.end_index], _IndexOrSlice[a1.static, a1.kept, a1.start_index, a1.end_index][idx].keeps_axis else ))]()], origin_of(self), Engine=Engine.ViewEngine.OffsetResultType[TypeList[ComptimeInt[_subscript_static_offset[TypeList[_IndexOrSlice[a0.static, a0.kept, a0.start_index, a0.end_index], _IndexOrSlice[a1.static, a1.kept, a1.start_index, a1.end_index]](), LayoutType]()], Scalar[_get_index_type[LayoutType](address_space)]]()], address_space=address_space]
Fixes or narrows each of 2 dimensions, returning a TileTensor view.
See the rank-1 overload.
Args:
- a0 (
_IndexOrSlice): The index or slice for dimension 0. - a1 (
_IndexOrSlice): The index or slice for dimension 1.
Returns:
def __getitem__(ref self, a0: _IndexOrSlice, a1: _IndexOrSlice, a2: _IndexOrSlice) -> TileTensor[dtype, Layout[TypeList[#kgen.param_list.concat(ComptimeInt[Int((add (mul a0.start_index, -1), a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)]))] if (xor identical(a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)], -1), True) if (a0.start_index != Int(-1)) else (a0.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a0.kept else , ComptimeInt[Int((add (mul a1.start_index, -1), a1.end_index if (xor identical(a1.end_index, -1), True) else LayoutType.static_shape[Int(1)]))] if (xor identical(a1.end_index if (xor identical(a1.end_index, -1), True) else LayoutType.static_shape[Int(1)], -1), True) if (a1.start_index != Int(-1)) else (a1.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a1.kept else , ComptimeInt[Int((add (mul a2.start_index, -1), a2.end_index if (xor identical(a2.end_index, -1), True) else LayoutType.static_shape[Int(2)]))] if (xor identical(a2.end_index if (xor identical(a2.end_index, -1), True) else LayoutType.static_shape[Int(2)], -1), True) if (a2.start_index != Int(-1)) else (a2.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a2.kept else )](), TypeList[#kgen.param_list.concat(#kgen.param_list.tabulate(len(LayoutType.__stride_types), [idx: __mlir_type.index] LayoutType.__stride_types[idx] if _IndexOrSlice[a0.static, a0.kept, a0.start_index, a0.end_index], _IndexOrSlice[a1.static, a1.kept, a1.start_index, a1.end_index], _IndexOrSlice[a2.static, a2.kept, a2.start_index, a2.end_index][idx].keeps_axis else ))]()], origin_of(self), Engine=Engine.ViewEngine.OffsetResultType[TypeList[ComptimeInt[_subscript_static_offset[TypeList[_IndexOrSlice[a0.static, a0.kept, a0.start_index, a0.end_index], _IndexOrSlice[a1.static, a1.kept, a1.start_index, a1.end_index], _IndexOrSlice[a2.static, a2.kept, a2.start_index, a2.end_index]](), LayoutType]()], Scalar[_get_index_type[LayoutType](address_space)]]()], address_space=address_space]
Fixes or narrows each of 3 dimensions, returning a TileTensor view.
See the rank-1 overload.
Args:
- a0 (
_IndexOrSlice): The index or slice for dimension 0. - a1 (
_IndexOrSlice): The index or slice for dimension 1. - a2 (
_IndexOrSlice): The index or slice for dimension 2.
Returns:
def __getitem__(ref self, a0: _IndexOrSlice, a1: _IndexOrSlice, a2: _IndexOrSlice, a3: _IndexOrSlice) -> TileTensor[dtype, Layout[TypeList[#kgen.param_list.concat(ComptimeInt[Int((add (mul a0.start_index, -1), a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)]))] if (xor identical(a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)], -1), True) if (a0.start_index != Int(-1)) else (a0.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a0.kept else , ComptimeInt[Int((add (mul a1.start_index, -1), a1.end_index if (xor identical(a1.end_index, -1), True) else LayoutType.static_shape[Int(1)]))] if (xor identical(a1.end_index if (xor identical(a1.end_index, -1), True) else LayoutType.static_shape[Int(1)], -1), True) if (a1.start_index != Int(-1)) else (a1.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a1.kept else , ComptimeInt[Int((add (mul a2.start_index, -1), a2.end_index if (xor identical(a2.end_index, -1), True) else LayoutType.static_shape[Int(2)]))] if (xor identical(a2.end_index if (xor identical(a2.end_index, -1), True) else LayoutType.static_shape[Int(2)], -1), True) if (a2.start_index != Int(-1)) else (a2.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a2.kept else , ComptimeInt[Int((add (mul a3.start_index, -1), a3.end_index if (xor identical(a3.end_index, -1), True) else LayoutType.static_shape[Int(3)]))] if (xor identical(a3.end_index if (xor identical(a3.end_index, -1), True) else LayoutType.static_shape[Int(3)], -1), True) if (a3.start_index != Int(-1)) else (a3.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a3.kept else )](), TypeList[#kgen.param_list.concat(#kgen.param_list.tabulate(len(LayoutType.__stride_types), [idx: __mlir_type.index] LayoutType.__stride_types[idx] if _IndexOrSlice[a0.static, a0.kept, a0.start_index, a0.end_index], _IndexOrSlice[a1.static, a1.kept, a1.start_index, a1.end_index], _IndexOrSlice[a2.static, a2.kept, a2.start_index, a2.end_index], _IndexOrSlice[a3.static, a3.kept, a3.start_index, a3.end_index][idx].keeps_axis else ))]()], origin_of(self), Engine=Engine.ViewEngine.OffsetResultType[TypeList[ComptimeInt[_subscript_static_offset[TypeList[_IndexOrSlice[a0.static, a0.kept, a0.start_index, a0.end_index], _IndexOrSlice[a1.static, a1.kept, a1.start_index, a1.end_index], _IndexOrSlice[a2.static, a2.kept, a2.start_index, a2.end_index], _IndexOrSlice[a3.static, a3.kept, a3.start_index, a3.end_index]](), LayoutType]()], Scalar[_get_index_type[LayoutType](address_space)]]()], address_space=address_space]
Fixes or narrows each of 4 dimensions, returning a TileTensor view.
See the rank-1 overload.
Args:
- a0 (
_IndexOrSlice): The index or slice for dimension 0. - a1 (
_IndexOrSlice): The index or slice for dimension 1. - a2 (
_IndexOrSlice): The index or slice for dimension 2. - a3 (
_IndexOrSlice): The index or slice for dimension 3.
Returns:
def __getitem__(ref self, a0: _IndexOrSlice, a1: _IndexOrSlice, a2: _IndexOrSlice, a3: _IndexOrSlice, a4: _IndexOrSlice) -> TileTensor[dtype, Layout[TypeList[#kgen.param_list.concat(ComptimeInt[Int((add (mul a0.start_index, -1), a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)]))] if (xor identical(a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)], -1), True) if (a0.start_index != Int(-1)) else (a0.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a0.kept else , ComptimeInt[Int((add (mul a1.start_index, -1), a1.end_index if (xor identical(a1.end_index, -1), True) else LayoutType.static_shape[Int(1)]))] if (xor identical(a1.end_index if (xor identical(a1.end_index, -1), True) else LayoutType.static_shape[Int(1)], -1), True) if (a1.start_index != Int(-1)) else (a1.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a1.kept else , ComptimeInt[Int((add (mul a2.start_index, -1), a2.end_index if (xor identical(a2.end_index, -1), True) else LayoutType.static_shape[Int(2)]))] if (xor identical(a2.end_index if (xor identical(a2.end_index, -1), True) else LayoutType.static_shape[Int(2)], -1), True) if (a2.start_index != Int(-1)) else (a2.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a2.kept else , ComptimeInt[Int((add (mul a3.start_index, -1), a3.end_index if (xor identical(a3.end_index, -1), True) else LayoutType.static_shape[Int(3)]))] if (xor identical(a3.end_index if (xor identical(a3.end_index, -1), True) else LayoutType.static_shape[Int(3)], -1), True) if (a3.start_index != Int(-1)) else (a3.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a3.kept else , ComptimeInt[Int((add (mul a4.start_index, -1), a4.end_index if (xor identical(a4.end_index, -1), True) else LayoutType.static_shape[Int(4)]))] if (xor identical(a4.end_index if (xor identical(a4.end_index, -1), True) else LayoutType.static_shape[Int(4)], -1), True) if (a4.start_index != Int(-1)) else (a4.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a4.kept else )](), TypeList[#kgen.param_list.concat(#kgen.param_list.tabulate(len(LayoutType.__stride_types), [idx: __mlir_type.index] LayoutType.__stride_types[idx] if _IndexOrSlice[a0.static, a0.kept, a0.start_index, a0.end_index], _IndexOrSlice[a1.static, a1.kept, a1.start_index, a1.end_index], _IndexOrSlice[a2.static, a2.kept, a2.start_index, a2.end_index], _IndexOrSlice[a3.static, a3.kept, a3.start_index, a3.end_index], _IndexOrSlice[a4.static, a4.kept, a4.start_index, a4.end_index][idx].keeps_axis else ))]()], origin_of(self), Engine=Engine.ViewEngine.OffsetResultType[TypeList[ComptimeInt[_subscript_static_offset[TypeList[_IndexOrSlice[a0.static, a0.kept, a0.start_index, a0.end_index], _IndexOrSlice[a1.static, a1.kept, a1.start_index, a1.end_index], _IndexOrSlice[a2.static, a2.kept, a2.start_index, a2.end_index], _IndexOrSlice[a3.static, a3.kept, a3.start_index, a3.end_index], _IndexOrSlice[a4.static, a4.kept, a4.start_index, a4.end_index]](), LayoutType]()], Scalar[_get_index_type[LayoutType](address_space)]]()], address_space=address_space]
Fixes or narrows each of 5 dimensions, returning a TileTensor view.
See the rank-1 overload.
Args:
- a0 (
_IndexOrSlice): The index or slice for dimension 0. - a1 (
_IndexOrSlice): The index or slice for dimension 1. - a2 (
_IndexOrSlice): The index or slice for dimension 2. - a3 (
_IndexOrSlice): The index or slice for dimension 3. - a4 (
_IndexOrSlice): The index or slice for dimension 4.
Returns:
def __getitem__(ref self, a0: _IndexOrSlice, a1: _IndexOrSlice, a2: _IndexOrSlice, a3: _IndexOrSlice, a4: _IndexOrSlice, a5: _IndexOrSlice) -> TileTensor[dtype, Layout[TypeList[#kgen.param_list.concat(ComptimeInt[Int((add (mul a0.start_index, -1), a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)]))] if (xor identical(a0.end_index if (xor identical(a0.end_index, -1), True) else LayoutType.static_shape[Int(0)], -1), True) if (a0.start_index != Int(-1)) else (a0.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a0.kept else , ComptimeInt[Int((add (mul a1.start_index, -1), a1.end_index if (xor identical(a1.end_index, -1), True) else LayoutType.static_shape[Int(1)]))] if (xor identical(a1.end_index if (xor identical(a1.end_index, -1), True) else LayoutType.static_shape[Int(1)], -1), True) if (a1.start_index != Int(-1)) else (a1.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a1.kept else , ComptimeInt[Int((add (mul a2.start_index, -1), a2.end_index if (xor identical(a2.end_index, -1), True) else LayoutType.static_shape[Int(2)]))] if (xor identical(a2.end_index if (xor identical(a2.end_index, -1), True) else LayoutType.static_shape[Int(2)], -1), True) if (a2.start_index != Int(-1)) else (a2.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a2.kept else , ComptimeInt[Int((add (mul a3.start_index, -1), a3.end_index if (xor identical(a3.end_index, -1), True) else LayoutType.static_shape[Int(3)]))] if (xor identical(a3.end_index if (xor identical(a3.end_index, -1), True) else LayoutType.static_shape[Int(3)], -1), True) if (a3.start_index != Int(-1)) else (a3.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a3.kept else , ComptimeInt[Int((add (mul a4.start_index, -1), a4.end_index if (xor identical(a4.end_index, -1), True) else LayoutType.static_shape[Int(4)]))] if (xor identical(a4.end_index if (xor identical(a4.end_index, -1), True) else LayoutType.static_shape[Int(4)], -1), True) if (a4.start_index != Int(-1)) else (a4.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a4.kept else , ComptimeInt[Int((add (mul a5.start_index, -1), a5.end_index if (xor identical(a5.end_index, -1), True) else LayoutType.static_shape[Int(5)]))] if (xor identical(a5.end_index if (xor identical(a5.end_index, -1), True) else LayoutType.static_shape[Int(5)], -1), True) if (a5.start_index != Int(-1)) else (a5.start_index != Int(-1)) else Scalar[_get_index_type[LayoutType](address_space)] if a5.kept else )](), TypeList[#kgen.param_list.concat(#kgen.param_list.tabulate(len(LayoutType.__stride_types), [idx: __mlir_type.index] LayoutType.__stride_types[idx] if _IndexOrSlice[a0.static, a0.kept, a0.start_index, a0.end_index], _IndexOrSlice[a1.static, a1.kept, a1.start_index, a1.end_index], _IndexOrSlice[a2.static, a2.kept, a2.start_index, a2.end_index], _IndexOrSlice[a3.static, a3.kept, a3.start_index, a3.end_index], _IndexOrSlice[a4.static, a4.kept, a4.start_index, a4.end_index], _IndexOrSlice[a5.static, a5.kept, a5.start_index, a5.end_index][idx].keeps_axis else ))]()], origin_of(self), Engine=Engine.ViewEngine.OffsetResultType[TypeList[ComptimeInt[_subscript_static_offset[TypeList[_IndexOrSlice[a0.static, a0.kept, a0.start_index, a0.end_index], _IndexOrSlice[a1.static, a1.kept, a1.start_index, a1.end_index], _IndexOrSlice[a2.static, a2.kept, a2.start_index, a2.end_index], _IndexOrSlice[a3.static, a3.kept, a3.start_index, a3.end_index], _IndexOrSlice[a4.static, a4.kept, a4.start_index, a4.end_index], _IndexOrSlice[a5.static, a5.kept, a5.start_index, a5.end_index]](), LayoutType]()], Scalar[_get_index_type[LayoutType](address_space)]]()], address_space=address_space]
Fixes or narrows each of 6 dimensions, returning a TileTensor view.
See the rank-1 overload.
Args:
- a0 (
_IndexOrSlice): The index or slice for dimension 0. - a1 (
_IndexOrSlice): The index or slice for dimension 1. - a2 (
_IndexOrSlice): The index or slice for dimension 2. - a3 (
_IndexOrSlice): The index or slice for dimension 3. - a4 (
_IndexOrSlice): The index or slice for dimension 4. - a5 (
_IndexOrSlice): The index or slice for dimension 5.
Returns: