pub struct ClassifyTrainer { /* private fields */ }Expand description
Production training loop for classification fine-tuning.
Wraps ClassifyPipeline with:
- Epoch management with per-epoch shuffling
- Validation on a disjoint, frozen split
- Warmup + cosine decay LR scheduling
- Periodic checkpointing (SafeTensors + metadata JSON)
- Early stopping with configurable patience
Implementations§
Source§impl ClassifyTrainer
impl ClassifyTrainer
Sourcepub fn new(
pipeline: ClassifyPipeline,
corpus: Vec<SafetySample>,
config: TrainingConfig,
) -> Result<Self>
pub fn new( pipeline: ClassifyPipeline, corpus: Vec<SafetySample>, config: TrainingConfig, ) -> Result<Self>
Sourcepub fn set_monitor_writer(&mut self, writer: TrainingStateWriter)
pub fn set_monitor_writer(&mut self, writer: TrainingStateWriter)
Attach a monitor writer for live TUI updates.
When set, training emits per-batch metrics to the experiment directory
via atomic JSON writes, enabling apr monitor <dir> from another shell.
Sourcepub fn train(&mut self) -> TrainResult
pub fn train(&mut self) -> TrainResult
Run the full training loop.
For each epoch:
- Shuffle training data (deterministic, seed varies per epoch)
- Process batches via
pipeline.train_batch() - Compute validation metrics (forward-only)
- Step LR scheduler
- Record metrics
- Save checkpoint if
save_everyor new best val_loss - Check early stopping
Sourcepub fn save_checkpoint(
&mut self,
path: &Path,
epoch: usize,
metrics: &EpochMetrics,
) -> Result<()>
pub fn save_checkpoint( &mut self, path: &Path, epoch: usize, metrics: &EpochMetrics, ) -> Result<()>
Save checkpoint with metadata JSON and SafeTensors model weights.
When GPU training is active, downloads GPU-updated transformer weights to CPU before saving so checkpoints include all trained parameters.
Creates: {path}/metadata.json and {path}/model.safetensors
§Contract (C-CKPT-001)
- Precondition:
pathis a writable directory (or will be created) - Postcondition: Checkpoint contains all trained parameters including GPU-updated transformer block weights (if GPU training active)
- Invariant: CPU model state is consistent with GPU state after save
Sourcepub fn resume_from_apr_checkpoint(&mut self, apr_path: &Path) -> Result<usize>
pub fn resume_from_apr_checkpoint(&mut self, apr_path: &Path) -> Result<usize>
Resume training state from an APR checkpoint (F-CKPT-006).
Loads model weights (classifier + LoRA) and optimizer state
(__training__.* tensors) from a .ckpt.apr or model.apr file.
Returns the epoch number stored in the checkpoint so the training loop can resume from the next epoch.
§Errors
Returns error if checkpoint is invalid or tensors are missing.
Sourcepub fn split_dataset(
data: &[SafetySample],
val_ratio: f32,
seed: u64,
) -> (Vec<SafetySample>, Vec<SafetySample>)
pub fn split_dataset( data: &[SafetySample], val_ratio: f32, seed: u64, ) -> (Vec<SafetySample>, Vec<SafetySample>)
Split dataset into disjoint train/val sets.
F-LOOP-008: Guarantees zero overlap between train and val. F-LOOP-009: Val set is deterministic given the same seed.
§Arguments
data- Full datasetval_ratio- Fraction for validation (0.0, 0.5]seed- Random seed for deterministic shuffling
Sourcepub fn train_data(&self) -> &[SafetySample]
pub fn train_data(&self) -> &[SafetySample]
Get a reference to the training data.
Sourcepub fn val_data(&self) -> &[SafetySample]
pub fn val_data(&self) -> &[SafetySample]
Get a reference to the validation data.
Sourcepub fn config(&self) -> &TrainingConfig
pub fn config(&self) -> &TrainingConfig
Get a reference to the training config.
Sourcepub fn pipeline_mut(&mut self) -> &mut ClassifyPipeline
pub fn pipeline_mut(&mut self) -> &mut ClassifyPipeline
Get a mutable reference to the underlying pipeline.
Sourcepub fn run_worker(&mut self) -> Result<TrainResult>
pub fn run_worker(&mut self) -> Result<TrainResult>
Run as a distributed worker node.
Connects to the coordinator, then enters a loop:
- Receive shard assignment (or shutdown)
- Compute forward/backward on assigned shard
- Collect LoRA gradients and send to coordinator
- Receive averaged gradients and apply optimizer step
§Contract: F-DP-001 (Weight Consistency)
After applying averaged gradients, worker weights match coordinator weights.
§Errors
Returns error on connection failure or protocol violation.
Sourcepub fn evaluate(
&mut self,
data: &[SafetySample],
label_names: &[String],
) -> ClassifyEvalReport
pub fn evaluate( &mut self, data: &[SafetySample], label_names: &[String], ) -> ClassifyEvalReport
Evaluate the model on a dataset, returning structured per-class metrics.
Runs forward-only on every sample, collects predictions, and computes
precision/recall/F1/confusion matrix via ConfusionMatrix and MultiClassMetrics.
§Arguments
data- Labeled samples to evaluate onlabel_names- Human-readable class names (length must match num_classes)
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for ClassifyTrainer
impl !RefUnwindSafe for ClassifyTrainer
impl !Send for ClassifyTrainer
impl !Sync for ClassifyTrainer
impl Unpin for ClassifyTrainer
impl UnsafeUnpin for ClassifyTrainer
impl !UnwindSafe for ClassifyTrainer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.