Module Dtype.Val
Accessors
val count : t -> intcount dt is the vector width of dt. 1 for scalar types.
Constructors
The named scalar constants below are all of_scalar applied to the matching scalar constructor, with count = 1.
val void : tval bool : tval int8 : tval int16 : tval int32 : tval int64 : tval uint8 : tval uint16 : tval uint32 : tval uint64 : tval float16 : tval bfloat16 : tval float32 : tval float64 : tval fp8e4m3 : tval fp8e5m2 : tval fp8e4m3fnuz : tval fp8e5m2fnuz : tval weakint : tval default_float : tdefault_float is the dtype named by the DEFAULT_FLOAT environment variable at module initialization, or float32 when unset. The value must name a floating-point scalar dtype using tinygrad dtype names or public aliases such as "float32", "float", "half", or "default_float". Short display mnemonics such as "f32", "f16", and "bf16" are not accepted. Used by least_upper_float.
val default_int : tdefault_int is int32. Used by sum_acc_dtype to widen narrow integer accumulators.
Transformers
with_scalar s dt is dt with its scalar identity replaced by s, preserving count. No validation is performed; the caller is responsible for preserving the count >= 0 invariant.
Predicates
val is_float : t -> boolis_float dt is true iff scalar dt is a floating-point type.
val is_int : t -> boolval is_unsigned : t -> boolval is_fp8 : t -> boolis_fp8 dt is true iff scalar dt is an fp8 dtype.
Properties
val bitsize : t -> intval priority : t -> intpriority dt is the promotion priority of scalar dt. See scalar for the range.
Promotion
least_upper_dtype ts is the least upper bound of the scalars of ts in the promotion lattice. The result always has count = 1; input counts are ignored.
Promotion is total over numeric types: any pair has a common supertype. Some edges are lossy (e.g., Int64 to Uint64 loses negative values; Uint64 to fp8 loses most precision).
Raises Invalid_argument if ts is empty.
See also least_upper_float and can_lossless_cast.
least_upper_float t is scalarize t if t is floating-point, or least_upper_dtype [scalarize t; default_float] otherwise.
See also least_upper_dtype.
can_lossless_cast src dst is true iff every value representable by scalar src is exactly representable by scalar dst. Bool casts losslessly to any type; any signed or unsigned integer type casts losslessly to Weakint. Only the scalar component is examined; count is ignored.
This checks exact representability, not promotion. For instance, can_lossless_cast int32 float32 is false (float32 cannot represent every 32-bit integer).
See also least_upper_dtype.
sum_acc_dtype dt is the accumulator dtype for sum-like reductions over dt. The result always has count = 1.
Widening rules:
- Unsigned integers promote to at least
uint32. - Signed integers,
Weakint, and booleans promote to at leastint32. - Floats promote to at least the dtype named by the
SUM_DTYPEenvironment variable, orfloat32when unset.SUM_DTYPEis read whensum_acc_dtypeis called and must name a scalar dtype using tinygrad dtype names or public aliases such as"float32","double","uchar", or"default_float". Short display mnemonics such as"f32","u8", and"bf16"are not accepted.
Comparison
compare a b is a total order over value dtypes. Orders first by scalar promotion priority, then scalar bit width, then constructor ordinal, then by count.
Formatting
val to_string : t -> stringto_string dt is the short name of dt. For count = 1 this is the scalar mnemonic (e.g., "f32", "i64"). For vector types the lane count is appended with the multiplication sign (e.g., "f32×4").
val repr : t -> string