Skip to main content

DType

Enum DType 

Source
pub enum DType {
    F32,
    F16,
    BF16,
    I8,
    I32,
    Q4_0,
    Q4_1,
    Q5_0,
    Q5_1,
    Q8_0,
}
Expand description

The element type of a tensor.

This enumerates every numeric format the Kopitiam Runtime can hold in a tensor, including the block-quantized formats used by GGUF weights. It deliberately does not say how a format is computed on — a kernel may dequantize a DType::Q4_0 block to f32 before multiplying, or it may have a fused quantized path. That is kopitiam-kernels’ business; this type only describes what the bytes mean.

§Why block-quantized formats are dtypes and not a separate concept

It is tempting to model quantized weights as “a f32 tensor with a compression scheme attached”. That is the wrong abstraction: a Q4_0 tensor genuinely cannot be indexed elementwise without decoding a whole block, so pretending it is a float tensor produces an API that lies. By making quantization a property of the element type, every consumer is forced to ask “can I index this?” (see DType::is_quantized) instead of finding out at runtime.

Variants§

§

F32

IEEE 754 single precision.

§

F16

IEEE 754 half precision.

§

BF16

bfloat16: same exponent range as f32, fewer mantissa bits. The usual format for model weights that must survive f32 dynamic range without paying for 32 bits.

§

I8

Signed 8-bit integer.

§

I32

Signed 32-bit integer, mostly for token ids and indices.

§

Q4_0

4-bit block quantization, symmetric, 32 elements per block plus one f16 scale.

§

Q4_1

4-bit block quantization, asymmetric: 32 elements per block plus an f16 scale and an f16 minimum.

§

Q5_0

5-bit block quantization, symmetric, 32 elements per block.

§

Q5_1

5-bit block quantization, asymmetric, 32 elements per block.

§

Q8_0

8-bit block quantization, symmetric, 32 elements per block plus one f16 scale.

Implementations§

Source§

impl DType

Source

pub const fn block_size(self) -> usize

Number of tensor elements encoded per storage block.

Non-quantized types are trivially one element per “block”. Quantized types pack Self::block_size elements into Self::block_bytes bytes, which is why a quantized tensor’s element count must be a multiple of this.

Source

pub const fn block_bytes(self) -> usize

Bytes occupied by one block of Self::block_size elements.

The quantized numbers below are the on-disk GGUF block layouts: Q4_0 is a 2-byte f16 scale plus 32 4-bit weights (16 bytes) = 18; Q4_1 adds a 2-byte minimum = 20; Q5_0 adds a 4-byte high-bit field to Q4_0 = 22; Q5_1 likewise on Q4_1 = 24; Q8_0 is a 2-byte scale plus 32 bytes = 34.

Source

pub const fn is_quantized(self) -> bool

Whether this type packs multiple elements into a shared block with its own scale, and therefore cannot be indexed one element at a time.

Source

pub const fn is_float(self) -> bool

Whether this type is a floating-point format that kernels can compute on directly.

Source

pub const fn storage_bytes(self, elements: usize) -> Option<usize>

Storage bytes needed to hold elements values of this type.

Returns None when elements is not a whole number of blocks, which for a quantized type is not a representable tensor rather than a rounding question — surfacing it as None keeps callers from silently allocating a buffer that cannot hold what they asked for.

Trait Implementations§

Source§

impl Clone for DType

Source§

fn clone(&self) -> DType

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for DType

Source§

impl Debug for DType

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for DType

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Eq for DType

Source§

impl Hash for DType

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for DType

Source§

fn eq(&self, other: &DType) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for DType

Auto Trait Implementations§

§

impl Freeze for DType

§

impl RefUnwindSafe for DType

§

impl Send for DType

§

impl Sync for DType

§

impl Unpin for DType

§

impl UnsafeUnpin for DType

§

impl UnwindSafe for DType

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.