Mojo struct
DType
@register_passable(trivial)
struct DType
Represents a data type specification and provides methods for working with it.
DType defines a set of compile-time constant that specify the precise
numeric representation of data in order to prevent runtime errors by
catching type mismatches at compile time. It directly maps to CPU/GPU
instruction sets, allowing the compiler to generate optimal SIMD and vector
operations.
DType behaves like an enum rather than a typical object. You don't
instantiate it, but instead use its compile-time constants (aliases) to
declare data types for SIMD vectors, tensors, and other data structures.
Key usage patterns:
- Type specification: Use aliases like
DType.float32to specify types for SIMD vectors, tensors, and other data structures - Type parameters: Pass
DTypevalues as compile-time parameters to parameterized types likeSIMD[dtype, size] - Type introspection: Call methods like
.is_floating_point()to query type properties at compile time - Type conversion: Use in casting operations to convert between different numeric representations
Note: Not all data types are supported on all platforms. For example,
DType.bfloat16 is currently not supported on Apple Silicon.
Example:
var data = SIMD[DType.float16, 4](1.5, 2.5, 3.5, 4.5)
var dtype = data.dtype
print("Is float:", dtype.is_floating_point()) # True
print("Is signed:", dtype.is_signed()) # TrueImplemented traits
AnyType,
Copyable,
EqualityComparable,
Hashable,
Identifiable,
ImplicitlyCopyable,
Movable,
Representable,
Stringable,
UnknownDestructibility,
Writable
Aliases
__copyinit__is_trivial
comptime __copyinit__is_trivial = True
__del__is_trivial
comptime __del__is_trivial = True
__moveinit__is_trivial
comptime __moveinit__is_trivial = True
bfloat16
comptime bfloat16
Represents a brain floating point value whose bitwidth is 16.
bool
comptime bool
Represents a boolean data type.
float16
comptime float16
Represents an IEEE754-2008 binary16 floating point value.
float32
comptime float32
Represents an IEEE754-2008 binary32 floating point value.
float4_e2m1fn
comptime float4_e2m1fn
Represents a 4-bit e2m1 floating point format, encoded as s.ee.m and defined by the Open Compute MX Format Specification:
- (s)ign: 1 bit
- (e)xponent: 2 bits
- (m)antissa: 1 bits
- exponent_bias: 1
float64
comptime float64
Represents an IEEE754-2008 binary64 floating point value.
float8_e3m4
comptime float8_e3m4
Represents an 8-bit e3m4 floating point format, encoded as s.eee.mmmm:
- (s)ign: 1 bit
- (e)xponent: 3 bits
- (m)antissa: 4 bits
- exponent bias: 3
- nan: {0,1}.111.1111
- fn: finite (no inf or -inf encodings)
- -0: 1.000.0000
float8_e4m3fn
comptime float8_e4m3fn
Represents the 8-bit E4M3 floating point format defined in the OFP8 standard.
This type is named differently across libraries and vendors, for example:
- Mojo, PyTorch, JAX, and LLVM refer to it as
e4m3fn. - OCP, NVIDIA CUDA, and AMD ROCm refer to it as
e4m3.
In these contexts, they are all referring to the same finite type specified
in the OFP8 standard above, encoded as s.eeee.mmm:
- (s)ign: 1 bit
- (e)xponent: 4 bits
- (m)antissa: 3 bits
- exponent bias: 7
- nan: {0,1}.1111.111
- fn: finite (no inf or -inf encodings)
- -0: 1.0000.000
float8_e4m3fnuz
comptime float8_e4m3fnuz
Represents an 8-bit e4m3fnuz floating point format (ref), encoded as s.eeee.mmm:
- (s)ign: 1 bit
- (e)xponent: 4 bits
- (m)antissa: 3 bits
- exponent bias: 8
- nan: 1.0000.000
- fn: finite (no inf or -inf encodings)
- uz: unsigned zero (no -0 encoding)
float8_e5m2
comptime float8_e5m2
Represents the 8-bit E5M2 floating point format defined in the OFP8 standard, encoded as s.eeeee.mm:
- (s)ign: 1 bit
- (e)xponent: 5 bits
- (m)antissa: 2 bits
- exponent bias: 15
- nan: {0,1}.11111.{01,10,11}
- inf: {0,1}.11111.00
- -0: 1.00000.00
float8_e5m2fnuz
comptime float8_e5m2fnuz
Represents an 8-bit e5m2fnuz floating point format (ref), encoded as s.eeeee.mm:
- (s)ign: 1 bit
- (e)xponent: 5 bits
- (m)antissa: 2 bits
- exponent bias: 16
- nan: 1.00000.00
- fn: finite (no inf or -inf encodings)
- uz: unsigned zero (no -0 encoding)
float8_e8m0fnu
comptime float8_e8m0fnu
Represents the 8-bit E8M0Fnu floating point format defined in the OFP8 standard, encoded as eeeeeeee:
- (e)xponent: 8 bits
- (m)antissa: 0 bits
- exponent bias: 127
- nan: 11111111
- fn: finite (no inf or -inf encodings)
- u: no sign or zero value.
int
comptime int = index
Represents an integral type whose bitwidth is the maximum integral value on the system.
int128
comptime int128
Represents a signed integer type whose bitwidth is 128.
int16
comptime int16
Represents a signed integer type whose bitwidth is 16.
int256
comptime int256
Represents a signed integer type whose bitwidth is 256.
int32
comptime int32
Represents a signed integer type whose bitwidth is 32.
int64
comptime int64
Represents a signed integer type whose bitwidth is 64.
int8
comptime int8
Represents a signed integer type whose bitwidth is 8.
invalid
comptime invalid
Represents an invalid or unknown data type.
uint
comptime uint = uindex
Represents an unsigned integral type whose bitwidth is the maximum unsigned integral value on the system.
uint128
comptime uint128
Represents an unsigned integer type whose bitwidth is 128.
uint16
comptime uint16
Represents an unsigned integer type whose bitwidth is 16.
uint256
comptime uint256
Represents an unsigned integer type whose bitwidth is 256.
uint32
comptime uint32
Represents an unsigned integer type whose bitwidth is 32.
uint64
comptime uint64
Represents an unsigned integer type whose bitwidth is 64.
uint8
comptime uint8
Represents an unsigned integer type whose bitwidth is 8.
Methods
__init__
__init__(*, mlir_value: dtype) -> Self
Construct a DType from MLIR dtype.
Args:
- mlir_value (
dtype): The MLIR dtype.
__eq__
__eq__(self, rhs: Self) -> Bool
Compares one DType to another for equality.
Args:
- rhs (
Self): The DType to compare against.
Returns:
Bool: True if the DTypes are the same and False otherwise.
__ne__
__ne__(self, rhs: Self) -> Bool
Compares one DType to another for inequality.
Args:
- rhs (
Self): The DType to compare against.
Returns:
Bool: False if the DTypes are the same and True otherwise.
__is__
__is__(self, rhs: Self) -> Bool
Compares one DType to another for equality.
Args:
- rhs (
Self): The DType to compare against.
Returns:
Bool: True if the DTypes are the same and False otherwise.
__isnot__
__isnot__(self, rhs: Self) -> Bool
Compares one DType to another for inequality.
Args:
- rhs (
Self): The DType to compare against.
Returns:
Bool: True if the DTypes are the different and False otherwise.
__str__
write_to
write_to(self, mut writer: T)
Formats this dtype to the provided Writer.
Args:
- writer (
T): The object to write to.
__repr__
__repr__(self) -> String
Gets the representation of the DType e.g. "DType.float32".
Returns:
String: The representation of the dtype.
get_value
get_value(self) -> dtype
Gets the associated internal kgen.dtype value.
Returns:
dtype: The kgen.dtype value.
__hash__
__hash__[H: Hasher](self, mut hasher: H)
Updates hasher with this DType value.
Parameters:
- H (
Hasher): The hasher type.
Args:
- hasher (
H): The hasher instance.
is_unsigned
is_unsigned(self) -> Bool
Returns True if the type parameter is unsigned and False otherwise.
Returns:
Bool: Returns True if the input type parameter is unsigned.
is_signed
is_signed(self) -> Bool
Returns True if the type parameter is signed and False otherwise.
Returns:
Bool: Returns True if the input type parameter is signed.
is_integral
is_integral(self) -> Bool
Returns True if the type parameter is an integer and False otherwise.
Returns:
Bool: Returns True if the input type parameter is an integer.
is_floating_point
is_floating_point(self) -> Bool
Returns True if the type parameter is a floating-point and False otherwise.
Returns:
Bool: Returns True if the input type parameter is a floating-point.
is_float8
is_float8(self) -> Bool
Returns True if the dtype is a 8bit-precision floating point type, e.g. float8_e5m2, float8_e5m2fnuz, float8_e4m3fn and float8_e4m3fnuz.
Returns:
Bool: True if the dtype is a 8bit-precision float, false otherwise.
is_half_float
is_half_float(self) -> Bool
Returns True if the dtype is a half-precision floating point type, e.g. either fp16 or bf16.
Returns:
Bool: True if the dtype is a half-precision float, false otherwise..
is_numeric
is_numeric(self) -> Bool
Returns True if the type parameter is numeric (i.e. you can perform arithmetic operations on).
Returns:
Bool: Returns True if the input type parameter is either integral or
floating-point.
mantissa_width
static mantissa_width[dtype: Self]() -> Int
Returns the mantissa width of a floating point type.
Parameters:
- dtype (
Self): The DType.
Returns:
Int: The mantissa width.
max_exponent
static max_exponent[dtype: Self]() -> Int
Returns the max exponent of a floating point dtype without accounting for inf representations. This is not the maximum representable exponent, which is generally equal to the exponent_bias.
Parameters:
- dtype (
Self): The DType.
Returns:
Int: The max exponent.
exponent_width
static exponent_width[dtype: Self]() -> Int
Returns the exponent width of a floating point type.
Parameters:
- dtype (
Self): The DType.
Returns:
Int: The exponent width.
exponent_bias
static exponent_bias[dtype: Self]() -> Int
Returns the exponent bias of a floating point type.
Parameters:
- dtype (
Self): The DType.
Returns:
Int: The exponent bias.
__mlir_type
__mlir_type(self) -> !kgen.deferred
Returns the MLIR type of the current DType as an MLIR type.
Returns:
!kgen.deferred: The MLIR type of the current DType.
get_dtype
static get_dtype[T: AnyType, size: Int = 1]() -> Self
Get the DType if the given Type is a SIMD[_, size] of a DType.
Parameters:
Returns:
Self: The DType if matched, otherwise DType.invalid.
is_scalar
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!