use alloc::vec::Vec;
use core::fmt::Debug;
use std::{
collections::HashMap,
time::SystemTime,
};
use burn::prelude::{
Shape,
TensorData,
};
use crate::{
burner::testing::audit::audit_probe::AuditProbeEventParams,
errors::{
BunsenError,
BunsenResult,
},
support::reflection::LocationDesc,
};
pub trait AuditProbeEventHandler: Debug {
fn name(&self) -> &str {
std::any::type_name::<Self>()
}
fn on_event(
&mut self,
stub: &AuditProbeEventStub<'_>,
) -> BunsenResult<()>;
}
#[derive(Debug, Clone, PartialEq)]
pub struct AuditProbeEventHeader {
pub label: Option<String>,
pub loc: Option<LocationDesc>,
pub ts: SystemTime,
}
impl AuditProbeEventHeader {
pub fn new(
label: Option<String>,
loc: Option<LocationDesc>,
ts: Option<SystemTime>,
) -> Self {
Self {
label,
loc,
ts: ts.unwrap_or_else(SystemTime::now),
}
}
}
pub trait AuditProbeEventView: Debug {
fn to_event(&self) -> AuditProbeEvent {
AuditProbeEvent {
header: self.header().clone(),
params: self.params().clone(),
data: self.to_owned_data_map(),
}
}
fn to_stub(&self) -> AuditProbeEventStub<'_>;
fn header(&self) -> &AuditProbeEventHeader;
fn params(&self) -> &AuditProbeEventParams;
fn data_map_iter(&self) -> impl Iterator<Item = (&str, Vec<&TensorData>)>;
fn data_map_view(&self) -> HashMap<&str, Vec<&TensorData>> {
self.data_map_iter().collect()
}
fn data_map_shape_signature(&self) -> HashMap<String, Vec<Shape>> {
self.data_map_iter()
.map(|(k, v)| {
(
k.to_owned(),
v.iter().map(|&d| d.shape.clone()).collect::<Vec<Shape>>(),
)
})
.collect()
}
fn to_owned_data_map(&self) -> HashMap<String, Vec<TensorData>> {
self.data_map_iter()
.map(|(k, v)| {
(
k.to_owned(),
v.iter().map(|&d| d.clone()).collect::<Vec<TensorData>>(),
)
})
.collect()
}
fn assert_shape_signatures_eq(
&self,
expected: &impl AuditProbeEventView,
) -> BunsenResult<()> {
let actual_shape_sig = self.data_map_shape_signature();
let expected_shape_sig = expected.data_map_shape_signature();
if actual_shape_sig != expected_shape_sig {
Err(BunsenError::Invalid(format!(
"data map signatures don't match:\nactual: {:#?}\nexpect: {:#?}",
actual_shape_sig, expected_shape_sig,
)))
} else {
Ok(())
}
}
}
#[derive(Debug, Clone)]
pub struct AuditProbeEvent {
pub header: AuditProbeEventHeader,
pub params: AuditProbeEventParams,
pub data: HashMap<String, Vec<TensorData>>,
}
impl AuditProbeEventView for AuditProbeEvent {
fn to_stub(&self) -> AuditProbeEventStub<'_> {
AuditProbeEventStub {
header: self.header.clone(),
params: self.params.clone(),
data: self
.data
.iter()
.map(|(k, v)| (k.clone(), v.iter().collect::<Vec<_>>()))
.collect(),
}
}
fn header(&self) -> &AuditProbeEventHeader {
&self.header
}
fn params(&self) -> &AuditProbeEventParams {
&self.params
}
fn data_map_iter(&self) -> impl Iterator<Item = (&str, Vec<&TensorData>)> {
self.data
.iter()
.map(|(k, v)| (k.as_ref(), v.iter().collect::<Vec<_>>()))
}
fn to_owned_data_map(&self) -> HashMap<String, Vec<TensorData>> {
self.data.clone()
}
}
#[derive(Debug, Clone)]
pub struct AuditProbeEventStub<'a> {
pub header: AuditProbeEventHeader,
pub params: AuditProbeEventParams,
pub data: HashMap<String, Vec<&'a TensorData>>,
}
impl<'a> AuditProbeEventView for AuditProbeEventStub<'a> {
fn to_stub(&self) -> AuditProbeEventStub<'a> {
self.clone()
}
fn header(&self) -> &AuditProbeEventHeader {
&self.header
}
fn params(&self) -> &AuditProbeEventParams {
&self.params
}
fn data_map_iter(&self) -> impl Iterator<Item = (&str, Vec<&TensorData>)> {
self.data.iter().map(|(k, v)| (k.as_ref(), v.clone()))
}
}
#[cfg(test)]
mod test {
use std::panic::Location;
use super::*;
use crate::burner::descriptors::{
ToleranceDesc,
TolerancePolicy,
};
#[test]
fn test_event_stub() -> BunsenResult<()> {
let header = AuditProbeEventHeader::new(
Some("example header".to_string()),
Some(Location::caller().into()),
None,
);
let params = AuditProbeEventParams::AssertTensorApproxEx {
tolerance: ToleranceDesc::of::<f32>(TolerancePolicy::default())?,
};
let a = TensorData::from([1, 2, 3]);
let b = TensorData::from([[2.0, 3.0], [4.0, 5.0]]);
let event = AuditProbeEvent {
header: header.clone(),
params: params.clone(),
data: HashMap::from([
("x".to_string(), vec![a.clone()]),
("y".to_string(), vec![a.clone(), b.clone()]),
]),
};
let stub = AuditProbeEventStub {
header: header.clone(),
params: params.clone(),
data: HashMap::from([("x".to_string(), vec![&a]), ("y".to_string(), vec![&a, &b])]),
};
assert_eq!(event.data_map_view(), stub.data_map_view());
Ok(())
}
}