hanzo_ml/
dummy_dtype.rs

1//! Dummy data types for experimental/future float formats
2//!
3//! These are placeholder types for experimental floating-point formats
4//! that are defined in the safetensors spec but not yet fully implemented.
5
6use crate::{DType, Error, Result, WithDType};
7
8/// 6-bit float with 2 exponent bits and 3 mantissa bits (MX6 format)
9/// This is a dummy type.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct F6E2M3;
12
13/// 6-bit float with 3 exponent bits and 2 mantissa bits (MX6 format)
14/// This is a dummy type.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
16pub struct F6E3M2;
17
18/// 4-bit float (MX4 format)
19/// This is a dummy type.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
21pub struct F4;
22
23/// 8-bit float with 8 exponent bits and 0 mantissa bits
24/// This is a dummy type.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
26pub struct F8E8M0;
27
28// Implement WithDType for dummy types
29macro_rules! dummy_with_dtype {
30    ($ty:ty, $dtype:ident) => {
31        impl WithDType for $ty {
32            const DTYPE: DType = DType::$dtype;
33
34            fn from_f64(_v: f64) -> Self {
35                panic!(
36                    "{} is a dummy type and cannot be constructed",
37                    stringify!($ty)
38                )
39            }
40
41            fn to_f64(self) -> f64 {
42                panic!(
43                    "{} is a dummy type and cannot be converted",
44                    stringify!($ty)
45                )
46            }
47
48            fn to_scalar(self) -> crate::scalar::Scalar {
49                panic!(
50                    "{} is a dummy type and cannot be converted to scalar",
51                    stringify!($ty)
52                )
53            }
54
55            fn cpu_storage_ref(_data: &[Self]) -> crate::CpuStorageRef<'_> {
56                panic!(
57                    "{} is a dummy type and does not support storage",
58                    stringify!($ty)
59                )
60            }
61
62            fn to_cpu_storage_owned(_data: Vec<Self>) -> crate::CpuStorage {
63                panic!(
64                    "{} is a dummy type and does not support storage",
65                    stringify!($ty)
66                )
67            }
68
69            fn cpu_storage_data(_s: crate::CpuStorage) -> Result<Vec<Self>> {
70                Err(Error::UnsupportedDTypeForOp(DType::$dtype, "cpu_storage_data").bt())
71            }
72
73            fn cpu_storage_as_slice(_s: &crate::CpuStorage) -> Result<&[Self]> {
74                Err(Error::UnsupportedDTypeForOp(DType::$dtype, "cpu_storage_as_slice").bt())
75            }
76        }
77    };
78}
79
80dummy_with_dtype!(F6E2M3, F6E2M3);
81dummy_with_dtype!(F6E3M2, F6E3M2);
82dummy_with_dtype!(F4, F4);
83dummy_with_dtype!(F8E8M0, F8E8M0);
84
85// Implement NumAssign traits for dummy types
86macro_rules! dummy_num_assign {
87    ($ty:ty) => {
88        impl std::ops::AddAssign for $ty {
89            fn add_assign(&mut self, _other: Self) {
90                panic!(
91                    "{} is a dummy type and does not support operations",
92                    stringify!($ty)
93                )
94            }
95        }
96
97        impl std::ops::SubAssign for $ty {
98            fn sub_assign(&mut self, _other: Self) {
99                panic!(
100                    "{} is a dummy type and does not support operations",
101                    stringify!($ty)
102                )
103            }
104        }
105
106        impl std::ops::MulAssign for $ty {
107            fn mul_assign(&mut self, _other: Self) {
108                panic!(
109                    "{} is a dummy type and does not support operations",
110                    stringify!($ty)
111                )
112            }
113        }
114
115        impl std::ops::DivAssign for $ty {
116            fn div_assign(&mut self, _other: Self) {
117                panic!(
118                    "{} is a dummy type and does not support operations",
119                    stringify!($ty)
120                )
121            }
122        }
123
124        impl std::ops::RemAssign for $ty {
125            fn rem_assign(&mut self, _other: Self) {
126                panic!(
127                    "{} is a dummy type and does not support operations",
128                    stringify!($ty)
129                )
130            }
131        }
132
133        impl std::ops::Add for $ty {
134            type Output = Self;
135            fn add(self, _other: Self) -> Self {
136                panic!(
137                    "{} is a dummy type and does not support operations",
138                    stringify!($ty)
139                )
140            }
141        }
142
143        impl std::ops::Sub for $ty {
144            type Output = Self;
145            fn sub(self, _other: Self) -> Self {
146                panic!(
147                    "{} is a dummy type and does not support operations",
148                    stringify!($ty)
149                )
150            }
151        }
152
153        impl std::ops::Mul for $ty {
154            type Output = Self;
155            fn mul(self, _other: Self) -> Self {
156                panic!(
157                    "{} is a dummy type and does not support operations",
158                    stringify!($ty)
159                )
160            }
161        }
162
163        impl std::ops::Div for $ty {
164            type Output = Self;
165            fn div(self, _other: Self) -> Self {
166                panic!(
167                    "{} is a dummy type and does not support operations",
168                    stringify!($ty)
169                )
170            }
171        }
172
173        impl std::ops::Rem for $ty {
174            type Output = Self;
175            fn rem(self, _other: Self) -> Self {
176                panic!(
177                    "{} is a dummy type and does not support operations",
178                    stringify!($ty)
179                )
180            }
181        }
182
183        impl num_traits::Zero for $ty {
184            fn zero() -> Self {
185                panic!(
186                    "{} is a dummy type and does not support operations",
187                    stringify!($ty)
188                )
189            }
190
191            fn is_zero(&self) -> bool {
192                panic!(
193                    "{} is a dummy type and does not support operations",
194                    stringify!($ty)
195                )
196            }
197        }
198
199        impl num_traits::One for $ty {
200            fn one() -> Self {
201                panic!(
202                    "{} is a dummy type and does not support operations",
203                    stringify!($ty)
204                )
205            }
206        }
207
208        impl num_traits::Num for $ty {
209            type FromStrRadixErr = std::num::ParseFloatError;
210
211            fn from_str_radix(
212                _str: &str,
213                _radix: u32,
214            ) -> std::result::Result<Self, Self::FromStrRadixErr> {
215                panic!(
216                    "{} is a dummy type and does not support parsing",
217                    stringify!($ty)
218                )
219            }
220        }
221
222        impl crate::cpu::kernels::VecOps for $ty {
223            fn min(self, _other: Self) -> Self {
224                panic!(
225                    "{} is a dummy type and does not support operations",
226                    stringify!($ty)
227                )
228            }
229
230            fn max(self, _other: Self) -> Self {
231                panic!(
232                    "{} is a dummy type and does not support operations",
233                    stringify!($ty)
234                )
235            }
236        }
237    };
238}
239
240dummy_num_assign!(F6E2M3);
241dummy_num_assign!(F6E3M2);
242dummy_num_assign!(F4);
243dummy_num_assign!(F8E8M0);
244
245// Display implementations
246impl std::fmt::Display for F6E2M3 {
247    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
248        write!(f, "F6E2M3")
249    }
250}
251
252impl std::fmt::Display for F6E3M2 {
253    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
254        write!(f, "F6E3M2")
255    }
256}
257
258impl std::fmt::Display for F4 {
259    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
260        write!(f, "F4")
261    }
262}
263
264impl std::fmt::Display for F8E8M0 {
265    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
266        write!(f, "F8E8M0")
267    }
268}