Mojo struct
Tensor
A tensor type which owns its underlying data and is parameterized on DType.
Example:
from max.tensor import Tensor, TensorSpec, TensorShape
from utils.index import Index
def main():
height = 256
width = 256
channels = 3
# Create the tensor of dimensions height, width, channels
# and fill with random values.
image = Tensor[DType.float32].rand(TensorShape(height, width, channels))
# Declare the grayscale image.
spec = TensorSpec(DType.float32, height, width)
gray_scale_image = Tensor[DType.float32](spec)
# Perform the RGB to grayscale transform.
for y in range(height):
for x in range(width):
r = image[y, x, 0]
g = image[y, x, 1]
b = image[y, x, 2]
gray_scale_image[Index(y, x)] = 0.299 * r + 0.587 * g + 0.114 * b
print(gray_scale_image.shape())
from max.tensor import Tensor, TensorSpec, TensorShape
from utils.index import Index
def main():
height = 256
width = 256
channels = 3
# Create the tensor of dimensions height, width, channels
# and fill with random values.
image = Tensor[DType.float32].rand(TensorShape(height, width, channels))
# Declare the grayscale image.
spec = TensorSpec(DType.float32, height, width)
gray_scale_image = Tensor[DType.float32](spec)
# Perform the RGB to grayscale transform.
for y in range(height):
for x in range(width):
r = image[y, x, 0]
g = image[y, x, 1]
b = image[y, x, 2]
gray_scale_image[Index(y, x)] = 0.299 * r + 0.587 * g + 0.114 * b
print(gray_scale_image.shape())
Parametersβ
- βtype (
DType
): The underlying element type of the tensor.
Implemented traitsβ
AnyType
,
CollectionElement
,
Copyable
,
EqualityComparable
,
Formattable
,
Movable
,
Stringable
Methodsβ
__init__
β
__init__(inout self: Self)
Default initializer for TensorShape.
__init__(inout self: Self, other: Self)
Creates a deep copy of an existing tensor.
Args:
- βother (
Self
): The tensor to copy from.
__init__(inout self: Self, *dims: Int)
Allocates a tensor using the shape provided.
Args:
- β*dims (
Int
): The tensor dimensions.
__init__(inout self: Self, owned shape: TensorShape)
Allocates a tensor using the shape provided.
Args:
- βshape (
TensorShape
): The tensor shape.
__init__(inout self: Self, owned spec: TensorSpec)
Allocates a tensor using the spec provided.
Args:
- βspec (
TensorSpec
): The tensor spec.
__init__(inout self: Self, shape: Tuple[element_types])
Allocates a tensor using the shape provided.
Args:
- βshape (
Tuple[element_types]
): The tensor shape.
__init__(inout self: Self, owned shape: TensorShape, owned ptr: UnsafePointer[SIMD[type, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1])
Initializes a Tensor from the pointer and shape provided. The caller relinquishes the ownership of the pointer being passed in.
Args:
- βshape (
TensorShape
): The tensor shapes. - βptr (
UnsafePointer[SIMD[type, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
): The data pointer.
__init__(inout self: Self, owned spec: TensorSpec, owned ptr: UnsafePointer[SIMD[type, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1])
Initializes a Tensor from the pointer and shape provided. The caller relinquishes the ownership of the pointer being passed in.
Args:
- βspec (
TensorSpec
): The tensor spec. - βptr (
UnsafePointer[SIMD[type, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
): The data pointer.
__init__(inout self: Self, shape: TensorShape, *data: SIMD[type, 1])
Initializes a Tensor from the shape and data provided. If a single scalar is passed in, then the scalar is splatted to all elements in the tensor.
Args:
- βshape (
TensorShape
): The tensor shape. - β*data (
SIMD[type, 1]
): Elements to place into the created tensor.
__init__(inout self: Self, shape: TensorShape, owned list: List[SIMD[type, 1], hint_trivial_type])
Initializes a 1-dimensional Tensor from the provided list.
Args:
- βshape (
TensorShape
): The tensor shape. - βlist (
List[SIMD[type, 1], hint_trivial_type]
): The list to construct this Tensor from.
__init__(inout self: Self, owned list: List[SIMD[type, 1], hint_trivial_type])
Initializes a 1-dimensional Tensor from the provided list.
Args:
- βlist (
List[SIMD[type, 1], hint_trivial_type]
): The list to construct this Tensor from.
__copyinit__
β
__copyinit__(inout self: Self, other: Self)
Creates a deep copy of an existing tensor.
Args:
- βother (
Self
): The tensor to copy from.
__moveinit__
β
__moveinit__(inout self: Self, owned existing: Self)
Move initializer for the tensor.
Args:
- βexisting (
Self
): The tensor to move.
__del__
β
__del__(owned self: Self)
Delete the spec and release any owned memory.
__getitem__
β
__getitem__(self: Self, index: Int) -> SIMD[type, 1]
Gets the value at the specified index.
Args:
- βindex (
Int
): The index of the value to retrieve.
Returns:
The value at the specified indices.
__getitem__(self: Self, *indices: Int) -> SIMD[type, 1]
Gets the value at the specified indices.
Args:
- β*indices (
Int
): The indices of the value to retrieve.
Returns:
The value at the specified indices.
__getitem__(self: Self, indices: VariadicList[Int]) -> SIMD[type, 1]
Gets the value at the specified indices.
Args:
- βindices (
VariadicList[Int]
): The indices of the value to retrieve.
Returns:
The value at the specified indices.
__getitem__[len: Int](self: Self, indices: StaticIntTuple[len]) -> SIMD[type, 1]
Gets the SIMD value at the specified indices.
Parameters:
- βlen (
Int
): The length of the indecies.
Args:
- βindices (
StaticIntTuple[len]
): The indices of the value to retrieve.
Returns:
The value at the specified indices.
__setitem__
β
__setitem__(inout self: Self, index: Int, val: SIMD[type, 1])
Sets the value at the specified index.
Args:
- βindex (
Int
): The index of the value to set. - βval (
SIMD[type, 1]
): The value to store.
__setitem__(inout self: Self, indices: VariadicList[Int], val: SIMD[type, 1])
Sets the value at the specified indices.
Args:
- βindices (
VariadicList[Int]
): The indices of the value to set. - βval (
SIMD[type, 1]
): The value to store.
__setitem__[len: Int](inout self: Self, indices: StaticIntTuple[len], val: SIMD[type, 1])
Sets the value at the specified indices.
Parameters:
- βlen (
Int
): The length of the indecies.
Args:
- βindices (
StaticIntTuple[len]
): The indices of the value to set. - βval (
SIMD[type, 1]
): The value to store.
__eq__
β
__eq__(self: Self, other: Self) -> Bool
Returns True if the two tensors are the same and False otherwise.
Args:
- βother (
Self
): The other Tensor to compare against.
Returns:
True if the two tensors are the same and False otherwise.
__ne__
β
__ne__(self: Self, other: Self) -> Bool
Returns True if the two tensors are not the same and False otherwise.
Args:
- βother (
Self
): The other Tensor to compare against.
Returns:
True if the two tensors are the not the same and False otherwise.
__add__
β
__add__(self: Self, other: Self) -> Self
Adds this tensor with another tensor.
Constraints:
The two tensors must have the same rank, type, and dimensions.
Args:
- βother (
Self
): The RHS of the add operation.
Returns:
The addition of both tensors.
__add__(self: Self, other: SIMD[type, 1]) -> Self
Adds this tensor with a scalar.
Args:
- βother (
SIMD[type, 1]
): The RHS of the add operation.
Returns:
The addition result.
__sub__
β
__sub__(self: Self, other: Self) -> Self
Subtracts a tensor from this tensor.
Constraints:
The two tensors must have the same rank, type, and dimensions.
Args:
- βother (
Self
): The RHS of the sub operation.
Returns:
The addition of both tensors.
__sub__(self: Self, other: SIMD[type, 1]) -> Self
Subtracts a scalar from this tensor.
Args:
- βother (
SIMD[type, 1]
): The RHS of the sub operation.
Returns:
The subtraction result.
__mul__
β
__mul__(self: Self, other: Self) -> Self
Multiplies this tensor with another tensor.
Constraints:
The two tensors must have the same rank, type, and dimensions.
Args:
- βother (
Self
): The RHS of the mul operation.
Returns:
The multiplication of both tensors.
__mul__(self: Self, other: SIMD[type, 1]) -> Self
Multiplies this tensor with a scalar.
Args:
- βother (
SIMD[type, 1]
): The RHS of the mul operation.
Returns:
The multiplication result.
__truediv__
β
__truediv__(self: Self, other: Self) -> Self
Divides this tensor by another tensor.
TODO: Change the return type if inputs are int
Constraints:
The two tensors must have the same rank, type, and dimensions.
Args:
- βother (
Self
): The RHS of the div operation.
Returns:
The division of both tensors.
__truediv__(self: Self, other: SIMD[type, 1]) -> Self
Divides this tensor by a scalar.
Args:
- βother (
SIMD[type, 1]
): The RHS of the div operation.
Returns:
The division result.
__pow__
β
__pow__(self: Self, exp: Int) -> Self
Returns a copy of the tensor with each element raised to the power of exponent
.
Constraints:
For integral values the exponent cannot be negative.
Args:
- βexp (
Int
): Integer power to raise tensor to.
Returns:
An exponentiated copy of tensor.
__radd__
β
__radd__(self: Self, other: SIMD[type, 1]) -> Self
Adds this tensor with a scalar.
Args:
- βother (
SIMD[type, 1]
): The LHS of the add operation.
Returns:
The addition result.
__rsub__
β
__rsub__(self: Self, other: SIMD[type, 1]) -> Self
Subtracts this tensor from a scalar.
Args:
- βother (
SIMD[type, 1]
): The LHS of the sub operation.
Returns:
The addition result.
__rmul__
β
__rmul__(self: Self, other: SIMD[type, 1]) -> Self
Multiplies this tensor with a scalar.
Args:
- βother (
SIMD[type, 1]
): The LHS of the mul operation.
Returns:
The multiplication result.
__rtruediv__
β
__rtruediv__(self: Self, other: SIMD[type, 1]) -> Self
Divides a scalar by this tensor, broadcasting elementwise.
Args:
- βother (
SIMD[type, 1]
): The LHS of the div operation.
Returns:
The division result.
__ipow__
β
__ipow__(inout self: Self, exponent: Int)
In-place pow operator.
Raises each element of the tensor to the power of exponent
in place.
Constraints:
For integral values the exponent cannot be negative.
Args:
- βexponent (
Int
): Integer power to raise tensor to.
rand
β
static rand(owned shape: TensorShape) -> Self
Constructs a new tensor with the specified shape and fills it with random elements.
Args:
- βshape (
TensorShape
): The tensor shape.
Returns:
A new tensor of specified shape and filled with random elements.
randn
β
static randn(owned shape: TensorShape, mean: SIMD[float64, 1] = #kgen.float_literal<0|1>, variance: SIMD[float64, 1] = #kgen.float_literal<1|1>) -> Self
Constructs a new Tensor from the shape and fills it with random values from a Normal(mean, variance) distribution.
Constraints:
The type should be floating point.
Args:
- βshape (
TensorShape
): The shape of the Tensor to fill with random values. - βmean (
SIMD[float64, 1]
): Normal distribution mean. - βvariance (
SIMD[float64, 1]
): Normal distribution variance.
Returns:
A Tensor filled with random dtype samples from Normal(mean, variance).
ireshape
β
ireshape(inout self: Self, new_shape: TensorShape)
(Inplace) Reshapes the tensor by assigning it a new shape.
Args:
- βnew_shape (
TensorShape
): The new shape.
reshape
β
reshape(inout self: Self, new_shape: TensorShape) -> Self
Returns a reshaped tensor.
Args:
- βnew_shape (
TensorShape
): The new shape.
Returns:
A Tensor that is a reshaped version of the original tensor.
astype
β
astype[new_type: DType](self: Self) -> Tensor[$0]
Copy the Tensor with elements cast to the new type.
Parameters:
- βnew_type (
DType
): The type to cast the values to.
Returns:
A new tensor with the same values but the new type.
clip
β
clip(self: Self, lower_bound: SIMD[type, 1], upper_bound: SIMD[type, 1]) -> Self
Clips the values of the tensor.
Args:
- βlower_bound (
SIMD[type, 1]
): The lower bound. - βupper_bound (
SIMD[type, 1]
): The upper bound.
Returns:
A clipped version of the tensor.
unsafe_ptr
β
unsafe_ptr(self: Self) -> UnsafePointer[SIMD[type, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
Gets the underlying Data pointer to the Tensor.
Returns:
The underlying data pointer of the tensor.
unsafe_uint8_ptr
β
unsafe_uint8_ptr(self: Self) -> UnsafePointer[SIMD[uint8, 1], 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
Gets the underlying Data pointer to the Tensor.
Returns:
The underlying data pointer of the tensor.
rank
β
rank(self: Self) -> Int
Gets the rank of the tensor.
Returns:
The rank of the tensor.
num_elements
β
num_elements(self: Self) -> Int
Gets the total number of elements in the tensor.
Returns:
The total number of elements in the tensor.
bytecount
β
bytecount(self: Self) -> Int
Gets the total bytecount of the tensor.
Returns:
The total bytecount of the tensor.
spec
β
spec(self: Self) -> TensorSpec
Gets the specification of the tensor.
Returns:
The underlying tensor spec of the tensor.
shape
β
shape(self: Self) -> TensorShape
Gets the shape of the tensor.
Returns:
The underlying tensor shape of the tensor.
dim
β
dim(self: Self, idx: Int) -> Int
Gets the dimension at the specified index.
Args:
- βidx (
Int
): The dimension index.
Returns:
The dimension at the specified index.
__str__
β
__str__(self: Self) -> String
Gets the tensor as a string.
Returns:
A compact string of the tensor.
format_to
β
format_to(self: Self, inout writer: Formatter)
Formats this Tensor to the provided formatter.
Args:
- βwriter (
Formatter
): The formatter to write to.
__repr__
β
__repr__(self: Self) -> String
Gets the tensor as a string.
Returns:
A compact string representation of the tensor.
load
β
load[*, width: Int = 1](self: Self, index: Int) -> SIMD[type, $0]
Gets the SIMD value at the specified index.
Parameters:
- βwidth (
Int
): The SIMD width of the vector.
Args:
- βindex (
Int
): The index of the value to retrieve.
Returns:
The SIMD value at the specified indices.
load[*, width: Int = 1](self: Self, *indices: Int) -> SIMD[type, $0]
Gets the SIMD value at the specified indices.
Parameters:
- βwidth (
Int
): The SIMD width of the vector.
Args:
- β*indices (
Int
): The indices of the value to retrieve.
Returns:
The SIMD value at the specified indices.
load[*, width: Int = 1](self: Self, indices: VariadicList[Int]) -> SIMD[type, $0]
Gets the SIMD value at the specified indices.
Parameters:
- βwidth (
Int
): The SIMD width of the vector.
Args:
- βindices (
VariadicList[Int]
): The indices of the value to retrieve.
Returns:
The SIMD value at the specified indices.
load[len: Int, /, *, width: Int = 1](self: Self, indices: StaticIntTuple[len]) -> SIMD[type, $1]
Gets the SIMD value at the specified indices.
Parameters:
- βlen (
Int
): The length of the indecies. - βwidth (
Int
): The SIMD width of the vector.
Args:
- βindices (
StaticIntTuple[len]
): The indices of the value to retrieve.
Returns:
The SIMD value at the specified indices.
static load(path: Path) -> Self
Read tensor from a file. The path should be a file saved with Tensor.save method.
Args:
- βpath (
Path
): Path to the output file.
Returns:
The tensor read from file.
store
β
store[*, width: Int = 1](inout self: Self, index: Int, val: SIMD[type, width])
Sets the SIMD value at the specified index.
Parameters:
- βwidth (
Int
): The SIMD width of the vector.
Args:
- βindex (
Int
): The index of the value to set. - βval (
SIMD[type, width]
): The SIMD value to store.
store[*, width: Int = 1](inout self: Self, indices: VariadicList[Int], val: SIMD[type, width])
Sets the SIMD value at the specified indices.
Parameters:
- βwidth (
Int
): The SIMD width of the vector.
Args:
- βindices (
VariadicList[Int]
): The indices of the value to set. - βval (
SIMD[type, width]
): The SIMD value to store.
store[len: Int, /, *, width: Int = 1](inout self: Self, indices: StaticIntTuple[len], val: SIMD[type, width])
Sets the SIMD value at the specified indices.
Parameters:
- βlen (
Int
): The length of the indecies. - βwidth (
Int
): The SIMD width of the vector.
Args:
- βindices (
StaticIntTuple[len]
): The indices of the value to set. - βval (
SIMD[type, width]
): The SIMD value to store.
argmax
β
argmax(self: Self, *, axis: Int = -1) -> Tensor[index]
Finds the indices of the maximum element along the specified axis.
Args:
- βaxis (
Int
): The axis.
Returns:
A new tensor containing the indices of the maximum elements along axis.
Raises:
Error if this Tensor's rank is larger than the maximum supported rank for argmax.
argmin
β
argmin(self: Self, *, axis: Int = -1) -> Tensor[index]
Finds the indices of the minimum element along the specified axis.
Args:
- βaxis (
Int
): The axis.
Returns:
A new tensor containing the indices of the minimum elements along axis.
tofile
β
tofile(self: Self, path: Path)
Write values to a file.
Args:
- βpath (
Path
): Path to the output file.
fromfile
β
static fromfile(path: Path) -> Self
Read tensor from a file.
Args:
- βpath (
Path
): Path to the output file.
Returns:
The tensor read from file.
save
β
save(self: Self, path: Path)
Save given tensor to file. This method preserves shape and datatype information.
Args:
- βpath (
Path
): Path of file.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
π What went wrong?