Skip to main content

Python module

type

Library for graph value types.

BufferType

class max.graph.type.BufferType(dtype, shape, device)

A symbolic buffer type.

This is a reference to a tensor that can be mutated in place.

Constructs a tensor type.

Parameters:

  • dtype (DType ) – The element type of the tensor data.
  • dims – The shape dimensions of the tensor. The number of dims is the rank of the tensor.
  • shape (Shape )
  • device (DeviceRef )

as_tensor()

as_tensor()

Returns the analogous tensor type.

Return type:

TensorType

from_mlir()

classmethod from_mlir(type)

Constructs a buffer type from an MLIR type.

Parameters:

  • t – The MLIR Type object to parse into a buffer type.
  • type (BufferType )

Returns:

The buffer type represented by the MLIR Type value.

Return type:

BufferType

to_mlir()

to_mlir()

Converts to an mlir.Type instance.

Returns:

An mlir.Type in the specified Context.

Return type:

BufferType

DeviceKind

class max.graph.type.DeviceKind(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

A device type representation.

CPU

CPU = 'cpu'

GPU

GPU = 'gpu'

from_string()

static from_string(txt)

Return type:

DeviceKind

DeviceRef

class max.graph.type.DeviceRef(device_type, id=0)

A symbolic device representation.

DeviceRef type representation consists of a DeviceKind and an id. This is a direct representation of the device attribute in mlir.

The following example demonstrates how to create and use device references:

from max.graph import DeviceRef
gpu_device = DeviceRef.GPU()
print(gpu_device) # Outputs: gpu:0
# Create a CPU device with specific id
cpu_device = DeviceRef.CPU(id=1)
print(cpu_device) # Outputs: cpu:1
from max.graph import DeviceRef
gpu_device = DeviceRef.GPU()
print(gpu_device) # Outputs: gpu:0
# Create a CPU device with specific id
cpu_device = DeviceRef.CPU(id=1)
print(cpu_device) # Outputs: cpu:1

Parameters:

CPU()

static CPU(id=0)

Static Method for creating a CPU device.

Parameters:

id (int )

Return type:

DeviceRef

GPU()

static GPU(id=0)

Static Method for creating a GPU device.

Parameters:

id (int )

Return type:

DeviceRef

device_type

device_type: DeviceKind

from_device()

static from_device(device)

Parameters:

device (Device )

Return type:

DeviceRef

from_mlir()

static from_mlir(attr)

Returns a device from mlir attribute

Parameters:

attr (DeviceRefAttr )

Return type:

DeviceRef

id

id: int

is_cpu()

is_cpu()

Returns true if the device is a CPU device.

Return type:

bool

is_gpu()

is_gpu()

Returns true if the device is a GPU device.

Return type:

bool

to_device()

to_device()

Convert device reference to a concrete driver Device.

Return type:

Device

to_mlir()

to_mlir()

Returns a mlir attribute representing device.

Return type:

DeviceRefAttr

TensorType

class max.graph.type.TensorType(dtype, shape, device)

A symbolic TensorType.

This is not an eager tensor type! This contains no actual data, but instead represents the type of a value at some point in time during model execution.

Most internal values in a model will be tensors. This type represents their element type (dtype) and dimensions (dims) at a specific point during model computation. It allows us to do some optimistic optimizations and shape inference during graph construction, and to provide more detailed shape information to the compiler for further optimization passes.

The following example shows how to create a tensor type with static dimensions and access its properties:

from max.graph import TensorType
from max.dtype import DType
# Create a tensor type with float32 elements and static dimensions 2x3
tensor_type = TensorType(DType.float32, (2, 3))
print(tensor_type.dtype) # Outputs: DType.float32
print(tensor_type.shape) # Outputs: [2, 3]
from max.graph import TensorType
from max.dtype import DType
# Create a tensor type with float32 elements and static dimensions 2x3
tensor_type = TensorType(DType.float32, (2, 3))
print(tensor_type.dtype) # Outputs: DType.float32
print(tensor_type.shape) # Outputs: [2, 3]

It can also represent a fully dynamic rank tensor. The presence of dynamic rank tensors in a graph will often degrade performance dramatically and prevents many classes of optimizations.

An optional device (device) can also be provided to indicate the explicit device the tensor is associated with.

Constructs a tensor type.

Parameters:

  • dtype (DType ) – The element type of the tensor data.
  • dims – The shape dimensions of the tensor. The number of dims is the rank of the tensor.
  • shape (Shape )
  • device (DeviceRef )

as_buffer()

as_buffer()

Returns the analogous buffer type.

Return type:

BufferType

from_mlir()

classmethod from_mlir(type)

Constructs a tensor type from an MLIR type.

Parameters:

  • t – The MLIR Type object to parse into a tensor type.
  • type (TensorType )

Returns:

The tensor type represented by the MLIR Type value.

Return type:

TensorType

to_mlir()

to_mlir()

Converts to an mlir.Type instance.

Returns:

An mlir.Type in the specified Context.

Return type:

TensorType

Type

class max.graph.type.Type

Represents any possible type for Graph values.

Every Value in the Graph has a Type, and that type is represented by an Type. This type may be inspected to get finer-grained types and learn more about an individual Value.

The following example shows how to work with types in a graph:

from max.graph import Graph, TensorType
from max.dtype import DType
with Graph() as g:
# Create a tensor constant with a specific type
tensor_type = TensorType(DType.float32, [2, 3])
# The type can be inspected to get information about the value
print(f"Tensor element type: {tensor_type.dtype}") # Outputs: DType.float32
print(f"Tensor shape: {tensor_type.shape}") # Outputs: [2, 3]
from max.graph import Graph, TensorType
from max.dtype import DType
with Graph() as g:
# Create a tensor constant with a specific type
tensor_type = TensorType(DType.float32, [2, 3])
# The type can be inspected to get information about the value
print(f"Tensor element type: {tensor_type.dtype}") # Outputs: DType.float32
print(f"Tensor shape: {tensor_type.shape}") # Outputs: [2, 3]

from_mlir()

static from_mlir(t)

Constructs a type from an MLIR type.

Parameters:

t (MlirType ) – The MLIR Type object to parse into a type.

Returns:

The type represented by the MLIR Type value.

Return type:

Type

to_mlir()

to_mlir()

Converts to an mlir.Type instance.

Returns:

An mlir.Type in the specified Context.

Return type:

MlirType