For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /get-started.md).
Python class
WeightData
WeightData
class max.graph.weights.WeightData(data, name, dtype, shape, quantization_encoding=None)
Bases: DLPackArray
Container for weight tensor data with metadata.
WeightData encapsulates a weight tensor along with its metadata,
providing utilities for type conversion and format compatibility.
It supports the DLPack protocol for efficient tensor sharing between
frameworks.
-
Parameters:
-
- data (DLPackArray)
- name (str)
- dtype (DType)
- shape (Shape)
- quantization_encoding (QuantizationEncoding | None)
astype()
astype(dtype)
Convert the weight data to a different dtype.
This method performs actual data conversion of the underlying tensor data. Special handling is provided for bfloat16 conversions using PyTorch when available.
During cross-compilation (warm-cache) scenarios there is no device to
run the conversion on, so the result holds an uninitialized buffer of
the target dtype. The values are never read in that mode, and
dtype still describes data, which consumers that read the
DLPack payload rather than this metadata rely on.
import numpy as np
from max.dtype import DType
from max.graph.weights import WeightData
weight_data = WeightData.from_numpy(
np.ones((4, 4), dtype=np.float32), "model.layer.weight"
)
fp16_data = weight_data.astype(DType.float16)-
Parameters:
-
dtype (DType) – Target data type for conversion.
-
Returns:
-
A new WeightData instance with the converted data.
-
Return type:
data
data: DLPackArray
The weight tensor as a DLPack array.
dtype
dtype: DType
Data type of the tensor (for example, DType.float32, DType.uint8).
from_numpy()
classmethod from_numpy(arr, name)
Create WeightData from a numpy array.
name
name: str
Hierarchical name of the weight (for example, model.layers.0.weight).
quantization_encoding
quantization_encoding: QuantizationEncoding | None = None
Optional quantization scheme applied to the weight.
shape
shape: Shape
Shape of the tensor as a Shape object.
to_buffer()
to_buffer()
Mutates the data into a Buffer.
-
Return type: