Python class
Graph
Graph
β
class max.graph.Graph(name: str, forward: Callable | None = None, input_types: Iterable[Type] = (), *args, **kwargs)
Represents a single MAX graph.
A Graph is a callable routine in MAX Engine. Like functions, graphs have a name and signature. Unlike a function, which follows an imperative programming model, a Graph follows a dataflow programming model, using lazily-executed, parallel operations instead of sequential instructions.
When you instantiate a graph, you must specify the input shapes as one or more TensorType or ListType values. Then, build a sequence of ops and set the graph output with output(). For example:
from dataclasses import dataclass
import numpy as np
from max.dtype import DType
from max.graph import Graph, TensorValue, TensorType
@dataclass
class Linear:
weight: np.ndarray
bias: np.ndarray
def __call__(self, x: TensorValue) -> TensorValue:
return x @ self.weight + self.bias
linear_graph = Graph(
"linear",
Linear(np.ones((2, 2)), np.ones((2,))),
input_types=[TensorType(DType.float32, (2,))],
)
You canβt call a Graph directly from Python. You must compile it and execute it with MAX Engine. For more detail, see the tutorial about how to build a graph with MAX Graph.
add_weight()
β
add_weight(name: str, dtype: DType | None = None, shape: Iterable[int | str | Dim | integer] | None = None, filepath: PathLike | str | None = None, offset: int | None = None, quantization_encoding: QuantizationEncoding | None = None) β Weight
Initializes a new weight in the current graph.
-
Parameters:
- name β The name of this weight. All weights in a graph must have unique names.
- dtype β The DType of the weight. Defaults to Float32.
- shape β The shape of the weight. Defaults to a scalar (shape=[1]).
- filepath β File pointing to file containing weight value.
- offset β Offset to weight in the file (defaults to 0).
- quantization_encoding β Quantization encoding of the weight. Defaults to not-quantized.
-
Returns:
The added Weight object.
-
Raises:
- ValueError if a weight with the same name already exists in the β
- graph. β
current
β
current
inputs
β
inputs*: tuple[max.graph.value.Value, ...]*
output()
β
unique_symbolic_dim()
β
unique_symbolic_dim(tag: str) β SymbolicDim
Create a new symbolic dim with a different name from any other.
-
Parameters:
tag β An additional identifier to help identify the dimension for debugging purposes.
Returns: The dimension.
weights
β
weights*: dict[str, max.graph.weight.Weight]*
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
If you'd like to share more information, please report an issue on GitHub
π What went wrong?