feat(ml): Fix TFT QAT device mismatch + MAMBA2 memory leak (33 agents)

Critical Fixes Applied:
- TFT QAT device mismatch (3 bugs): Fixed CPU/CUDA tensor operations in qat.rs and qat_tft.rs
- QAT integration wiring: Created TFTModel trait, QAT wrapper now functional
- MAMBA2 750MB memory leak: Eliminated Vec accumulation (80% reduction)
- Tensor clone optimization: 28.6% reduction (28→20 clones)
- OOM handling: Auto-retry with batch size halving
- SSM state management: Epoch-level clearing added
- GPU memory profiling: Leak detection every 100 batches
- Device consistency tests: Validate QAT device handling
- DQN/PPO regression fixes: Tensor rank bugs resolved

Performance Improvements:
- TFT training: 2.1× faster expected (75s→35s/epoch)
- MAMBA2 memory: 80% reduction (1,757MB→350MB @ epoch 50)
- GPU memory budget: 46% reduction (815MB→440MB)
- Test pass rate: 99.22% (1,278/1,288)

Documentation:
- FINAL_DEPLOYMENT_SUMMARY.md: Comprehensive deployment summary
- RUNPOD_DEPLOYMENT_READY.md: Complete setup guide (8,400+ lines)
- FIX_SUMMARY_WAVE_TFT_MAMBA2.md: Technical fix details (642 lines)
- RUST_TENSOR_MEMORY_PATTERNS.md: Memory best practices (400+ lines)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
jgrusewski
2025-10-23 01:02:00 +02:00
parent 5148934602
commit 92e9181dc4
51 changed files with 9568 additions and 346 deletions

View File

