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', 'l', 1)

uint64 ¤

uint64: Final[DType] = DType(8, 8, 'unsigned long', 'L', 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
46
47
48
@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
49
50
51
@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
52
53
54
@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
55
56
57
58
59
60
61
62
@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
63
64
65
66
67
68
69
@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
70
71
72
73
74
@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
75
76
77
78
79
@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]
Source code in tinygrad/dtype.py
80
81
82
83
@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
84
85
@staticmethod
def fields() -> Dict[str, DType]: return DTYPES_DICT

imageh ¤

imageh(shp)
Source code in tinygrad/dtype.py
108
109
@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
110
111
@staticmethod
def imagef(shp): return ImageDType(100, 4, "imagef", 'f', 1, shape=shp, base=dtypes.float32)

ConstType module-attribute ¤

ConstType = Union[float, int, bool]