Python class
TensorValue
TensorValue
class max.graph.TensorValue(value)
Bases: Value
[TensorType
]
Represents a value semantic tensor within a Graph
. It provides
various methods and properties to manipulate and query tensor attributes
such as shape
, data type (dtype
), device placement (device
), and more.
The following example demonstrates how to create and manipulate tensor values in a graph:
import numpy as np
from max.dtype import DType
from max.graph import Graph, ops
matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
# Create a Graph context to work with tensors
with Graph("tensor_demo") as graph:
# Create a constant tensor from the matrix
tensor = ops.constant(matrix, dtype=DType.float32, device=DeviceRef.CPU())
# Access tensor properties
print(f"Shape: {tensor.shape}") # Output: [2, 2]
print(f"Data type: {tensor.dtype}") # Output: DType.float32
# Perform operations on the tensor
transposed = tensor.T
doubled = tensor * 2
print(f"Original shape: {tensor.shape}") # Output: [2, 2]
print(f"Transposed shape: {transposed.shape}") # Output: [2, 2]
Initializes a TensorValue
from a tensor-like value.
-
Parameters:
-
value (TensorValueLike) – The value to wrap. Can be an MLIR tensor value, another
TensorValue
, aDim
, or aShape
.
T
property T: TensorValue
Returns the transposed tensor.
T
is the shorthand notation for transposing.
For more information, see transpose()
.
-
Returns:
-
A new
TensorValue
with swapped dimensions.
argmax()
argmax(axis=-1)
Reduces the tensor using an argmax operation along axis
.
When the result is ambiguous ie. there are multiple maxima, selects one index arbitrarily.
from max.dtype import DType
from max.graph import Graph, TensorType, DeviceRef
# Define a 2x3 float32 input tensor for the graph
input_type = TensorType(DType.float32, (2, 3), device=DeviceRef.CPU())
with Graph("argmax_demo", input_types=[input_type]) as graph:
x = graph.inputs[0].tensor
# Argmax along axis 1 (last dimension of each row)
indices = x.argmax(axis=1)
print(f"Input shape: {x.shape}") # [2, 3]
print(f"Argmax shape: {indices.shape}") # [2, 1]
-
Parameters:
-
axis (int) – The axis along which to compute the reduction. If negative, indexes from the last dimension (e.g.,
-1
is the last dimension). -
Returns:
-
A
TensorValue
of dtypeDType.int64
with the same rank as the input, and the same shape except alongaxis
, which will have size 1. -
Return type:
broadcast_to()
broadcast_to(shape)
Broadcasts the tensor to a new shape.
The following example demonstrates how to broadcast a tensor to a larger shape:
import numpy as np
from max.dtype import DType
from max.graph import Graph, ops
# Create a 2x2 matrix
matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
# Create a Graph context to work with tensors
with Graph("broadcast_to_demo") as graph:
# Create a constant tensor from the matrix
tensor = ops.constant(matrix, dtype=DType.float32, device=DeviceRef.CPU())
# Broadcast tensor to a 3x2x2 tensor (add a new dimension of size 3)
broadcasted_tensor = tensor.broadcast_to((3, 2, 2))
print(f"Original shape: {tensor.shape}") # Output: [2, 2]
print(f"Broadcasted shape: {broadcasted_tensor.shape}") # Output: [3, 2, 2]
cast()
cast(dtype)
Casts a symbolic tensor to a different data type.
The following example demonstrates how to cast a tensor from one data type to another:
import numpy as np
from max.dtype import DType
from max.graph import Graph, ops
# Create a matrix with float32 values
matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
# Create a Graph context to work with tensors
with Graph("cast_demo") as graph:
# Create a constant tensor from the matrix
tensor = ops.constant(matrix, dtype=DType.float32, device=DeviceRef.CPU())
# Cast tensor to integer type
casted_tensor = tensor.cast(DType.int32)
print(f"Original dtype: {tensor.dtype}") # Output: DType.float32
print(f"Casted dtype: {casted_tensor.dtype}") # Output: DType.int32
-
Parameters:
-
dtype (DType) – The target data type (e.g.,
DType.int32
,DType.float64
). -
Returns:
-
A new
TensorValue
with the casted data type. -
Return type:
device
property device: DeviceRef
Returns the device of the TensorValue.
dtype
property dtype: DType
Returns the tensor data type.
The following example demonstrates how to access the data type of a tensor:
import numpy as np
from max.dtype import DType
from max.graph import Graph, ops
# Create a matrix with float32 values
matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
# Create a Graph context to work with tensors
with Graph("dtype_demo") as graph:
# Create a constant tensor from the matrix
tensor = ops.constant(matrix, dtype=DType.float32, device=DeviceRef.CPU())
# Access tensor data type
print(f"Data type: {tensor.dtype}") # Output: DType.float32
flatten()
flatten(start_dim=0, end_dim=-1)
Flattens the specified dims of a symbolic tensor.
The number and order of the elements in the tensor is unchanged.
All dimensions from start_dim
to end_dim
(inclusive) are merged into a single output dim.
The following example demonstrates how to flatten a multi-dimensional tensor:
import numpy as np
from max.dtype import DType
from max.graph import Graph, ops
# Create a 2x2 matrix
matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
# Create a Graph context to work with tensors
with Graph("flatten_demo") as graph:
# Create a constant tensor from the matrix
tensor = ops.constant(matrix, dtype=DType.float32, device=DeviceRef.CPU())
# Flatten the tensor to a 1D array
flattened_tensor = tensor.flatten()
print(f"Original shape: {tensor.shape}") # Output: [2, 2]
print(f"Flattened shape: {flattened_tensor.shape}") # Output: [4]
-
Parameters:
-
Returns:
-
A new
TensorValue
with the flattened dimensions. -
Return type:
from_mlir()
classmethod from_mlir(value)
Creates a TensorValue
from an MLIR tensor value.
-
Parameters:
-
value (Value[TensorType]) – The MLIR tensor value to wrap.
-
Return type:
max()
max(axis=-1)
Reduces the tensor using a max operation along axis
.
from max.dtype import DType
from max.graph import Graph, TensorType, DeviceRef
# Define a 2x3 float32 input tensor for the graph
input_type = TensorType(DType.float32, (2, 3), device=DeviceRef.CPU())
with Graph("max_demo", input_types=[input_type]) as graph:
x = graph.inputs[0].tensor
# Max along axis 1 (last dimension of each row)
m = x.max(axis=1)
print(f"Input shape: {x.shape}") # [2, 3]
print(f"Max shape: {m.shape}") # [2, 1]
-
Parameters:
-
axis (int) – The axis along which to compute the reduction. If negative, indexes from the last dimension (e.g.,
-1
is the last dimension). -
Returns:
-
A
TensorValue
with the same rank as the input and the same shape except alongaxis
, which will have size 1. -
Return type:
mean()
mean(axis=-1)
Reduces the tensor using a mean operation along axis
.
from max.dtype import DType
from max.graph import Graph, TensorType, DeviceRef
# Define a 2x3 float32 input tensor for the graph
input_type = TensorType(DType.float32, (2, 3), device=DeviceRef.CPU())
with Graph("mean_demo", input_types=[input_type]) as graph:
x = graph.inputs[0].tensor
# Mean along axis 1 (last dimension of each row)
mu = x.mean(axis=1)
print(f"Input shape: {x.shape}") # [2, 3]
print(f"Mean shape: {mu.shape}") # [2, 1]
-
Parameters:
-
axis (int) – The axis along which to compute the reduction. If negative, indexes from the last dimension (e.g.,
-1
is the last dimension). -
Returns:
-
A
TensorValue
with the same rank as the input and the same shape except alongaxis
, which will have size 1. -
Return type:
min()
min(axis=-1)
Reduces the tensor using a min operation along axis
.
from max.dtype import DType
from max.graph import Graph, TensorType, DeviceRef
# Define a 2x3 float32 input tensor for the graph
input_type = TensorType(DType.float32, (2, 3), device=DeviceRef.CPU())
with Graph("min_demo", input_types=[input_type]) as graph:
x = graph.inputs[0].tensor
# Min along axis 1 (last dimension of each row)
mn = x.min(axis=1)
print(f"Input shape: {x.shape}") # [2, 3]
print(f"Min shape: {mn.shape}") # [2, 1]
-
Parameters:
-
axis (int) – The axis along which to compute the reduction. If negative, indexes from the last dimension (e.g.,
-1
is the last dimension). -
Returns:
-
A
TensorValue
with the same rank as the input and the same shape except alongaxis
, which will have size 1. -
Return type:
permute()
permute(dims)
Permutes the tensor’s dimensions based on provided indices.
-
Parameters:
-
dims (list[int]) – A list of integers specifying the new order of dimensions.
-
Returns:
-
A new
TensorValue
with permuted dimensions. -
Return type:
print()
print(label='debug_tensor')
Prints detailed information about the tensor.
-
Parameters:
-
label (str) – A string label for the printed output. Defaults
debug_tensor
. -
Return type:
-
None
rank
property rank: int
Returns the rank (number of dims) of the buffer.
The following example demonstrates how to access the rank of a tensor:
import numpy as np
from max.dtype import DType
from max.graph import Graph, ops
# Create a 2x2 matrix (2-dimensional array)
matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
# Create a Graph context to work with tensors
with Graph("rank_demo") as graph:
# Create a constant tensor from the matrix
tensor = ops.constant(matrix, dtype=DType.float32, device=DeviceRef.CPU())
# Access tensor rank (number of dimensions)
print(f"Rank: {tensor.rank}") # Output: 2
rebind()
rebind(shape, message='')
Rebinds the tensor to a new shape with error handling.
-
Parameters:
-
Returns:
-
A new
TensorValue
with the updated shape. -
Return type:
reshape()
reshape(shape)
Creates a new tensor with the same data but reshaped.
The following example demonstrates how to reshape a tensor to change its dimensions:
import numpy as np
from max.dtype import DType
from max.graph import Graph, ops
# Create a 2x2 matrix
matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
# Create a Graph context to work with tensors
with Graph("reshape_demo") as graph:
# Create a constant tensor from the matrix
tensor = ops.constant(matrix, dtype=DType.float32, device=DeviceRef.CPU())
# Reshape tensor to a 1x4 matrix
reshaped_tensor = tensor.reshape((1, 4))
print(f"Original shape: {tensor.shape}") # Output: [2, 2]
print(f"Reshaped shape: {reshaped_tensor.shape}") # Output: [1, 4]
shape
property shape: Shape
Returns the shape of the TensorValue
.
The following example demonstrates how to access the shape of a tensor:
import numpy as np
from max.dtype import DType
from max.graph import Graph, ops
# Create a 2x2 matrix
matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
# Create a Graph context to work with tensors
with Graph("shape_demo") as graph:
# Create a constant tensor from the matrix
tensor = ops.constant(matrix, dtype=DType.float32, device=DeviceRef.CPU())
# Access tensor shape
print(f"Shape: {tensor.shape}") # Shape: [Dim(2), Dim(2)]
stdev()
stdev(axis=-1)
Reduces the tensor using a standard deviation operation along axis
.
The standard deviation is computed as the square root of the population variance along the specified axis.
from max.dtype import DType
from max.graph import Graph, TensorType, DeviceRef
# Define a 2x3 float32 input tensor for the graph
input_type = TensorType(DType.float32, (2, 3), device=DeviceRef.CPU())
with Graph("stdev_demo", input_types=[input_type]) as graph:
x = graph.inputs[0].tensor
# Standard deviation along axis 1 (last dimension of each row)
sd = x.stdev(axis=1)
print(f"Input shape: {x.shape}") # [2, 3]
print(f"Stdev shape: {sd.shape}") # [2, 1]
-
Parameters:
-
axis (int) – The axis along which to compute the reduction. If negative, indexes from the last dimension (e.g.,
-1
is the last dimension). -
Returns:
-
A
TensorValue
with the same rank as the input and the same shape except alongaxis
, which will have size 1. -
Return type:
to()
to(device)
Transfers the tensor to a specified device without mutation.
The following example demonstrates how to move a tensor from one device to another:
import numpy as np
from max.dtype import DType
from max.graph import Graph, ops, DeviceRef
# Create a 2x2 matrix
matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
with Graph("to_device_example") as graph:
# Create a tensor on the default device
tensor = ops.constant(matrix, dtype=DType.float32, device=DeviceRef.CPU())
# Move the tensor to a GPU device
gpu_tensor = tensor.to(DeviceRef.GPU())
print(f"Original device: {tensor.device}") # Output depends on default device
print(f"New device: {gpu_tensor.device}") # Output: gpu:0
-
Parameters:
-
device (DeviceRef) – A
DeviceRef
object specifying the target device. -
Returns:
-
A new
TensorValue
on the specified device. -
Return type:
transpose()
transpose(dim_1, dim_2)
Swaps two dimensions of the tensor.
The following example demonstrates how to transpose a tensor by swapping its dimensions:
import numpy as np
from max.dtype import DType
from max.graph import Graph, ops
# Create a 2x3 matrix
matrix = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32)
with Graph("transpose_demo") as graph:
tensor = ops.constant(matrix, dtype=DType.float32, device=DeviceRef.CPU())
# Transpose the tensor (swap dimensions 0 and 1)
transposed_tensor = tensor.transpose(dim_1=0, dim_2=1)
print(f"Original shape: {tensor.shape}") # Output: [2, 3]
print(f"Transposed shape: {transposed_tensor.shape}") # Output: [3, 2]
-
Parameters:
-
Returns:
-
A new
TensorValue
with swapped dimensions. -
Return type:
type
property type: TensorType
Returns the type of the TensorValue
as a TensorType
.
var()
var(axis=-1)
Reduces the tensor using a variance operation along axis
.
The variance is computed as the mean of squared deviations from the mean (population variance, i.e., without Bessel’s correction) along the specified axis.
from max.dtype import DType
from max.graph import Graph, TensorType, DeviceRef
# Define a 2x3 float32 input tensor for the graph
input_type = TensorType(DType.float32, (2, 3), device=DeviceRef.CPU())
with Graph("var_demo", input_types=[input_type]) as graph:
x = graph.inputs[0].tensor
# Variance along axis 1 (last dimension of each row)
vr = x.var(axis=1)
print(f"Input shape: {x.shape}") # [2, 3]
print(f"Var shape: {vr.shape}") # [2, 1]
-
Parameters:
-
axis (int) – The axis along which to compute the reduction. If negative, indexes from the last dimension (e.g.,
-1
is the last dimension). -
Returns:
-
A
TensorValue
with the same rank as the input and the same shape except alongaxis
, which will have size 1. -
Return type:
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!