Skip to main content
Log in

Python class

TensorValue

Library for the graph TensorValue class.

TensorValue

class max.graph.TensorValue(value: Value | Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray)

Bases: Value

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.

For example:

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)

# Perform a simple operation: transpose the tensor
transposed_tensor = tensor.T # Output: Tensor representing [[1, 3], [2, 4]]
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)

# Perform a simple operation: transpose the tensor
transposed_tensor = tensor.T # Output: Tensor representing [[1, 3], [2, 4]]

T

property T*: TensorValue*

Returns the transposed tensor. T is the shorthand notation for transposing. For more information, see transpose().

broadcast_to()

broadcast_to(shape: Iterable[int | str | Dim | integer]) → TensorValue

Broadcasts the tensor to a new shape.

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)

broadcasted_tensor = tensor.broadcast_to((3, 2, 2)) # Output: Tensor with shape (3, 2, 2)
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)

broadcasted_tensor = tensor.broadcast_to((3, 2, 2)) # Output: Tensor with shape (3, 2, 2)
  • Parameters:

    shape – An iterable of integers or symbolic dimensions.

  • Returns:

    A new TensorValue with the broadcasted shape.

cast()

cast(dtype: DType) → TensorValue

Casts a symbolic tensor to a different data type.

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)

casted_tensor = tensor.cast(DType.int32) # Output: Tensor representing [[1, 2], [3, 4]] with dtype=int32
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)

casted_tensor = tensor.cast(DType.int32) # Output: Tensor representing [[1, 2], [3, 4]] with dtype=int32
  • Parameters:

    dtype – The target data type (e.g., DType.int32, DType.float64).

  • Returns:

    A new TensorValue with the casted data type.

device

property device*: DeviceRef | None*

Returns the device of the TensorValue.

dtype

property dtype*: DType*

Returns the tensor data type.

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)
print(f"Data type: {tensor.dtype}") # Output: Data type: DType.float32
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)
print(f"Data type: {tensor.dtype}") # Output: Data type: DType.float32

flatten()

flatten(start_dim: int = 0, end_dim: int = -1) → TensorValue

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.

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)
# Flatten the tensor
flattened_tensor = tensor.flatten() # Output: Tensor representing [1, 2, 3, 4]
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)
# Flatten the tensor
flattened_tensor = tensor.flatten() # Output: Tensor representing [1, 2, 3, 4]
  • Parameters:

    • start_dim – The starting dimension to flatten. Defaults to 1.
    • end_dim – The ending dimension to flatten. Defaults to -1.
  • Returns:

    A new TensorValue with the broadcasted shape.

from_dim()

static from_dim(dim: int | str | Dim | integer) → TensorValue

Creates a new tensor based on provided MLIR dimension type.

  • Parameters:

    dim – The dimension value.

  • Returns:

    A new TensorValue.

from_shape()

static from_shape(shape: Iterable[int | str | Dim | integer]) → TensorValue

Creates a new tensor with the specified shape.

  • Parameters:

    shape – An iterable of integers or symbolic dimensions.

  • Returns:

    A new TensorValue.

permute()

permute(dims: list[int]) → TensorValue

Permutes the tensor’s dimensions based on provided indices.

  • Parameters:

    dims – A list of integers specifying the new order of dimensions.

  • Returns:

    A new TensorValue with permuted dimensions.

print()

print(label: str = 'debug_tensor')

Prints detailed information about the tensor.

  • Parameters:

    label – A string label for the printed output. Defaults debug_tensor.

rank

property rank*: int*

Returns the rank (number of dims) of the buffer.

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)
print(f"Rank (number of dimensions): {tensor.rank}") # Output: Rank: 2
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)
print(f"Rank (number of dimensions): {tensor.rank}") # Output: Rank: 2

rebind()

rebind(shape: Iterable[int | str | Dim | integer], message: str = '') → TensorValue

Rebinds the tensor to a new shape with error handling.

  • Parameters:

    • shape – The new shape as an iterable of integers or symbolic dimensions.
    • message – (optional) A message for logging or debugging.
  • Returns:

    A new TensorValue with the updated shape.

reshape()

reshape(shape: Iterable[int | str | Dim | integer]) → TensorValue

Creates a new tensor with the same data but reshaped.

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)

reshaped_tensor = tensor.reshape((1, 4)) # Output: Tensor representing [[1, 2, 3, 4]]
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)

reshaped_tensor = tensor.reshape((1, 4)) # Output: Tensor representing [[1, 2, 3, 4]]
  • Parameters:

    shape – The new shape as an iterable of integers or symbolic dimensions.

  • Returns:

    A new TensorValue with the reshaped dimensions.

shape

property shape*: Shape*

Returns the shape of the TensorValue.

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)

# Access tensor properties
print(f"Shape: {tensor.shape}") # Output: Shape: (2, 2)
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)

# Access tensor properties
print(f"Shape: {tensor.shape}") # Output: Shape: (2, 2)

to()

to(device: DeviceRef) → TensorValue

Transfers the tensor to a specified device without mutation.

matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)

with Graph("to_example") as graph:
tensor = ops.constant(matrix, dtype=DType.float32)
print(tensor.device)
matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)

with Graph("to_example") as graph:
tensor = ops.constant(matrix, dtype=DType.float32)
print(tensor.device)
  • Parameters:

    device – A DeviceRef object specifying the target device.

  • Returns:

    A new TensorValue on the specified device.

transpose()

transpose(dim_1: int, dim_2: int) → TensorValue

Swaps two dimensions of the tensor.

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)
with Graph("transpose_demo") as graph:
tensor = ops.constant(matrix, dtype=DType.float32)
transposed_tensor = tensor.transpose(dim_1=0, dim_2=1)
print(transposed_tensor)
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)
with Graph("transpose_demo") as graph:
tensor = ops.constant(matrix, dtype=DType.float32)
transposed_tensor = tensor.transpose(dim_1=0, dim_2=1)
print(transposed_tensor)
  • Parameters:

    • dim_1 – The first dimension to swap.
    • dim_2 – The second dimension to swap.
  • Returns:

    A new TensorValue with swapped dimensions.

type

property type*: TensorType*

Returns the type of the TensorValue as a TensorType.