Skip to main content
Log in

Mojo struct

DType

@register_passable(trivial) struct DType

Represents DType and provides methods for working with it.

Fields

  • value (dtype): The underlying storage for the DType value.

Implemented traits

AnyType, Copyable, EqualityComparable, ExplicitlyCopyable, Hashable, Movable, Representable, Stringable, UnknownDestructibility, Writable, _HashableWithHasher

Aliases

bfloat16

alias bfloat16

Represents a brain floating point value whose bitwidth is 16.

bool

alias bool

Represents a boolean data type.

float16

alias float16

Represents an IEEE754-2008 binary16 floating point value.

float32

alias float32

Represents an IEEE754-2008 binary32 floating point value.

float64

alias float64

Represents an IEEE754-2008 binary64 floating point value.

float8_e3m4

alias float8_e3m4

Represents an 8-bit e3m4 floating point format, encoded as seeemmmm: - (s)ign: 1 bit - (e)xponent: 3 bits - (m)antissa: 4 bits - exponent bias: 3 - nan: 00111111, 11111111 - -0: 10000000 - fn: finite (no inf or -inf encodings)

float8_e4m3fn

alias float8_e4m3fn

Represents the 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 seeeemmm:

  • (s)ign: 1 bit
  • (e)xponent: 4 bits
  • (m)antissa: 3 bits
  • exponent bias: 7
  • nan: 01111111, 11111111
  • -0: 10000000
  • fn: finite (no inf or -inf encodings)

float8_e4m3fnuz

alias float8_e4m3fnuz

Represents an 8-bit e4m3fnuz floating point format, encoded as seeeemmm: - (s)ign: 1 bit - (e)xponent: 4 bits - (m)antissa: 3 bits - exponent bias: 8 - nan: 10000000 - fn: finite (no inf or -inf encodings) - uz: unsigned zero (no -0 encoding)

float8_e5m2

alias float8_e5m2

Represents the 8-bit E5M2 floating point format from the OFP8 standard, encoded as seeeeemm: - (s)ign: 1 bit - (e)xponent: 5 bits - (m)antissa: 2 bits - exponent bias: 15 - nan: {0,1}11111{01,10,11} - inf: 01111100 - -inf: 11111100 - -0: 10000000

float8_e5m2fnuz

alias float8_e5m2fnuz

Represents an 8-bit floating point format, encoded as seeeeemm: - (s)ign: 1 bit - (e)xponent: 5 bits - (m)antissa: 2 bits - exponent bias: 16 - nan: 10000000 - fn: finite (no inf or -inf encodings) - uz: unsigned zero (no -0 encoding)

index

alias index

Represents an integral type whose bitwidth is the maximum integral value on the system.

int128

alias int128 = si128

Represents a signed integer type whose bitwidth is 128.

int16

alias int16

Represents a signed integer type whose bitwidth is 16.

int256

alias int256 = si256

Represents a signed integer type whose bitwidth is 256.

int32

alias int32

Represents a signed integer type whose bitwidth is 32.

int64

alias int64

Represents a signed integer type whose bitwidth is 64.

int8

alias int8

Represents a signed integer type whose bitwidth is 8.

invalid

alias invalid

Represents an invalid or unknown data type.

tensor_float32

alias tensor_float32

Represents a special floating point format supported by NVIDIA Tensor Cores, with the same range as float32 and reduced precision (>=10 bits). Note that this dtype is only available on NVIDIA GPUs.

type

alias type = dtype

uint128

alias uint128 = ui128

Represents an unsigned integer type whose bitwidth is 128.

uint16

alias uint16

Represents an unsigned integer type whose bitwidth is 16.

uint256

alias uint256 = ui256

Represents an unsigned integer type whose bitwidth is 256.

uint32

alias uint32

Represents an unsigned integer type whose bitwidth is 32.

uint64

alias uint64

Represents an unsigned integer type whose bitwidth is 64.

uint8

alias uint8

Represents an unsigned integer type whose bitwidth is 8.

Methods

__init__

@implicit __init__(value: dtype) -> Self

Construct a DType from MLIR dtype.

Args:

  • 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:

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:

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:

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:

True if the DTypes are the same and False otherwise.

copy

copy(self) -> Self

Copy this DType.

Returns:

A copy of the value.

__str__

__str__(self) -> String

Gets the name of the DType.

Returns:

The name of the dtype.

write_to

write_to[W: Writer](self, mut writer: W)

Formats this dtype to the provided Writer.

Parameters:

  • W (Writer): A type conforming to the Writable trait.

Args:

  • writer (W): The object to write to.

__repr__

__repr__(self) -> String

Gets the representation of the DType e.g. "DType.float32".

Returns:

The representation of the dtype.

get_value

get_value(self) -> dtype

Gets the associated internal kgen.dtype value.

Returns:

The kgen.dtype value.

__hash__

__hash__(self) -> UInt

Return a 64-bit hash for this DType value.

Returns:

A 64-bit integer hash of this DType value.

__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:

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:

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:

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:

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:

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:

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:

Returns True if the input type parameter is either integral or floating-point.

sizeof

sizeof(self) -> Int

Returns the size in bytes of the current DType.

Returns:

Returns the size in bytes of the current DType.

bitwidth

bitwidth(self) -> Int

Returns the size in bits of the current DType.

Returns:

Returns the size in bits of the current DType.

dispatch_integral

dispatch_integral[: origin.set, //, func: fn[DType]() capturing -> None](self)

Dispatches an integral function corresponding to the current DType.

Constraints:

DType must be integral.

Parameters:

  • func (fn[DType]() capturing -> None): A parametrized on dtype function to dispatch.

dispatch_floating

dispatch_floating[: origin.set, //, func: fn[DType]() capturing -> None](self)

Dispatches a floating-point function corresponding to the current DType.

Constraints:

DType must be floating-point or integral.

Parameters:

  • func (fn[DType]() capturing -> None): A parametrized on dtype function to dispatch.

dispatch_arithmetic

dispatch_arithmetic[: origin.set, //, func: fn[DType]() capturing -> None](self)

Dispatches a function corresponding to the current DType.

Parameters:

  • func (fn[DType]() capturing -> None): A parametrized on dtype function to dispatch.

__mlir_type

__mlir_type(self) -> !kgen.deferred

Returns the MLIR type of the current DType as an MLIR type.

Returns:

The MLIR type of the current DType.