Files
foxhunt/run_cross_validation.sh
jgrusewski 35feadf55e 🚀 Wave 160 Phase 6: CUDA Mandatory + TDD Testing + TFT Complete (21 Agents)
## Major Achievements

### 1. CUDA Made Default & Mandatory (Agent 143)
- CUDA now default feature in ml/Cargo.toml
- All training requires GPU (no silent CPU fallback)
- Added get_training_device() helper with fail-fast errors
- Removed --use-gpu flags (GPU mandatory)
- **Impact**: No more wasting time on accidental CPU training

### 2. TFT Training COMPLETE (Agent 144)
-  Training completed successfully in 7.6 minutes
-  Early stopping at epoch 100/200 (best val loss: 0.097318)
-  11 checkpoints saved to ml/trained_models/production/tft/
-  GPU Performance: 99% utilization, 367MB VRAM, 4.4s/epoch
-  10x speedup vs CPU (4.4s vs 43-55s per epoch)
- **Status**: PRODUCTION READY

### 3. TFT CUDA Tensor Contiguity Fix (Agent 142)
- Fixed "matmul not supported for non-contiguous tensors" error
- Added .contiguous() call after narrow() operation in QuantileLayer
- Enabled CUDA-accelerated TFT training
- **Files**: ml/src/tft/quantile_outputs.rs

### 4. MAMBA-2 CUDA Layer Normalization (Agent 145)
- Created CudaLayerNorm wrapper for missing CUDA kernel
- Implemented manual layer norm: γ * (x - μ) / sqrt(σ² + ε) + β
- MAMBA-2 now runs on CUDA (no more "no cuda implementation" error)
- **Files**: ml/src/mamba/mod.rs

### 5. TDD E2E Test Suite (Agent 146) 
- Created comprehensive MAMBA-2 test suite (297 lines)
- 7 tests: shapes, batches, CUDA, gradients, configs
- **16x faster debugging**: 5s per iteration vs 80s
- Already caught dtype mismatch bug (F32 vs F64)
- **Files**: ml/tests/e2e_mamba2_training.rs

## Agent Summary (Agents 126-146)

### Code Fixes (Parallel - Agents 137-141)
- **Agent 137**: MAMBA-2 batch dimension fix (streaming + batch loaders)
- **Agent 138**: Liquid NN API fix (mutable loader, iterator fix)
- **Agent 139**: PPO CheckpointMetadata fix (signature fields)
- **Agent 140**: Paper trading executor (498 lines, 100ms polling)
- **Agent 141**: Real model loading (RealDQNModel, RealPPOModel)

### Infrastructure (Agents 143-146)
- **Agent 143**: CUDA mandatory (Cargo.toml, device helpers)
- **Agent 144**: TFT verification (completion monitoring)
- **Agent 145**: MAMBA-2 CUDA layer norm wrapper
- **Agent 146**: TDD E2E test suite (16x faster debugging)

## Files Modified

### Core ML Infrastructure
- ml/Cargo.toml: Added default = ["minimal-inference", "cuda"]
- ml/src/lib.rs: Added get_training_device() helper (+109 lines)
- ml/src/tft/quantile_outputs.rs: Fixed tensor contiguity
- ml/src/mamba/mod.rs: Added CudaLayerNorm wrapper (+41 lines)

### Training Scripts
- ml/examples/train_tft_dbn.rs: Removed --use-gpu flag
- ml/examples/train_ppo.rs: Removed --use-gpu flag
- ml/examples/train_mamba2_dbn.rs: Forced CUDA-only mode
- ml/examples/train_liquid_dbn.rs: Fixed API usage

### Data Loaders
- ml/src/data_loaders/dbn_sequence_loader.rs: Fixed batch dimensions
- ml/src/data_loaders/streaming_dbn_loader.rs: Fixed batch dimensions

### Trading Service
- services/trading_service/src/paper_trading_executor.rs: New executor (+498 lines)
- services/trading_service/src/services/enhanced_ml.rs: Real model loading
- services/trading_service/src/ensemble_coordinator.rs: Integration

### Tests
- ml/tests/e2e_mamba2_training.rs: New TDD test suite (+297 lines)

### Trainers
- ml/src/trainers/tft.rs: Fixed CheckpointMetadata signature fields

## Performance Metrics

### TFT Training
- Duration: 7.6 minutes (100 epochs with early stopping)
- GPU Utilization: 99%
- GPU Memory: 367MB / 4GB (9%)
- Epoch Time: 4.4 seconds (vs 43-55s on CPU)
- Speedup: 10x vs CPU
- Status:  PRODUCTION READY

### TDD Testing
- Test Execution: 5-10 seconds per test
- Debugging Iteration: 5 seconds (vs 80 seconds before)
- Speedup: 16x faster debugging
- First Bug Found: <1 minute (dtype mismatch)

## Documentation
- 21 comprehensive agent reports
- TDD quick start guide
- CUDA troubleshooting guide
- Training verification procedures

