Skip to main content

singe_cuda/
data_type.rs

1//! CUDA scalar data types and native type mappings.
2
3use std::fmt::Debug;
4
5use num_enum::{IntoPrimitive, TryFromPrimitive};
6use singe_core::impl_enum_display;
7use singe_cuda_sys::library_types::cudaDataType_t;
8
9use crate::{
10    types::{
11        Complex, Complex32, Complex64, bf16, f4e2m1, f6e2m3, f6e3m2, f8e4m3, f8e5m2, f8ue5m3,
12        f8ue8m0, f16,
13    },
14    view::{AnyBitPatternDeviceRepr, NoUninitDeviceRepr},
15};
16
17singe_core::native_enum! {
18    /// Rust wrapper for CUDA's data type enum.
19    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
20    #[non_exhaustive]
21    #[repr(u32)]
22    pub enum DataType: cudaDataType_t {
23        /// Real as an unsigned `nv_fp8_ue5m3`.
24        F8UE5M3 = CUDA_R_8F_UE5M3,
25        /// 16-bit real half precision floating-point (IEEE 754-2008 binary16).
26        F16 = CUDA_R_16F,
27        /// 32-bit complex (2x16-bit half precision floats).
28        ComplexF16 = CUDA_C_16F,
29        /// 16-bit real bfloat16 floating-point.
30        Bf16 = CUDA_R_16BF,
31        /// 32-bit complex (2x16-bit bfloat16 floats).
32        ComplexBf16 = CUDA_C_16BF,
33        /// 32-bit real single precision floating-point (IEEE 754 binary32).
34        F32 = CUDA_R_32F,
35        /// 64-bit complex (2x32-bit single precision floats).
36        ComplexF32 = CUDA_C_32F,
37        /// 64-bit real double precision floating-point (IEEE 754 binary64).
38        F64 = CUDA_R_64F,
39        /// 128-bit complex (2x64-bit double precision floats).
40        ComplexF64 = CUDA_C_64F,
41        /// 4-bit real signed integer.
42        I4 = CUDA_R_4I,
43        /// 8-bit complex (2x4-bit signed integers).
44        ComplexI4 = CUDA_C_4I,
45        /// 4-bit real unsigned integer.
46        U4 = CUDA_R_4U,
47        /// 8-bit complex (2x4-bit unsigned integers).
48        ComplexU4 = CUDA_C_4U,
49        /// 8-bit real signed integer.
50        I8 = CUDA_R_8I,
51        /// 16-bit complex (2x8-bit signed integers).
52        ComplexI8 = CUDA_C_8I,
53        /// 8-bit real unsigned integer.
54        U8 = CUDA_R_8U,
55        /// 16-bit complex (2x8-bit unsigned integers).
56        ComplexU8 = CUDA_C_8U,
57        /// 16-bit real signed integer.
58        I16 = CUDA_R_16I,
59        /// 32-bit complex (2x16-bit signed integers).
60        ComplexI16 = CUDA_C_16I,
61        /// 16-bit real unsigned integer.
62        U16 = CUDA_R_16U,
63        /// 32-bit complex (2x16-bit unsigned integers).
64        ComplexU16 = CUDA_C_16U,
65        /// 32-bit real signed integer.
66        I32 = CUDA_R_32I,
67        /// 64-bit complex (2x32-bit signed integers).
68        ComplexI32 = CUDA_C_32I,
69        /// 32-bit real unsigned integer.
70        U32 = CUDA_R_32U,
71        /// 64-bit complex (2x32-bit unsigned integers).
72        ComplexU32 = CUDA_C_32U,
73        /// 64-bit real signed integer.
74        I64 = CUDA_R_64I,
75        /// 128-bit complex (2x64-bit signed integers).
76        ComplexI64 = CUDA_C_64I,
77        /// 64-bit real unsigned integer.
78        U64 = CUDA_R_64U,
79        /// 128-bit complex (2x64-bit unsigned integers).
80        ComplexU64 = CUDA_C_64U,
81        /// 8-bit real floating point in E4M3 format.
82        F8E4M3 = CUDA_R_8F_E4M3,
83        /// 8-bit real floating point in E5M2 format.
84        F8E5M2 = CUDA_R_8F_E5M2,
85        /// 8-bit real floating point in E8M0 format (unsigned exponent, zero mantissa bits).
86        F8UE8M0 = CUDA_R_8F_UE8M0,
87        /// 6-bit real floating point in E2M3 format (2-bit exponent, 3-bit mantissa).
88        F6E2M3 = CUDA_R_6F_E2M3,
89        /// 6-bit real floating point in E3M2 format (3-bit exponent, 2-bit mantissa).
90        F6E3M2 = CUDA_R_6F_E3M2,
91        /// 4-bit real floating point in E2M1 format (2-bit exponent, 1-bit mantissa).
92        F4E2M1 = CUDA_R_4F_E2M1,
93    }
94}
95
96impl DataType {
97    /// Returns the whole-byte storage size of one element.
98    ///
99    /// Sub-byte formats use one byte per storage element here.
100    /// This method does not compute packed array lengths.
101    #[must_use]
102    pub const fn size_of(self) -> usize {
103        match self {
104            Self::ComplexF16
105            | Self::ComplexBf16
106            | Self::F32
107            | Self::I32
108            | Self::U32
109            | Self::ComplexI16
110            | Self::ComplexU16 => 4,
111            Self::ComplexF32
112            | Self::F64
113            | Self::I64
114            | Self::U64
115            | Self::ComplexI32
116            | Self::ComplexU32 => 8,
117            Self::ComplexF64 | Self::ComplexI64 | Self::ComplexU64 => 16,
118            Self::I4
119            | Self::U4
120            | Self::F4E2M1
121            | Self::ComplexI4
122            | Self::ComplexU4
123            | Self::F8E4M3
124            | Self::F8E5M2
125            | Self::F8UE8M0
126            | Self::F8UE5M3
127            | Self::F6E2M3
128            | Self::F6E3M2
129            | Self::I8
130            | Self::U8 => 1,
131            Self::F16 | Self::Bf16 | Self::I16 | Self::U16 | Self::ComplexI8 | Self::ComplexU8 => 2,
132        }
133    }
134}
135
136impl_enum_display!(DataType, {
137    Self::F8UE5M3 => "CUDA_R_8F_UE5M3",
138    Self::F16 => "CUDA_R_16F",
139    Self::ComplexF16 => "CUDA_C_16F",
140    Self::Bf16 => "CUDA_R_16BF",
141    Self::ComplexBf16 => "CUDA_C_16BF",
142    Self::F32 => "CUDA_R_32F",
143    Self::ComplexF32 => "CUDA_C_32F",
144    Self::F64 => "CUDA_R_64F",
145    Self::ComplexF64 => "CUDA_C_64F",
146    Self::I4 => "CUDA_R_4I",
147    Self::ComplexI4 => "CUDA_C_4I",
148    Self::U4 => "CUDA_R_4U",
149    Self::ComplexU4 => "CUDA_C_4U",
150    Self::I8 => "CUDA_R_8I",
151    Self::ComplexI8 => "CUDA_C_8I",
152    Self::U8 => "CUDA_R_8U",
153    Self::ComplexU8 => "CUDA_C_8U",
154    Self::I16 => "CUDA_R_16I",
155    Self::ComplexI16 => "CUDA_C_16I",
156    Self::U16 => "CUDA_R_16U",
157    Self::ComplexU16 => "CUDA_C_16U",
158    Self::I32 => "CUDA_R_32I",
159    Self::ComplexI32 => "CUDA_C_32I",
160    Self::U32 => "CUDA_R_32U",
161    Self::ComplexU32 => "CUDA_C_32U",
162    Self::I64 => "CUDA_R_64I",
163    Self::ComplexI64 => "CUDA_C_64I",
164    Self::U64 => "CUDA_R_64U",
165    Self::ComplexU64 => "CUDA_C_64U",
166    Self::F8E4M3 => "CUDA_R_8F_E4M3",
167    Self::F8E5M2 => "CUDA_R_8F_E5M2",
168    Self::F8UE8M0 => "CUDA_R_8F_UE8M0",
169    Self::F6E2M3 => "CUDA_R_6F_E2M3",
170    Self::F6E3M2 => "CUDA_R_6F_E3M2",
171    Self::F4E2M1 => "CUDA_R_4F_E2M1",
172});
173
174/// A scalar storage representation matching a CUDA numeric datatype.
175///
176/// # Safety
177///
178/// The reported datatype, complexity, size, alignment, and storage ABI must match this Rust type.
179/// Packed formats must follow their CUDA storage-unit conventions.
180/// All bytes of valid values must be initialized, and every initialized storage pattern must be valid for the Rust representation.
181pub unsafe trait DataTypeLike:
182    AnyBitPatternDeviceRepr + NoUninitDeviceRepr + Default + Debug
183{
184    /// Returns the CUDA numeric type matching this Rust storage representation.
185    fn data_type() -> DataType;
186
187    /// Returns whether each stored element contains real and imaginary components.
188    fn is_complex() -> bool;
189
190    /// Returns the Rust storage type name supplied by this implementation.
191    fn rust_type_name() -> &'static str;
192}
193
194macro_rules! impl_data_type {
195    ($ty:ty, $data_type:ident, $is_complex:expr) => {
196        // SAFETY: each invocation below pairs a Rust storage type with the CUDA data type of the same size, alignment, and encoding, and the trait bounds supply the initialization and bit-validity requirements.
197        unsafe impl DataTypeLike for $ty {
198            fn data_type() -> DataType {
199                DataType::$data_type
200            }
201
202            fn is_complex() -> bool {
203                $is_complex
204            }
205
206            fn rust_type_name() -> &'static str {
207                stringify!($ty)
208            }
209        }
210    };
211}
212
213impl_data_type!(f32, F32, false);
214
215impl_data_type!(f64, F64, false);
216
217impl_data_type!(f16, F16, false);
218
219impl_data_type!(bf16, Bf16, false);
220
221impl_data_type!(f8e4m3, F8E4M3, false);
222
223impl_data_type!(f8e5m2, F8E5M2, false);
224
225impl_data_type!(f8ue8m0, F8UE8M0, false);
226
227impl_data_type!(f8ue5m3, F8UE5M3, false);
228
229impl_data_type!(f6e2m3, F6E2M3, false);
230
231impl_data_type!(f6e3m2, F6E3M2, false);
232
233impl_data_type!(f4e2m1, F4E2M1, false);
234
235impl_data_type!(i8, I8, false);
236
237impl_data_type!(u8, U8, false);
238
239impl_data_type!(i32, I32, false);
240
241impl_data_type!(u32, U32, false);
242
243impl_data_type!(Complex<f16>, ComplexF16, true);
244
245impl_data_type!(Complex<bf16>, ComplexBf16, true);
246
247impl_data_type!(Complex32, ComplexF32, true);
248
249impl_data_type!(Complex64, ComplexF64, true);
250
251#[cfg(test)]
252mod tests {
253    use super::*;
254
255    #[test]
256    fn test_eight_bit_integer_sizes_match_rust_storage() {
257        assert_eq!(DataType::I8.size_of(), size_of::<i8>());
258        assert_eq!(DataType::U8.size_of(), size_of::<u8>());
259    }
260
261    #[test]
262    fn test_low_precision_module_reexports_expected_types() {
263        let _ = f16::from_f32(1.0);
264        let _ = bf16::from_f32(1.0);
265        let _ = f8e4m3::from_bits(0);
266        let _ = f8e5m2::from_bits(0);
267        let _ = f8ue8m0::from_bits(0);
268        let _ = f6e2m3::from_bits(0);
269        let _ = f6e3m2::from_bits(0);
270        let _ = f4e2m1::from_bits(0);
271    }
272
273    #[test]
274    fn test_data_type_like_maps_low_precision_storage_types() {
275        assert_eq!(f16::data_type(), DataType::F16);
276        assert_eq!(bf16::data_type(), DataType::Bf16);
277        assert_eq!(f8e4m3::data_type(), DataType::F8E4M3);
278        assert_eq!(f8e5m2::data_type(), DataType::F8E5M2);
279        assert_eq!(f8ue8m0::data_type(), DataType::F8UE8M0);
280        assert_eq!(f6e2m3::data_type(), DataType::F6E2M3);
281        assert_eq!(f6e3m2::data_type(), DataType::F6E3M2);
282        assert_eq!(f4e2m1::data_type(), DataType::F4E2M1);
283    }
284}