Module Dtype.Val

type t

Value dtype: a scalar identity with a vector width (count).

Invariant: count >= 0. Constructors produce scalar values with count = 1; use vec for vector dtypes. A zero-lane vector is represented with count = 0, matching tinygrad's raw dtype layer.

Accessors

val scalar : t -> scalar

scalar dt is the element type of dt.

val count : t -> int

count dt is the vector width of dt. 1 for scalar types.

Constructors

val of_scalar : scalar -> t

of_scalar s is the scalar dtype for s with count = 1.

The named scalar constants below are all of_scalar applied to the matching scalar constructor, with count = 1.

val void : t
val bool : t
val int8 : t
val int16 : t
val int32 : t
val int64 : t
val uint8 : t
val uint16 : t
val uint32 : t
val uint64 : t
val float16 : t
val bfloat16 : t
val float32 : t
val float64 : t
val fp8e4m3 : t
val fp8e5m2 : t
val fp8e4m3fnuz : t
val fp8e5m2fnuz : t
val weakint : t
val default_float : t

default_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 : t

default_int is int32. Used by sum_acc_dtype to widen narrow integer accumulators.

Transformers

val scalarize : t -> t

scalarize dt is dt with count = 1.

See also vec.

val vec : int -> t -> t

vec n dt is a vector type with n lanes of scalar dt.

Returns dt unchanged when n = 1 or scalar dt is Void. For n = 0 and a non-Void scalar, returns a zero-lane vector with bitsize 0.

Raises Invalid_argument if count dt <> 1 (already vectorized), or n < 0.

See also scalarize.

val with_scalar : scalar -> t -> t

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 -> bool

is_float dt is true iff scalar dt is a floating-point type.

val is_int : t -> bool

is_int dt is true iff scalar dt is an integer type (including Weakint). Uint128 and Uint256 are private storage helpers and are not classified as integers, matching tinygrad.

val is_unsigned : t -> bool

is_unsigned dt is true iff scalar dt is a public unsigned integer dtype. Uint128 and Uint256 are private storage helpers and are not classified as unsigned, matching tinygrad.

val is_bool : t -> bool

is_bool dt is true iff scalar dt is Bool.

val is_fp8 : t -> bool

is_fp8 dt is true iff scalar dt is an fp8 dtype.

Properties

val bitsize : t -> int

bitsize dt is the total size in bits: scalar bit width times count. Void has bitsize 0, Weakint has 800.

val itemsize : t -> int

itemsize dt is bitsize rounded up to whole bytes.

val priority : t -> int

priority dt is the promotion priority of scalar dt. See scalar for the range.

Promotion

val least_upper_dtype : t list -> t

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.

val least_upper_float : t -> t

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.

val can_lossless_cast : t -> t -> bool

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.

val sum_acc_dtype : t -> t

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 least int32.
  • Floats promote to at least the dtype named by the SUM_DTYPE environment variable, or float32 when unset. SUM_DTYPE is read when sum_acc_dtype is 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

val equal : t -> t -> bool

equal a b is true iff a and b have the same scalar and count.

val compare : t -> t -> int

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 -> string

to_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

repr dt is the tinygrad-style dtype representation, such as "dtypes.int" or "dtypes.float.vec(4)".

Raises Invalid_argument if dt is a private wide helper dtype (Uint128 or Uint256), matching tinygrad's lack of public repr entries for those helpers.

val pp : Stdlib.Format.formatter -> t -> unit

pp formats a value dtype using to_string.