Files
foxhunt/docs/codebase-cleanup/dqn-logging-summary.txt
jgrusewski 4080f73ba4 feat(ml): WAVE 30 - Implement optimized DQN logging module
Add comprehensive logging utilities for DQN training with best practices:

- LoggingConfig: Configurable log levels, intervals, and sampling rates
- MetricsAggregator: Windowed statistics (mean, std_dev) for training metrics
- SampledLogger: Rate-limited logging for high-frequency events

Key features:
- Structured logging with tracing crate (info/debug/trace hierarchy)
- 23 unit tests for full coverage
- Integration with existing DQN training pipeline

Bug fixes:
- Fix u8 overflow in prioritized_replay.rs test (500 > u8::MAX)
- Fix GradStore assertion in residual.rs (no is_empty method)
- Fix Tensor::get() Option/Result handling in quantile_regression.rs
- Fix Device PartialEq comparison in ensemble_network.rs

Documentation:
- Add Rust logging best practices guide for ML training
- Add DQN logging analysis and design summary

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 00:15:23 +01:00

193 lines
6.0 KiB
Plaintext

DQN Trainer Logging Analysis - Quick Summary
==============================================
MODULE: ml/src/trainers/dqn/
DATE: 2025-11-27
ANALYZER: Code Quality Analyzer
OVERVIEW
--------
- Logging Crate: tracing (consistent ✓)
- Total Statements: 169 (98 info!, 33 debug!, 32 warn!, 5 tracing::, 1 error!)
- File Size: 4,658 lines (trainer.rs)
- Log Volume: ~20,670 lines for 100-epoch run (2.47 MB with debug!)
KEY FINDINGS
------------
✓ GOOD PATTERNS:
- Consistent use of tracing crate
- Appropriate levels in early_stopping.rs
- Good checkpoint save/load logging
- Error context in some critical paths
✗ PROBLEMATIC PATTERNS:
1. High-frequency logs (6 info! per epoch = 600 lines for 100 epochs)
2. Excessive initialization verbosity (20+ info! logs at startup)
3. Per-step debug logs (20,000+ lines if debug! enabled)
4. Inconsistent error levels (training failures as warn! instead of error!)
5. Missing context in error logs (no epoch/step numbers)
6. No structured logging (all string interpolation)
CRITICAL ISSUES
---------------
Priority 1: High-Frequency Training Loop Logs
Location: Lines 2129-2333
Problem: 6 info! logs EVERY epoch (100+ epochs)
Impact: 600+ log lines, I/O bottleneck
Fix: Move to debug! OR log every 10th epoch only
Priority 2: Inconsistent Error Levels
Location: Line 2085
Problem: "Training step failed" logged as warn! (should be error!)
Impact: Critical failures not visible in error-only logs
Fix: Change to error!("Training step failed at epoch {}: {}", epoch, e)
Priority 3: Excessive Initialization Logs
Location: Lines 590-730
Problem: 20+ info! logs during setup
Impact: Startup log spam, obscures critical info
Fix: Consolidate to 3-line summary, move details to debug!
FILE BREAKDOWN
--------------
trainer.rs (4,658 lines) - 165 logging statements
- Initialization: 20+ info! (TOO MANY)
- Training loop: 6 info! per epoch (HIGH FREQUENCY)
- Per-step: 2 debug! per step (VERY HIGH FREQUENCY)
- Errors: 32 warn! (INCONSISTENT LEVELS)
- Checkpoints: 8 logs (GOOD)
early_stopping.rs (257 lines) - 3 logging statements
- All appropriate levels (GOOD)
- Context included (GOOD)
- Low frequency (GOOD)
lr_scheduler.rs (229 lines) - 0 logging statements (GOOD)
statistics.rs (135 lines) - 0 logging statements (GOOD)
config.rs (869 lines) - 0 logging statements (GOOD)
PERFORMANCE IMPACT
------------------
Current (100-epoch training):
- Total log lines: ~20,670
- File size: 2.47 MB (with debug!)
- Production (info only): 69 KB
After optimization:
- Total log lines: ~313 (98.5% reduction)
- File size: 37.8 KB (98.5% reduction)
- No performance degradation
TOP 10 LOG LOCATIONS BY FREQUENCY
----------------------------------
1. Line 2129 - REWARD_STATS (100 logs, EVERY EPOCH)
2. Line 2252 - Validation loss (100 logs, EVERY EPOCH)
3. Line 2271 - Action diversity (100 logs, EVERY EPOCH)
4. Line 2296 - Episode stats (100 logs, EVERY EPOCH)
5. Line 2306 - Exit breakdown (100 logs, EVERY EPOCH)
6. Line 2324 - Risk metrics VaR/CVaR (100 logs, EVERY EPOCH)
7. Line 1607 - Triple barrier start (20,000 logs, EVERY STEP)
8. Line 1640 - Barrier episode end (variable, PER BARRIER HIT)
9. Lines 590-730 - Initialization (20+ logs, STARTUP)
10. Lines 1374-1406 - Rainbow components (10+ logs, STARTUP)
SPECIFIC RECOMMENDATIONS
------------------------
IMMEDIATE (1-2 hours):
[X] Lines 590-730: info! → debug! (initialization logs)
[X] Line 2085: warn! → error! (training step failure)
[X] Lines 2129-2333: Add epoch % 10 == 0 condition
SHORT-TERM (4-6 hours):
[ ] Add epoch/step context to all error logs
[ ] Consolidate initialization to 3-line summary
[ ] Remove lines 2287-2288 (redundant recommendations)
MEDIUM-TERM (1-2 days):
[ ] Migrate to structured logging (tracing fields)
[ ] Add sampling to per-step logs (1/100 steps)
[ ] Configure log level via environment variable
LONG-TERM (1 week):
[ ] Add JSON log output support
[ ] Integrate with observability platform
[ ] Create log aggregation dashboard
CODE EXAMPLES
-------------
BEFORE (Line 2129):
info!("REWARD_STATS: epoch={}, mean={:.6}, ...", epoch + 1, ...);
AFTER:
if (epoch + 1) % 10 == 0 || epoch == 0 {
info!("REWARD_STATS: epoch={}, mean={:.6}, ...", epoch + 1, ...);
}
BEFORE (Line 2085):
warn!("Training step failed: {}, continuing...", e);
AFTER:
error!("Training step failed at epoch {}, step {}: {}",
epoch, step, e);
BEFORE (Lines 590-730, 20+ logs):
info!("Creating regime-conditional DQN with 3 heads...");
info!(" - Regime detection: ADX (index 211)...");
info!("Kelly optimizer enabled...");
// ... 17 more lines ...
AFTER (3 logs):
info!("DQN Trainer initialized with {} features", config.state_dim);
info!(" Architecture: {} ({})", arch_type, hidden_dims);
debug!("Advanced: Kelly={}, Entropy={}, Masking={}, Stress={}",
kelly_en, entropy_en, mask_en, stress_en);
TESTING VERIFICATION
--------------------
# Before optimization
RUST_LOG=debug cargo test dqn 2>&1 | wc -l
Expected: ~20,000 lines
# After optimization
RUST_LOG=debug cargo test dqn 2>&1 | wc -l
Expected: ~300 lines (98.5% reduction)
# Verify critical logs still present
grep "Early stopping triggered" log.txt # Must exist
grep "Best model saved" log.txt # Must exist
grep "error!" log.txt # Must exist (if errors)
RISK ASSESSMENT
---------------
Risk Level: LOW
- Logging changes are non-functional
- No business logic affected
- Existing tests will pass
- Only affects observability, not behavior
Breaking Changes: NONE
- Log format may change (users parsing logs manually)
- Frequency reduction may break log-scraping scripts
- Mitigation: Document changes, provide migration guide
RELATED DOCUMENTS
-----------------
- Full Analysis: docs/codebase-cleanup/dqn-logging-analysis.md
- Logging Best Practices: docs/logging-guidelines.md (TODO)
- Tracing Crate Docs: https://docs.rs/tracing/
CONTACT
-------
For questions or clarifications, see:
- Code Quality Analyzer Role (CLAUDE.md)
- Project: /home/jgrusewski/Work/foxhunt