Python class
TensorValue
Library for the graph TensorValue class.
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]
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]
Value is abstract, it shouldn’t be constructed directly.
-
Parameters:
-
value (
TensorValueLike
)
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.
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]
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
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
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]
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:
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. Defaultsdebug_tensor
.
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
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]
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)]
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)]
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
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
) – ADeviceRef
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]
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
.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!