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*: int*
Returns the alignment of the dtype.
bfloat16
bfloat16 = 71
bool
bool = 1
float16
float16 = 70
float32
float32 = 72
float64
float64 = 73
float8_e4m3
float8_e4m3 = 65
float8_e4m3fn
float8_e4m3fn = 66
float8_e4m3fnuz
float8_e4m3fnuz = 67
float8_e5m2
float8_e5m2 = 68
float8_e5m2fnuz
float8_e5m2fnuz = 69
from_numpy()
from_numpy() → DType
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:
-
Raises:
ValueError – If the input dtype is not supported.
int16
int16 = 137
int32
int32 = 139
int64
int64 = 141
int8
int8 = 135
is_float
is_float
Returns true if the dtype is floating point.
is_float8
is_float8
Returns true if the dtype is any variant of float8.
is_half
is_half
Returns true if the dtype is half-precision floating point.
is_integral
is_integral
Returns true if the dtype is an integer.
size_in_bytes
property size_in_bytes
Returns the size of the dtype in bytes.
to_numpy()
to_numpy() → dtype
Converts this DType
to the corresponding NumPy dtype.
-
Returns:
The corresponding NumPy dtype object.
-
Return type:
-
Raises:
ValueError – If the dtype is not supported.
uint16
uint16 = 136
uint32
uint32 = 138
uint64
uint64 = 140
uint8
uint8 = 134
max_to_torch_type()
max.dtype.max_to_torch_type(dtype: DType) → dtype
Converts a MAX dtype.DType to a torch.dtype.
-
Parameters:
dtype (dtype.DType) – MAX dtype.DType.
-
Returns:
The corresponding torch.dtype.
-
Return type:
torch.dtype
torch_to_max_type()
max.dtype.torch_to_max_type(dtype: dtype) → DType
Converts a torch.dtype to a MAX dtype.DType.
-
Parameters:
dtype (torch.dtype) – PyTorch torch.dtype.
-
Returns:
The corresponding dtype.DType.
-
Return type:
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!