Skip to content

Commit e460b61

Browse files
voidstackloopclaude
andcommitted
dtype: float8 E4M3FN and E5M2 tensors (round-to-nearest-even, torch overflow semantics, safetensors/torch I/O); codes match torch on a 1642-code sweep
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent faa6a6e commit e460b61

10 files changed

Lines changed: 387 additions & 14 deletions

File tree

‎crates/oxideon-core/src/backend/cpu.rs‎

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use rayon::prelude::*;
55

66
use crate::backend::{BinaryOp, BitOp, CmpOp, LogicOp, PredOp, ReduceOp, UnaryOp};
7-
use crate::dtype::{DType, Element, bf16, f16};
7+
use crate::dtype::{DType, Element, bf16, f16, f8e4m3, f8e5m2};
88
use crate::error::{Error, Result};
99
use crate::gemm::{batched_gemm_f32, batched_gemm_f64};
1010
use crate::layout::{DimMap, Layout, ViewSpec, broadcast_shapes};
@@ -23,6 +23,8 @@ pub enum CpuStorage {
2323
I64(Vec<i64>),
2424
F16(Vec<f16>),
2525
BF16(Vec<bf16>),
26+
F8E4M3(Vec<f8e4m3>),
27+
F8E5M2(Vec<f8e5m2>),
2628
F32(Vec<f32>),
2729
F64(Vec<f64>),
2830
}
@@ -95,6 +97,8 @@ impl CpuStorage {
9597
Self::I64(_) => DType::I64,
9698
Self::F16(_) => DType::F16,
9799
Self::BF16(_) => DType::BF16,
100+
Self::F8E4M3(_) => DType::F8E4M3,
101+
Self::F8E5M2(_) => DType::F8E5M2,
98102
Self::F32(_) => DType::F32,
99103
Self::F64(_) => DType::F64,
100104
}
@@ -113,6 +117,8 @@ impl CpuStorage {
113117
Self::I64(v) => v.len(),
114118
Self::F16(v) => v.len(),
115119
Self::BF16(v) => v.len(),
120+
Self::F8E4M3(v) => v.len(),
121+
Self::F8E5M2(v) => v.len(),
116122
Self::F32(v) => v.len(),
117123
Self::F64(v) => v.len(),
118124
}
@@ -160,6 +166,20 @@ impl CpuStorage {
160166
layout.storage_offsets().all(|i| v[i].to_f32().is_finite())
161167
}
162168
}
169+
Self::F8E4M3(v) => {
170+
if let Some(r) = layout.contiguous_range_in(v.len()) {
171+
v[r].iter().all(|x| x.to_f32().is_finite())
172+
} else {
173+
layout.storage_offsets().all(|i| v[i].to_f32().is_finite())
174+
}
175+
}
176+
Self::F8E5M2(v) => {
177+
if let Some(r) = layout.contiguous_range_in(v.len()) {
178+
v[r].iter().all(|x| x.to_f32().is_finite())
179+
} else {
180+
layout.storage_offsets().all(|i| v[i].to_f32().is_finite())
181+
}
182+
}
163183
_ => true,
164184
}
165185
}
@@ -178,6 +198,8 @@ impl CpuStorage {
178198
DType::I64 => Self::I64(data.iter().map(|x| x.to_i64()).collect()),
179199
DType::F16 => Self::F16(data.iter().map(|x| f16::from_f32(x.to_f32())).collect()),
180200
DType::BF16 => Self::BF16(data.iter().map(|x| bf16::from_f32(x.to_f32())).collect()),
201+
DType::F8E4M3 => Self::F8E4M3(data.iter().map(|x| f8e4m3::from_f32(x.to_f32())).collect()),
202+
DType::F8E5M2 => Self::F8E5M2(data.iter().map(|x| f8e5m2::from_f32(x.to_f32())).collect()),
181203
DType::F32 => Self::F32(data.iter().map(|x| x.to_f32()).collect()),
182204
DType::F64 => Self::F64(data.iter().map(|x| x.to_f64()).collect()),
183205
})
@@ -197,6 +219,8 @@ impl CpuStorage {
197219
DType::I64 => Self::I64(vec![val as i64; numel]),
198220
DType::F16 => Self::F16(vec![f16::from_f32(val as f32); numel]),
199221
DType::BF16 => Self::BF16(vec![bf16::from_f32(val as f32); numel]),
222+
DType::F8E4M3 => Self::F8E4M3(vec![f8e4m3::from_f32(val as f32); numel]),
223+
DType::F8E5M2 => Self::F8E5M2(vec![f8e5m2::from_f32(val as f32); numel]),
200224
DType::F32 => Self::F32(vec![val as f32; numel]),
201225
DType::F64 => Self::F64(vec![val; numel]),
202226
})
@@ -257,6 +281,8 @@ impl CpuStorage {
257281
Self::I64(v) => extract!(v, |&x: &i64| T::from_i64(x)),
258282
Self::F16(v) => extract!(v, |&x: &f16| T::from_f32(x.to_f32())),
259283
Self::BF16(v) => extract!(v, |&x: &bf16| T::from_f32(x.to_f32())),
284+
Self::F8E4M3(v) => extract!(v, |&x: &f8e4m3| T::from_f32(x.to_f32())),
285+
Self::F8E5M2(v) => extract!(v, |&x: &f8e5m2| T::from_f32(x.to_f32())),
260286
Self::F32(v) => extract!(v, |&x: &f32| T::from_f32(x)),
261287
Self::F64(v) => extract!(v, |&x: &f64| T::from_f64(x)),
262288
}
@@ -333,6 +359,8 @@ impl CpuStorage {
333359
Self::I64(v) => copy_typed!(I64, v),
334360
Self::F16(v) => copy_typed!(F16, v),
335361
Self::BF16(v) => copy_typed!(BF16, v),
362+
Self::F8E4M3(v) => copy_typed!(F8E4M3, v),
363+
Self::F8E5M2(v) => copy_typed!(F8E5M2, v),
336364
Self::F32(v) => copy_typed!(F32, v),
337365
Self::F64(v) => copy_typed!(F64, v),
338366
}
@@ -354,6 +382,8 @@ impl CpuStorage {
354382
DType::I64 => Ok(Self::I64(self.to_vec::<i64>(layout))),
355383
DType::F16 => Ok(Self::F16(self.to_vec::<f16>(layout))),
356384
DType::BF16 => Ok(Self::BF16(self.to_vec::<bf16>(layout))),
385+
DType::F8E4M3 => Ok(Self::F8E4M3(self.to_vec::<f8e4m3>(layout))),
386+
DType::F8E5M2 => Ok(Self::F8E5M2(self.to_vec::<f8e5m2>(layout))),
357387
DType::F32 => Ok(Self::F32(self.to_vec::<f32>(layout))),
358388
DType::F64 => Ok(Self::F64(self.to_vec::<f64>(layout))),
359389
}
@@ -407,6 +437,12 @@ impl CpuStorage {
407437
Self::BF16(v) => scatter_typed!(BF16, v, bf16::ZERO, |a: &mut bf16, b: bf16| {
408438
*a = bf16::from_f32(a.to_f32() + b.to_f32())
409439
}),
440+
Self::F8E4M3(v) => scatter_typed!(F8E4M3, v, f8e4m3::ZERO, |a: &mut f8e4m3, b: f8e4m3| {
441+
*a = f8e4m3::from_f32(a.to_f32() + b.to_f32())
442+
}),
443+
Self::F8E5M2(v) => scatter_typed!(F8E5M2, v, f8e5m2::ZERO, |a: &mut f8e5m2, b: f8e5m2| {
444+
*a = f8e5m2::from_f32(a.to_f32() + b.to_f32())
445+
}),
410446
Self::F32(v) => scatter_typed!(F32, v, 0.0f32, |a: &mut f32, b: f32| *a += b),
411447
Self::F64(v) => scatter_typed!(F64, v, 0.0f64, |a: &mut f64, b: f64| *a += b),
412448
}
@@ -478,6 +514,8 @@ impl CpuStorage {
478514
Self::F64(v) => run_unary!(F64, v, |x: f64| eval_unary_f64(x, op)),
479515
Self::F16(v) => run_unary!(F16, v, |x: f16| f16::from_f32(eval_unary_f32(x.to_f32(), op))),
480516
Self::BF16(v) => run_unary!(BF16, v, |x: bf16| bf16::from_f32(eval_unary_f32(x.to_f32(), op))),
517+
Self::F8E4M3(v) => run_unary!(F8E4M3, v, |x: f8e4m3| f8e4m3::from_f32(eval_unary_f32(x.to_f32(), op))),
518+
Self::F8E5M2(v) => run_unary!(F8E5M2, v, |x: f8e5m2| f8e5m2::from_f32(eval_unary_f32(x.to_f32(), op))),
481519
Self::I32(v) => run_unary!(I32, v, |x: i32| eval_unary_f64(x as f64, op) as i64 as i32),
482520
Self::I64(v) => run_unary!(I64, v, |x: i64| eval_unary_f64(x as f64, op) as i64),
483521
Self::U8(v) => run_unary!(U8, v, |x: u8| eval_unary_f64(x as f64, op) as i64 as u8),
@@ -787,6 +825,12 @@ impl CpuStorage {
787825
(Self::BF16(a), Self::BF16(b)) => run_bin!(BF16, a, b, |x: bf16, y: bf16| {
788826
bf16::from_f32(eval_bin_f32(x.to_f32(), y.to_f32(), op))
789827
}),
828+
(Self::F8E4M3(a), Self::F8E4M3(b)) => run_bin!(F8E4M3, a, b, |x: f8e4m3, y: f8e4m3| {
829+
f8e4m3::from_f32(eval_bin_f32(x.to_f32(), y.to_f32(), op))
830+
}),
831+
(Self::F8E5M2(a), Self::F8E5M2(b)) => run_bin!(F8E5M2, a, b, |x: f8e5m2, y: f8e5m2| {
832+
f8e5m2::from_f32(eval_bin_f32(x.to_f32(), y.to_f32(), op))
833+
}),
790834
(Self::I32(a), Self::I32(b)) => run_bin!(I32, a, b, |x: i32, y: i32| match op {
791835
BinaryOp::Add => x.wrapping_add(y),
792836
BinaryOp::Sub => x.wrapping_sub(y),
@@ -1153,6 +1197,12 @@ impl CpuStorage {
11531197
(Self::BF16(a), Self::BF16(b)) => {
11541198
run_cmp!(a, b, |x: bf16, y: bf16| eval_cmp(x.to_f32(), y.to_f32(), op))
11551199
}
1200+
(Self::F8E4M3(a), Self::F8E4M3(b)) => {
1201+
run_cmp!(a, b, |x: f8e4m3, y: f8e4m3| eval_cmp(x.to_f32(), y.to_f32(), op))
1202+
}
1203+
(Self::F8E5M2(a), Self::F8E5M2(b)) => {
1204+
run_cmp!(a, b, |x: f8e5m2, y: f8e5m2| eval_cmp(x.to_f32(), y.to_f32(), op))
1205+
}
11561206
_ => unreachable!(),
11571207
}
11581208
}
@@ -1348,6 +1398,8 @@ impl CpuStorage {
13481398
Self::I64(_) => select_typed!(I64, i64),
13491399
Self::F16(_) => select_typed!(F16, f16),
13501400
Self::BF16(_) => select_typed!(BF16, bf16),
1401+
Self::F8E4M3(_) => select_typed!(F8E4M3, f8e4m3),
1402+
Self::F8E5M2(_) => select_typed!(F8E5M2, f8e5m2),
13511403
Self::F32(_) => select_typed!(F32, f32),
13521404
Self::F64(_) => select_typed!(F64, f64),
13531405
}
@@ -1431,6 +1483,16 @@ impl CpuStorage {
14311483
*a = bf16::from_f32(a.to_f32() + b.to_f32())
14321484
})
14331485
}
1486+
(Self::F8E4M3(_), Self::F8E4M3(_)) => {
1487+
add_typed!(F8E4M3, f8e4m3, |a: &mut f8e4m3, b: f8e4m3| {
1488+
*a = f8e4m3::from_f32(a.to_f32() + b.to_f32())
1489+
})
1490+
}
1491+
(Self::F8E5M2(_), Self::F8E5M2(_)) => {
1492+
add_typed!(F8E5M2, f8e5m2, |a: &mut f8e5m2, b: f8e5m2| {
1493+
*a = f8e5m2::from_f32(a.to_f32() + b.to_f32())
1494+
})
1495+
}
14341496
(Self::F32(_), Self::F32(_)) => {
14351497
add_typed!(F32, f32, |a: &mut f32, b: f32| *a += b)
14361498
}
@@ -1497,6 +1559,8 @@ impl CpuStorage {
14971559
Self::I64(_) => gather_typed!(I64, i64),
14981560
Self::F16(_) => gather_typed!(F16, f16),
14991561
Self::BF16(_) => gather_typed!(BF16, bf16),
1562+
Self::F8E4M3(_) => gather_typed!(F8E4M3, f8e4m3),
1563+
Self::F8E5M2(_) => gather_typed!(F8E5M2, f8e5m2),
15001564
Self::F32(_) => gather_typed!(F32, f32),
15011565
Self::F64(_) => gather_typed!(F64, f64),
15021566
}
@@ -1590,6 +1654,16 @@ impl CpuStorage {
15901654
*a = bf16::from_f32(a.to_f32() + b.to_f32())
15911655
})
15921656
}
1657+
(Self::F8E4M3(_), Self::F8E4M3(_)) => {
1658+
scatter_typed!(F8E4M3, f8e4m3, |a: &mut f8e4m3, b: f8e4m3| {
1659+
*a = f8e4m3::from_f32(a.to_f32() + b.to_f32())
1660+
})
1661+
}
1662+
(Self::F8E5M2(_), Self::F8E5M2(_)) => {
1663+
scatter_typed!(F8E5M2, f8e5m2, |a: &mut f8e5m2, b: f8e5m2| {
1664+
*a = f8e5m2::from_f32(a.to_f32() + b.to_f32())
1665+
})
1666+
}
15931667
(Self::F32(_), Self::F32(_)) => {
15941668
scatter_typed!(F32, f32, |a: &mut f32, b: f32| *a += b)
15951669
}
@@ -1666,6 +1740,8 @@ impl CpuStorage {
16661740
(Self::I64(_), Self::I64(_)) => scatter_write!(I64, i64),
16671741
(Self::F16(_), Self::F16(_)) => scatter_write!(F16, f16),
16681742
(Self::BF16(_), Self::BF16(_)) => scatter_write!(BF16, bf16),
1743+
(Self::F8E4M3(_), Self::F8E4M3(_)) => scatter_write!(F8E4M3, f8e4m3),
1744+
(Self::F8E5M2(_), Self::F8E5M2(_)) => scatter_write!(F8E5M2, f8e5m2),
16691745
(Self::F32(_), Self::F32(_)) => scatter_write!(F32, f32),
16701746
(Self::F64(_), Self::F64(_)) => scatter_write!(F64, f64),
16711747
_ => Err(Error::DTypeMismatch {
@@ -1722,6 +1798,8 @@ impl CpuStorage {
17221798
(Self::I64(tv), Self::I64(fv)) => select_typed!(I64, tv, fv),
17231799
(Self::F16(tv), Self::F16(fv)) => select_typed!(F16, tv, fv),
17241800
(Self::BF16(tv), Self::BF16(fv)) => select_typed!(BF16, tv, fv),
1801+
(Self::F8E4M3(tv), Self::F8E4M3(fv)) => select_typed!(F8E4M3, tv, fv),
1802+
(Self::F8E5M2(tv), Self::F8E5M2(fv)) => select_typed!(F8E5M2, tv, fv),
17251803
(Self::F32(tv), Self::F32(fv)) => select_typed!(F32, tv, fv),
17261804
(Self::F64(tv), Self::F64(fv)) => select_typed!(F64, tv, fv),
17271805
_ => unreachable!(),
@@ -2281,6 +2359,24 @@ impl CpuStorage {
22812359
let lc = Layout::contiguous(shape.clone());
22822360
Ok((c_f32.to_dtype(&lc, self.dtype())?, shape))
22832361
}
2362+
(Self::F8E4M3(_), Self::F8E4M3(_)) => {
2363+
let a_f32 = self.to_dtype(lhs_l, DType::F32)?;
2364+
let b_f32 = rhs.to_dtype(rhs_l, DType::F32)?;
2365+
let la = Layout::contiguous(lhs_l.shape().to_vec());
2366+
let lb = Layout::contiguous(rhs_l.shape().to_vec());
2367+
let (c_f32, shape) = a_f32.matmul(&la, &b_f32, &lb)?;
2368+
let lc = Layout::contiguous(shape.clone());
2369+
Ok((c_f32.to_dtype(&lc, self.dtype())?, shape))
2370+
}
2371+
(Self::F8E5M2(_), Self::F8E5M2(_)) => {
2372+
let a_f32 = self.to_dtype(lhs_l, DType::F32)?;
2373+
let b_f32 = rhs.to_dtype(rhs_l, DType::F32)?;
2374+
let la = Layout::contiguous(lhs_l.shape().to_vec());
2375+
let lb = Layout::contiguous(rhs_l.shape().to_vec());
2376+
let (c_f32, shape) = a_f32.matmul(&la, &b_f32, &lb)?;
2377+
let lc = Layout::contiguous(shape.clone());
2378+
Ok((c_f32.to_dtype(&lc, self.dtype())?, shape))
2379+
}
22842380
_ => {
22852381
let a_f64 = self.to_dtype(lhs_l, DType::F64)?;
22862382
let b_f64 = rhs.to_dtype(rhs_l, DType::F64)?;

0 commit comments

Comments
 (0)