DType

Enum DType 

Source
pub enum DType {
Show 16 variants U8, I8, I16, I32, U32, I64, U64, F16, F32, F64, Bool, BF16, C64, C128, QInt8, QUInt8,
}
Expand description

Supported data types for tensors

This enum represents all the fundamental data types that can be stored in ToRSh tensors. Each variant corresponds to a specific numeric representation with well-defined size and arithmetic properties.

Variants§

§

U8

8-bit unsigned integer (0 to 255)

§

I8

8-bit signed integer (-128 to 127)

§

I16

16-bit signed integer (-32,768 to 32,767)

§

I32

32-bit signed integer (-2,147,483,648 to 2,147,483,647)

§

U32

32-bit unsigned integer (0 to 4,294,967,295)

§

I64

64-bit signed integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)

§

U64

64-bit unsigned integer (0 to 18,446,744,073,709,551,615)

§

F16

16-bit floating point (half precision) - IEEE 754-2008 binary16

§

F32

32-bit floating point (single precision) - IEEE 754 binary32

§

F64

64-bit floating point (double precision) - IEEE 754 binary64

§

Bool

Boolean (true/false)

§

BF16

Brain floating point 16-bit - Google’s bfloat16 format

§

C64

Complex 64-bit (32-bit real + 32-bit imaginary)

§

C128

Complex 128-bit (64-bit real + 64-bit imaginary)

§

QInt8

Quantized 8-bit signed integer with scale and zero-point

§

QUInt8

Quantized 8-bit unsigned integer with scale and zero-point

Implementations§

Source§

impl DType

Source

pub const fn size(&self) -> usize

Get the size of the dtype in bytes

Returns the number of bytes required to store a single value of this data type. This is fundamental for memory allocation and tensor storage calculations.

§Examples
use torsh_core::dtype::DType;

// Integer types
assert_eq!(DType::U8.size(), 1);
assert_eq!(DType::I8.size(), 1);
assert_eq!(DType::I16.size(), 2);
assert_eq!(DType::I32.size(), 4);
assert_eq!(DType::U32.size(), 4);
assert_eq!(DType::I64.size(), 8);
assert_eq!(DType::U64.size(), 8);

// Floating point types
assert_eq!(DType::F16.size(), 2);
assert_eq!(DType::F32.size(), 4);
assert_eq!(DType::F64.size(), 8);
assert_eq!(DType::BF16.size(), 2);

// Complex types
assert_eq!(DType::C64.size(), 8);
assert_eq!(DType::C128.size(), 16);

// Other types
assert_eq!(DType::Bool.size(), 1);
assert_eq!(DType::QInt8.size(), 1);
assert_eq!(DType::QUInt8.size(), 1);
Source

pub const fn size_bytes(&self) -> usize

Alias for size() method for compatibility

This provides an alternative name for the size method to maintain compatibility with different naming conventions.

Source

pub const fn is_float(&self) -> bool

Check if the dtype is a floating point type

Returns true for IEEE 754 floating point types (F16, F32, F64) and Google’s bfloat16 format (BF16). Complex types are not considered floating point types by this method.

§Examples
use torsh_core::dtype::DType;

// Floating point types return true
assert!(DType::F16.is_float());
assert!(DType::F32.is_float());
assert!(DType::F64.is_float());
assert!(DType::BF16.is_float());

// Non-floating point types return false
assert!(!DType::U8.is_float());
assert!(!DType::I8.is_float());
assert!(!DType::I16.is_float());
assert!(!DType::I32.is_float());
assert!(!DType::I64.is_float());
assert!(!DType::Bool.is_float());
assert!(!DType::C64.is_float());
assert!(!DType::C128.is_float());
assert!(!DType::QInt8.is_float());
assert!(!DType::QUInt8.is_float());
Source

pub const fn is_complex(&self) -> bool

Check if the dtype is a complex number type

Returns true for complex number types that store both real and imaginary components. Complex types use twice the storage of their component type.

§Examples
use torsh_core::dtype::DType;

// Complex types return true
assert!(DType::C64.is_complex());
assert!(DType::C128.is_complex());

// Non-complex types return false
assert!(!DType::U8.is_complex());
assert!(!DType::I8.is_complex());
assert!(!DType::I16.is_complex());
assert!(!DType::I32.is_complex());
assert!(!DType::I64.is_complex());
assert!(!DType::F16.is_complex());
assert!(!DType::F32.is_complex());
assert!(!DType::F64.is_complex());
assert!(!DType::BF16.is_complex());
assert!(!DType::Bool.is_complex());
assert!(!DType::QInt8.is_complex());
assert!(!DType::QUInt8.is_complex());
Source

pub const fn is_int(&self) -> bool

Check if the dtype is an integer type

Returns true for all signed and unsigned integer types, including quantized integer types. Boolean and floating point types return false.

§Examples
use torsh_core::dtype::DType;

// Integer types return true
assert!(DType::U8.is_int());
assert!(DType::I8.is_int());
assert!(DType::I16.is_int());
assert!(DType::I32.is_int());
assert!(DType::U32.is_int());
assert!(DType::I64.is_int());
assert!(DType::U64.is_int());
assert!(DType::QInt8.is_int());
assert!(DType::QUInt8.is_int());

// Non-integer types return false
assert!(!DType::F16.is_int());
assert!(!DType::F32.is_int());
assert!(!DType::F64.is_int());
assert!(!DType::BF16.is_int());
assert!(!DType::Bool.is_int());
assert!(!DType::C64.is_int());
assert!(!DType::C128.is_int());
Source

pub const fn is_quantized(&self) -> bool

Check if the dtype is a quantized type

