use burn_core::data::dataloader::Progress;
use burn_optim::lr_scheduler::module_lr_scheduler::ModuleLearningRate;
use burn_std::ExecutionError;
use super::EventProcessorError;
use crate::{
LearnerSummary,
renderer::{EvaluationName, MetricsRenderer},
};
pub enum LearnerEvent<T> {
Start {
total_epochs: usize,
starting_epoch: usize,
label: Option<String>,
},
ProcessedItem(TrainingItem<T>),
StartSplit {
epoch_number: usize,
total_items: usize,
},
EndSplit(usize),
EndEpoch(usize),
End(Option<LearnerSummary>),
}
pub enum EvaluatorEvent<T> {
Start {
total_tests: usize,
},
StartTest(EvaluationName, usize),
ProcessedItem(EvaluationName, EvaluationItem<T>),
EndTest,
End(Option<LearnerSummary>),
}
pub trait ItemLazy: Send + Sized {
fn sync(self) -> Result<Self, ExecutionError>;
}
pub trait EventProcessorTraining<TrainEvent, ValidEvent>: Send {
fn process_train(&mut self, event: TrainEvent) -> Result<(), EventProcessorError>;
fn process_valid(&mut self, event: ValidEvent) -> Result<(), EventProcessorError>;
fn flush(&mut self) -> Result<(), EventProcessorError> {
Ok(())
}
fn renderer(self) -> Box<dyn MetricsRenderer>;
}
pub trait EventProcessorEvaluation: Send {
type ItemTest: ItemLazy;
fn process_test(
&mut self,
event: EvaluatorEvent<Self::ItemTest>,
) -> Result<(), EventProcessorError>;
fn flush(&mut self) -> Result<(), EventProcessorError> {
Ok(())
}
fn renderer(self) -> Box<dyn MetricsRenderer>;
}
#[derive(new)]
pub struct TrainingItem<T> {
pub item: T,
pub progress: Progress,
pub iteration: Option<usize>,
pub lr: Option<ModuleLearningRate>,
}
impl<T: ItemLazy> ItemLazy for TrainingItem<T> {
fn sync(self) -> Result<Self, ExecutionError> {
Ok(TrainingItem {
item: self.item.sync()?,
progress: self.progress,
iteration: self.iteration,
lr: self.lr,
})
}
}
#[derive(new)]
pub struct EvaluationItem<T> {
pub item: T,
pub progress: Progress,
pub iteration: Option<usize>,
}
impl<T: ItemLazy> ItemLazy for EvaluationItem<T> {
fn sync(self) -> Result<Self, ExecutionError> {
Ok(EvaluationItem {
item: self.item.sync()?,
progress: self.progress,
iteration: self.iteration,
})
}
}
impl ItemLazy for () {
fn sync(self) -> Result<Self, ExecutionError> {
Ok(())
}
}