For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).
Mojo struct
ManagedTensorSlice
struct ManagedTensorSlice[mut: Bool, input: IO, dtype: DType, rank: Int, InFusion: InputFusion, OutFusion: OutputFusion, ComputeFusion: ComputeOutputFusion, ComputeFusionTile: ComputeOutputFusionTile, //, io_spec: IOSpec[mut, input], *, static_spec: StaticTensorSpec[dtype, rank, static_spec.static_layout, InFusion, OutFusion, ComputeFusion, ComputeFusionTile]]
A view of a tensor that does not own the underlying allocated pointer. When the object lifetime ends it does not free the underlying pointer. Conversely, if a ManagedTensorSlice is created, it will not extend the life of the underlying pointer.
Therefore, the user must take care to keep the pointer alive until the last
use of a ManagedTensorSlice instance. This class is useful for writing
custom operations where memory is managed by an external runtime like in
MAX's inference stack.
Fieldsβ
- βin_fusion (
InFusion): - βout_fusion (
OutFusion): - βcompute_fusion (
ComputeFusion): - βcompute_fusion_tile (
ComputeFusionTile):
Implemented traitsβ
AnyType,
Copyable,
DevicePassable,
ImplicitlyCopyable,
ImplicitlyDeletable,
Movable,
RegisterPassable,
TrivialRegisterPassable,
Writable
comptime membersβ
address_spaceβ
comptime address_space = static_spec.address_space
alignmentβ
comptime alignment = static_spec.alignment
device_typeβ
comptime device_type = LayoutTensor[dtype, static_spec.to_layout(), MutAnyOrigin]
RuntimeLayoutβ
comptime RuntimeLayout = Layout[static_layout._shape_types, static_layout._stride_types]
Methodsβ
__init__β
def __init__(ptr: UnsafePointer[Scalar[dtype]], shape: IndexList[rank]) -> Self
Initializes a ManagedTensorSlice from a pointer and shape.
In general, custom operations should not create ManagedTensorSlice
instances, but instead use the ones provided by the MAX inference
engine.
def __init__(ptr: UnsafePointer[Scalar[dtype]], shape: IndexList[rank], strides: IndexList[rank]) -> Self
Initializes a ManagedTensorSlice from a pointer, shape, and strides.
In general, custom operations should not create ManagedTensorSlice
instances, but instead use the ones provided by the MAX inference
engine.
def __init__(ptr: UnsafePointer[Scalar[dtype]], shape: Coord[static_spec.static_layout._shape_types], strides: Coord[static_spec.static_layout._stride_types]) -> Self
Initializes a ManagedTensorSlice from a pointer, shape, and strides.
The shape and strides are provided as Coords; their runtime values
fill the dynamic dimensions of the tensor slice's layout.
In general, custom operations should not create ManagedTensorSlice
instances, but instead use the ones provided by the MAX inference
engine.
__getitem__β
def __getitem__(self, indices: IndexList[rank]) -> Scalar[dtype]
Gets the value at the specified indices.
Args:
- βindices (
IndexList[rank]): The indices of the value to retrieve.
Returns:
Scalar[dtype]: The value at the specified indices.
def __getitem__(self, *indices: Int) -> Scalar[dtype]
Gets the value at the specified indices.
Args:
- β*indices (
Int): The indices of the value to retrieve.
Returns:
Scalar[dtype]: The value at the specified indices.
__setitem__β
def __setitem__(self, *indices: Int, *, val: Scalar[dtype])
Stores the value at the specified indices.
Args:
- β*indices (
Int): The indices of the value to store. - βval (
Scalar[dtype]): The value to store.
def __setitem__(self, indices: IndexList[rank], val: Scalar[dtype])
Stores the value at the specified indices.
Args:
- βindices (
IndexList[rank]): The indices of the value to store. - βval (
Scalar[dtype]): The value to store.
get_type_nameβ
shapeβ
def shape(self) -> IndexList[rank]
Gets the shape of this tensor slice, as an IndexList.
Returns:
IndexList[rank]: The shape of this tensor slice.
shape_coordβ
def shape_coord(self) -> Coord[static_spec.static_layout._shape_types]
Gets the shape of this tensor slice as a Coord.
Unlike shape, which returns a runtime IndexList, the returned
Coord preserves the static-vs-dynamic structure of the tensor's static
layout: statically-known dimensions are encoded as compile-time values
in the Coord's type, while dynamic dimensions are filled from the
runtime shape.
Returns:
Coord[static_spec.static_layout._shape_types]: The shape of this tensor slice as a Coord.
strides_coordβ
def strides_coord(self) -> Coord[static_spec.static_layout._stride_types]
Gets the strides of this tensor slice as a Coord.
Unlike strides, which returns a runtime IndexList, the returned
Coord preserves the static-vs-dynamic structure of the tensor's static
layout: statically-known strides are encoded as compile-time values in
the Coord's type, while dynamic strides are filled from the runtime
strides.
Returns:
Coord[static_spec.static_layout._stride_types]: The strides of this tensor slice as a Coord.
runtime_layoutβ
def runtime_layout(self) -> Self.RuntimeLayout
Gets the runtime layout of this tensor slice.
The layout bundles the shape and strides as Coords, preserving the
static-vs-dynamic structure of the tensor's static layout.
Returns:
Self.RuntimeLayout: The runtime layout of this tensor slice.
dim_sizeβ
def dim_size(self, index: Int) -> Int
Gets the size of a given dimension of this tensor slice using a run time value.
Args:
- βindex (
Int): The zero-based index of the dimension.
Returns:
Int: The size of the tensor slice in the given dimension.
def dim_size[index: Int](self) -> Int
Gets the size of a given dimension of this tensor slice using a compile time value.
Parameters:
- βindex (
Int): The zero-based index of the dimension.
Returns:
Int: The size of the tensor slice in the given dimension.
stridesβ
def strides(self) -> IndexList[rank]
Gets the strides of this tensor slice, as an IndexList.
Returns:
IndexList[rank]: The strides of this tensor slice.
stride_lengthβ
def stride_length(self, index: Int) -> Int
Gets the length of the stride of a given dimension of this tensor slice using a run time value.
Args:
- βindex (
Int): The zero-based index of the dimension.
Returns:
Int: The size of the tensor slice in the given dimension.
def stride_length[index: Int](self) -> Int
Gets the length of the stride of a given dimension of this tensor slice using a compile time value.
Parameters:
- βindex (
Int): The zero-based index of the dimension.
Returns:
Int: The size of the tensor slice in the given dimension.
sizeβ
def size(self) -> Int
Computes the tensor slice's number of elements.
Returns:
Int: The total number of elements in the tensor slice.
bytecountβ
def bytecount(self) -> Int
Returns the size of the tensor slice in bytes.
Returns:
Int: The total number of bytes in the tensor slice.
unsafe_ptrβ
def unsafe_ptr[_dtype: DType = dtype](self) -> UnsafePointer[Scalar[_dtype], MutAnyOrigin]
Get the pointer stored in this tensor slice.
Since this method obtains the pointer stored in this tensor slice, it can modify the invariants of this tensor slice and lead to unexpected behavior. It should be used with caution.
Parameters:
- β_dtype (
DType): The type of theUnsafePointerin this tensor slice.
Returns:
UnsafePointer[Scalar[_dtype], MutAnyOrigin]: The UnsafePointer which contains the data for this tensor slice.
to_device_bufferβ
def to_device_buffer(self, ctx: DeviceContext) -> DeviceBuffer[dtype]
Returns:
DeviceBuffer[dtype]
loadβ
def load[width: Int, _rank: Int, element_alignment: Int = 1](self, index: IndexList[_rank]) -> SIMD[dtype, width]
Gets data from this tensor slice as a SIMD.
Parameters:
- βwidth (
Int): The width of theSIMDvalue. This must be large enough to contain the data from this tensor slice. - β_rank (
Int): The rank of the tensor slice. - βelement_alignment (
Int): Indicate the alignment of the pointer stored to memory. This is needed to issue vector load for GPUs with strict alignment requirements.
Args:
- βindex (
IndexList[_rank]): AnIndexListof size_rankto indicate the dimension of the tensor slice to obtain data from.
Returns:
SIMD[dtype, width]: Data from this tensor slice at dimension index.
def load[width: Int, element_alignment: Int = 1](self, index: Coord) -> SIMD[dtype, width]
Gets data from this tensor slice as a SIMD, indexed by a Coord.
Parameters:
- βwidth (
Int): The width of theSIMDvalue. This must be large enough to contain the data from this tensor slice. - βelement_alignment (
Int): Indicate the alignment of the pointer stored to memory. This is needed to issue vector load for GPUs with strict alignment requirements.
Args:
- βindex (
Coord): ACoordindicating the dimension of the tensor slice to obtain data from.
Returns:
SIMD[dtype, width]: Data from this tensor slice at dimension index.
storeβ
def store[width: Int, _rank: Int, element_alignment: Int = 1](self: ManagedTensorSlice[static_spec=static_spec], index: IndexList[_rank], val: SIMD[dtype, width])
Sets data in this tensor slice from a SIMD.
Parameters:
- βwidth (
Int): The width of theSIMDvalue. - β_rank (
Int): The rank of the tensor slice. - βelement_alignment (
Int): Indicate the alignment of the pointer stored to memory. This is needed to issue vector store for GPUs with strict alignment requirements.
Args:
- βindex (
IndexList[_rank]): AnIndexListof size_rankto indicate the dimension of the tensor slice to set data in. - βval (
SIMD[dtype, width]): The data to set into this tensor slice.
def store[width: Int, element_alignment: Int = 1](self: ManagedTensorSlice[static_spec=static_spec], index: Coord, val: SIMD[dtype, width])
Sets data in this tensor slice from a SIMD, indexed by a Coord.
Parameters:
- βwidth (
Int): The width of theSIMDvalue. - βelement_alignment (
Int): Indicate the alignment of the pointer stored to memory. This is needed to issue vector store for GPUs with strict alignment requirements.
Args:
- βindex (
Coord): ACoordindicating the dimension of the tensor slice to set data in. - βval (
SIMD[dtype, width]): The data to set into this tensor slice.
with_tile_layoutβ
def with_tile_layout[new_layout: TensorLayout](self, new_runtime_shape: IndexList[new_layout.rank], new_runtime_strides: IndexList[new_layout.rank], offset_ptr: Optional[UnsafePointer[Scalar[dtype], MutAnyOrigin]] = None) -> ManagedTensorSlice[io_spec, static_spec=static_spec.with_tile_layout[new_layout]()]
Returns:
ManagedTensorSlice[io_spec, static_spec=static_spec.with_tile_layout[new_layout]()]
to_layout_tensorβ
def to_layout_tensor(self) -> LayoutTensor[dtype, static_spec.to_layout(), MutAnyOrigin]
Returns:
LayoutTensor[dtype, static_spec.to_layout(), MutAnyOrigin]
to_tile_tensorβ
def to_tile_tensor[coord_dtype: DType = DType.int64](self) -> TileTensor[dtype, Layout[static_spec.static_layout._shape_types, static_spec.static_layout._stride_types], MutUntrackedOrigin]
Returns:
TileTensor[dtype, Layout[static_spec.static_layout._shape_types, static_spec.static_layout._stride_types], MutUntrackedOrigin]
write_toβ
def write_to(self, mut writer: T)
Formats this buffer to the provided Writer.
Args:
- βwriter (
T): The object to write to.
write_repr_toβ
def write_repr_to(self, mut writer: T)
Formats this buffer to the provided Writer.
Args:
- βwriter (
T): The object to write to.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!