Skip to content

dtypes

DType dataclass ¤

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

dtypes ¤

Methods:

Attributes:

void ¤

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

bool ¤

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

int8 ¤

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

uint8 ¤

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

int16 ¤

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

uint16 ¤

uint16: Final[DType] = DType(4, 2, "unsigned short", "H", 1)

int32 ¤

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

uint32 ¤

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

int64 ¤

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

uint64 ¤

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

float16 ¤

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

bfloat16 ¤

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

float32 ¤

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

float64 ¤

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

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
47
48
49
@staticmethod
@functools.lru_cache(None)
def is_float(x: DType) -> bool: return x.scalar() in dtypes.floats

is_int ¤

is_int(x: DType) -> bool
Source code in tinygrad/dtype.py
50
51
52
@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
53
54
55
@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
56
57
58
59
60
61
62
63
@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
64
65
66
67
68
69
70
@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
71
72
73
74
75
@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
76
77
78
79
80
@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
81
82
83
84
85
@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
86
87
@staticmethod
def fields() -> Dict[str, DType]: return DTYPES_DICT

imageh ¤

imageh(shp)
Source code in tinygrad/dtype.py
110
111
@staticmethod
def imageh(shp): return ImageDType(100, 2, "imageh", 'e', 1, shape=shp, base=dtypes.float32)

imagef ¤

imagef(shp)
Source code in tinygrad/dtype.py
112
113
@staticmethod
def imagef(shp): return ImageDType(100, 4, "imagef", 'f', 1, shape=shp, base=dtypes.float32)

ConstType module-attribute ¤

ConstType = Union[float, int, bool]