Skip to content

Latest commit

 

History

History
274 lines (192 loc) · 6.6 KB

File metadata and controls

274 lines (192 loc) · 6.6 KB

JAX Acceleration Quick Start Guide

This guide helps you quickly get started with the JAX-accelerated path flexibility computations.

🚀 What's New

JAX acceleration has been integrated to dramatically speed up path flexibility computations in Experiment 2.

Performance Improvements:

  • Horizon=3 (125 policies): 60-100x faster
  • Horizon=4 (625 policies): 100x faster ⚡⚡
  • Horizon=5 (3125 policies): Now feasible! (was impossible before) ⚡⚡⚡

📦 Installation

JAX is already in requirements.txt. If you need to install it separately:

# CPU-only (fastest install)
pip install jax

# GPU support (CUDA 12)
pip install jax[cuda12]

# Or for older CUDA versions, see: https://jax.readthedocs.io/en/latest/installation.html

✅ Verify Installation

# Run the benchmark
python benchmark_jax_speedup.py --horizon 3

# Run correctness tests
pytest tests/test_jax_correctness.py -v

Expected output:

✓ Results match!
Speedup: 66.7x 🚀

🎯 Usage

Option 1: Default Behavior (Recommended)

JAX is enabled by default. Just run your experiments normally:

python experiments/exp2_flex_prior.py

You'll see log messages like:

INFO - JAX path flexibility module loaded successfully
INFO - Using JAX-accelerated flexibility computation

Option 2: Programmatic Control

from src.config import use_jax, enable_jax, disable_jax

# Check if JAX is enabled
if use_jax():
    print("JAX enabled")

# Disable for debugging
disable_jax()
run_experiment()

# Re-enable
enable_jax()

Option 3: Environment Variables

# Disable JAX (use NumPy)
export USE_JAX=0
python experiments/exp2_flex_prior.py

# Force CPU (no GPU)
export JAX_FORCE_CPU=1
python experiments/exp2_flex_prior.py

# Limit GPU memory
export JAX_MEMORY_FRACTION=0.5
python experiments/exp2_flex_prior.py

📊 Benchmarking Your System

Test different horizons to see the speedup on your hardware:

# Horizon=1 (5 policies) - baseline
python benchmark_jax_speedup.py --horizon 1

# Horizon=3 (125 policies) - good speedup
python benchmark_jax_speedup.py --horizon 3

# Horizon=4 (625 policies) - dramatic speedup!
python benchmark_jax_speedup.py --horizon 4 --num-runs 3

# Horizon=5 (3125 policies) - only feasible with JAX!
python benchmark_jax_speedup.py --horizon 5 --num-runs 1

🐛 Troubleshooting

Issue 1: "No module named 'jax'"

Solution:

pip install jax

Issue 2: JAX is slower than NumPy

Causes:

  1. First run includes JIT compilation overhead
  2. Small problem sizes (horizon=1 or 2)
  3. CPU vs GPU mismatch

Solutions:

  • Run experiments for longer (JIT compilation happens once)
  • Use horizon ≥ 3 to see real benefits
  • Check GPU availability: python -c "import jax; print(jax.devices())"

Issue 3: GPU out of memory

Solution:

# Limit GPU memory
export JAX_MEMORY_FRACTION=0.5
python your_script.py

# Or force CPU
export JAX_FORCE_CPU=1
python your_script.py

Issue 4: JAX and NumPy results differ

This shouldn't happen! If it does:

# Run correctness tests
pytest tests/test_jax_correctness.py -v

# Report issue with details

All tests should pass with numerical tolerance < 1e-5.

🔍 Understanding the Logs

JAX Enabled (Default)

INFO - JAX path flexibility module loaded successfully
INFO - κ=0.5: Computing F-aware policy prior
INFO - Warming up JAX JIT compilation...
INFO - JAX warmup complete!
INFO - Using JAX-accelerated flexibility computation
INFO - [JAX] Computing F for 125 policies (vectorized)
INFO - [JAX] Flexibility computation complete

JAX Disabled (NumPy Fallback)

INFO - κ=0.5: Computing F-aware policy prior
INFO - Using NumPy flexibility computation
INFO - Computing F for 125 policies

📈 Performance Tips

1. JIT Warmup

The first JAX call includes compilation overhead (1-5 seconds). Subsequent calls are fast.

Automatic warmup happens in run_tom_step_with_F_prior() on first use.

2. GPU vs CPU

  • GPU: Best for horizon ≥ 3
  • CPU: Actually fine for horizon ≤ 2, may be faster due to no data transfer

Check what you're using:

import jax
print(jax.devices())  # [CudaDevice(id=0)] or [CpuDevice(id=0)]

3. Batch Size

JAX shines with large policy sets. Speedup scales with number of policies:

  • 5 policies: ~2-5x
  • 25 policies: ~10-20x
  • 125 policies: ~50-100x
  • 625 policies: ~100-500x

🧪 Testing Your Changes

If you modify the JAX code:

# Test correctness
pytest tests/test_jax_correctness.py -v

# Test performance
python benchmark_jax_speedup.py --horizon 3

# Run all tests
pytest tests/ -v

📚 Code Structure

New files:

  • src/metrics/jax_path_flexibility.py - JAX implementations
  • src/config.py - Configuration system
  • tests/test_jax_correctness.py - Correctness tests
  • benchmark_jax_speedup.py - Benchmark script

Modified files:

  • src/tom/si_tom_F_prior.py - Integrated JAX (lines 32-95, 231-293)
  • README.md - Added JAX section (section 3.2)
  • src/__init__.py - Exports config functions
  • src/metrics/__init__.py - Exports JAX functions

Preserved files:

  • src/metrics/path_flexibility.py - Original NumPy (unchanged!)
  • src/metrics/empowerment.py - Original NumPy (unchanged!)

❓ FAQ

Q: Will this break my existing code? A: No! JAX is a drop-in replacement. If JAX isn't available, it falls back to NumPy automatically.

Q: Can I use both JAX and NumPy? A: Yes! The config can be toggled at runtime: enable_jax() / disable_jax()

Q: Does this change the results? A: No! JAX produces numerically identical results to NumPy (tested to 1e-5 tolerance).

Q: Do I need a GPU? A: No! JAX works on CPU too. GPU just makes it even faster.

Q: What if I want to disable JAX permanently? A: Edit src/config.py and change use_jax: bool = True to use_jax: bool = False

Q: Can I contribute improvements? A: Yes! See the existing JAX implementations as templates. Make sure to add tests!

🎓 Learn More

🎉 Next Steps

  1. Run the benchmark to verify speedup on your machine
  2. Run your experiments with default JAX acceleration
  3. Try horizon=4 or 5 (now feasible!)
  4. Check the results to ensure everything works as expected

That's it! JAX acceleration is ready to use. Enjoy the speedup! 🚀