Skip to main content

Python class

Value

Value

class max.graph.Value

Represents a symbolic value within a Graph.

A Value can represent the output of a node, the arguments of a Graph (as seen from within its body), and more generally any symbolic value available within the Graph. Other nodes receive Value values as inputs to form a computation graph.

A Value may also refer to an existing input or output of a node, and you can change them, such as by swapping a new Value.

Conceptually, think of a Value as an edge in the dataflow graph, with the other end being the user of that value.

The following example shows how to work with Values in a graph to create a simple computation:

from max.graph import Graph, ops, Value
from max.dtype import DType
import numpy as np

with Graph("value_example") as graph:
# Create input values
a = ops.constant(np.array([1, 2, 3]), dtype=DType.float32, device=DeviceRef.CPU())
b = ops.constant(np.array([4, 5, 6]), dtype=DType.float32, device=DeviceRef.CPU())

# Use values to perform operations
c = a + b # c is a Value representing the addition

# Demonstrate that the result is a Value
print(f"Type of c: {type(c)}")
print(f"Is c a Value? {isinstance(c, Value)}")
from max.graph import Graph, ops, Value
from max.dtype import DType
import numpy as np

with Graph("value_example") as graph:
# Create input values
a = ops.constant(np.array([1, 2, 3]), dtype=DType.float32, device=DeviceRef.CPU())
b = ops.constant(np.array([4, 5, 6]), dtype=DType.float32, device=DeviceRef.CPU())

# Use values to perform operations
c = a + b # c is a Value representing the addition

# Demonstrate that the result is a Value
print(f"Type of c: {type(c)}")
print(f"Is c a Value? {isinstance(c, Value)}")

Similar to a regular variable, a Value has a data type.

Value is abstract, it shouldn’t be constructed directly.

buffer

property buffer: BufferValue

Returns the Value as a BufferValue.

Raises an exception if the Value is not a BufferValue.

from_mlir()

classmethod from_mlir(value)

Creates a Value from an MLIR value.

Parameters:

value (Value [ MlirType ] ) – The MLIR value to wrap.

Return type:

Value

opaque

property opaque: _OpaqueValue

Returns the Value as an _OpaqueValue.

Raises an exception if the Value is not a _OpaqueValue.

tensor

property tensor: TensorValue

Returns the Value as a TensorValue.

Raises an exception if the Value is not a TensorValue.

type

property type: Type[MlirType]

Returns the type of the Value as a Type.