Skip to content

dtypes

DType dataclass ¤

DType(
    priority: int,
    bitsize: int,
    name: str,
    fmt: FmtStr | None,
)

dtypes ¤

Methods:

Attributes:

void ¤

void: Final[DType] = DType.new(-1, 0, 'void', None)

weakint ¤

weakint: Final[DType] = DType.new(0, 800, 'weakint', None)

bool ¤

bool: Final[DType] = DType.new(0, 1, 'bool', '?')

i8 ¤

i8: Final[DType] = DType.new(1, 8, 'i8', 'b')

u8 ¤

u8: Final[DType] = DType.new(2, 8, 'u8', 'B')

i16 ¤

i16: Final[DType] = DType.new(3, 16, 'i16', 'h')

u16 ¤

u16: Final[DType] = DType.new(4, 16, 'u16', 'H')

i32 ¤

i32: Final[DType] = DType.new(5, 32, 'i32', 'i')

u32 ¤

u32: Final[DType] = DType.new(6, 32, 'u32', 'I')

i64 ¤

i64: Final[DType] = DType.new(7, 64, 'i64', 'q')

u64 ¤

u64: Final[DType] = DType.new(8, 64, 'u64', 'Q')

weakfloat ¤

weakfloat: Final[DType] = DType.new(
    9, 800, "weakfloat", None
)

fp8e4m3 ¤

fp8e4m3: Final[DType] = DType.new(10, 8, 'fp8e4m3', None)

fp8e5m2 ¤

fp8e5m2: Final[DType] = DType.new(11, 8, 'fp8e5m2', None)

fp8e4m3fnuz ¤

fp8e4m3fnuz: Final[DType] = DType.new(
    10, 8, "fp8e4m3fnuz", None
)

fp8e5m2fnuz ¤

fp8e5m2fnuz: Final[DType] = DType.new(
    11, 8, "fp8e5m2fnuz", None
)

f16 ¤

f16: Final[DType] = DType.new(12, 16, 'f16', 'e')

bf16 ¤

bf16: Final[DType] = DType.new(13, 16, 'bf16', None)

f32 ¤

f32: Final[DType] = DType.new(14, 32, 'f32', 'f')

f64 ¤

f64: Final[DType] = DType.new(15, 64, 'f64', 'd')

float16 ¤

float16 = f16

half ¤

half = f16

bfloat16 ¤

bfloat16 = bf16

float32 ¤

float32 = f32

float ¤

float = f32

float64 ¤

float64 = f64

double ¤

double = f64

uint8 ¤

uint8 = u8

uchar ¤

uchar = u8

uint16 ¤

uint16 = u16

ushort ¤

ushort = u16

uint32 ¤

uint32 = u32

uint ¤

uint = u32

uint64 ¤

uint64 = u64

ulong ¤

ulong = u64

int8 ¤

int8 = i8

char ¤

char = i8

int16 ¤

int16 = i16

short ¤

short = i16

int32 ¤

int32 = i32

int ¤

int = i32

int64 ¤

int64 = i64

long ¤

long = i64

default_float ¤

default_float: DType

default_int ¤

default_int: DType

fp8_ocp ¤

fp8_ocp = (fp8e4m3, fp8e5m2)

fp8_fnuz ¤

fp8_fnuz = (fp8e4m3fnuz, fp8e5m2fnuz)

fp8s ¤

fp8s = fp8_ocp + fp8_fnuz

floats ¤

int8s ¤

int8s = (uint8, int8)

int16s ¤

int16s = (uint16, int16)

int32s ¤

int32s = (uint32, int32)

int64s ¤

int64s = (uint64, int64)

uints ¤

uints = (uint8, uint16, uint32, uint64)

sints ¤

sints = (int8, int16, int32, int64)

ints ¤

ints = uints + sints

weaks ¤

weaks = (weakint, weakfloat)

all ¤

all = floats + ints + (bool,)

is_float ¤

is_float(x: DType) -> bool
Source code in tinygrad/dtype.py
88
89
90
@staticmethod
@functools.cache
def is_float(x: DType) -> bool: return x in (dtypes.floats + (dtypes.weakfloat,))

is_int ¤

is_int(x: DType) -> bool
Source code in tinygrad/dtype.py
91
92
93
@staticmethod # static methods on top, or bool in the type info will refer to dtypes.bool
@functools.cache
def is_int(x: DType) -> bool: return x in (dtypes.ints + (dtypes.weakint,))

is_unsigned ¤

is_unsigned(x: DType) -> bool
Source code in tinygrad/dtype.py
94
95
96
@staticmethod
@functools.cache
def is_unsigned(x: DType) -> bool: return x in dtypes.uints

is_bool ¤

is_bool(x: DType) -> bool
Source code in tinygrad/dtype.py
97
98
@staticmethod
def is_bool(x: DType) -> bool: return x == dtypes.bool

from_py ¤

from_py(x) -> DType
Source code in tinygrad/dtype.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
@staticmethod
def from_py(x) -> DType:
  # NOTE: isinstance(True, int) is True, so bool must be checked before int
  if isinstance(x, (bool, InvalidType)): return dtypes.bool
  if isinstance(x, float): return dtypes.weakfloat
  if isinstance(x, int): return dtypes.weakint
  # put this in the last is faster because there are more items than lists/tuples to check
  if isinstance(x, (list, tuple)):
    dt = max(dtypes.from_py(xi) for xi in x) if x else dtypes.weakfloat
    if dt is not dtypes.weakint: return strong_dtype(dt)
    ints = [xi for xi in x if isinstance(xi, int)]  # a vconst also holds Invalid
    return commit_int(min(ints), max(ints))
  raise RuntimeError(f"Could not infer dtype of {x} with type {type(x)}")

finfo ¤

finfo(dtype: DType) -> tuple[int, int]

(exponent, mantissa)

Source code in tinygrad/dtype.py
112
113
114
115
116
117
@staticmethod
def finfo(dtype:DType) -> tuple[int, int]:
  """(exponent, mantissa)"""
  if not dtypes.is_float(dtype): raise ValueError(f"{dtype} is not a floating point type")
  return {dtypes.float16: (5, 10), dtypes.bfloat16: (8, 7), dtypes.float32: (8, 23), dtypes.float64: (11, 52),
          dtypes.fp8e4m3: (4, 3), dtypes.fp8e5m2: (5, 2), dtypes.fp8e4m3fnuz: (4, 3), dtypes.fp8e5m2fnuz: (5, 2)}[dtype]

ConstType module-attribute ¤

ConstType = PyConst | InvalidType