use burn::{
prelude::TensorData,
tensor::{
BoolStore,
DType,
Element,
Tolerance,
bf16,
f16,
},
};
use num_traits::{
Float,
ToPrimitive,
};
use crate::{
burner::descriptors::unpack_tolerance,
errors::{
BunsenError,
BunsenResult,
ValueMismatch,
},
};
const MAX_LISTED_DIFFS: usize = 5;
pub trait TensorDataCheckExt {
fn try_assert_eq(
&self,
expected: &TensorData,
strict: bool,
) -> BunsenResult<()>;
#[track_caller]
fn try_assert_eq_elem<E: Element>(
&self,
other: &Self,
) -> BunsenResult<()>;
#[track_caller]
fn try_assert_approx_eq<F: Float + Element>(
&self,
other: &Self,
tolerance: Tolerance<F>,
strict: bool,
) -> BunsenResult<()>;
}
impl TensorDataCheckExt for TensorData {
fn try_assert_eq(
&self,
other: &TensorData,
strict: bool,
) -> BunsenResult<()> {
if strict && self.dtype() != other.dtype() {
return Err(dtype_mismatch(self, other));
}
match self.dtype() {
DType::F64 => self.try_assert_eq_elem::<f64>(other),
DType::F32 | DType::Flex32 => self.try_assert_eq_elem::<f32>(other),
DType::F16 => self.try_assert_eq_elem::<f16>(other),
DType::BF16 => self.try_assert_eq_elem::<bf16>(other),
DType::I64 => self.try_assert_eq_elem::<i64>(other),
DType::I32 => self.try_assert_eq_elem::<i32>(other),
DType::I16 => self.try_assert_eq_elem::<i16>(other),
DType::I8 => self.try_assert_eq_elem::<i8>(other),
DType::U64 => self.try_assert_eq_elem::<u64>(other),
DType::U32 => self.try_assert_eq_elem::<u32>(other),
DType::U16 => self.try_assert_eq_elem::<u16>(other),
DType::U8 => self.try_assert_eq_elem::<u8>(other),
DType::Bool(BoolStore::Native) => self.try_assert_eq_elem::<bool>(other),
DType::Bool(BoolStore::U8) => self.try_assert_eq_elem::<u8>(other),
DType::Bool(BoolStore::U32) => self.try_assert_eq_elem::<u32>(other),
DType::QFloat(q) => {
let q_other = if let DType::QFloat(q_other) = other.dtype() {
q_other
} else {
return Err(ValueMismatch::Quantization {
actual: format!("{q:?}"),
expected: format!("not quantized ({:?})", other.dtype()),
}
.into());
};
if q.value == q_other.value
&& q.block_size() == q_other.block_size()
&& q.scale_dtype() == q_other.scale_dtype()
{
self.try_assert_eq_elem::<i8>(other)
} else {
Err(ValueMismatch::Quantization {
actual: format!("{q:?}"),
expected: format!("{q_other:?}"),
}
.into())
}
}
}
}
#[track_caller]
fn try_assert_eq_elem<E: Element>(
&self,
other: &Self,
) -> BunsenResult<()> {
if self.shape() != other.shape() {
return Err(shape_mismatch(self, other));
}
let mut num_diff = 0;
let mut total = 0;
let mut first = Vec::new();
for (i, (a, b)) in self.iter::<E>().zip(other.iter::<E>()).enumerate() {
total += 1;
if !a.eq(&b) {
if num_diff < MAX_LISTED_DIFFS {
first.push(format!("position {i}: {a} != {b}"));
}
num_diff += 1;
}
}
values_result(num_diff, total, first)
}
#[track_caller]
fn try_assert_approx_eq<F: Float + Element>(
&self,
other: &Self,
tolerance: Tolerance<F>,
strict: bool,
) -> BunsenResult<()> {
if strict && self.dtype() != other.dtype() {
return Err(dtype_mismatch(self, other));
}
if self.shape() != other.shape() {
return Err(shape_mismatch(self, other));
}
let mut num_diff = 0;
let mut total = 0;
let mut first = Vec::new();
let (relative, absolute) = unpack_tolerance(tolerance);
let tol_rel = ToPrimitive::to_f64(&relative).unwrap();
let tol_abs = ToPrimitive::to_f64(&absolute).unwrap();
for (i, (a, b)) in self.iter::<F>().zip(other.iter::<F>()).enumerate() {
total += 1;
let both_nan = a.is_nan() && b.is_nan();
let both_inf =
a.is_infinite() && b.is_infinite() && ((a > F::zero()) == (b > F::zero()));
if both_nan || both_inf {
continue;
}
if !tolerance.approx_eq(F::from(a).unwrap(), F::from(b).unwrap()) {
if num_diff < MAX_LISTED_DIFFS {
let diff_abs = ToPrimitive::to_f64(&(a - b).abs()).unwrap();
let max = F::max(a.abs(), b.abs());
let diff_rel = diff_abs / ToPrimitive::to_f64(&max).unwrap();
first.push(format!(
"position {i}: {a} != {b}; diff (rel = {diff_rel:+.2e}, abs = {diff_abs:+.2e}), tol (rel = {tol_rel:+.2e}, abs = {tol_abs:+.2e})"
));
}
num_diff += 1;
}
}
values_result(num_diff, total, first)
}
}
#[track_caller]
fn dtype_mismatch(
actual: &TensorData,
expected: &TensorData,
) -> BunsenError {
ValueMismatch::DType {
actual: format!("{:?}", actual.dtype()),
expected: format!("{:?}", expected.dtype()),
}
.into()
}
#[track_caller]
fn shape_mismatch(
actual: &TensorData,
expected: &TensorData,
) -> BunsenError {
ValueMismatch::Shape {
actual: actual.shape().to_vec(),
expected: expected.shape().to_vec(),
}
.into()
}
#[track_caller]
fn values_result(
count: usize,
total: usize,
first: Vec<String>,
) -> BunsenResult<()> {
if count == 0 {
Ok(())
} else {
Err(ValueMismatch::Values {
count,
total,
first,
}
.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::errors::{
BunsenErrorKind,
testing::{
ErrorMatcher,
predicate,
},
};
#[test]
fn test_try_assert_eq_dtype() {
let a = TensorData::from([1.0f32]);
let b = TensorData::from([1.0f64]);
a.try_assert_eq(&b, false).unwrap();
ErrorMatcher::kind(BunsenErrorKind::Policy)
.message_eq("data types differ: F32 != F64")
.assert_err(&a.try_assert_eq(&b, true));
}
#[test]
fn test_try_assert_eq_shape() {
let a = TensorData::from([1.0f32, 2.0]);
let b = TensorData::from([[1.0f32, 2.0]]);
ErrorMatcher::kind(BunsenErrorKind::Policy)
.message_eq("shapes differ: [2] != [1, 2]")
.assert_err(&a.try_assert_eq(&b, true));
}
#[test]
fn test_try_assert_eq_lists_five_values() {
let a = TensorData::from([0.0f32; 7]);
let b = TensorData::from([1.0f32; 7]);
let five = TensorData::from([1.0f32, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]);
ErrorMatcher::kind(BunsenErrorKind::Policy)
.message_eq("7 of 7 values differ")
.details_contains("position 4: 0 != 1\n... and 2 more")
.cause(predicate("five listed", |m: &ValueMismatch| {
matches!(m, ValueMismatch::Values { count: 7, total: 7, first } if first.len() == 5)
}))
.assert_err(&a.try_assert_eq(&b, true));
let e = a.try_assert_eq(&five, true).unwrap_err();
assert_eq!(e.message(), "5 of 7 values differ");
assert!(!e.details().unwrap().contains("more"), "{e:#}");
}
#[test]
fn test_try_assert_approx_eq() {
let a = TensorData::from([1.0f32, 2.0]);
let b = TensorData::from([1.0f32, 2.5]);
ErrorMatcher::kind(BunsenErrorKind::Policy)
.message_eq("1 of 2 values differ")
.details_contains("position 1: 2 != 2.5")
.assert_err(&a.try_assert_approx_eq(&b, Tolerance::<f32>::balanced(), true));
}
}