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