Best-checkpoint save (val Sharpe improvement, ~30% of epochs in convergent runs) blocked the epoch loop for 20-40s on each improvement: serialize_model() chained N (~26) per-tensor DtoH downloads via GpuTensor::to_host → memcpy_dtoh, each forcing an implicit stream sync on a busy training stream. The DtoH chain also violates feedback_no_htod_htoh_only_mapped_pinned.md (only cuMemHostAlloc DEVICEMAP allowed for CPU↔GPU paths). Plan B: - Introduce snapshot_model_to_pinned (mod.rs): allocate one MappedF32Buffer sized for all named weight slices concatenated, cuMemcpyDtoDAsync each slice into the buffer's device pointer (which aliases the host page), single stream sync, copy bytes out to a Send + 'static Vec<u8>. One sync per snapshot, replaces N. - serialize_snapshot_bytes (mod.rs): pure-CPU safetensors construction from CheckpointSnapshot. Static — callable without &self, so the worker can move the snapshot across thread boundary. - handle_epoch_checkpoints_and_early_stopping on val-Sharpe improvement: save_best_gpu_params (DtoD, fast) + snapshot to pinned + tokio::task::spawn_blocking the safetensors construction + checkpoint_callback invocation. JoinHandle parked on pending_checkpoint_handles. Training loop continues immediately. - await_pending_checkpoint_handles drains in-flight workers at training end (success branch + early-stop branches) and before any synchronous cold-path checkpoint write to keep disk ordering deterministic. - F bound on train / train_walk_forward / train_fold_from_slices gains + 'static so the callback can be moved into the worker. All public callers already use 'static-compatible move closures (test fixtures with shared mutable state migrate to Arc<Mutex<T>>). Internal pipeline uses CheckpointCallbackHandle = Arc<std::sync::Mutex<Box<dyn FnMut + Send + 'static>>> so the same callback flows through multi-fold walk-forward into every fold's worker. - serialize_model itself rewritten via the snapshot path: the no-DtoH rule now holds across ALL checkpoint paths (best, periodic, early-stop, plateau-exhausted). The pre-existing GpuTensor::to_host path is no longer reachable from the DQN trainer. The audit's spec called for an mpsc channel(1) drop-old worker, but the multi-fold + &mut F pre-existing API made the simpler fire-and-forget spawn_blocking pattern a cleaner fit (Mutex serialises any concurrent invocations; Vec<JoinHandle> drain at end guarantees disk writes complete before the trainer returns). Same overlap benefit (training rolls while serialize+disk run on a blocking thread); upper bound on in-flight work is one-per-improved- epoch which approximates the spec's depth=1 in realistic training runs. Per feedback_no_partial_refactor: every site that constructs a checkpoint payload migrated in lockstep — best-improvement uses the worker; periodic / plateau-exhausted / early-stop call the shared Arc<Mutex<F>> handle inline. All paths read params via snapshot_model_to_pinned, so the no-DtoH rule applies uniformly. Test fixtures (8 .rs files) updated for the + 'static bound (move closures + cloned PathBufs / Arc<Mutex<T>> for shared mutable state). Verified: SQLX_OFFLINE=true cargo check --workspace --tests clean (warnings unchanged from baseline). cargo test -p ml --lib --no-run clean. No fingerprint change. Wire-up audit entry extended with Plan B file:line edit sites (rides under the same Async-validation overlap section started by the companion Plan A commit). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
434 lines
14 KiB
Rust
434 lines
14 KiB
Rust
#![allow(
|
|
clippy::assertions_on_constants,
|
|
clippy::assertions_on_result_states,
|
|
clippy::clone_on_copy,
|
|
clippy::decimal_literal_representation,
|
|
clippy::doc_markdown,
|
|
clippy::empty_line_after_doc_comments,
|
|
clippy::field_reassign_with_default,
|
|
clippy::get_unwrap,
|
|
clippy::identity_op,
|
|
clippy::inconsistent_digit_grouping,
|
|
clippy::indexing_slicing,
|
|
clippy::integer_division,
|
|
clippy::len_zero,
|
|
clippy::let_underscore_must_use,
|
|
clippy::manual_div_ceil,
|
|
clippy::manual_let_else,
|
|
clippy::manual_range_contains,
|
|
clippy::modulo_arithmetic,
|
|
clippy::needless_range_loop,
|
|
clippy::non_ascii_literal,
|
|
clippy::redundant_clone,
|
|
clippy::shadow_reuse,
|
|
clippy::shadow_same,
|
|
clippy::shadow_unrelated,
|
|
clippy::single_match_else,
|
|
clippy::str_to_string,
|
|
clippy::string_slice,
|
|
clippy::tests_outside_test_module,
|
|
clippy::too_many_lines,
|
|
clippy::unnecessary_wraps,
|
|
clippy::unseparated_literal_suffix,
|
|
clippy::use_debug,
|
|
clippy::useless_vec,
|
|
clippy::wildcard_enum_match_arm,
|
|
clippy::else_if_without_else,
|
|
clippy::expect_used,
|
|
clippy::missing_const_for_fn,
|
|
clippy::similar_names,
|
|
clippy::type_complexity,
|
|
clippy::collapsible_else_if,
|
|
clippy::doc_lazy_continuation,
|
|
clippy::items_after_test_module,
|
|
clippy::map_clone,
|
|
clippy::multiple_unsafe_ops_per_block,
|
|
clippy::unwrap_or_default,
|
|
clippy::assign_op_pattern,
|
|
clippy::needless_borrow,
|
|
clippy::println_empty_string,
|
|
clippy::unnecessary_cast,
|
|
clippy::used_underscore_binding,
|
|
clippy::create_dir,
|
|
clippy::implicit_saturating_sub,
|
|
clippy::exit,
|
|
clippy::expect_fun_call,
|
|
clippy::too_many_arguments,
|
|
clippy::unnecessary_map_or,
|
|
clippy::unwrap_used,
|
|
dead_code,
|
|
unused_imports,
|
|
unused_variables,
|
|
clippy::cloned_ref_to_slice_refs,
|
|
clippy::neg_multiply,
|
|
clippy::while_let_loop,
|
|
clippy::bool_assert_comparison,
|
|
clippy::excessive_precision,
|
|
clippy::trivially_copy_pass_by_ref,
|
|
clippy::op_ref,
|
|
clippy::redundant_closure,
|
|
clippy::unnecessary_lazy_evaluations,
|
|
clippy::if_then_some_else_none,
|
|
clippy::unnecessary_to_owned,
|
|
clippy::single_component_path_imports,
|
|
)]
|
|
//! Production Training Integration Smoke Test
|
|
//!
|
|
//! Validates that all production training pipeline components added in the
|
|
//! production-training worktree work together correctly:
|
|
//!
|
|
//! 2. 26D parameter space with round-trip preservation
|
|
//! 3. EarlyStoppingConfig with SuccessiveHalving
|
|
//! 4. EarlyStoppingConfig with Hyperband (rung-based pruning)
|
|
//! 5. ObjectiveMode equality comparison
|
|
//! 6. QR-DQN training on real data (graceful skip if unavailable)
|
|
|
|
#![allow(unused_crate_dependencies)]
|
|
|
|
use anyhow::{Context, Result};
|
|
use ml::hyperopt::adapters::dqn::{DQNParams, ObjectiveMode};
|
|
use ml::hyperopt::early_stopping::{
|
|
EarlyStoppingConfig, EarlyStoppingObserver, EarlyStoppingStrategy, EpochMetrics,
|
|
ObserverDecision, TrialObserver,
|
|
};
|
|
use ml::hyperopt::traits::ParameterSpace;
|
|
use tracing::{info, warn};
|
|
|
|
/// Test 1: Verify DQNParams::default() has correct 14D defaults
|
|
/// iqn_lambda > 0 means IQN is enabled; num_quantiles and cql_alpha are now
|
|
/// fixed in DQNHyperparameters (not in DQNParams).
|
|
#[test]
|
|
fn test_qr_dqn_defaults() -> Result<()> {
|
|
let params = DQNParams::default();
|
|
|
|
// IQN is always enabled by default (iqn_lambda > 0)
|
|
assert!(params.iqn_lambda > 0.0, "IQN should be enabled by default (iqn_lambda > 0)");
|
|
assert!(
|
|
(params.gamma - 0.99).abs() < 1e-6,
|
|
"DQNParams::default() should have gamma: 0.99, got {}",
|
|
params.gamma
|
|
);
|
|
assert!(
|
|
(params.learning_intensity - 1.0).abs() < 1e-6,
|
|
"DQNParams::default() should have learning_intensity: 1.0, got {}",
|
|
params.learning_intensity
|
|
);
|
|
|
|
info!(
|
|
gamma = params.gamma,
|
|
iqn_lambda = params.iqn_lambda,
|
|
learning_intensity = params.learning_intensity,
|
|
"Test 1 PASSED: DQN 14D defaults verified"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Test 2: Verify 14D parameter space and round-trip preservation
|
|
#[test]
|
|
fn test_14d_parameter_space_round_trip() -> Result<()> {
|
|
// Verify dimensionality (14D search space: 3 breakouts + 5 core + 6 gen families)
|
|
let bounds = DQNParams::continuous_bounds();
|
|
assert_eq!(
|
|
bounds.len(),
|
|
14,
|
|
"DQNParams::continuous_bounds() should return 14 dimensions, got {}",
|
|
bounds.len()
|
|
);
|
|
|
|
// Verify all bounds are valid (min < max)
|
|
for (i, (lo, hi)) in bounds.iter().enumerate() {
|
|
assert!(
|
|
lo < hi,
|
|
"Bound {} has invalid range: [{}, {}]",
|
|
i, lo, hi
|
|
);
|
|
}
|
|
|
|
// Round-trip: default -> continuous -> from_continuous
|
|
let original = DQNParams::default();
|
|
let continuous = original.to_continuous();
|
|
assert_eq!(
|
|
continuous.len(),
|
|
14,
|
|
"to_continuous() should return 14 values, got {}",
|
|
continuous.len()
|
|
);
|
|
|
|
let reconstructed = DQNParams::from_continuous(&continuous)
|
|
.map_err(|e| anyhow::anyhow!("from_continuous failed: {}", e))?;
|
|
|
|
// Verify gamma survives round trip
|
|
let gamma_diff = (reconstructed.gamma - original.gamma).abs();
|
|
assert!(
|
|
gamma_diff < 0.01,
|
|
"Round-trip should preserve gamma: expected {}, got {} (diff={})",
|
|
original.gamma, reconstructed.gamma, gamma_diff
|
|
);
|
|
|
|
// Verify learning_intensity survives the round trip
|
|
let li_diff = (reconstructed.learning_intensity - original.learning_intensity).abs();
|
|
assert!(
|
|
li_diff < 1e-8,
|
|
"Round-trip should preserve learning_intensity: expected {}, got {}",
|
|
original.learning_intensity, reconstructed.learning_intensity
|
|
);
|
|
|
|
info!(
|
|
dimensions = bounds.len(),
|
|
gamma = reconstructed.gamma,
|
|
"Test 2 PASSED: 14D parameter space round-trip verified"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Test 3: EarlyStoppingConfig with SuccessiveHalving
|
|
///
|
|
/// Verifies that EarlyStoppingObserver with SHA strategy can be constructed
|
|
/// and returns Continue for a good initial trial (no history to prune against).
|
|
#[test]
|
|
fn test_early_stopping_successive_halving() -> Result<()> {
|
|
let config = EarlyStoppingConfig {
|
|
patience_epochs: 10,
|
|
min_delta: 1e-4,
|
|
min_epochs: 0, // Allow early decisions for testing
|
|
strategy: EarlyStoppingStrategy::SuccessiveHalving {
|
|
reduction_factor: 3,
|
|
},
|
|
..Default::default()
|
|
};
|
|
|
|
let mut observer = EarlyStoppingObserver::new(config);
|
|
|
|
// Start a trial with no prior history -- should not prune
|
|
observer.on_trial_start(0, "sha_test_trial_0");
|
|
let metrics = EpochMetrics {
|
|
epoch: 1,
|
|
train_loss: 0.3,
|
|
val_loss: 0.3,
|
|
timestamp: 1.0,
|
|
};
|
|
let decision = observer.on_epoch_complete(0, 1, &metrics);
|
|
assert_eq!(
|
|
decision,
|
|
ObserverDecision::Continue,
|
|
"SHA should not prune first trial with no history (got {:?})",
|
|
decision
|
|
);
|
|
|
|
// Complete a few trials to build history, then verify pruning works
|
|
for trial in 0..6 {
|
|
observer.on_trial_start(trial, &format!("sha_trial_{}", trial));
|
|
let loss = (trial as f64 + 1.0) * 0.1; // 0.1, 0.2, ..., 0.6
|
|
observer.on_trial_complete(trial, loss);
|
|
}
|
|
|
|
// New trial with bad loss should be pruned (6 trials, keep top 1/3 = 2, threshold ~ 0.2)
|
|
observer.on_trial_start(7, "sha_bad_trial");
|
|
let bad_metrics = EpochMetrics {
|
|
epoch: 1,
|
|
train_loss: 0.9,
|
|
val_loss: 0.9,
|
|
timestamp: 2.0,
|
|
};
|
|
let bad_decision = observer.on_epoch_complete(7, 1, &bad_metrics);
|
|
assert_eq!(
|
|
bad_decision,
|
|
ObserverDecision::StopTrial,
|
|
"SHA should prune trial with val_loss=0.9 when threshold is ~0.2"
|
|
);
|
|
|
|
info!("Test 3 PASSED: SuccessiveHalving early stopping verified (good trial: Continue, bad trial: StopTrial)");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Test 4: EarlyStoppingConfig with Hyperband
|
|
///
|
|
/// Verifies that Hyperband does NOT prune between rungs (e.g. epoch 5)
|
|
/// but DOES prune at rung epochs (e.g. epoch 27) for a bad trial.
|
|
#[test]
|
|
fn test_early_stopping_hyperband_rung_behavior() -> Result<()> {
|
|
// max_resource=81, reduction_factor=3
|
|
// Rung epochs: 81/3=27, 81/9=9, 81/27=3, 81/81=1
|
|
let config = EarlyStoppingConfig {
|
|
patience_epochs: 100, // High patience so only Hyperband logic matters
|
|
min_delta: 1e-4,
|
|
min_epochs: 0, // Allow early decisions
|
|
strategy: EarlyStoppingStrategy::Hyperband {
|
|
max_resource: 81,
|
|
reduction_factor: 3,
|
|
},
|
|
..Default::default()
|
|
};
|
|
|
|
let mut observer = EarlyStoppingObserver::new(config);
|
|
|
|
// Seed with completed trials so pruning has history
|
|
for trial in 0..9 {
|
|
observer.on_trial_start(trial, &format!("hb_trial_{}", trial));
|
|
observer.on_trial_complete(trial, (trial as f64 + 1.0) * 0.1);
|
|
}
|
|
|
|
// Test: epoch 5 is NOT a rung -- should NOT prune even with bad loss
|
|
observer.on_trial_start(10, "hb_non_rung_trial");
|
|
let non_rung_metrics = EpochMetrics {
|
|
epoch: 5,
|
|
train_loss: 0.95,
|
|
val_loss: 0.95,
|
|
timestamp: 5.0,
|
|
};
|
|
let non_rung_decision = observer.on_epoch_complete(10, 5, &non_rung_metrics);
|
|
assert_eq!(
|
|
non_rung_decision,
|
|
ObserverDecision::Continue,
|
|
"Hyperband should NOT prune at non-rung epoch 5 (got {:?})",
|
|
non_rung_decision
|
|
);
|
|
|
|
// Test: epoch 27 IS a rung (81/3=27) -- should prune bad trial
|
|
observer.on_trial_start(11, "hb_rung_trial");
|
|
let rung_metrics = EpochMetrics {
|
|
epoch: 27,
|
|
train_loss: 0.95,
|
|
val_loss: 0.95,
|
|
timestamp: 27.0,
|
|
};
|
|
let rung_decision = observer.on_epoch_complete(11, 27, &rung_metrics);
|
|
assert_eq!(
|
|
rung_decision,
|
|
ObserverDecision::StopTrial,
|
|
"Hyperband should prune bad trial at rung epoch 27 (got {:?})",
|
|
rung_decision
|
|
);
|
|
|
|
info!("Test 4 PASSED: Hyperband rung-based pruning verified (non-rung epoch 5: Continue, rung epoch 27: StopTrial)");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Test 5: ObjectiveMode equality comparison
|
|
#[test]
|
|
fn test_objective_mode_equality() -> Result<()> {
|
|
let mode_a = ObjectiveMode::EpisodeReward;
|
|
let mode_b = ObjectiveMode::Sharpe;
|
|
let mode_c = ObjectiveMode::EpisodeReward;
|
|
|
|
// Same variant should be equal
|
|
assert_eq!(
|
|
mode_a, mode_c,
|
|
"EpisodeReward should equal EpisodeReward"
|
|
);
|
|
|
|
// Different variants should not be equal
|
|
assert_ne!(
|
|
mode_a, mode_b,
|
|
"EpisodeReward should not equal Sharpe"
|
|
);
|
|
|
|
// Default should be Sharpe
|
|
let default_mode = ObjectiveMode::default();
|
|
assert_eq!(
|
|
default_mode, mode_b,
|
|
"ObjectiveMode::default() should be Sharpe"
|
|
);
|
|
|
|
info!("Test 5 PASSED: ObjectiveMode PartialEq verified");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Test 6: QR-DQN training on real data
|
|
///
|
|
/// Gracefully skips if test data is not available.
|
|
#[tokio::test]
|
|
async fn test_qr_dqn_training_real_data() -> Result<()> {
|
|
use ml::trainers::dqn::{DQNHyperparameters, DQNTrainer};
|
|
use std::path::PathBuf;
|
|
|
|
// Locate test data (same pattern as dqn_training_smoke_test.rs)
|
|
let workspace_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
.ancestors()
|
|
.find(|p| p.join("test_data").exists())
|
|
.context("Failed to find workspace root with test_data/")?
|
|
.to_path_buf();
|
|
let data_dir = workspace_root.join("test_data/real/databento");
|
|
|
|
if !data_dir.exists() {
|
|
warn!(path = %data_dir.display(), "Skipping test: data not found");
|
|
return Ok(());
|
|
}
|
|
let data_dir_str = data_dir.to_string_lossy().to_string();
|
|
|
|
// Configure hyperparameters with QR-DQN enabled
|
|
let mut hyperparams = DQNHyperparameters::conservative();
|
|
hyperparams.epochs = 5;
|
|
hyperparams.batch_size = 64;
|
|
hyperparams.learning_rate = 0.0001;
|
|
hyperparams.epsilon_start = 1.0;
|
|
hyperparams.epsilon_end = 0.05;
|
|
hyperparams.epsilon_decay = 0.90;
|
|
hyperparams.early_stopping_enabled = false; // No early stopping for 5 epochs
|
|
hyperparams.num_quantiles = 32;
|
|
hyperparams.qr_kappa = 1.0;
|
|
|
|
// Train
|
|
let checkpoint_dir = tempfile::tempdir()?;
|
|
let mut trainer = DQNTrainer::new(hyperparams)?;
|
|
|
|
// Take an owned PathBuf so the closure satisfies the `+ 'static` bound
|
|
// required by the async-checkpoint worker (`tokio::task::spawn_blocking`).
|
|
// The TempDir handle stays alive in the outer scope to keep the
|
|
// directory; we only need the path for callbacks.
|
|
let ckpt_dir_path = checkpoint_dir.path().to_path_buf();
|
|
let metrics = trainer
|
|
.train(&data_dir_str, "ES.FUT", move |epoch, checkpoint_data, is_best| {
|
|
let name = if is_best {
|
|
"qrdqn_best.safetensors".to_string()
|
|
} else {
|
|
format!("qrdqn_epoch_{}.safetensors", epoch)
|
|
};
|
|
let path = ckpt_dir_path.join(&name);
|
|
std::fs::write(&path, &checkpoint_data)?;
|
|
Ok(path.to_string_lossy().to_string())
|
|
})
|
|
.await?;
|
|
|
|
// Verify epochs completed
|
|
assert!(
|
|
metrics.epochs_trained >= 1,
|
|
"Should complete at least 1 epoch, got {}",
|
|
metrics.epochs_trained
|
|
);
|
|
|
|
// Verify losses are finite and non-zero
|
|
let loss_history = trainer.loss_history();
|
|
assert!(
|
|
!loss_history.is_empty(),
|
|
"Loss history should not be empty after training"
|
|
);
|
|
|
|
for (i, loss) in loss_history.iter().enumerate() {
|
|
assert!(
|
|
loss.is_finite(),
|
|
"Loss at epoch {} should be finite, got {}",
|
|
i, loss
|
|
);
|
|
assert!(
|
|
*loss > 0.0,
|
|
"Loss at epoch {} should be non-zero, got {}",
|
|
i, loss
|
|
);
|
|
}
|
|
|
|
info!(
|
|
epochs_trained = metrics.epochs_trained,
|
|
loss_history = ?loss_history,
|
|
training_time_secs = metrics.training_time_seconds,
|
|
"Test 6 PASSED: QR-DQN training on real data verified"
|
|
);
|
|
|
|
Ok(())
|
|
}
|