Skip to main content

Python module

dtype

Provides data type definitions for tensors in MAX Engine. These data types are essential for defining the precision and memory layout of tensor data when working with machine learning models.

This module defines the DType enum, which represents all supported tensor data types in MAX Engine, including:

  • Integer types (signed and unsigned): int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64
  • Floating-point types: float8 variants | float16 | bfloat16 | float32 | float64
  • Boolean type

The module also provides utilities for converting between MAX Engine data types and NumPy dtypes, making it easy to interoperate with the NumPy ecosystem.

import numpy as np
from max.dtype import DType

tensor = np.zeros((2, 3), dtype=DType.float32.to_numpy())

# Convert NumPy dtype to MAX DType
array = np.ones((4, 4), dtype=np.float16)
max_dtype = DType.from_numpy(array.dtype)

# Check properties of data types
is_float = DType.float32.is_float() # True
is_int = DType.int64.is_integral() # True
size = DType.float64.size_in_bytes # 8
import numpy as np
from max.dtype import DType

tensor = np.zeros((2, 3), dtype=DType.float32.to_numpy())

# Convert NumPy dtype to MAX DType
array = np.ones((4, 4), dtype=np.float16)
max_dtype = DType.from_numpy(array.dtype)

# Check properties of data types
is_float = DType.float32.is_float() # True
is_int = DType.int64.is_integral() # True
size = DType.float64.size_in_bytes # 8

DType

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

The tensor data type.

align

property align

Returns the alignment requirement of the data type in bytes.

The alignment specifies the memory boundary that values of this data type must be aligned to for optimal performance and correctness.

bfloat16

bfloat16 = 71

bool

bool = 1

float16

float16 = 70

float32

float32 = 72

float64

float64 = 73

float8_e4m3fn

float8_e4m3fn = 66

float8_e4m3fnuz

float8_e4m3fnuz = 67

float8_e5m2

float8_e5m2 = 68

float8_e5m2fnuz

float8_e5m2fnuz = 69

from_numpy()

from_numpy()

Converts a NumPy dtype to the corresponding DType.

Parameters:

dtype (np.dtype ) – The NumPy dtype to convert.

Returns:

The corresponding DType enum value.

Return type:

DType

Raises:

ValueError – If the input dtype is not supported.

from_torch()

from_torch()

Parameters:

dtype (dtype )

Return type:

DType

int16

int16 = 137

int32

int32 = 139

int64

int64 = 141

int8

int8 = 135

is_float

is_float

Checks if the data type is a floating-point type.

is_float8

is_float8

Checks if the data type is an 8-bit floating-point type.

is_half

is_half

Checks if the data type is a half-precision floating-point type.

is_integral

is_integral

Checks if the data type is an integer type.

is_signed_integral

is_signed_integral

Checks if the data type is a signed integer type.

is_unsigned_integral

is_unsigned_integral

Checks if the data type is an unsigned integer type.

size_in_bytes

property size_in_bytes

Returns the size of the data type in bytes.

This indicates how many bytes are required to store a single value of this data type in memory.

to_numpy()

to_numpy()

Converts this DType to the corresponding NumPy dtype.

Returns:

The corresponding NumPy dtype object.

Return type:

DType

Raises:

ValueError – If the dtype is not supported.

to_torch()

to_torch()

Parameters:

dtype (DType )

Return type:

dtype

uint16

uint16 = 136

uint32

uint32 = 138

uint64

uint64 = 140

uint8

uint8 = 134