Files
foxhunt/scripts/README_test_tli_tuning.md
jgrusewski 3799c04064 🎯 Wave 159: Fix ML Training Infrastructure (22 Parallel Agents)
Critical Discovery: Training scripts used benchmark tool instead of trainers
- No .safetensors model files were being saved
- Fixed by creating real training examples with checkpoint callbacks

## Training Infrastructure Fixed (Agents 1-24)

### Root Cause Identified (Agent 1-2)
- scripts/train_all_models_full.sh used gpu_training_benchmark (benchmark only)
- Benchmarks measure performance but DO NOT save models
- Created 4 new training examples with proper model persistence

### Module Exports Fixed (Agents 3-6)
- ml/src/trainers/mod.rs: Added DQN module export
- All trainer types now accessible: DQNTrainer, PPOTrainer, Mamba2Trainer, TFTTrainer

### Training Examples Created (Agents 7-14)
- ml/examples/train_dqn.rs (170 lines) - DQN with Experience replay
- ml/examples/train_ppo.rs (140 lines) - PPO with GAE
- ml/examples/train_mamba2.rs (210 lines) - MAMBA-2 with state space
- ml/examples/train_tft.rs (250 lines) - TFT with temporal fusion

### Trainer Bugs Fixed (Agents 11, 23)
- ml/src/trainers/dqn.rs: Fixed Experience initialization (timestamp, type conversions)
- ml/src/trainers/ppo.rs: Fixed tensor shape mismatches (flatten before scalar)
- ml/src/trainers/dqn.rs: Fixed epsilon type conversion (f64 → f32 cast)

### E2E Test Infrastructure (Agents 15-18, TDD Approach)
- tests/e2e/tests/dqn_training_test.rs (369 lines) - 2/2 passing
- tests/e2e/tests/ppo_training_test.rs (512 lines) - Comprehensive validation
- tests/e2e/tests/mamba2_training_test.rs (459 lines) - gRPC integration
- tests/e2e/tests/tft_training_test.rs (616 lines) - Progress streaming

### Scripts & Validation (Agents 19-20)
- scripts/train_all_models_fixed.sh - Uses real trainers
- scripts/validate_training.sh (268 lines) - Quick validation
- scripts/test_dqn_training.sh - Individual model testing

### API Documentation (Agents 7-10)
- TRAINING_GUIDE.md - Comprehensive training guide
- docs/AGENT_19_TRAINING_SCRIPT_VALIDATION.md - Script validation
- 200+ pages of trainer API documentation

## Technical Achievements

### Performance
- DQN Experience constructor: Proper type handling
- PPO tensor operations: .flatten_all()?.to_vec1::<f32>()?[0]
- GPU memory optimization: Batch size limits for RTX 3050 Ti (4GB)

### Architecture
- Checkpoint callbacks: |epoch, model_data| → .safetensors files
- Real-time progress streaming: tokio::sync::mpsc channels
- E2E testing: Fast iteration without Docker rebuilds

### Production Readiness
- Module exports: 100% 
- Training examples: 100%  (all compile and run)
- E2E tests: 100%  (4 comprehensive test suites)
- Build status: 100%  (zero compilation errors)

## Files Modified: 50+
- Core trainers: dqn.rs, ppo.rs, mamba2.rs, tft.rs
- Module exports: mod.rs
- Training examples: 4 new files (770 lines total)
- E2E tests: 4 new files (1956 lines total)
- Scripts: 5 new validation scripts
- Documentation: 7 new docs (100K+ words)

## Tests Created: 8 E2E Tests
- DQN: Checkpoint creation, model loading
- PPO: Training metrics, convergence
- MAMBA-2: State space validation, gRPC
- TFT: Temporal fusion, progress streaming

Status:  Ready for model training (500 epochs per model)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-14 09:06:37 +02:00

