Skip to content

dtypes

DType dataclass ¤

DType(
    priority: int,
    itemsize: int,
    name: str,
    fmt: Optional[str],
    count: int,
    _scalar: Optional[DType],
)

dtypes ¤

Methods:

Attributes:

void ¤

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

bool ¤

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

int8 ¤

int8: Final[DType] = new(1, 1, 'signed char', 'b')

uint8 ¤

uint8: Final[DType] = new(2, 1, 'unsigned char', 'B')

int16 ¤

int16: Final[DType] = new(3, 2, 'short', 'h')

uint16 ¤

uint16: Final[DType] = new(4, 2, 'unsigned short', 'H')

int32 ¤

int32: Final[DType] = new(5, 4, 'int', 'i')

uint32 ¤

uint32: Final[DType] = new(6, 4, 'unsigned int', 'I')

int64 ¤

int64: Final[DType] = new(7, 8, 'long', 'q')

uint64 ¤

uint64: Final[DType] = new(8, 8, 'unsigned long', 'Q')

float16 ¤

float16: Final[DType] = new(9, 2, 'half', 'e')

bfloat16 ¤

bfloat16: Final[DType] = new(10, 2, '__bf16', None)

float32 ¤

float32: Final[DType] = new(11, 4, 'float', 'f')

float64 ¤

float64: Final[DType] = new(12, 8, 'double', 'd')

half ¤

half = float16

float ¤

float = float32

double ¤

double = float64

uchar ¤

uchar = uint8

ushort ¤

ushort = uint16

uint ¤

uint = uint32

ulong ¤

ulong = uint64

char ¤

char = int8

short ¤

short = int16

int ¤

int = int32

long ¤

long = int64

default_float ¤

default_float: DType = float32

default_int ¤

default_int: DType = int32

floats ¤

uints ¤

uints = (uint8, uint16, uint32, uint64)

sints ¤

sints = (int8, int16, int32, int64)

ints ¤

ints = uints + sints

is_float ¤

is_float(x: DType) -> bool
Source code in tinygrad/dtype.py
68
69
70
@staticmethod
@functools.lru_cache(None)
def is_float(x: DType) -> bool: return x.scalar() in dtypes.floats or isinstance(x, ImageDType)

is_int ¤

is_int(x: DType) -> bool
Source code in tinygrad/dtype.py
71
72
73
@staticmethod # static methds on top, or bool in the type info will refer to dtypes.bool
@functools.lru_cache(None)
def is_int(x: DType) -> bool: return x.scalar() in dtypes.ints

is_unsigned ¤

is_unsigned(x: DType) -> bool
Source code in tinygrad/dtype.py
74
75
76
@staticmethod
@functools.lru_cache(None)
def is_unsigned(x: DType) -> bool: return x.scalar() in dtypes.uints

from_py ¤

from_py(x) -> DType
Source code in tinygrad/dtype.py
77
78
79
80
81
82
83
84
@staticmethod
def from_py(x) -> DType:
  if x.__class__ is float: return dtypes.default_float
  if x.__class__ is int: return dtypes.default_int
  if x.__class__ is bool: return dtypes.bool
  # put this in the last is faster because there are more items than lists/tuples to check
  if x.__class__ is list or x.__class__ is tuple: return max(dtypes.from_py(xi) for xi in x) if x else dtypes.default_float
  raise RuntimeError(f"Could not infer dtype of {x} with type {type(x)}")

as_const ¤

as_const(
    val: Tuple[ConstType, ...] | ConstType, dtype: DType
)
Source code in tinygrad/dtype.py
85
86
87
88
89
90
91
@staticmethod
def as_const(val: Tuple[ConstType, ...]|ConstType, dtype:DType):
  if isinstance(val, tuple):
    assert len(val) == dtype.count, f"mismatch {val} {dtype}"
    return tuple(dtypes.as_const(x, dtype) for x in val)
  # TODO: should truncate here
  return int(val) if dtypes.is_int(dtype) else float(val) if dtypes.is_float(dtype) else bool(val)

min ¤

min(dtype: DType)
Source code in tinygrad/dtype.py
92
93
94
95
96
@staticmethod
@functools.lru_cache(None)
def min(dtype:DType):
  if dtypes.is_int(dtype): return 0 if dtypes.is_unsigned(dtype) else -2**(dtype.itemsize*8-1)
  return -float("inf") if dtypes.is_float(dtype) else False

max ¤

max(dtype: DType)
Source code in tinygrad/dtype.py
 97
 98
 99
100
101
@staticmethod
@functools.lru_cache(None)
def max(dtype:DType):
  if dtypes.is_int(dtype): return (2**(dtype.itemsize*8-(0 if dtypes.is_unsigned(dtype) else 1)))-1
  return float("inf") if dtypes.is_float(dtype) else True

finfo ¤

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

(exponent, mantissa)

Source code in tinygrad/dtype.py
102
103
104
105
106
@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)}[dtype]

fields ¤

fields() -> Dict[str, DType]
Source code in tinygrad/dtype.py
107
108
@staticmethod
def fields() -> Dict[str, DType]: return DTYPES_DICT

imageh ¤

imageh(shp)
Source code in tinygrad/dtype.py
131
132
@staticmethod
def imageh(shp): return ImageDType(100, 2, "imageh", 'e', 1, None, dtypes.float32, False, 1, shp)

imagef ¤

imagef(shp)
Source code in tinygrad/dtype.py
133
134
@staticmethod
def imagef(shp): return ImageDType(100, 4, "imagef", 'f', 1, None, dtypes.float32, False, 1, shp)

ConstType module-attribute ¤

ConstType = Union[float, int, bool]