Files
foxhunt/scripts/record_baseline_metrics.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

295 lines
8.8 KiB
Bash
Executable File

#!/bin/bash
#
# Performance Baseline Metrics Recording Script
#
# This script records baseline performance metrics for the Foxhunt HFT system.
# Baselines are saved and used for regression detection in CI.
#
# Usage:
# ./scripts/record_baseline_metrics.sh [baseline-name]
#
# Examples:
# ./scripts/record_baseline_metrics.sh main # Record main branch baseline
# ./scripts/record_baseline_metrics.sh v1.0.0 # Record version baseline
# ./scripts/record_baseline_metrics.sh # Record 'current' baseline
set -euo pipefail
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
BASELINE_NAME="${1:-current}"
BASELINE_DIR="target/criterion/baselines"
METRICS_DIR="performance_metrics"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
echo -e "${BLUE}=================================================${NC}"
echo -e "${BLUE}Performance Baseline Metrics Recording${NC}"
echo -e "${BLUE}=================================================${NC}"
echo ""
echo -e "${GREEN}Baseline name:${NC} $BASELINE_NAME"
echo -e "${GREEN}Timestamp:${NC} $TIMESTAMP"
echo ""
# Create metrics directory
mkdir -p "$METRICS_DIR"
# Function to record system info
record_system_info() {
echo -e "${YELLOW}Recording system information...${NC}"
cat > "$METRICS_DIR/system_info_${BASELINE_NAME}_${TIMESTAMP}.txt" <<EOF
Performance Baseline - System Information
==========================================
Baseline: $BASELINE_NAME
Timestamp: $TIMESTAMP
Date: $(date)
Hardware:
---------
CPU: $(lscpu | grep "Model name" | sed 's/Model name:\s*//')
Cores: $(nproc)
Memory: $(free -h | grep Mem | awk '{print $2}')
GPU: $(nvidia-smi --query-gpu=name --format=csv,noheader 2>/dev/null || echo "No GPU detected")
Software:
---------
OS: $(uname -a)
Rust: $(rustc --version)
Cargo: $(cargo --version)
Git:
----
Commit: $(git rev-parse HEAD)
Branch: $(git rev-parse --abbrev-ref HEAD)
Status: $(git status --short | wc -l) files changed
EOF
echo -e "${GREEN}✓ System info recorded${NC}"
}
# Function to run benchmarks and save baseline
run_benchmarks() {
echo ""
echo -e "${YELLOW}Running performance regression benchmarks...${NC}"
echo -e "${BLUE}This will take approximately 5-10 minutes${NC}"
echo ""
# Run with baseline save
if cargo bench --bench performance_regression -- --save-baseline "$BASELINE_NAME"; then
echo ""
echo -e "${GREEN}✓ Benchmarks completed successfully${NC}"
return 0
else
echo ""
echo -e "${RED}✗ Benchmarks failed${NC}"
return 1
fi
}
# Function to extract key metrics
extract_metrics() {
echo ""
echo -e "${YELLOW}Extracting key performance metrics...${NC}"
local metrics_file="$METRICS_DIR/metrics_${BASELINE_NAME}_${TIMESTAMP}.json"
cat > "$metrics_file" <<EOF
{
"baseline": "$BASELINE_NAME",
"timestamp": "$TIMESTAMP",
"date": "$(date -Iseconds)",
"git_commit": "$(git rev-parse HEAD)",
"git_branch": "$(git rev-parse --abbrev-ref HEAD)",
"metrics": {
"ml_prediction_latency": {
"description": "ML model prediction latency",
"target_p50_us": 20,
"target_p99_us": 50,
"note": "See criterion HTML report for actual values"
},
"hot_swap_latency": {
"description": "Model hot-swap latency",
"target_p50_us": 1,
"note": "See criterion HTML report for actual values"
},
"database_writes": {
"description": "Database write throughput",
"target_writes_per_sec": 1000,
"note": "See criterion HTML report for actual values"
},
"backtest_performance": {
"description": "Backtest bar processing rate",
"target_bars_per_sec": 1100,
"note": "See criterion HTML report for actual values"
},
"order_processing": {
"description": "Order processing latency",
"target_p99_us": 100,
"note": "See criterion HTML report for actual values"
},
"risk_validation": {
"description": "Risk validation latency",
"target_p99_us": 50,
"note": "See criterion HTML report for actual values"
}
},
"baseline_location": "$BASELINE_DIR/$BASELINE_NAME"
}
EOF
echo -e "${GREEN}✓ Metrics extracted to: $metrics_file${NC}"
}
# Function to generate baseline summary
generate_summary() {
echo ""
echo -e "${YELLOW}Generating baseline summary...${NC}"
local summary_file="$METRICS_DIR/baseline_summary_${BASELINE_NAME}.md"
cat > "$summary_file" <<EOF
# Performance Baseline: $BASELINE_NAME
**Timestamp**: $TIMESTAMP
**Date**: $(date)
**Git Commit**: $(git rev-parse --short HEAD)
**Git Branch**: $(git rev-parse --abbrev-ref HEAD)
## System Configuration
- **CPU**: $(lscpu | grep "Model name" | sed 's/Model name:\s*//' | xargs)
- **Cores**: $(nproc)
- **Memory**: $(free -h | grep Mem | awk '{print $2}')
- **GPU**: $(nvidia-smi --query-gpu=name --format=csv,noheader 2>/dev/null || echo "No GPU detected")
- **OS**: $(uname -s) $(uname -r)
- **Rust**: $(rustc --version | awk '{print $2}')
## Baseline Metrics
| Component | Target | Status |
|-----------|--------|--------|
| ML Prediction Latency | P50 < 20μs, P99 < 50μs | ⏳ See HTML Report |
| Hot-Swap Latency | P50 < 1μs | ⏳ See HTML Report |
| Database Writes | >1000/sec | ⏳ See HTML Report |
| Backtest Performance | >1100 bars/sec | ⏳ See HTML Report |
| Order Processing | P99 < 100μs | ⏳ See HTML Report |
| Risk Validation | P99 < 50μs | ⏳ See HTML Report |
## Usage
### Compare Against This Baseline
\`\`\`bash
# Run benchmarks and compare
cargo bench --bench performance_regression -- --baseline $BASELINE_NAME
# View detailed HTML report
open target/criterion/report/index.html
\`\`\`
### Detect Regressions
Regressions are flagged when performance degrades >10% from baseline:
- **Red** (❌): Significant regression detected
- **Yellow** (⚠️): Borderline regression
- **Green** (✅): Performance maintained or improved
## Files
- **Baseline data**: \`$BASELINE_DIR/$BASELINE_NAME/\`
- **Metrics JSON**: \`$METRICS_DIR/metrics_${BASELINE_NAME}_${TIMESTAMP}.json\`
- **System info**: \`$METRICS_DIR/system_info_${BASELINE_NAME}_${TIMESTAMP}.txt\`
- **HTML reports**: \`target/criterion/\`
## Notes
- Criterion automatically saves detailed statistics
- HTML reports include latency distributions and percentiles
- Use \`--baseline $BASELINE_NAME\` to compare future runs
- Baselines are stored in \`target/criterion/baselines/\`
EOF
echo -e "${GREEN}✓ Summary generated: $summary_file${NC}"
}
# Function to create comparison script
create_comparison_script() {
echo ""
echo -e "${YELLOW}Creating comparison helper script...${NC}"
local script_file="$METRICS_DIR/compare_with_${BASELINE_NAME}.sh"
cat > "$script_file" <<'EOFSCRIPT'
#!/bin/bash
# Auto-generated comparison script
BASELINE_NAME="BASELINE_PLACEHOLDER"
echo "Comparing current performance against baseline: $BASELINE_NAME"
echo ""
cargo bench --bench performance_regression -- --baseline "$BASELINE_NAME"
echo ""
echo "View detailed HTML report:"
echo " open target/criterion/report/index.html"
EOFSCRIPT
sed -i "s/BASELINE_PLACEHOLDER/$BASELINE_NAME/" "$script_file"
chmod +x "$script_file"
echo -e "${GREEN}✓ Comparison script created: $script_file${NC}"
}
# Main execution
main() {
echo -e "${BLUE}Step 1/5: Recording system information${NC}"
record_system_info
echo ""
echo -e "${BLUE}Step 2/5: Running benchmarks${NC}"
if ! run_benchmarks; then
echo -e "${RED}Failed to complete benchmarks. Exiting.${NC}"
exit 1
fi
echo ""
echo -e "${BLUE}Step 3/5: Extracting metrics${NC}"
extract_metrics
echo ""
echo -e "${BLUE}Step 4/5: Generating summary${NC}"
generate_summary
echo ""
echo -e "${BLUE}Step 5/5: Creating comparison helpers${NC}"
create_comparison_script
echo ""
echo -e "${GREEN}=================================================${NC}"
echo -e "${GREEN}✓ Baseline '$BASELINE_NAME' recorded successfully${NC}"
echo -e "${GREEN}=================================================${NC}"
echo ""
echo -e "${BLUE}Next steps:${NC}"
echo -e " 1. View HTML report: ${YELLOW}open target/criterion/report/index.html${NC}"
echo -e " 2. Compare later: ${YELLOW}cargo bench --bench performance_regression -- --baseline $BASELINE_NAME${NC}"
echo -e " 3. Review summary: ${YELLOW}cat $METRICS_DIR/baseline_summary_${BASELINE_NAME}.md${NC}"
echo ""
echo -e "${BLUE}Files created:${NC}"
echo -e " - Baseline: ${YELLOW}$BASELINE_DIR/$BASELINE_NAME/${NC}"
echo -e " - Metrics: ${YELLOW}$METRICS_DIR/metrics_${BASELINE_NAME}_${TIMESTAMP}.json${NC}"
echo -e " - Summary: ${YELLOW}$METRICS_DIR/baseline_summary_${BASELINE_NAME}.md${NC}"
echo -e " - System: ${YELLOW}$METRICS_DIR/system_info_${BASELINE_NAME}_${TIMESTAMP}.txt${NC}"
echo ""
}
# Run main function
main "$@"