Files
foxhunt/crates/ml/tests/wave16_checkpoint_regression_test.rs
jgrusewski 9c3d741a08 refactor: restructure repo — crates/, bin/, testing/ layout
Move 17 library crates into crates/, CLI binary into bin/fxt,
consolidate 10 test crates into testing/, split config crate
from deployment config files.

Root directory reduced from 38+ to ~17 directories.
All Cargo.toml paths and build.rs proto refs updated.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 11:56:00 +01:00

275 lines
9.9 KiB
Rust

/// Wave 16S-V14: Checkpoint Regression Investigation Test
///
/// This test validates checkpoint saving behavior with volatile validation loss patterns.
/// Tests the hypothesis that V13's lower checkpoint count (10/12 vs V12's 12/12) is
/// EXPECTED BEHAVIOR due to fewer validation loss improvements, not a bug.
use std::sync::{Arc, Mutex};
#[derive(Debug, Clone)]
struct CheckpointRecord {
epoch: usize,
val_loss: f64,
checkpoint_type: String, // "BEST" or "PERIODIC"
}
struct MockCheckpointTracker {
best_val_loss: f64,
checkpoints: Arc<Mutex<Vec<CheckpointRecord>>>,
}
impl MockCheckpointTracker {
fn new() -> Self {
Self {
best_val_loss: f64::INFINITY,
checkpoints: Arc::new(Mutex::new(Vec::new())),
}
}
/// Simulates the actual checkpoint logic from dqn.rs lines 1187-1189
fn process_epoch(&mut self, epoch: usize, val_loss: f64, checkpoint_frequency: usize) {
let mut records = self.checkpoints.lock().unwrap();
// Best model checkpoint: Save if validation loss improved
// This matches the actual logic: if val_loss < self.best_val_loss
if val_loss < self.best_val_loss {
self.best_val_loss = val_loss;
records.push(CheckpointRecord {
epoch,
val_loss,
checkpoint_type: "BEST".to_string(),
});
}
// Periodic checkpoint: Save every N epochs
// This matches the actual logic: if checkpoint_frequency > 0 && (epoch + 1) % checkpoint_frequency == 0
if checkpoint_frequency > 0 && epoch % checkpoint_frequency == 0 {
records.push(CheckpointRecord {
epoch,
val_loss,
checkpoint_type: "PERIODIC".to_string(),
});
}
}
fn get_checkpoint_summary(&self) -> (usize, usize, usize) {
let records = self.checkpoints.lock().unwrap();
let total = records.len();
let best_count = records.iter().filter(|r| r.checkpoint_type == "BEST").count();
let periodic_count = records
.iter()
.filter(|r| r.checkpoint_type == "PERIODIC")
.count();
(total, best_count, periodic_count)
}
fn print_checkpoint_log(&self) {
let records = self.checkpoints.lock().unwrap();
println!("\nCheckpoint Log:");
for record in records.iter() {
println!(
" Epoch {}: {} checkpoint (val_loss={:.2})",
record.epoch, record.checkpoint_type, record.val_loss
);
}
}
}
#[test]
fn test_v12_decreasing_loss_pattern() {
// V12 Pattern: Steadily decreasing validation loss (6 improvements)
// Expected: 6 BEST checkpoints + 5 PERIODIC = 11 total
let v12_losses = vec![
12980.33, // Epoch 1: BEST (first epoch always saves)
12783.01, // Epoch 2: BEST + PERIODIC (improvement + checkpoint_frequency)
6498.01, // Epoch 3: BEST (improvement)
3111.01, // Epoch 4: BEST + PERIODIC (improvement + checkpoint_frequency)
1336.24, // Epoch 5: BEST (improvement)
34611.01, // Epoch 6: PERIODIC only (spike, no improvement)
1749.28, // Epoch 7: no save (higher than epoch 5)
26107.40, // Epoch 8: PERIODIC only (spike, no improvement)
1938.18, // Epoch 9: no save (higher than epoch 5)
865.29, // Epoch 10: BEST + PERIODIC (improvement + checkpoint_frequency)
];
let mut tracker = MockCheckpointTracker::new();
for (idx, &loss) in v12_losses.iter().enumerate() {
tracker.process_epoch(idx + 1, loss, 2); // checkpoint_frequency = 2
}
let (total, best, periodic) = tracker.get_checkpoint_summary();
tracker.print_checkpoint_log();
println!("\nV12 Results:");
println!(" Total checkpoints: {}", total);
println!(" BEST checkpoints: {}", best);
println!(" PERIODIC checkpoints: {}", periodic);
// Expected: 6 BEST + 5 PERIODIC = 11 total (matches actual V12 logs)
assert_eq!(
best, 6,
"V12 should have 6 BEST checkpoints (epochs 1,2,3,4,5,10)"
);
assert_eq!(
periodic, 5,
"V12 should have 5 PERIODIC checkpoints (epochs 2,4,6,8,10)"
);
assert_eq!(total, 11, "V12 should have 11 total checkpoints");
}
#[test]
fn test_v13_volatile_loss_pattern() {
// V13 Pattern: Volatile validation loss with fewer improvements (4 improvements)
// Expected: 4 BEST checkpoints + 5 PERIODIC = 9 total
let v13_losses = vec![
24713.61, // Epoch 1: BEST (first epoch always saves)
14304.86, // Epoch 2: BEST + PERIODIC (improvement + checkpoint_frequency)
25258.00, // Epoch 3: no save (spike, worse than epoch 2)
5553.62, // Epoch 4: BEST + PERIODIC (improvement + checkpoint_frequency)
30351.40, // Epoch 5: no save (spike, worse than epoch 4)
967.53, // Epoch 6: BEST + PERIODIC (improvement + checkpoint_frequency)
15262.83, // Epoch 7: no save (spike, worse than epoch 6)
8294.97, // Epoch 8: PERIODIC only (spike, no improvement)
1308.29, // Epoch 9: no save (worse than epoch 6)
8265.35, // Epoch 10: PERIODIC only (spike, no improvement)
];
let mut tracker = MockCheckpointTracker::new();
for (idx, &loss) in v13_losses.iter().enumerate() {
tracker.process_epoch(idx + 1, loss, 2); // checkpoint_frequency = 2
}
let (total, best, periodic) = tracker.get_checkpoint_summary();
tracker.print_checkpoint_log();
println!("\nV13 Results:");
println!(" Total checkpoints: {}", total);
println!(" BEST checkpoints: {}", best);
println!(" PERIODIC checkpoints: {}", periodic);
// Expected: 4 BEST + 5 PERIODIC = 9 total (matches actual V13 logs)
assert_eq!(
best, 4,
"V13 should have 4 BEST checkpoints (epochs 1,2,4,6)"
);
assert_eq!(
periodic, 5,
"V13 should have 5 PERIODIC checkpoints (epochs 2,4,6,8,10)"
);
assert_eq!(total, 9, "V13 should have 9 total checkpoints");
}
#[test]
fn test_extreme_volatility_no_improvement() {
// Edge case: High volatility with NO improvements after epoch 1
// Expected: 1 BEST (epoch 1 only) + 5 PERIODIC = 6 total
let extreme_losses = vec![
1000.0, // Epoch 1: BEST (first epoch)
5000.0, // Epoch 2: PERIODIC only (worse than epoch 1)
8000.0, // Epoch 3: no save
3000.0, // Epoch 4: PERIODIC only (worse than epoch 1)
9000.0, // Epoch 5: no save
4000.0, // Epoch 6: PERIODIC only (worse than epoch 1)
7000.0, // Epoch 7: no save
2000.0, // Epoch 8: PERIODIC only (worse than epoch 1)
6000.0, // Epoch 9: no save
1500.0, // Epoch 10: PERIODIC only (worse than epoch 1)
];
let mut tracker = MockCheckpointTracker::new();
for (idx, &loss) in extreme_losses.iter().enumerate() {
tracker.process_epoch(idx + 1, loss, 2);
}
let (total, best, periodic) = tracker.get_checkpoint_summary();
tracker.print_checkpoint_log();
println!("\nExtreme Volatility Results:");
println!(" Total checkpoints: {}", total);
println!(" BEST checkpoints: {}", best);
println!(" PERIODIC checkpoints: {}", periodic);
// Expected: 1 BEST + 5 PERIODIC = 6 total
assert_eq!(best, 1, "Should only have 1 BEST checkpoint (epoch 1)");
assert_eq!(
periodic, 5,
"Should have 5 PERIODIC checkpoints (epochs 2,4,6,8,10)"
);
assert_eq!(total, 6, "Should have 6 total checkpoints");
}
#[test]
fn test_all_improving_loss() {
// Edge case: Every epoch improves (monotonic decrease)
// Expected: 10 BEST + 5 PERIODIC = 15 total (but some overlap)
// Note: Epochs 2,4,6,8,10 will have BOTH best + periodic saves
let improving_losses = vec![
10000.0, // Epoch 1: BEST
9000.0, // Epoch 2: BEST + PERIODIC
8000.0, // Epoch 3: BEST
7000.0, // Epoch 4: BEST + PERIODIC
6000.0, // Epoch 5: BEST
5000.0, // Epoch 6: BEST + PERIODIC
4000.0, // Epoch 7: BEST
3000.0, // Epoch 8: BEST + PERIODIC
2000.0, // Epoch 9: BEST
1000.0, // Epoch 10: BEST + PERIODIC
];
let mut tracker = MockCheckpointTracker::new();
for (idx, &loss) in improving_losses.iter().enumerate() {
tracker.process_epoch(idx + 1, loss, 2);
}
let (total, best, periodic) = tracker.get_checkpoint_summary();
tracker.print_checkpoint_log();
println!("\nAll Improving Results:");
println!(" Total checkpoints: {}", total);
println!(" BEST checkpoints: {}", best);
println!(" PERIODIC checkpoints: {}", periodic);
// Expected: 10 BEST + 5 PERIODIC = 15 total
assert_eq!(
best, 10,
"Should have 10 BEST checkpoints (all epochs improve)"
);
assert_eq!(
periodic, 5,
"Should have 5 PERIODIC checkpoints (epochs 2,4,6,8,10)"
);
assert_eq!(total, 15, "Should have 15 total checkpoints");
}
#[test]
fn test_checkpoint_frequency_zero() {
// Edge case: checkpoint_frequency = 0 (disable periodic saves)
// Expected: Only BEST checkpoints saved
let losses = vec![
10000.0, 9000.0, 8000.0, 7000.0, 6000.0, 5000.0, 4000.0, 3000.0, 2000.0, 1000.0,
];
let mut tracker = MockCheckpointTracker::new();
for (idx, &loss) in losses.iter().enumerate() {
tracker.process_epoch(idx + 1, loss, 0); // checkpoint_frequency = 0
}
let (total, best, periodic) = tracker.get_checkpoint_summary();
tracker.print_checkpoint_log();
println!("\nCheckpoint Frequency 0 Results:");
println!(" Total checkpoints: {}", total);
println!(" BEST checkpoints: {}", best);
println!(" PERIODIC checkpoints: {}", periodic);
// Expected: 10 BEST + 0 PERIODIC = 10 total
assert_eq!(
best, 10,
"Should have 10 BEST checkpoints (all epochs improve)"
);
assert_eq!(
periodic, 0,
"Should have 0 PERIODIC checkpoints (frequency=0)"
);
assert_eq!(total, 10, "Should have 10 total checkpoints");
}