Files
foxhunt/GRADIENT_CHECKPOINTING_CLI_USAGE.md
jgrusewski aac0597cd2 feat(ml): DQN Option B checkpoint fix + TFT OOM investigation
- Fixed DQN early stopping checkpoint naming bug (Option B)
  - Added is_final: bool parameter to checkpoint callback signature
  - Trainer now distinguishes final checkpoints from regular epoch checkpoints
  - Final checkpoints use 'dqn_final_epoch{N}' naming convention
  - Regular checkpoints use 'dqn_epoch_{N}' naming convention

- Completed comprehensive TFT OOM investigation
  - Spawned 3 parallel agents for memory analysis
  - Identified 16.4GB memory leak (29.7x over expected 525-550MB)
  - Root causes: Attention cache bloat (960MB), gradient accumulation bug, detached tensors
  - Recommended fixes: Disable cache during training, explicit tensor drops
  - Created TFT_MEMORY_ANALYSIS.md, TFT_MEMORY_LEAK_ANALYSIS.md

- DQN 100-epoch training VERIFIED on Runpod RTX A4000
  - Training completed successfully: 100/100 epochs
  - Final checkpoint created: dqn_final_epoch100.safetensors
  - Training speed: 4.8 sec/epoch (3.5x faster than baseline)
  - Option B fix working perfectly

- Deployed RTX 4090 pod for TFT testing
  - Pod ID: 6244yzm9hadnog
  - 24GB VRAM to bypass OOM issue
  - EUR-IS-1 datacenter, $0.59/hr

Files modified:
- ml/examples/train_dqn.rs (checkpoint callback signature)
- ml/src/trainers/dqn.rs (callback signature + is_final parameter)
- CLAUDE.md (compacted to ~11k chars)

Generated reports:
- TFT_MEMORY_ANALYSIS.md (15-section memory breakdown)
- TFT_MEMORY_QUICK_SUMMARY.md (executive summary)
- TFT_MEMORY_LEAK_ANALYSIS.md (5 critical leaks identified)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-25 23:49:24 +02:00

8.0 KiB

Gradient Checkpointing CLI Usage Guide

Quick Reference: Enable gradient checkpointing to reduce GPU memory usage by 30-40% at cost of ~20% slower training.


Quick Start

FP32 Training (train_tft binary)

# Standard training
cargo run -p ml --bin train_tft --release -- \
  --data test_data/ES_FUT_180d.parquet \
  --epochs 100 \
  --gpu

# With gradient checkpointing (30-40% memory reduction)
cargo run -p ml --bin train_tft --release -- \
  --data test_data/ES_FUT_180d.parquet \
  --epochs 100 \
  --gpu \
  --gradient-checkpointing

Parquet Training (train_tft_parquet example)

# Standard training
cargo run -p ml --example train_tft_parquet --release --features cuda -- \
  --parquet-file test_data/ES_FUT_180d.parquet \
  --epochs 50

# With gradient checkpointing (30-40% memory reduction)
cargo run -p ml --example train_tft_parquet --release --features cuda -- \
  --parquet-file test_data/ES_FUT_180d.parquet \
  --epochs 50 \
  --use-gradient-checkpointing

When to Use Gradient Checkpointing

Scenario Benefit Trade-off
Large models Fit 225 features in 4GB VRAM ~20% slower training
Limited GPU memory RTX 3050 Ti (4GB) users Worth the speed cost
Long sequences Lookback >120 timesteps Prevents OOM errors
Batch size tuning Increase batch from 32→48 Better convergence
Multi-model training Run TFT + MAMBA-2 concurrently Share GPU resources
Scenario Reason Alternative
QAT training Not implemented (workaround required) Use 2-phase approach
Fast iteration 20% slower training time Use FP32 without checkpointing
Large GPU memory RTX 4090 (24GB) has headroom No benefit, just slower
Small models <100 features, <60 lookback No memory pressure
CPU training Already slow, no benefit Use GPU instead

Performance Impact

Memory Reduction

Model Configuration Without Checkpointing With Checkpointing Savings
TFT-225 (batch=32) ~525MB ~315-368MB 30-40%
TFT-225 (batch=48) ~787MB ~472-551MB 30-40%
TFT-201 (batch=32) ~500MB ~300-350MB 30-40%

Training Speed

Model Configuration Without Checkpointing With Checkpointing Overhead
TFT-225 (50 epochs) ~2.0 min ~2.4 min +20%
TFT-201 (50 epochs) ~1.8 min ~2.2 min +22%
TFT-150 (50 epochs) ~1.5 min ~1.8 min +20%

CLI Flags

train_tft (binary)

--gradient-checkpointing
    Enable gradient checkpointing for memory reduction
    Reduces GPU memory usage by 30-40% at cost of ~20% slower training
    Not compatible with QAT (will be ignored if --use-qat is enabled)

train_tft_parquet (example)

--use-gradient-checkpointing
    ⚠️ WARNING: Gradient checkpointing NOT IMPLEMENTED for QAT

    This flag is IGNORED when --use-qat is enabled.
    For QAT memory reduction: Use 2-phase workaround

    For non-QAT training: Reduces GPU memory usage by 30-40%
    but increases training time by ~20%

