Skip to main content

DType

Enum DType 

Source
pub enum DType {
    Scalar(ScalarDType),
    Vector {
        scalar: ScalarDType,
        count: usize,
    },
    Ptr {
        base: Box<DType>,
        addrspace: AddrSpace,
        size: Option<usize>,
        vcount: usize,
    },
    Image {
        kind: ImageKind,
        shape: Vec<usize>,
    },
}
Expand description

Data type including scalars, vectors, pointers, and images.

Variants§

§

Scalar(ScalarDType)

Scalar type (single value).

§

Vector

Vector type (SIMD).

Fields

§count: usize
§

Ptr

Pointer type. vcount is the vector count of the pointer itself (1 = scalar pointer, >1 = vector of pointers). This matches Tinygrad’s PtrDType.v field.

Fields

§base: Box<DType>
§addrspace: AddrSpace
§vcount: usize
§

Image

Image type (for texture operations).

Fields

§shape: Vec<usize>

Implementations§

Source§

impl DType

Source

pub fn can_safe_cast(from: DType, to: DType) -> bool

Check if casting from from to to is safe (preserves value).

Source

pub fn least_upper_dtype(dtypes: &[DType]) -> Option<DType>

Find the least upper bound type for a set of dtypes.

Returns the smallest type that all input types can be safely cast to.

Type promotion rules:

  • Scalar + Scalar → promoted Scalar
  • Ptr + Ptr → Ptr (same Ptr types)
  • Ptr + Scalar(T) → Scalar(T) (Ptr will be auto-loaded in codegen)
  • Ptr + Scalar(U) → promoted Scalar (if T and U are compatible)
Source§

impl DType

Source

pub fn vec(&self, count: usize) -> DType

Create a vector type from this dtype.

Source

pub fn ptr(self, size: Option<usize>, addrspace: AddrSpace) -> DType

Create a pointer type from this dtype.

Source

pub fn scalar(&self) -> Option<ScalarDType>

Source

pub fn is_vector(&self) -> bool

Check if this is a vector type.

Source

pub fn base(&self) -> ScalarDType

Get the base scalar type (works for both scalars and vectors).

Source

pub fn scalar_dtype(&self) -> DType

Get scalar DType (works on both Scalar and Vector).

Unlike base() which returns ScalarDType, this returns DType. This enables chaining with .vec().

§Examples
use morok_dtype::DType;

let vec_dtype = DType::Float32.vec(4);
assert_eq!(vec_dtype.scalar_dtype(), DType::Float32);

// Enable chaining: dtype.scalar_dtype().vec(new_count)
let new_vec = vec_dtype.scalar_dtype().vec(8);
assert_eq!(new_vec, DType::Float32.vec(8));
Source

pub fn with_base(&self, new_base: ScalarDType) -> DType

Create a new dtype with a different base scalar type, preserving vector count.

Useful for type conversions like bool→uint8 where the structure is preserved.

Source

pub fn with_ptr_base(&self, new_base: DType) -> Option<DType>

For Ptr types: replace the base dtype while preserving addrspace, size, and vcount. Returns None if not a Ptr.

Source

pub fn count(&self) -> usize

Get the vector count (1 for scalars).

Source

pub fn vcount(&self) -> usize

Get effective vectorization count (for pointers to vectors).

Source

pub fn bytes(&self) -> usize

Source

pub fn is_bool(&self) -> bool

Source

pub fn is_signed(&self) -> bool

Source

pub fn is_unsigned(&self) -> bool

Source

pub fn is_int(&self) -> bool

Source

pub fn is_float(&self) -> bool

Source

pub fn is_fp8(&self) -> bool

Source

pub fn min_value(&self) -> f64

Source

pub fn max_value(&self) -> f64

Source

pub fn c_style(&self) -> String

Source§

impl DType

Source

pub const fn bool_() -> DType

Source

pub const fn int8() -> DType

Source

pub const fn int16() -> DType

Source

pub const fn int32() -> DType

Source

pub const fn int64() -> DType

Source

pub const fn uint8() -> DType

Source

pub const fn uint16() -> DType

Source

pub const fn uint32() -> DType

Source

pub const fn uint64() -> DType

Source

pub const fn float16() -> DType

Source

pub const fn bfloat16() -> DType

Source

pub const fn float32() -> DType

Source

pub const fn float64() -> DType

Source

pub const fn void_() -> DType

Source

pub const fn index() -> DType

Source§

impl DType

Source

pub const Bool: DType

Source

pub const Int8: DType

Source

pub const Int16: DType

Source

pub const Int32: DType

Source

pub const Int64: DType

Source

pub const UInt8: DType

Source

pub const UInt16: DType

Source

pub const UInt32: DType

Source

pub const UInt64: DType

Source

pub const FP8E4M3: DType

Source

pub const FP8E5M2: DType

Source

pub const Float16: DType

Source

pub const BFloat16: DType

Source

pub const Float32: DType

Source

pub const Float64: DType

Source

pub const Void: DType

Source

pub const Index: DType

Trait Implementations§

Source§

impl Clone for DType

Source§

fn clone(&self) -> DType

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
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<'de> Deserialize<'de> for DType

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<DType, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<ScalarDType> for DType

Source§

fn from(scalar: ScalarDType) -> DType

Converts to this type from the input type.
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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for DType

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for DType

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,