-
Notifications
You must be signed in to change notification settings - Fork 596
Add deterministic RL training experiment with vLLM #1975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+3,368
−0
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5fc60f8
Add deterministic RL training experiment with vLLM
bwasti 2823b41
Add missing weight conversion files and fix imports
teja-rao aea9af1
Address review comments
teja-rao 62b20b6
Remove redundant round-trip weight loading
teja-rao 3c18115
Optimize vLLM weight reloading using collective_rpc
teja-rao 7be6112
Add comprehensive gradient checking and debugging
bwasti 602e92e
Fix missing gradients for FFN layers by adding SiluAndMul backward pass
bwasti d53740a
Remove gradient debugging statements
bwasti 53b56a5
Update readme
bwasti 90bc444
Cleanup readme and remove unneeded flashv3 backward
bwasti ae4ebd3
lint
bwasti 5c00c9a
More cleanup
bwasti 1d41a08
Remove extra function
bwasti 44c9946
add better loss controls and a real dataset
bwasti 3268821
Update torchtitan/experiments/deterministic_vllm_rl/models/qwen3/mode…
bwasti 24f4cca
Update README.md
bwasti e502c65
Improve reference log prob computation and increase max tokens
teja-rao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add deterministic RL training experiment with vLLM
This experiment provides a complete framework for bitwise-deterministic reinforcement learning training that combines vLLM for fast rollouts and TorchTitan for training with gradients. Key features: - Bitwise-deterministic forward and backward passes - vLLM-compatible Qwen3 model with merged projections - Custom Flash Attention with gradient support - Gradient support for vLLM's batch-invariant operations - Complete RL training loop with GRPO-style advantages - Comprehensive test suite for determinism verification Components: - models/attention.py: VLLMCompatibleFlashAttention - models/qwen3/model_vllm_compat.py: vLLM-compatible Qwen3 model - batch_invariant_backward.py: Gradient support for vLLM operations - simple_rl.py: End-to-end RL training loop - tests/: Test suite for backward passes and determinism
- Loading branch information
commit 5fc60f823f49e36d3d2d2cce5898a206be7a9be5
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| # Deterministic RL Training with vLLM | ||
|
|
||
| This experiment provides a complete framework for **bitwise-deterministic reinforcement learning training** that combines: | ||
| - **vLLM** for fast, deterministic rollouts | ||
| - **TorchTitan** for efficient training with gradients | ||
| - **Custom backward passes** to maintain determinism through the entire training loop | ||
|
|
||
| ## Overview | ||
|
|
||
| Traditional RL training faces a challenge: you want fast inference for generating rollouts, but you also need gradients for training. vLLM is extremely fast but doesn't support gradients. Standard PyTorch supports gradients but can be non-deterministic. | ||
|
|
||
| This experiment solves both problems by: | ||
| 1. Using vLLM's deterministic kernels for forward passes (both rollouts and training) | ||
| 2. Adding custom backward passes that are also deterministic | ||
| 3. Achieving **bitwise-identical results** across runs for the entire training loop | ||
|
|
||
| ### Key Features | ||
|
|
||
| - **Bitwise Determinism**: Same inputs always produce identical outputs (bit-for-bit) | ||
| - **vLLM Speed**: Fast rollouts using vLLM's optimized kernels | ||
| - **Gradient Support**: Full backward pass support for training | ||
| - **Model Compatibility**: Drop-in replacement for standard Qwen3 models in TorchTitan | ||
|
|
||
| ## Architecture | ||
|
|
||
| ### Components | ||
|
|
||
| 1. **`models/attention.py`**: VLLMCompatibleFlashAttention | ||
| - Uses vLLM's Flash Attention for forward pass | ||
| - Implements custom backward pass for gradients | ||
| - Maintains determinism with `num_splits=1` | ||
|
|
||
| 2. **`models/qwen3/model_vllm_compat.py`**: Qwen3VLLMCompatModel | ||
| - vLLM-compatible Qwen3 implementation | ||
| - Merged gate/up projections (like vLLM) | ||
| - Uses VLLMRMSNorm with gradient support | ||
|
|
||
| 3. **`batch_invariant_backward.py`**: Gradient support for vLLM operations | ||
| - Registers backward passes for vLLM's batch-invariant operations | ||
| - Supports matmul, linear, and RMSNorm | ||
| - Patches Flash Attention to work with autograd | ||
|
|
||
| 4. **`simple_rl.py`**: End-to-end RL training loop | ||
| - Generates rollouts using vLLM | ||
| - Computes advantages using GRPO-style ranking | ||
| - Updates policy using PPO with bitwise-deterministic gradients | ||
|
|
||
| ## Installation | ||
|
|
||
| ### Prerequisites | ||
|
|
||
| ```bash | ||
| # Install vLLM with deterministic support | ||
| pip install vllm | ||
|
|
||
| # Install TorchTitan (from the repository root) | ||
| pip install -e . | ||
|
|
||
| # Install additional dependencies | ||
| pip install transformers safetensors huggingface_hub tensorboard | ||
| ``` | ||
|
|
||
| ### Enable Batch Invariance | ||
|
|
||
| Before running any training, you must initialize vLLM's batch-invariant mode: | ||
|
|
||
| ```python | ||
| from vllm.model_executor.layers.batch_invariant import init_batch_invariance | ||
| init_batch_invariance() | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Quick Start | ||
|
|
||
| ```python | ||
| import torch | ||
| from vllm.model_executor.layers.batch_invariant import init_batch_invariance | ||
| from torchtitan.experiments.deterministic_vllm_rl import ( | ||
| patch_batch_invariant_with_gradients, | ||
| Qwen3VLLMCompatModel, | ||
| ) | ||
|
|
||
| # 1. Enable deterministic mode | ||
| init_batch_invariance() | ||
| patch_batch_invariant_with_gradients() | ||
|
|
||
| # 2. Load model | ||
| from torchtitan.models.qwen3.model.args import Qwen3ModelArgs | ||
| model_args = Qwen3ModelArgs( | ||
| dim=2048, | ||
| n_layers=24, | ||
| n_heads=16, | ||
| n_kv_heads=2, | ||
| vocab_size=151936, | ||
| ) | ||
| model = Qwen3VLLMCompatModel(model_args) | ||
|
|
||
| # 3. Forward pass (deterministic) | ||
| input_ids = torch.randint(0, 151936, (2, 128), device='cuda') | ||
| logits = model(input_ids) | ||
|
|
||
| # 4. Backward pass (also deterministic!) | ||
| loss = logits.sum() | ||
| loss.backward() | ||
| ``` | ||
|
|
||
| ### Full RL Training | ||
|
|
||
| Run the complete RL training loop: | ||
|
|
||
| ```bash | ||
| cd torchtitan/experiments/deterministic_vllm_rl | ||
| python simple_rl.py | ||
| ``` | ||
|
|
||
| This will: | ||
| 1. Download Qwen3-1.7B from HuggingFace | ||
| 2. Initialize vLLM engine for rollouts | ||
| 3. Generate samples for training prompts | ||
| 4. Compute rewards and advantages | ||
| 5. Update the policy using PPO | ||
| 6. Log metrics to TensorBoard | ||
|
|
||
| View training progress: | ||
| ```bash | ||
| tensorboard --logdir=./outputs/rl_training | ||
| ``` | ||
|
|
||
| ## How It Works | ||
|
|
||
| ### Deterministic Forward Pass | ||
|
|
||
| vLLM's batch-invariant mode ensures that all operations are deterministic: | ||
|
|
||
| ```python | ||
| # These operations are deterministic when batch_invariance is enabled | ||
| y = torch.matmul(a, b) # Uses vLLM's deterministic matmul | ||
| output = flash_attn_varlen_func(q, k, v, num_splits=1) # Deterministic FA | ||
| ``` | ||
|
|
||
| ### Backward Pass with Gradients | ||
|
|
||
| We add custom backward passes that: | ||
| 1. Re-compute attention weights (deterministic) | ||
| 2. Use standard chain rule for gradients | ||
| 3. Apply gradients through vLLM's deterministic operations | ||
|
|
||
| ```python | ||
| class FlashAttnWithBackward(torch.autograd.Function): | ||
| @staticmethod | ||
| def forward(ctx, q, k, v, ...): | ||
| # Use vLLM's fast forward | ||
| return flash_attn_varlen_func(q, k, v, num_splits=1, ...) | ||
|
|
||
| @staticmethod | ||
| def backward(ctx, grad_output): | ||
| # Compute gradients deterministically | ||
| # (re-compute attention weights and apply chain rule) | ||
| return grad_q, grad_k, grad_v, ... | ||
| ``` | ||
|
|
||
| ### Bitwise Determinism Verification | ||
|
|
||
| The training loop verifies that vLLM and TorchTitan produce identical logprobs: | ||
|
|
||
| ```python | ||
| # During training, compare logprobs | ||
| vllm_logprobs = [from vLLM rollout] | ||
| titan_logprobs = [from TorchTitan forward pass] | ||
|
|
||
| assert torch.equal(vllm_logprobs, titan_logprobs) # Should be true! | ||
| ``` | ||
|
|
||
| ## Testing | ||
|
|
||
| Run the test suite to verify determinism: | ||
|
|
||
| ```bash | ||
| cd torchtitan/experiments/deterministic_vllm_rl/tests | ||
|
|
||
| # Test backward passes work correctly | ||
| python test_batch_invariant_backward.py | ||
|
|
||
| # Test exact determinism (bit-for-bit identical) | ||
| python test_exact_determinism.py | ||
| ``` | ||
|
|
||
| Expected output: | ||
| ``` | ||
| ✓ All operations are exactly deterministic! | ||
| ✓ vLLM-TorchTitan bitwise determinism verified: N tokens match exactly | ||
| ``` | ||
|
|
||
| ## Technical Details | ||
|
|
||
| ### Why Determinism Matters for RL | ||
|
|
||
| In RL training, we need to: | ||
| 1. Generate rollouts (sampling from the policy) | ||
| 2. Compute rewards based on the samples | ||
| 3. Update the policy using gradients | ||
|
|
||
| **The problem**: If the forward pass during training differs from the forward pass during rollout, the gradients will be wrong! This is especially important for PPO, which compares old and new policy probabilities. | ||
|
|
||
| **The solution**: Use the same deterministic kernels for both rollouts (vLLM) and training (TorchTitan). This ensures that `logprobs_rollout == logprobs_training` (bitwise). | ||
|
|
||
| ### Performance | ||
|
|
||
| - **Rollout speed**: ~100x faster than standard PyTorch (thanks to vLLM) | ||
|
||
| - **Training speed**: Same as standard TorchTitan | ||
| - **Memory**: Slightly higher (saves activations for custom backward) | ||
|
|
||
| ### Limitations | ||
|
|
||
| 1. **Sequence length**: Custom backward requires uniform sequence lengths | ||
| 2. **Attention**: Only causal attention is supported | ||
| 3. **Hardware**: Requires NVIDIA GPUs with Flash Attention support | ||
|
|
||
| ## Project Structure | ||
bwasti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ``` | ||
| deterministic_vllm_rl/ | ||
| ├── README.md # This file | ||
| ├── __init__.py # Package initialization | ||
| ├── batch_invariant_backward.py # Gradient support for vLLM ops | ||
| ├── simple_rl.py # End-to-end RL training loop | ||
| ├── models/ | ||
| │ ├── __init__.py | ||
| │ ├── attention.py # VLLMCompatibleFlashAttention | ||
| │ └── qwen3/ | ||
| │ ├── __init__.py | ||
| │ └── model_vllm_compat.py # vLLM-compatible Qwen3 model | ||
| └── tests/ | ||
| ├── __init__.py | ||
| ├── test_batch_invariant_backward.py # Test gradients work | ||
| └── test_exact_determinism.py # Test bitwise determinism | ||
| ``` | ||
|
|
||
| ## Contributing | ||
|
|
||
| This experiment is part of TorchTitan. To contribute: | ||
|
|
||
| 1. Test your changes with `pytest tests/` | ||
| 2. Verify bitwise determinism is maintained | ||
| 3. Update this README if adding new features | ||
|
|
||
| ## References | ||
|
|
||
| - [vLLM Documentation](https://docs.vllm.ai/) | ||
| - [Flash Attention Paper](https://arxiv.org/abs/2205.14135) | ||
| - [PPO Algorithm](https://arxiv.org/abs/1707.06347) | ||
| - [GRPO: Group Relative Policy Optimization](https://arxiv.org/abs/2402.03300) | ||
|
|
||
| ## License | ||
|
|
||
| This code is licensed under the BSD-style license found in the LICENSE file in the TorchTitan repository root directory. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| """ | ||
| Deterministic RL training with vLLM experiment. | ||
|
|
||
| This experiment provides tools for bitwise-deterministic reinforcement learning | ||
| training using vLLM for fast rollouts and TorchTitan for training. | ||
|
|
||
| Key components: | ||
| - VLLMCompatibleFlashAttention: Flash attention with custom backward pass | ||
| - Qwen3VLLMCompatModel: vLLM-compatible model with merged projections | ||
| - batch_invariant_backward: Gradient support for vLLM's deterministic operations | ||
| - simple_rl: End-to-end RL training loop | ||
| """ | ||
|
|
||
| from .batch_invariant_backward import ( | ||
| patch_batch_invariant_with_gradients, | ||
| rms_norm_with_gradients, | ||
| ) | ||
| from .models import VLLMCompatibleFlashAttention | ||
| from .models.qwen3 import Qwen3VLLMCompatModel | ||
|
|
||
| __all__ = [ | ||
| "VLLMCompatibleFlashAttention", | ||
| "Qwen3VLLMCompatModel", | ||
| "patch_batch_invariant_with_gradients", | ||
| "rms_norm_with_gradients", | ||
| ] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we mention that this only works for single device right now, and we plan to extend it to work with parallelisms?