600 lines
16 KiB
Markdown
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.
# TLI Tuning Workflow Test Script
**Script**: `/home/jgrusewski/Work/foxhunt/scripts/test_tli_tuning.sh`
**Purpose**: End-to-end testing of the TLI hyperparameter tuning workflow
**Version**: 1.0.0
**Last Updated**: 2025-10-13
---
## Overview
This script provides comprehensive testing of the TLI hyperparameter tuning functionality, including:
- Prerequisite validation (services, authentication, data)
- Job submission and tracking
- Status polling with real-time progress
- Best parameter retrieval and export
- Graceful error handling and cleanup
## Features
### 1. Prerequisite Checks ✅
- TLI binary existence and location
- JWT token validation (with masking for security)
- API Gateway health (HTTP + gRPC)
- ML Training Service availability
- Config file creation (auto-generates if missing)
- Test data validation with size reporting
- Optional tool detection (grpcurl, jq)
### 2. Three Operating Modes
#### Full Workflow (Default)
```bash
./scripts/test_tli_tuning.sh
```
**Steps**:
1. Check prerequisites
2. Start tuning job (DQN, 5 trials)
3. Poll status every 5 seconds
4. Display real-time progress with timestamps
5. Retrieve best parameters when complete
6. Export results to YAML file
**Duration**: ~5-10 minutes (depending on trials)
#### Quick Test Mode
```bash
./scripts/test_tli_tuning.sh --quick
```
**Steps**:
1. Check prerequisites
2. Start tuning job
3. Display job ID and monitoring commands
4. Exit immediately (no polling)
**Duration**: ~10 seconds
**Use Case**: Verify job submission works without waiting
#### Check-Only Mode
```bash
./scripts/test_tli_tuning.sh --check-only
```
**Steps**:
1. Validate all prerequisites
2. Report status of services/files
3. Exit with detailed diagnostics
**Duration**: ~5 seconds
**Use Case**: Pre-flight checks before running tests
### 3. Customization Options
```bash
# Custom model type
./scripts/test_tli_tuning.sh --model PPO --trials 10
# Environment variable overrides
export POLL_INTERVAL=10 # Poll every 10 seconds
export MAX_WAIT_TIME=600 # Wait up to 10 minutes
export TLI_BIN=/custom/path/tli # Custom TLI location
./scripts/test_tli_tuning.sh
```
### 4. Error Handling
- **Automatic cleanup**: Stops jobs on script failure/interrupt
- **Exit traps**: SIGINT, SIGTERM handled gracefully
- **Detailed diagnostics**: Clear error messages with remediation steps
- **Progress tracking**: Job IDs saved to `~/.foxhunt/test_tuning_job.txt`
### 5. Output Features
- **Color-coded status**: ✅ Green (success), ❌ Red (error), ⚠️ Yellow (warning)
- **Real-time progress**: Timestamps, trial progress, elapsed time
- **Progress visualization**: Text-based progress bar
- **Duration tracking**: Total test duration reported
- **Masked credentials**: JWT tokens masked for security
---
## Prerequisites
### 1. Build TLI Binary
```bash
cargo build --release -p tli
# Binary: target/release/tli
```
### 2. Authenticate with TLI
```bash
./target/release/tli auth login --username <user> --password <pass>
# Creates: ~/.foxhunt/jwt_token
```
### 3. Start Infrastructure Services
```bash
docker-compose up -d
# Services: postgres, redis, vault, influxdb
```
### 4. Start API Gateway
```bash
cargo run --release -p api_gateway
# Ports: 50051 (gRPC), 8080 (HTTP health)
```
### 5. Start ML Training Service
```bash
cargo run --release -p ml_training_service
# Ports: 50054 (gRPC), 8095 (HTTP health)
```
### 6. Verify Health
```bash
./scripts/comprehensive_health_check.sh
# Should show all services healthy
```
---
## Usage Examples
### Example 1: Basic Full Workflow Test
```bash
$ ./scripts/test_tli_tuning.sh
================================================================================
Prerequisite Checks
================================================================================
[STEP] Checking TLI binary...
✓ TLI binary found
Location: /home/jgrusewski/Work/foxhunt/target/release/tli
[STEP] Checking JWT token...
✓ JWT token found
Token (masked): eyJhbGciOiJIUzI1NiIs...WXZ6aGJHVnU=
[STEP] Checking API Gateway health...
✓ API Gateway is healthy
[STEP] Checking ML Training Service health...
✓ ML Training Service is healthy
[STEP] Checking tuning config file...
✓ Config file found
Path: /home/jgrusewski/Work/foxhunt/test_data/tuning_config.yaml
[STEP] Checking test data...
✓ Test data found
Path: /home/jgrusewski/Work/foxhunt/test_data/btcusdt_sample_100.parquet
Size: 1.2M
✓ All prerequisites satisfied
================================================================================
Starting Tuning Job
================================================================================
[STEP] Submitting tuning job to TLI...
Command: ./target/release/tli tune start --model DQN --trials 5 --config test_data/tuning_config.yaml
🚀 Starting hyperparameter tuning job...
Model: DQN
Trials: 5
Config: test_data/tuning_config.yaml
GPU: ❌ Disabled
✅ Tuning job started successfully!
Job ID: 550e8400-e29b-41d4-a716-446655440000
Saved to ~/.foxhunt/tuning_jobs.json
================================================================================
Polling Job Status
================================================================================
Polling every 5s (max wait: 5m)
[14:30:05] Status: RUNNING | Progress: 0/5 (0.0%) | Elapsed: 0s
[14:30:10] Status: RUNNING | Progress: 1/5 (20.0%) | Elapsed: 5s
[14:30:15] Status: RUNNING | Progress: 2/5 (40.0%) | Elapsed: 10s
[14:30:20] Status: RUNNING | Progress: 3/5 (60.0%) | Elapsed: 15s
[14:30:25] Status: RUNNING | Progress: 4/5 (80.0%) | Elapsed: 20s
[14:30:30] Status: RUNNING | Progress: 5/5 (100.0%) | Elapsed: 25s
✅ Job completed successfully!
📊 Tuning Job Status
Status: TUNING_COMPLETED
Progress: 5/5 trials (100.0%)
[██████████████████████████████████████████████████] 100.0%
🏆 Best Results So Far
Sharpe Ratio: 2.3456
Elapsed Time: 30 seconds
================================================================================
Retrieving Best Parameters
================================================================================
[STEP] Fetching best hyperparameters...
🏆 Best Performance Metrics
sharpe_ratio: 2.3456
total_return: 15.6789
max_drawdown: 8.4321
📋 Best Hyperparameters
┌──────────────────┬──────────┬───────────────┐
│ Parameter │ Value │ Type │
├──────────────────┼──────────┼───────────────┤
│ learning_rate │ 0.000512 │ Learning Rate │
│ batch_size │ 64.000000│ Integer │
│ gamma │ 0.976543 │ Float │
│ epsilon_decay │ 0.995678 │ Float │
└──────────────────┴──────────┴───────────────┘
[STEP] Exporting best parameters to file...
✅ Parameters exported to: best_params_550e8400.yaml
💡 Use these parameters in your training configuration.
================================================================================
Test Completed Successfully
================================================================================
✅ Full workflow executed without errors
Total duration: 35s
```
### Example 2: Quick Test (No Waiting)
```bash
$ ./scripts/test_tli_tuning.sh --quick
================================================================================
Quick TLI Tuning Test (Start Only)
================================================================================
[Prerequisites checks...]
✅ All prerequisites satisfied
================================================================================
Starting Tuning Job
================================================================================
✅ Tuning job started successfully!
Job ID: 550e8400-e29b-41d4-a716-446655440000
✅ Job started successfully
Monitor with: ./target/release/tli tune status --job-id 550e8400-e29b-41d4-a716-446655440000
Get results: ./target/release/tli tune best --job-id 550e8400-e29b-41d4-a716-446655440000
Stop job: ./target/release/tli tune stop --job-id 550e8400-e29b-41d4-a716-446655440000
Total test duration: 8s
```
### Example 3: Custom Model and Trials
```bash
$ ./scripts/test_tli_tuning.sh --model MAMBA_2 --trials 10
[Prerequisites checks...]
✅ All prerequisites satisfied
🚀 Starting hyperparameter tuning job...
Model: MAMBA_2
Trials: 10
Config: test_data/tuning_config.yaml
GPU: ❌ Disabled
[Polling progress 0-100%...]
```
### Example 4: Check Prerequisites Only
```bash
$ ./scripts/test_tli_tuning.sh --check-only
================================================================================
Prerequisite Checks
================================================================================
[STEP] Checking TLI binary...
✓ TLI binary found
Location: /home/jgrusewski/Work/foxhunt/target/release/tli
[STEP] Checking JWT token...
✗ JWT token not found at: /home/jgrusewski/.foxhunt/jwt_token
Authenticate with: ./target/release/tli auth login
[STEP] Checking API Gateway health...
✗ API Gateway not responding at http://localhost:8080/health
Start services with: docker-compose up -d
[STEP] Checking ML Training Service health...
✓ ML Training Service is healthy
✗ Prerequisites check FAILED
Please resolve issues above and retry
```
---
## Configuration
### Environment Variables
| Variable | Default | Description |
|----------|---------|-------------|
| `TLI_BIN` | `target/release/tli` | Path to TLI binary |
| `JWT_TOKEN_FILE` | `~/.foxhunt/jwt_token` | JWT token location |
| `POLL_INTERVAL` | `5` | Status polling interval (seconds) |
| `MAX_WAIT_TIME` | `300` | Maximum wait time (seconds) |
### Generated Files
| File | Location | Purpose |
|------|----------|---------|
| JWT Token | `~/.foxhunt/jwt_token` | Authentication |
| Tuning Jobs | `~/.foxhunt/tuning_jobs.json` | Job tracking |
| Test Job ID | `~/.foxhunt/test_tuning_job.txt` | Current test job |
| Minimal Config | `test_data/tuning_config.yaml` | Auto-generated config |
| Best Params | `best_params_<job_id>.yaml` | Exported parameters |
### Minimal Config Structure
The script auto-generates a minimal config if none exists:
```yaml
# test_data/tuning_config.yaml
tuning:
study_name: "test_study"
storage: "sqlite:///optuna_test.db"
direction: "maximize" # Maximize Sharpe ratio
search_space:
learning_rate:
type: "float"
low: 0.0001
high: 0.01
log: true
batch_size:
type: "int"
low: 32
high: 128
step: 32
gamma:
type: "float"
low: 0.90
high: 0.99
epsilon_decay:
type: "float"
low: 0.990
high: 0.999
training:
epochs: 10
validation_split: 0.2
early_stopping_patience: 3
metrics:
- "sharpe_ratio"
- "total_return"
- "max_drawdown"
- "win_rate"
```
---
## Troubleshooting
### Issue: TLI Binary Not Found
**Error**:
```
✗ TLI binary not found at: target/release/tli
Build with: cargo build --release -p tli
```
**Solution**:
```bash
cargo build --release -p tli
```
---
### Issue: JWT Token Missing
**Error**:
```
✗ JWT token not found at: ~/.foxhunt/jwt_token
Authenticate with: ./target/release/tli auth login
```
**Solution**:
```bash
./target/release/tli auth login --username admin --password admin123
```
**Note**: Default dev credentials from `docker-compose.yml`
---
### Issue: API Gateway Not Running
**Error**:
```
✗ API Gateway not responding at http://localhost:8080/health
Start services with: docker-compose up -d
```
**Solution**:
```bash
# Start infrastructure
docker-compose up -d
# Start API Gateway
cargo run --release -p api_gateway
```
---
### Issue: ML Training Service Unavailable
**Error**:
```
✗ ML Training Service not responding at http://localhost:8095/health
```
**Solution**:
```bash
# Check service status
curl http://localhost:8095/health
# Start if not running
cargo run --release -p ml_training_service
# Check logs
docker-compose logs ml_training_service
```
---
### Issue: Job Times Out
**Symptom**: Job exceeds MAX_WAIT_TIME (300s default)
**Solution**:
```bash
# Increase max wait time
export MAX_WAIT_TIME=600 # 10 minutes
./scripts/test_tli_tuning.sh
# Or reduce trials for faster completion
./scripts/test_tli_tuning.sh --trials 3
```
---
### Issue: Config File Not Found
**Symptom**: Warning about missing config file
**Behavior**: Script auto-creates minimal config at `test_data/tuning_config.yaml`
**Verification**:
```bash
cat test_data/tuning_config.yaml
```
---
## Integration with CI/CD
### GitHub Actions Example
```yaml
name: Test TLI Tuning
on: [push, pull_request]
jobs:
test-tli-tuning:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Start infrastructure
run: docker-compose up -d
- name: Build TLI
run: cargo build --release -p tli
- name: Start services
run: |
cargo run --release -p api_gateway &
cargo run --release -p ml_training_service &
sleep 10
- name: Authenticate TLI
run: |
./target/release/tli auth login \
--username admin \
--password ${{ secrets.TLI_PASSWORD }}
- name: Run quick test
run: ./scripts/test_tli_tuning.sh --quick
- name: Check prerequisites
run: ./scripts/test_tli_tuning.sh --check-only
```
---
## Performance Characteristics
| Operation | Duration | Notes |
|-----------|----------|-------|
| Prerequisites check | ~5s | Validates 10+ conditions |
| Job submission | ~1-2s | gRPC call to API Gateway |
| Status polling | ~5s/poll | Configurable via `POLL_INTERVAL` |
| Full workflow (5 trials) | ~5-10min | Depends on model complexity |
| Quick test | ~10s | Start job only |
---
## Security Considerations
1. **JWT Token Masking**: Tokens displayed as `first20...last10` characters
2. **Token Storage**: `~/.foxhunt/jwt_token` (chmod 600 recommended)
3. **Cleanup on Exit**: Jobs stopped automatically on script failure
4. **No Hardcoded Credentials**: All auth via TLI login flow
5. **HTTPS Support**: Change `API_GATEWAY_URL` for production
---
## Future Enhancements
### Planned Features
1. **Real-time Streaming**: Replace polling with server-side streaming gRPC
2. **Multi-job Testing**: Test multiple concurrent tuning jobs
3. **Performance Metrics**: Track latency, throughput, success rates
4. **GPU Testing**: Add `--gpu` flag validation
5. **Data Validation**: Verify parquet file structure before submission
### Configuration Improvements
1. **Custom Metrics**: Support custom objective functions
2. **Pruning Strategies**: Test Optuna pruning algorithms
3. **Multi-model Tests**: Iterate over all supported models
4. **Resource Limits**: Memory/CPU constraints testing
---
## Related Documentation
- **TLI Tune Command**: `/home/jgrusewski/Work/foxhunt/tli/src/commands/tune.rs`
- **API Gateway Proxy**: `/home/jgrusewski/Work/foxhunt/services/api_gateway/src/proxy_handlers.rs`
- **ML Training Service**: `/home/jgrusewski/Work/foxhunt/services/ml_training_service/`
- **CLAUDE.md**: Architecture and development guide
- **TESTING_PLAN.md**: ML testing strategy
---
## Version History
### 1.0.0 (2025-10-13)
- Initial release
- Full workflow testing (start → poll → results)
- Three operating modes (full, quick, check-only)
- Comprehensive error handling
- Auto-config generation
- JWT token masking
- Real-time progress tracking
- Export best parameters to YAML
---
## License
Part of the Foxhunt HFT Trading System
Copyright (c) 2025