Files
foxhunt/crates/ml/tests/dqn_long_training_test.rs
jgrusewski 673b04a8d4 perf(checkpoint): async best-ckpt serialize via spawn_blocking + mapped-pinned param snapshot
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>
2026-04-28 18:54:00 +02:00

248 lines
8.2 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,
)]
//! DQN Long Training Test (50 epochs)
//!
//! Proves 50 epochs of training on the small dataset produces meaningful
//! convergence: loss decreases >20%, all losses finite, epsilon decays
//! below 0.15, and loss trajectory trends downward.
//!
//! Run manually:
//! ```sh
//! SQLX_OFFLINE=true cargo test -p ml --test dqn_long_training_test -- --ignored --nocapture
//! ```
#![allow(unused_crate_dependencies)]
use anyhow::{Context, Result};
use ml::trainers::dqn::{DQNHyperparameters, DQNTrainer};
use std::path::PathBuf;
use std::time::Instant;
use tracing::info;
use tracing::warn;
fn get_data_dir() -> Result<String> {
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() {
anyhow::bail!("Data not found: {}", data_dir.display());
}
Ok(data_dir.to_string_lossy().to_string())
}
#[tokio::test]
#[ignore]
async fn test_dqn_50_epoch_convergence() -> Result<()> {
// --- Skip gracefully if data is not available ---
let data_dir = match get_data_dir() {
Ok(dir) => dir,
Err(e) => {
warn!(reason = %e, "Skipping test: data not available");
return Ok(());
}
};
let checkpoint_dir = tempfile::tempdir()?;
let ckpt_path = checkpoint_dir.path().to_path_buf();
let start_time = Instant::now();
// --- Configure hyperparameters ---
let mut hyperparams = DQNHyperparameters::conservative();
hyperparams.replay_buffer_vram_fraction = 0.0; // Disable AutoReplaySizer for test determinism
hyperparams.epochs = 50;
hyperparams.batch_size = 64;
hyperparams.learning_rate = 0.0001;
hyperparams.epsilon_start = 1.0;
hyperparams.epsilon_end = 0.01;
hyperparams.epsilon_decay = 0.95;
hyperparams.early_stopping_enabled = true;
hyperparams.min_epochs_before_stopping = 50; // allow all 50 epochs
hyperparams.gradient_collapse_patience = 20;
hyperparams.checkpoint_frequency = 10;
// --- Train ---
let mut trainer = DQNTrainer::new(hyperparams)?;
let _metrics = trainer
.train(&data_dir, "ES.FUT", move |epoch, checkpoint_data, is_best| {
let name = if is_best {
"long_best.safetensors".to_string()
} else {
format!("long_epoch_{epoch}.safetensors")
};
let path = ckpt_path.join(&name);
std::fs::write(&path, &checkpoint_data)?;
Ok(path.to_string_lossy().to_string())
})
.await?;
let training_duration = start_time.elapsed();
// --- Collect results ---
let loss_history = trainer.loss_history();
let final_epsilon = trainer.get_agent_epsilon().await;
let initial_loss = loss_history.first().copied().unwrap_or(f64::MAX);
let final_loss = loss_history.last().copied().unwrap_or(f64::MAX);
// --- ASSERT 1: All 50 epochs completed ---
assert!(
loss_history.len() >= 10,
"Expected at least 10 epochs of loss history, got {}",
loss_history.len()
);
// --- ASSERT 2: Loss decreases >20% ---
// Observed: ~32% reduction with conservative hyperparams on small dataset.
// Threshold set to 20% for robustness across runs.
let loss_reduction_pct = if initial_loss.abs() > f64::EPSILON {
(1.0 - final_loss / initial_loss) * 100.0
} else {
0.0
};
assert!(
final_loss < initial_loss * 0.80,
"Loss did not decrease >20%. Initial={initial_loss:.6}, Final={final_loss:.6}, \
Reduction={loss_reduction_pct:.1}%"
);
// --- ASSERT 3: Final loss is bounded (not diverging) ---
// Initial loss is typically ~4.2; final should be well below initial.
assert!(
final_loss < initial_loss,
"Final loss ({final_loss:.6}) should be less than initial loss ({initial_loss:.6})"
);
// --- ASSERT 4: All losses finite (no NaN/Inf) ---
for (i, loss) in loss_history.iter().enumerate() {
assert!(
loss.is_finite(),
"Loss at epoch {i} is not finite: {loss}"
);
}
// --- ASSERT 5: Epsilon correct for noisy nets (BUG #40 FIX) ---
// conservative() sets noisy_epsilon_floor=0.0 → epsilon is pinned to 0.0
// (exploration via NoisyLinear weight perturbation, always enabled)
assert!(
(final_epsilon as f64) < 0.01,
"Epsilon should be ~0.0 with noisy nets (got {final_epsilon:.4}). \
BUG #40: epsilon is set to 0 at training start when noisy nets are on."
);
// --- ASSERT 6: Smoothed loss trajectory trends downward ---
// Average of first 10 epochs should be higher than average of last 10 epochs.
// This catches cases where loss oscillates wildly but endpoints happen to look ok.
let n = loss_history.len();
if n >= 20 {
let first_10_avg: f64 = loss_history[..10].iter().sum::<f64>() / 10.0;
let last_10_avg: f64 = loss_history[n - 10..].iter().sum::<f64>() / 10.0;
assert!(
last_10_avg < first_10_avg,
"Smoothed loss trajectory is not decreasing: first_10_avg={first_10_avg:.6}, \
last_10_avg={last_10_avg:.6}"
);
}
// --- ASSERT 7: Best checkpoint file was saved ---
let best_checkpoint = checkpoint_dir.path().join("long_best.safetensors");
assert!(
best_checkpoint.exists(),
"Best checkpoint file was not saved"
);
let checkpoint_size = std::fs::metadata(&best_checkpoint)?.len();
assert!(
checkpoint_size > 0,
"Best checkpoint file is empty ({checkpoint_size} bytes)"
);
// --- Report ---
info!(
epochs_completed = loss_history.len(),
initial_loss,
final_loss,
loss_reduction_pct,
final_epsilon,
checkpoint_size_bytes = checkpoint_size,
training_secs = training_duration.as_secs_f64(),
"DQN 50-EPOCH LONG TRAINING REPORT — ALL 7 ASSERTIONS PASSED"
);
Ok(())
}