Returns true for quantized integer types that include scale and zero-point parameters for representing floating point values with reduced precision.

§Examples
use torsh_core::dtype::DType;

// Quantized types return true
assert!(DType::QInt8.is_quantized());
assert!(DType::QUInt8.is_quantized());

// Non-quantized types return false
assert!(!DType::U8.is_quantized());
assert!(!DType::I8.is_quantized());
assert!(!DType::F32.is_quantized());
assert!(!DType::Bool.is_quantized());
Source

pub const fn name(&self) -> &'static str

Get the name of the dtype as a string

Returns a human-readable string representation of the data type. This is useful for debugging, serialization, and user interfaces.

§Examples
use torsh_core::dtype::DType;

assert_eq!(DType::F32.name(), "f32");
assert_eq!(DType::I64.name(), "i64");
assert_eq!(DType::Bool.name(), "bool");
assert_eq!(DType::C64.name(), "c64");
Source

pub const fn is_signed(&self) -> bool

Check if the dtype is signed

Returns true for signed integer types, floating point types, and complex types. Unsigned integers, booleans, and quantized types return false.

§Examples
use torsh_core::dtype::DType;

// Signed types
assert!(DType::I8.is_signed());
assert!(DType::I16.is_signed());
assert!(DType::I32.is_signed());
assert!(DType::I64.is_signed());
assert!(DType::F32.is_signed());
assert!(DType::F64.is_signed());
assert!(DType::C64.is_signed());

// Unsigned types
assert!(!DType::U8.is_signed());
assert!(!DType::U32.is_signed());
assert!(!DType::U64.is_signed());
assert!(!DType::Bool.is_signed());
Source

pub const fn bits(&self) -> usize

Get the number of bits used by this data type

Returns the total number of bits required to represent this data type. This is useful for bit-level operations and memory calculations.

§Examples
use torsh_core::dtype::DType;

assert_eq!(DType::U8.bits(), 8);
assert_eq!(DType::I16.bits(), 16);
assert_eq!(DType::F32.bits(), 32);
assert_eq!(DType::F64.bits(), 64);
assert_eq!(DType::C64.bits(), 64); // Complex64 = 2 * 32-bit floats
assert_eq!(DType::C128.bits(), 128); // Complex128 = 2 * 64-bit floats
Source

pub const fn component_type(&self) -> Option<DType>

Get the component type for complex numbers

For complex types, returns the data type of the real and imaginary components. For non-complex types, returns None.

§Examples
use torsh_core::dtype::DType;

assert_eq!(DType::C64.component_type(), Some(DType::F32));
assert_eq!(DType::C128.component_type(), Some(DType::F64));
assert_eq!(DType::F32.component_type(), None);
assert_eq!(DType::I32.component_type(), None);
Source

pub const fn default_value_f32(&self) -> f32

Get default value for this data type

Returns the zero/default value appropriate for this data type. This is useful for initialization and padding operations.

§Examples
use torsh_core::dtype::DType;

assert_eq!(DType::F32.default_value_f32(), 0.0);
assert_eq!(DType::I32.default_value_i32(), 0);
assert_eq!(DType::Bool.default_value_bool(), false);
Source

pub const fn default_value_i32(&self) -> i32

Source

pub const fn default_value_bool(&self) -> bool

Trait Implementations§

Source§

impl AutoPromote<DType> for DType

Source§

fn auto_promote() -> DType

Get the promoted type when combining with type T
Source§

impl AutoPromote<bool> for DType

Source§

fn auto_promote() -> DType

Get the promoted type when combining with type T
Source§

impl AutoPromote<f32> for DType

Source§

fn auto_promote() -> DType

Get the promoted type when combining with type T
Source§

impl AutoPromote<f64> for DType

Source§

fn auto_promote() -> DType

Get the promoted type when combining with type T
Source§

impl AutoPromote<i32> for DType

Source§

fn auto_promote() -> DType

Get the promoted type when combining with type T
Source§

impl AutoPromote<i64> for DType

Source§

fn auto_promote() -> DType

Get the promoted type when combining with type T
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 Default for DType

Default implementation for DType

The default data type is F32 (32-bit floating point), which is the most commonly used type for deep learning and scientific computing.

Source§

fn default() -> DType

Returns the “default value” for a type. 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 Display for DType

Display implementation for DType

Provides a human-readable string representation using the name() method. This is used when printing or formatting DType values.

Source§

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

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

impl FromStr for DType

Convert from string to DType

Parses a string representation of a data type name into the corresponding DType enum variant. This is useful for configuration files, command-line arguments, and serialization.

Source§

type Err = String

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<DType, <DType as FromStr>::Err>

Parses a string s to return a value of this type. Read more
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 TryFrom<OnnxDataType> for DType

Source§

type Error = TorshError

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

fn try_from(onnx_type: OnnxDataType) -> Result<DType, TorshError>

Performs the conversion.
Source§

impl TypePromotion for DType

Source§

fn promote_types(type1: DType, type2: DType) -> DType

Promote two types to a common type that can represent both Read more
Source§

fn can_promote_to(from: DType, to: DType) -> bool

Check if a type can be safely converted to another type Read more
Source§

fn promotion_precedence(dtype: DType) -> u8

Get the promotion precedence of a type Read more
Source§

fn result_type(types: &[DType]) -> DType

Find the result type for a sequence of operations Read more
Source§

fn allows_implicit_conversion(from: DType, to: DType) -> bool

Check if implicit conversion is allowed Read more
Source§

fn common_type(types: &[DType]) -> Option<DType>

Find the common type for a collection of types Read more
Source§

impl Copy for DType

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 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<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

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

Source§

fn vzip(self) -> V

Source§

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