Real-World Examples

Example 1: 4GB RTX 3050 Ti (Memory-Constrained)

Problem: TFT-225 with batch=32 uses 525MB, leaving little headroom for other processes.

Solution:

cargo run -p ml --example train_tft_parquet --release --features cuda -- \
  --parquet-file test_data/ES_FUT_180d.parquet \
  --epochs 50 \
  --batch-size 32 \
  --use-gradient-checkpointing

Result:

  • Memory: 525MB → 315-368MB (30-40% reduction)
  • Training: 2.0 min → 2.4 min (+20% slower)
  • Enables batch=48 without OOM (better convergence)

Example 2: RTX 4090 24GB (Memory-Rich)

Problem: Plenty of GPU memory available.

Solution: DO NOT USE gradient checkpointing

cargo run -p ml --example train_tft_parquet --release --features cuda -- \
  --parquet-file test_data/ES_FUT_180d.parquet \
  --epochs 50 \
  --batch-size 64  # Larger batch, no checkpointing needed

Result:

  • Memory: ~1.0GB (no problem on 24GB GPU)
  • Training: 1.5 min (20% faster than checkpointing)
  • Best performance without memory constraints

Example 3: QAT Training (Checkpointing NOT Supported)

Problem: Need to use QAT for INT8 quantization.

Solution: Use 2-phase workaround (no CLI flag)

# Phase 1: Calibrate observers (NO checkpointing)
cargo run -p ml --example train_tft_parquet --release --features cuda -- \
  --parquet-file test_data/ES_FUT_180d.parquet \
  --use-qat \
  --qat-calibration-batches 100

# Phase 2: Freeze observers, train with checkpointing (manual)
# See QAT_GRADIENT_CHECKPOINTING_WORKAROUND.md

Result:

  • Phase 1: Observers calibrated (100 batches)
  • Phase 2: Train with frozen stats + checkpointing
  • Workaround required (automatic support not implemented)

Troubleshooting

Issue: Flag Ignored

Symptom: --gradient-checkpointing has no effect on memory usage.

Causes:

  1. QAT enabled: Checkpointing IGNORED with --use-qat
    • Fix: Use 2-phase workaround or disable QAT
  2. Wrong flag name: Used --gradient-checkpointing in train_tft_parquet
    • Fix: Use --use-gradient-checkpointing for parquet example
  3. CPU training: No GPU memory pressure
    • Fix: Use --use-gpu or --gpu flag

Issue: OOM Despite Checkpointing

Symptom: Out of memory error even with --gradient-checkpointing.

Causes:

  1. Batch size too large: Even with checkpointing, batch=64 may exceed 4GB
    • Fix: Reduce to --batch-size 32 or --batch-size 16
  2. Other processes using GPU: CUDA context overhead
    • Fix: Stop other GPU processes (e.g., browsers, display managers)
  3. Very long sequences: Lookback >120 may exceed budget
    • Fix: Reduce --lookback-window 60

Issue: Training Too Slow

Symptom: 50% slower training instead of 20%.

Causes:

  1. Very small batch size: Checkpointing overhead dominates at batch=8
    • Fix: Increase --batch-size 32 for better amortization
  2. CPU bottleneck: Data loading slower than GPU compute
    • Fix: Use Parquet data (10x faster loading)
  3. Debugging enabled: cargo run instead of cargo run --release
    • Fix: Always use --release for benchmarks

Compatibility Matrix

Feature train_tft train_tft_parquet Compatible?
FP32 training Yes
INT8 PTQ Yes
QAT training No (workaround required)
Auto batch size Yes (orthogonal)
Mixed precision Yes (future)
Multi-GPU ⚠️ ⚠️ ⚠️ Untested

FAQ

Q: Does gradient checkpointing affect model accuracy? A: No. It's a memory optimization technique that recomputes activations instead of storing them. Numerically identical outputs.

Q: Can I use gradient checkpointing with QAT? A: Not directly. Use the 2-phase workaround (calibrate → freeze → train with checkpointing). See QAT_GRADIENT_CHECKPOINTING_WORKAROUND.md.

Q: Should I always enable gradient checkpointing? A: No. Only if GPU memory is limited. RTX 4090 users won't benefit (just slower training).

Q: Can I tune the checkpointing frequency? A: Not yet. Currently boolean (all-or-nothing). Future enhancement: --checkpointing-frequency N.

Q: Does this work on CPU? A: Yes, but no benefit. Gradient checkpointing is for GPU memory optimization.


See Also

  • AGENT_GRAD-B6_CLI_INTEGRATION_COMPLETE.md: Full technical report
  • GRADIENT_CHECKPOINTING_QUICK_REFERENCE.md: Implementation details
  • QAT_GRADIENT_CHECKPOINTING_WORKAROUND.md: QAT 2-phase workaround
  • ML_TRAINING_PARQUET_GUIDE.md: Parquet training documentation

Last Updated: 2025-10-25 Agent: GRAD-B6