Files
foxhunt/crates/ml/tests/ppo_lstm_training_loop_tests.rs
jgrusewski a5c3d73d9e cleanup: declarative rewrites for ml-tests and trading-engine TODOs
- ml/tests/dqn_training_pipeline_test.rs: the inline TODO speculated
  about a future `load_checkpoint` hook; loader round-trip coverage
  already lives in dqn_checkpoint_tests. Reword to point there.
- ml/tests/ppo_lstm_training_loop_tests.rs: the assertion on
  `hidden_state_manager.is_some()` is the public-surface proxy for
  "LSTM path active"; deeper introspection isn't exposed. Say so.
- ml/tests/ppo_recurrent_integration_tests.rs: the test is already
  `#[ignore]`d; rewrite the inline TODO as a description of the
  missing `from_varbuilder` constructors on LSTMPolicyNetwork /
  LSTMValueNetwork.
- risk/risk_engine.rs: VarEngine receives a default asset-class
  config because the schema-to-config conversion is not wired.
  Reword the TODO to describe that plainly.
- trading_engine/types/errors.rs: `common::ConversionError` does
  not exist; keep ConversionError local and drop the aspirational
  re-export comment.
- trading_engine/tests/audit_persistence_tests.rs: describe why
  the query assertion only checks the Ok shape (row-to-event
  mapping not wired) rather than pointing at a nonexistent line
  number.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-23 08:44:05 +02:00

261 lines
7.7 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,
)]
//! Integration tests for PPO LSTM training loop
//!
//! Tests verify that:
//! 1. Training works with LSTM enabled (use_lstm=true)
//! 2. Training works with standard MLP (use_lstm=false) - backward compatibility
//! 3. Hidden state management is properly integrated
//! 4. Networks are correctly initialized based on config
use ml::ppo::{PPOConfig, PPO};
use ml::ppo::trajectories::{Trajectory, TrajectoryBatch, TrajectoryStep};
use ml_core::common::action::{ExposureLevel, FactoredAction, OrderType, Urgency};
use tracing::info;
/// Create a small dummy trajectory batch for testing
fn create_dummy_trajectory_batch(num_steps: usize, state_dim: usize) -> TrajectoryBatch {
let mut trajectory = Trajectory::new();
for i in 0..num_steps {
let state = vec![0.1 * i as f32; state_dim];
let reward = if i % 2 == 0 { 1.0 } else { -0.5 };
trajectory.add_step(TrajectoryStep {
state,
action: FactoredAction::new(ExposureLevel::Flat, OrderType::Market, Urgency::Normal),
log_prob: -1.5,
value: 0.5,
reward,
done: i == num_steps - 1,
});
}
// Create dummy advantages and returns (same length as num_steps)
let advantages = vec![0.1; num_steps];
let returns = vec![0.5; num_steps];
TrajectoryBatch::from_trajectories(vec![trajectory], advantages, returns)
}
#[test]
fn test_ppo_training_with_lstm_disabled() {
// Test backward compatibility: standard MLP networks should work
let config = PPOConfig {
state_dim: 32,
num_actions: 63,
policy_hidden_dims: vec![64, 32],
value_hidden_dims: vec![64, 32],
policy_learning_rate: 3e-4,
value_learning_rate: 1e-3,
batch_size: 64,
mini_batch_size: 32,
num_epochs: 2,
use_lstm: false, // Standard MLP mode
lstm_hidden_dim: 128,
lstm_num_layers: 1,
..PPOConfig::default()
};
let mut ppo = PPO::new(config.clone()).expect("Failed to create PPO");
// Verify LSTM is disabled
assert!(
ppo.hidden_state_manager.is_none(),
"Hidden state manager should be None when use_lstm=false"
);
// Create dummy trajectory batch
let mut batch = create_dummy_trajectory_batch(10, config.state_dim);
// Run single training update
let result = ppo.update(&mut batch);
assert!(
result.is_ok(),
"Training update failed with LSTM disabled: {:?}",
result.err()
);
let (policy_loss, value_loss) = result.unwrap();
info!(policy_loss, value_loss, "MLP mode");
// Verify losses are reasonable (not NaN or Inf)
assert!(
policy_loss.is_finite(),
"Policy loss should be finite, got: {}",
policy_loss
);
assert!(
value_loss.is_finite(),
"Value loss should be finite, got: {}",
value_loss
);
}
#[test]
fn test_ppo_training_with_lstm_enabled() {
// Test LSTM mode: LSTM networks should be used when enabled
// NOTE: LSTM integration now complete via enum-based architecture
let config = PPOConfig {
state_dim: 32,
num_actions: 63,
policy_hidden_dims: vec![64, 32],
value_hidden_dims: vec![64, 32],
policy_learning_rate: 3e-4,
value_learning_rate: 1e-3,
batch_size: 64,
mini_batch_size: 32,
num_epochs: 2,
use_lstm: true, // Enable LSTM
lstm_hidden_dim: 64,
lstm_num_layers: 2,
..PPOConfig::default()
};
let mut ppo = PPO::new(config.clone()).expect("Failed to create PPO");
// Verify LSTM is enabled
assert!(
ppo.hidden_state_manager.is_some(),
"Hidden state manager should be initialized when use_lstm=true"
);
// The `hidden_state_manager.is_some()` check above is the observable
// proxy for "LSTM path is active"; deeper introspection of the
// underlying network types is not surfaced through the public API.
// Create dummy trajectory batch
let mut batch = create_dummy_trajectory_batch(10, config.state_dim);
// Run single training update
let result = ppo.update(&mut batch);
assert!(
result.is_ok(),
"Training update failed with LSTM enabled: {:?}",
result.err()
);
let (policy_loss, value_loss) = result.unwrap();
info!(policy_loss, value_loss, "LSTM mode");
// Verify losses are reasonable (not NaN or Inf)
assert!(
policy_loss.is_finite(),
"Policy loss should be finite, got: {}",
policy_loss
);
assert!(
value_loss.is_finite(),
"Value loss should be finite, got: {}",
value_loss
);
}
#[test]
fn test_lstm_network_initialization() {
// Test that LSTM networks are correctly initialized based on config
let lstm_config = PPOConfig {
state_dim: 32,
num_actions: 63,
use_lstm: true,
lstm_hidden_dim: 128,
lstm_num_layers: 2,
..PPOConfig::default()
};
let mlp_config = PPOConfig {
state_dim: 32,
num_actions: 63,
use_lstm: false,
..PPOConfig::default()
};
// Create LSTM-based PPO
let lstm_ppo = PPO::new(lstm_config)
.expect("Failed to create LSTM PPO");
assert!(
lstm_ppo.hidden_state_manager.is_some(),
"LSTM PPO should have hidden state manager"
);
// Create MLP-based PPO
let mlp_ppo = PPO::new(mlp_config)
.expect("Failed to create MLP PPO");
assert!(
mlp_ppo.hidden_state_manager.is_none(),
"MLP PPO should NOT have hidden state manager"
);
}