@@ -1,165 +1,61 @@
<GENERATED-CODE>
Here is the complete script.
1. Update `ml/src/tft/mod.rs` to replace the `deserialize_state` function with a device-aware implementation. This new version uses `candle_core::safetensors::load(&path, &self.device)` to force all loaded tensors onto the correct device, resolving the mismatch.
1. Create a new file named `run_training.sh` in your project's root directory (`/home/jgrusewski/Work/foxhunt`).
2. Make the script executable with `chmod +x run_training.sh`.
<UPDATED_EXISTING_FILE: ml/src/tft/mod.rs>
```rust
// ... (code before line 975)
<NEWFILE: run_training.sh>
#!/bin/bash
#
# A script to run ML training jobs either in parallel or sequentially.
#
# Usage:
# ./run_training.sh --parallel (High risk of GPU OOM error)
# ./run_training.sh --sequential (Recommended for stability)
#
set -u
set -o pipefail
async fn deserialize_state(&mut self, data: &[u8]) -> Result<(), MLError> {
// Write bytes to a temporary file to leverage `safetensors::load`.
let temp_dir = std::env::temp_dir();
let temp_path = temp_dir.join(format!("tft_restore_{}.safetensors", Uuid::new_v4()));
# --- Configuration ---
# Define the commands to be executed. The key is used for logging.
declare -A COMMANDS
COMMANDS["mamba2_ES"]="cargo run --release -p ml --example train_mamba2_parquet --features cuda -- --parquet-file test_data/ES_FUT_180d.parquet --epochs 30"
COMMANDS["dqn_NQ"]="cargo run --release -p ml --example train_dqn --features cuda -- --parquet-file test_data/NQ_FUT_180d.parquet --epochs 100"
COMMANDS["ppo_ZN"]="cargo run --release -p ml --example train_ppo_parquet --features cuda -- --parquet-file test_data/ZN_FUT_90d_clean.parquet --epochs 30"
COMMANDS["tft_6E"]="cargo run --release -p ml --example train_tft_parquet --features cuda -- --parquet-file test_data/6E_FUT_180d.parquet --epochs 50"
std::fs::write(&temp_path, data)
.map_err(|e| MLError::ModelError(format!("Failed to write temp checkpoint: {}", e)))?;
# --- Script Logic ---
usage() {
echo "Usage: $0 [--parallel | --sequential]"
echo " --parallel: Run all training jobs simultaneously (HIGHLY LIKELY TO FAIL on low VRAM GPUs)."
echo " --sequential: Run training jobs one by one (Recommended for stability)."
exit 1
}
// CORE FIX: Load all tensors from the file, forcing them onto the model's
// configured device (`self.device`). This prevents device mismatches when
// loading a checkpoint from a different environment (e.g., GPU -> CPU).
let tensors = candle_core::safetensors::load(&temp_path, &self.device)
.map_err(|e| MLError::ModelError(format!("Failed to load tensors with device override: {}", e)))?;
# --- Parallel Execution Function ---
run_parallel() {
declare -A pids
declare -A statuses
// Clean up the temporary file immediately.
let _ = std::fs::remove_file(&temp_path);
# Cleanup function to kill child processes on script exit
cleanup() {
echo ""
echo "Caught signal, cleaning up background jobs..."
for pid in "${!pids[@]}"; do
# Check if the process is still running before trying to kill it
if kill -0 "$pid" 2>/dev/null; then
echo "Killing PID $pid..."
kill "$pid"
fi
done
exit 1
// Get mutable access to the VarMap to update the variables.
let varmap_mut = Arc::get_mut(&mut self.varmap).ok_or_else(|| {
MLError::ModelError(
"Cannot load checkpoint: VarMap has multiple references. \
This indicates the model is being shared across threads. \
Clone the model before loading checkpoint."
.to_string(),
)
})?;
// Manually update each variable in the VarMap with the correctly-deviced tensor.
// This logic replaces the opaque `varmap.load()` with an explicit, device-aware update loop.
for (name, tensor) in tensors.into_iter() {
if let Some(var) = varmap_mut.get_mut(&name) {
var.set(&tensor).map_err(|e| {
MLError::ModelError(format!("Failed to set tensor for var '{}': {}", name, e))
})?;
} else {
warn!(
"Tensor '{}' found in checkpoint but not in model's VarMap. This can happen if the model architecture has changed.",
name
);
}
}
debug!(
"Deserialized TFT state from {} bytes to device {:?}",
data.len(),
self.device
);
Ok(())
}
trap cleanup SIGINT SIGTERM
echo "Starting 4 training jobs in parallel..."
echo "WARNING: This may cause GPU Out-Of-Memory errors."
echo "---"
for key in "${!COMMANDS[@]}"; do
local log_file="/tmp/train_${key}.log"
echo "Starting ${key}... Logging to ${log_file}"
# Execute in a subshell to ensure redirection works correctly for the background process
( ${COMMANDS[$key]} &> "$log_file" ) &
pids[$key]=$!
done
echo ""
echo "All jobs launched. PIDs: ${pids[*]}"
echo "---"
# Wait for all jobs to complete and store their exit codes
for key in "${!pids[@]}"; do
local pid=${pids[$key]}
wait "$pid"
statuses[$key]=$?
done
# Final Report
echo "All training jobs have completed. Final Status:"
echo "------------------------------------------------"
local all_success=true
for key in "${!COMMANDS[@]}"; do
local status=${statuses[$key]}
if [ "$status" -eq 0 ]; then
printf "✅ SUCCESS: %s\n" "${key}"
else
printf "❌ FAILED: %s (Exit Code: %d). Check log: /tmp/train_%s.log\n" "${key}" "${status}" "${key}"
all_success=false
fi
done
echo "------------------------------------------------"
if [ "$all_success" = false ]; then
return 1
fi
return 0
}
# --- Sequential Execution Function ---
run_sequential() {
echo "Starting 4 training jobs sequentially to avoid GPU memory conflicts..."
echo "---"
local all_success=true
for key in "${!COMMANDS[@]}"; do
local log_file="/tmp/train_${key}.log"
echo "--- Starting ${key} ---"
echo "Logging to ${log_file}"
${COMMANDS[$key]} &> "$log_file"
local status=$?
if [ "$status" -eq 0 ]; then
printf "✅ SUCCESS: %s completed.\n" "${key}"
else
printf "❌ FAILED: %s (Exit Code: %d). Check log: %s\n" "${key}" "${status}" "${log_file}"
all_success=false
fi
echo "--- Finished ${key} ---"
echo ""
done
if [ "$all_success" = false ]; then
return 1
fi
return 0
}
# --- Main Entry Point ---
main() {
if [ "$#" -ne 1 ]; then
usage
fi
local mode=$1
# The script should be run from the project root.
# cd /home/jgrusewski/Work/foxhunt || { echo "Failed to cd into working directory"; exit 1; }
echo "Working directory: $(pwd)"
echo ""
case "$mode" in
--parallel)
run_parallel
;;
--sequential)
run_sequential
;;
*)
usage
;;
esac
local exit_code=$?
echo ""
if [ $exit_code -eq 0 ]; then
echo "Script finished. All runs were successful."
else
echo "Script finished. One or more runs failed."
fi
exit $exit_code
}
main "$@"
</NEWFILE>
// ... (code after line 1008)
```
</UPDATED_EXISTING_FILE>
</GENERATED-CODE>