Skip to main content

Python class

WeightData

WeightData

class max.graph.weights.WeightData(data, name, dtype, shape, quantization_encoding=None)

source

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:

astype()

astype(dtype)

source

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 where no GPU is available, this method skips the actual data conversion but still returns a WeightData with the target dtype. This is safe because the weight data won’t be used for inference during compilation - only the dtype metadata matters for graph construction.

weight_data = weights.model.layer.weight.data()
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:

WeightData

data

data: DLPackArray

source

The weight tensor as a DLPack array.

dtype

dtype: DType

source

Data type of the tensor (for example, DType.float32, DType.uint8).

from_numpy()

classmethod from_numpy(arr, name)

source

Create WeightData from a numpy array.

Parameters:

  • arr (ndarray[tuple[Any, ...], dtype[Any]]) – Numpy array containing the weight data.
  • name (str) – Name to assign to this weight.

Returns:

A new WeightData instance with dtype and shape inferred from the numpy array.

Return type:

WeightData

name

name: str

source

Hierarchical name of the weight (for example, model.layers.0.weight).

quantization_encoding

quantization_encoding: QuantizationEncoding | None = None

source

Optional quantization scheme applied to the weight.

shape

shape: Shape

source

Shape of the tensor as a Shape object.

to_buffer()

to_buffer()

source

Mutates the data into a Buffer.

Return type:

Buffer