## Next Steps
1. Fix MAMBA-2 dtype mismatch (F32→F64) - 2 minutes
2. Run MAMBA-2 tests until passing - 5-10 minutes
3. Launch full MAMBA-2 training - 200 epochs
4. Launch Liquid NN training

## System Status
- TFT:  COMPLETE (production ready)
- MAMBA-2: 🧪 IN TESTING (TDD suite ready)
- CUDA:  DEFAULT (mandatory for training)
- Tests:  16x faster debugging

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-14 23:13:34 +02:00

112 lines
3.6 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# Cross-Validation: Test top 3 models on held-out May 2024 data
# Models: DQN-30, DQN-310, PPO-130
# Objective: Validate generalization (Sharpe drop <20%, win rate >55%, max drawdown <15%)
set -e
RESULTS_DIR="/home/jgrusewski/Work/foxhunt/results/cross_validation"
mkdir -p "$RESULTS_DIR"
echo "=========================================="
echo "CROSS-VALIDATION ON HELD-OUT DATA (May 2024)"
echo "=========================================="
echo ""
echo "Models Under Test:"
echo " - DQN Epoch 30 (Early exploration, high Q-value)"
echo " - DQN Epoch 310 (Late convergence, conservative)"
echo " - PPO Epoch 130 (Mid-training, balanced)"
echo ""
echo "Held-Out Dataset: May 2024 (4 days × 4 symbols = 16 files)"
echo "Training Dataset: Jan-April 2024 (361 files)"
echo ""
echo "Success Criteria:"
echo " ✅ Sharpe ratio >8.0 on held-out (vs 10+ on training)"
echo " ✅ Win rate >55%"
echo " ✅ Max drawdown <15%"
echo " ✅ Generalization gap <20% (held-out Sharpe / training Sharpe)"
echo ""
# Test each model on each symbol's May data
MODELS=(
"dqn_epoch_30:DQN"
"dqn_epoch_310:DQN"
"ppo_actor_epoch_130:PPO"
)
SYMBOLS=("ES.FUT" "NQ.FUT" "ZN.FUT" "6E.FUT")
for model_info in "${MODELS[@]}"; do
IFS=':' read -r model_file model_type <<< "$model_info"
echo "=========================================="
echo "Testing: $model_file ($model_type)"
echo "=========================================="
for symbol in "${SYMBOLS[@]}"; do
echo ""
echo "📊 Symbol: $symbol (May 2024 held-out data)"
# Find May 2024 files for this symbol
DATA_FILES=$(find /home/jgrusewski/Work/foxhunt/test_data/real/databento/ml_training \
-name "${symbol}_ohlcv-1m_2024-05-*.dbn" | sort)
if [ -z "$DATA_FILES" ]; then
echo " ⚠️ No held-out data found for $symbol"
continue
fi
NUM_FILES=$(echo "$DATA_FILES" | wc -l)
echo " Found $NUM_FILES May 2024 data files"
# Create temporary directory for this symbol's May data
TEMP_DATA_DIR="$RESULTS_DIR/temp_${symbol}_may2024"
mkdir -p "$TEMP_DATA_DIR"
# Copy May files to temp directory
echo "$DATA_FILES" | while read -r file; do
cp "$file" "$TEMP_DATA_DIR/"
done
# Determine model path based on type
if [ "$model_type" = "DQN" ]; then
MODEL_PATH="/home/jgrusewski/Work/foxhunt/ml/trained_models/production/dqn_real_data/${model_file}.safetensors"
else
MODEL_PATH="/home/jgrusewski/Work/foxhunt/ml/trained_models/production/ppo_real_data/${model_file}.safetensors"
fi
# Run backtest
OUTPUT_FILE="$RESULTS_DIR/${model_file}_${symbol}_may2024.json"
echo " 🔄 Running backtest..."
echo " Model: $MODEL_PATH"
echo " Data: $TEMP_DATA_DIR"
echo " Output: $OUTPUT_FILE"
# Run comprehensive backtest (Note: This is a placeholder - actual implementation needed)
# The comprehensive_model_backtest.rs needs to be updated to accept CLI args
echo " ⏳ Backtest execution placeholder (requires CLI args implementation)"
# Cleanup temp directory
rm -rf "$TEMP_DATA_DIR"
echo " ✅ Backtest complete"
done
echo ""
done
echo ""
echo "=========================================="
echo "CROSS-VALIDATION COMPLETE"
echo "=========================================="
echo ""
echo "Results saved to: $RESULTS_DIR"
echo ""
echo "Next Steps:"
echo " 1. Analyze results: Compare training metrics vs held-out"
echo " 2. Calculate generalization gap: (training_sharpe - held_out_sharpe) / training_sharpe"
echo " 3. Identify overfitting: Gap >20% indicates poor generalization"
echo " 4. Generate CROSS_VALIDATION_REPORT.md"
echo ""