use burn::{
Tensor,
prelude::TensorData,
};
pub fn bf16_round(x: f32) -> f32 {
if !x.is_finite() {
return x;
}
let bits = x.to_bits();
let round = 0x7FFF + ((bits >> 16) & 1);
f32::from_bits(bits.wrapping_add(round) & 0xFFFF_0000)
}
pub fn round_to_bf16<const D: usize>(x: Tensor<D>) -> Tensor<D> {
let device = x.device();
let shape = x.dims();
let values: Vec<f32> = x
.into_data()
.try_into_vec_as::<f32>()
.expect("read back f32 values")
.into_iter()
.map(bf16_round)
.collect();
Tensor::from_data(TensorData::new(values, shape), &device)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::support::testing::cpu_device;
#[test]
fn test_bf16_round() {
assert_eq!(bf16_round(1.0), 1.0);
assert_eq!(bf16_round(0.0), 0.0);
assert_eq!(bf16_round(1.0 + 2f32.powi(-8)), 1.0);
assert_eq!(bf16_round(1.0 + 3.0 * 2f32.powi(-8)), 1.0 + 2f32.powi(-6));
assert_eq!(bf16_round(1.0 + 2f32.powi(-7)), 1.0 + 2f32.powi(-7));
let x = 0.123_456_7f32;
assert_eq!(bf16_round(bf16_round(x)), bf16_round(x));
assert!((bf16_round(x) - x).abs() <= x * 2f32.powi(-8));
assert_eq!(bf16_round(f32::INFINITY), f32::INFINITY);
assert!(bf16_round(f32::NAN).is_nan());
}
#[test]
fn test_round_to_bf16() {
let device = cpu_device();
let values = vec![1.0f32 + 2f32.powi(-8), 0.123_456_7, -3.0, 1e-3];
let x = Tensor::<2>::from_data(TensorData::new(values.clone(), [2, 2]), &device);
let y = round_to_bf16(x);
assert_eq!(y.dims(), [2, 2]);
let got: Vec<f32> = y.into_data().try_into_vec_as().unwrap();
let want: Vec<f32> = values.into_iter().map(bf16_round).collect();
assert_eq!(got, want);
}
}