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
impl DType
Sourcepub const fn size(&self) -> usize
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);Sourcepub const fn size_bytes(&self) -> usize
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.
Sourcepub const fn is_float(&self) -> bool
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());Sourcepub const fn is_complex(&self) -> bool
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());Sourcepub const fn is_int(&self) -> bool
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());Sourcepub const fn is_quantized(&self) -> bool
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());Sourcepub const fn name(&self) -> &'static str
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");Sourcepub const fn is_signed(&self) -> bool
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());Sourcepub const fn bits(&self) -> usize
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 floatsSourcepub const fn component_type(&self) -> Option<DType>
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);Sourcepub const fn default_value_f32(&self) -> f32
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);pub const fn default_value_i32(&self) -> i32
pub const fn default_value_bool(&self) -> bool
Trait Implementations§
Source§impl AutoPromote<DType> for DType
impl AutoPromote<DType> for DType
Source§fn auto_promote() -> DType
fn auto_promote() -> DType
Source§impl AutoPromote<bool> for DType
impl AutoPromote<bool> for DType
Source§fn auto_promote() -> DType
fn auto_promote() -> DType
Source§impl AutoPromote<f32> for DType
impl AutoPromote<f32> for DType
Source§fn auto_promote() -> DType
fn auto_promote() -> DType
Source§impl AutoPromote<f64> for DType
impl AutoPromote<f64> for DType
Source§fn auto_promote() -> DType
fn auto_promote() -> DType
Source§impl AutoPromote<i32> for DType
impl AutoPromote<i32> for DType
Source§fn auto_promote() -> DType
fn auto_promote() -> DType
Source§impl AutoPromote<i64> for DType
impl AutoPromote<i64> for DType
Source§fn auto_promote() -> DType
fn auto_promote() -> DType
Source§impl Default for DType
Default implementation for DType
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§impl<'de> Deserialize<'de> for DType
impl<'de> Deserialize<'de> for DType
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<DType, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<DType, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Display for DType
Display implementation for DType
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§impl FromStr for DType
Convert from string to DType
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§impl Serialize for DType
impl Serialize for DType
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Source§impl TryFrom<OnnxDataType> for DType
impl TryFrom<OnnxDataType> for DType
Source§type Error = TorshError
type Error = TorshError
Source§fn try_from(onnx_type: OnnxDataType) -> Result<DType, TorshError>
fn try_from(onnx_type: OnnxDataType) -> Result<DType, TorshError>
Source§impl TypePromotion for DType
impl TypePromotion for DType
Source§fn promote_types(type1: DType, type2: DType) -> DType
fn promote_types(type1: DType, type2: DType) -> DType
Source§fn can_promote_to(from: DType, to: DType) -> bool
fn can_promote_to(from: DType, to: DType) -> bool
Source§fn result_type(types: &[DType]) -> DType
fn result_type(types: &[DType]) -> DType
impl Copy for DType
impl Eq for DType
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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