#[inline]
pub fn quantize(x: f32, exp_bits: u32, man_bits: u32, max_normal: f32) -> f32 {
rlx_ir::FloatFormat::ieee(exp_bits as u8, man_bits as u8, max_normal).round(x)
}
#[inline]
pub fn quantize_slice(xs: &mut [f32], exp_bits: u32, man_bits: u32, max_normal: f32) {
for x in xs {
*x = quantize(*x, exp_bits, man_bits, max_normal);
}
}
#[inline]
pub fn quantize_slice_scaled(xs: &mut [f32], exp_bits: u32, man_bits: u32, max_normal: f32) {
let absmax = xs.iter().fold(0f32, |a, &x| a.max(x.abs()));
if absmax == 0.0 {
return;
}
let scale = absmax / max_normal;
for x in xs {
*x = quantize(*x / scale, exp_bits, man_bits, max_normal) * scale;
}
}
pub trait FloatFormat: Copy {
const EXP_BITS: u32;
const MAN_BITS: u32;
const MAX_NORMAL: f32;
const NAME: &'static str;
const BITS: u32 = 1 + Self::EXP_BITS + Self::MAN_BITS;
#[inline]
fn quantize(x: f32) -> f32 {
quantize(x, Self::EXP_BITS, Self::MAN_BITS, Self::MAX_NORMAL)
}
}
#[macro_export]
macro_rules! float_format {
($($(#[$m:meta])* $name:ident = ($exp:expr, $man:expr, $max:expr, $spec:literal);)*) => {
$(
$(#[$m])*
#[derive(Clone, Copy, Debug, Default)]
pub struct $name;
impl $crate::lowp::FloatFormat for $name {
const EXP_BITS: u32 = $exp;
const MAN_BITS: u32 = $man;
const MAX_NORMAL: f32 = $max;
const NAME: &'static str = $spec;
}
)*
pub const BUILTIN: &[(&str, u32, u32, f32)] = &[ $(($spec, $exp, $man, $max)),* ];
};
}
float_format! {
F16 = (5, 10, 65504.0, "f16");
Bf16 = (8, 7, 3.3895314e38, "bf16");
F8E4M3 = (4, 3, 448.0, "f8e4m3");
F8E5M2 = (5, 2, 57344.0, "bf8");
Nvf4 = (2, 1, 6.0, "nvf4");
F6E3M2 = (3, 2, 28.0, "f6e3m2");
}
pub fn parse_format(spec: &str) -> Option<(u32, u32, f32)> {
let f = rlx_ir::FloatFormat::parse(spec)?;
Some((f.exp_bits as u32, f.man_bits as u32, f.max_normal))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exact_values_pass_through() {
for &(e, m, max) in &[(5u32, 10u32, 65504.0f32), (4, 3, 448.0), (2, 1, 6.0)] {
for &v in &[0.0, 1.0, -1.0, 0.5, 2.0, -4.0] {
assert_eq!(quantize(v, e, m, max), v, "fmt e{e}m{m} v{v}");
}
}
}
#[test]
fn saturates_no_inf() {
assert_eq!(quantize(1e30, 4, 3, 448.0), 448.0);
assert_eq!(quantize(-1e30, 4, 3, 448.0), -448.0);
assert_eq!(quantize(f32::INFINITY, 5, 10, 65504.0), 65504.0);
assert_eq!(quantize(100.0, 2, 1, 6.0), 6.0); }
#[test]
fn nvf4_grid_is_exact() {
for &v in &[0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0] {
assert_eq!(quantize(v, 2, 1, 6.0), v, "nvf4 {v}");
assert_eq!(quantize(-v, 2, 1, 6.0), -v);
}
assert_eq!(quantize(5.0, 2, 1, 6.0), 4.0);
assert_eq!(quantize(2.5, 2, 1, 6.0), 2.0);
}
#[test]
fn rounds_toward_nearest() {
assert_eq!(quantize(1.0 + 2f32.powi(-11), 5, 10, 65504.0), 1.0);
assert_eq!(
quantize(1.0 + 2f32.powi(-9), 5, 10, 65504.0),
1.0 + 2f32.powi(-9)
);
assert_eq!(quantize(1.1, 4, 3, 448.0), 1.125);
}
#[test]
fn parse_named_and_generic() {
assert_eq!(parse_format("nvf4"), Some((2, 1, 6.0)));
assert_eq!(parse_format("bf8"), Some((5, 2, 57344.0)));
assert_eq!(parse_format("f8e4m3"), Some((4, 3, 448.0)));
assert_eq!(parse_format("f8m3e4").map(|(e, m, _)| (e, m)), Some((4, 3)));
assert_eq!(parse_format("f4m1e2").map(|(e, m, _)| (e, m)), Some((2, 1)));
assert_eq!(
parse_format("f16m10e5").map(|(e, m, _)| (e, m)),
Some((5, 10))
);
assert_eq!(parse_format("f8m4e4"), None); assert_eq!(parse_format("nonsense"), None);
}
#[test]
fn scaled_uses_full_range() {
let mut unscaled = vec![0.02f32, -0.05, 0.1, -0.01];
quantize_slice(&mut unscaled, 2, 1, 6.0);
assert!(unscaled.iter().all(|&x| x == 0.0), "{unscaled:?}");
let mut scaled = vec![0.02f32, -0.05, 0.1, -0.01];
quantize_slice_scaled(&mut scaled, 2, 1, 6.0);
assert!(scaled.iter().any(|&x| x != 0.0), "{scaled:?}");
assert!(
(scaled[2] - 0.1).abs() < 1e-6,
"absmax preserved: {}",
scaled[2]
);
}
#[test]
fn format_trait_consts() {
assert_eq!(Nvf4::BITS, 4);
assert_eq!(F8E4M3::BITS, 8);
assert_eq!(F16::BITS, 16);
assert_eq!(F8E4M3::quantize(1.1), 1.125);
}
}