Mojo struct
ManagedTensorSlice
struct ManagedTensorSlice[mut: Bool, input: IO, dtype: DType, rank: Int, InFusion: InputFusion, OutFusion: OutputFusion, ComputeFusion: ComputeOutputFusion, //, io_spec: IOSpec[mut, input], *, static_spec: StaticTensorSpec[dtype, rank, static_spec.static_layout, InFusion, OutFusion, ComputeFusion]]
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):
Implemented traitsβ
AnyType,
Copyable,
DevicePassable,
ImplicitlyCopyable,
ImplicitlyDestructible,
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]
exclusiveβ
comptime exclusive = static_spec.exclusive
Methodsβ
__init__β
__init__(ptr: UnsafePointer[Scalar[dtype]], slices: InlineArray[Slice, rank], slicer_spec: RuntimeTensorSpec[dtype, rank]) -> Self
Initializes a ManagedTensorSlice from a pointer, array of slices and tensor spec.
In general, custom operations should not create ManagedTensorSlice
instances, but instead use the ones provided by the MAX inference
engine.
__init__(ptr: UnsafePointer[Scalar[dtype], MutAnyOrigin], spec: RuntimeTensorSpec[dtype, rank], strides: IndexList[rank]) -> Self
Initializes a ManagedTensorSlice from a pointer, runtime tensor spec, and strides.
In general, custom operations should not create ManagedTensorSlice
instances, but instead use the ones provided by the MAX inference
engine.
__init__(ptr: UnsafePointer[Scalar[dtype], MutAnyOrigin], 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.
__init__(ptr: UnsafePointer[Scalar[dtype], MutAnyOrigin], 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.
__getitem__β
__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.
__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__β
__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.
__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β
specβ
spec(self) -> RuntimeTensorSpec[dtype, rank]
Gets the TensorSpec of this tensor slice, which provides meta-data about the tensor slice.
Returns:
RuntimeTensorSpec[dtype, rank]: The static TensorSpec for this tensor slice.
shapeβ
shape(self) -> IndexList[rank]
Gets the shape of this tensor slice, as an IndexList.
Returns:
IndexList[rank]: The shape of this tensor slice.
dim_sizeβ
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.
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β
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β
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.
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β
size(self) -> Int
Computes the tensor slice's number of elements.
Returns:
Int: The total number of elements in the tensor slice.
bytecountβ
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β
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β
to_device_buffer(self, ctx: DeviceContext) -> DeviceBuffer[dtype]
Returns:
DeviceBuffer[dtype]
loadβ
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.
storeβ
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.
with_tile_layoutβ
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β
to_layout_tensor(self) -> LayoutTensor[dtype, static_spec.to_layout(), MutAnyOrigin]
Returns:
LayoutTensor[dtype, static_spec.to_layout(), MutAnyOrigin]
to_tile_tensorβ
to_tile_tensor[coord_dtype: DType = DType.int64](self) -> TileTensor[dtype, Layout[static_spec.static_layout._shape_types, static_spec.static_layout._stride_types], MutExternalOrigin]
Returns:
TileTensor[dtype, Layout[static_spec.static_layout._shape_types, static_spec.static_layout._stride_types], MutExternalOrigin]
write_toβ
write_to(self, mut writer: T)
Formats this buffer to the provided Writer.
Args:
- βwriter (
T): The object to write to.
write_repr_toβ
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!