use alloc::collections::BTreeMap;
use std::time::SystemTime;
use burn::prelude::TensorData;
use serde::{
Deserialize,
Serialize,
};
use crate::{
audit::{
AuditProbeEvent,
AuditProbeEventHeader,
AuditProbeEventParams,
},
burner::descriptors::ToleranceDesc,
rust_ext::reflection::LocationDesc,
};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum AuditParamsRecord {
AssertTensorEq,
AssertTensorApproxEq {
tolerance: ToleranceDesc,
},
}
impl From<&AuditProbeEventParams> for AuditParamsRecord {
fn from(params: &AuditProbeEventParams) -> Self {
match params {
AuditProbeEventParams::AssertTensorEq => Self::AssertTensorEq,
AuditProbeEventParams::AssertTensorApproxEx { tolerance } => {
Self::AssertTensorApproxEq {
tolerance: *tolerance,
}
}
}
}
}
impl From<AuditParamsRecord> for AuditProbeEventParams {
fn from(record: AuditParamsRecord) -> Self {
match record {
AuditParamsRecord::AssertTensorEq => Self::AssertTensorEq,
AuditParamsRecord::AssertTensorApproxEq { tolerance } => {
Self::AssertTensorApproxEx { tolerance }
}
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AuditEventRecord {
pub label: Option<String>,
pub loc: Option<LocationDesc>,
pub ts: SystemTime,
pub params: AuditParamsRecord,
pub data: BTreeMap<String, Vec<TensorData>>,
}
impl From<&AuditProbeEvent> for AuditEventRecord {
fn from(event: &AuditProbeEvent) -> Self {
Self {
label: event.header.label.clone(),
loc: event.header.loc.clone(),
ts: event.header.ts,
params: (&event.params).into(),
data: event
.data
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect(),
}
}
}
impl From<AuditEventRecord> for AuditProbeEvent {
fn from(record: AuditEventRecord) -> Self {
Self {
header: AuditProbeEventHeader::new(record.label, record.loc, Some(record.ts)),
params: record.params.into(),
data: record.data.into_iter().collect(),
}
}
}