Python class
Weights
Weights
class max.graph.weights.Weights(*args, **kwargs)
Bases: Protocol
Protocol for managing and accessing model weights hierarchically.
The Weights protocol provides a convenient interface for loading and organizing neural network weights. It supports hierarchical naming through attribute and index access, making it easy to work with complex model architectures.
Weights in MAX are tensors backed by external memory (buffers or memory-mapped files) that remain separate from the compiled graph.
from max.graph import Graph
from max.dtype import DType
graph = Graph("my_model")
weights = graph.weights()
# Allocate weights with hierarchical naming
attn_weight = weights.transformer.layers[0].attention.weight.allocate(
dtype=DType.float32,
shape=(768, 768)
)
# Creates weight named "transformer.layers.0.attention.weight"
# Check if a weight exists before allocating
if weights.transformer.layers[0].mlp.weight.exists():
mlp_weight = weights.transformer.layers[0].mlp.weight.allocate(
dtype=DType.float16,
shape=(768, 3072)
)allocate()
allocate(dtype=None, shape=None, quantization_encoding=None, device=cpu:0)
Creates a Weight object for this tensor.
# Allocate a weight with specific configuration
weight = weights.model.layers[0].weight.allocate(
dtype=DType.float16, # Convert to half precision
shape=(768, 768),
device=DeviceRef.GPU(0) # Place on first GPU
)
# Add to graph
with graph:
weight_tensor = graph.add_weight(weight)-
Parameters:
-
- dtype (DType | None) – Data type for the weight. If
None, uses the original dtype. - shape (Iterable[int | str | Dim | integer[Any] | TypedAttr] | None) – Shape of the weight tensor. If
None, uses the original shape. - quantization_encoding (QuantizationEncoding | None) – Quantization scheme to apply (for example,
Q4_K,Q8_0). - device (DeviceRef) – Target device for the weight (CPU or GPU).
- dtype (DType | None) – Data type for the weight. If
-
Returns:
-
A
Weightobject that can be added to a graph usinggraph.add_weight(). -
Return type:
allocated_weights
property allocated_weights: dict[str, DLPackArray]
Returns all previously allocated weights.
This only includes weights that were explicitly allocated using
Weights.allocate(), not all available weights.
-
Returns:
-
A dictionary mapping weight names to their numpy arrays for all weights that have been allocated through this interface.
data()
data()
Returns weight data with metadata.
weight_data = weights.model.embeddings.weight.data()
print(f"Shape: {weight_data.shape}")
print(f"Dtype: {weight_data.dtype}")
# Convert to different dtype
fp16_data = weight_data.astype(DType.float16)-
Returns:
-
A
WeightDataobject containing the tensor data along with metadata like name, dtype, shape, and quantization encoding. -
Raises:
-
KeyError – If no weight exists at the current hierarchical name.
-
Return type:
exists()
exists()
Checks if a weight with this exact name exists.
if weights.model.classifier.weight.exists():
classifier = weights.model.classifier.weight.allocate(...)
else:
print("Classifier weight not found")-
Returns:
-
Trueif a weight with the current hierarchical name exists in the loaded weights,Falseotherwise. -
Return type:
items()
items()
Iterates through all weights that start with the current prefix.
# Iterate through all weights in a specific layer
for name, weight in weights.transformer.layers[0].items():
print(f"Found weight: {name}")name
property name: str
The current weight name or prefix.
-
Returns:
-
The hierarchical name built from attribute and index access. For example, if accessed as
weights.model.layers[0], returnsmodel.layers.0.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!