Repository navigation
[Experimental][rl][vllm compat] Update simple_rl example to work with vLLM nightly - #2219
Conversation
|
cc @wwwjn @acisseJZhong for review |
|
cc @bwasti for review on this actually |
| try: | ||
| init_batch_invariance() | ||
| except TypeError: | ||
| # Fallback to new version requiring positional arg for attention |
There was a problem hiding this comment.
Can we converge to one single way instead of maintaining backward compatible code for vllm?
Is it OK if we always run with latest vllm nightly? @jessicazhongeee @zhxchen17
There was a problem hiding this comment.
Seconding here - if we want to only support nightly we can remove this try/except
| vocab_size=151936, | ||
| ) | ||
| model = Qwen3VLLMCompatModel(model_args) | ||
| model = Qwen3VLLMCompatModel(model_args).to('cuda').to(torch.bfloat16) |
There was a problem hiding this comment.
curious
- why in the past we don't need this but now we need? Is it because previously it was AI-generated and it actually wouldn't work?
- why making it bfloat16? I'm assuming we need to align train and inference, but do we know that inference is everywhere bf16? e.g. how about RMSNorm in vllm.
There was a problem hiding this comment.
- I am not sure why it didn't work, but when I tried running it it did not. These lines were necessary to make the example in the readme run correctly
- This is the dtype that the model is expected in (see https://huggingface.co/Qwen/Qwen3-0.6B model card tensortype). Without it, the code does not run with this error:
File "/home/lucaskabela/torchtitan/torchtitan/experiments/rl/vllm_compat/models/attention.py", line 180, in forward
output_varlen = FlashAttnWithBackward.apply(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lucaskabela/pytorch/torch/autograd/function.py", line 583, in apply
return super().apply(*args, **kwargs) # type: ignore[misc]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lucaskabela/torchtitan/torchtitan/experiments/rl/vllm_compat/models/attention.py", line 78, in forward
output = flash_fn(
^^^^^^^^^
File "/home/lucaskabela/vllm/vllm/vllm_flash_attn/flash_attn_interface.py", line 279, in flash_attn_varlen_func
out, softmax_lse, _, _ = torch.ops._vllm_fa3_C.fwd(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/lucaskabela/pytorch/torch/_ops.py", line 1243, in __call__
return self._op(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: FlashAttention only supports fp16, bf16, and fp8_e4m3 data type
There was a problem hiding this comment.
This is the dtype that the model is expected in (see https://huggingface.co/Qwen/Qwen3-0.6B model card tensortype
This indicating the model weight in the safetensor file is stored as bf16. Looking at the error message, it's complaining the input (activation) of flash_attn kernel needs to be bf16. In torchtitan, we can keep the model weights float32 but make the activations to be bf16
So I also want to understand why we need to do this and explicitly convert model weights to bf16
There was a problem hiding this comment.
From the error, the flash attention backend is not compatible with float32 (which to my understanding was the backend we are using)
| from vllm.model_executor.layers.batch_invariant import init_batch_invariance | ||
|
|
||
| init_batch_invariance() | ||
| try: |
61fae61 to
724ab19
Compare
| vllm_silu_and_mul = VLLMSiluAndMul() | ||
| # vLLM custom ops require a config context to be set | ||
| # Since these are parameter free we instantiate default config | ||
| with set_current_vllm_config(VllmConfig()): |
There was a problem hiding this comment.
For this part I'm not sure , cc @bwasti for review
| gpu_memory_utilization=0.3, # Reduced from 0.5 | ||
| seed=42, # Fixed seed for determinism | ||
| enforce_eager=True, | ||
| attention_config={"backend": AttentionBackendEnum.FLASH_ATTN}, |
There was a problem hiding this comment.
Why we need this AttentionBackendEnum again in vLLM Engine params?
There was a problem hiding this comment.
Requirement of the new API
wwwjn
left a comment
There was a problem hiding this comment.
I tested reinstall from source using vllm latest code, the AttentionBackend changes are necessary
| vocab_size=151936, | ||
| ) | ||
| model = Qwen3VLLMCompatModel(model_args) | ||
| model = Qwen3VLLMCompatModel(model_args).to('cuda').to(torch.bfloat16) |
There was a problem hiding this comment.
From the error, the flash attention backend is not compatible with float32 (which to my understanding was the backend we are using)
| gpu_memory_utilization=0.3, # Reduced from 0.5 | ||
| seed=42, # Fixed seed for determinism | ||
| enforce_eager=True, | ||
| attention_config={"backend": AttentionBackendEnum.FLASH_ATTN}, |
There was a problem hiding this comment.
Requirement of the new API
|
@pytorchbot merge |
|
Mergebot is not configured for this repository. Please use the merge button provided by GitHub. |
… nightly (#2226) NOTE: This PR should be landed on top of #2219 Few changes seem to be needed to get working; namely: 1) Update to use `spawn` method for CUDA fork 2) Modify attention import to be correct after refactor 3) Modify attention dimensions to be correct after refactor ### Test Plan Follow steps in the readme then run ``` python torchtitan/experiments/rl/unified/infer.py --model-ckpt-path torchtitan/experiments/rl/example_checkpoint/Qwen3-0.6B ``` ### Output ``` [2026-01-13 15:20:58] INFO infer.py:113: Generation complete Prompt: Hello, my name is Generated text: " Josh and I'm in the middle of a project to develop a hybrid mobile app. I'm looking for guidance on how to go about using modular frameworks. I want to use React and Vue. I need to decide on the framework to use. Can you help me choose the right framework and suggest some best practices for using them?\n\nAdditionally, I want to know what are the best practices for using a modular framework in the context of a web application? Also, what are the best practices for using a" ```
… vLLM nightly (pytorch#2219) See title; running with the latest version of vLLM nightly ran into a few issues as these APIs have changed and within torch titan some paths have changed ``` VLLM_BATCH_INVARIANT=1 VLLM_FLASH_ATTN_VERSION=3 python -m torchtitan.experiments.rl.vllm_compat.simple_rl ``` ``` ✓ vLLM-TorchTitan bitwise determinism verified: 100 tokens match exactly Step 2 | Loss: -0.0033 | Reward: +0.213 | Samples: 160 Sample: Natalia sold 48 clips in April. In May, she sold half as many as she sold in Ap... Converted to 311 vLLM weights ```
… nightly (pytorch#2226) NOTE: This PR should be landed on top of pytorch#2219 Few changes seem to be needed to get working; namely: 1) Update to use `spawn` method for CUDA fork 2) Modify attention import to be correct after refactor 3) Modify attention dimensions to be correct after refactor Follow steps in the readme then run ``` python torchtitan/experiments/rl/unified/infer.py --model-ckpt-path torchtitan/experiments/rl/example_checkpoint/Qwen3-0.6B ``` ``` [2026-01-13 15:20:58] INFO infer.py:113: Generation complete Prompt: Hello, my name is Generated text: " Josh and I'm in the middle of a project to develop a hybrid mobile app. I'm looking for guidance on how to go about using modular frameworks. I want to use React and Vue. I need to decide on the framework to use. Can you help me choose the right framework and suggest some best practices for using them?\n\nAdditionally, I want to know what are the best practices for using a modular framework in the context of a web application? Also, what are the best practices for using a" ```
…LLM version (#2359) ## Summary We make a handful of QOL changes to make `simple_rl_multiprocess.py` work with recent vLLM. These are: 1) Update to `README.md` to add monarch requirement 2) `AttentionBackendEnum` forwarded in locations needed (similar to #2219) 3) Wiring through `enable_gqa` support in vllm_compat attention - this requires reshaping when `q.shape[1]` != `k.shape[1]` and using an output tensor explicitly with the proper shape With these changes, we are able to run the simple_rl_multiprocess script again ## Test Plan ```bash VLLM_BATCH_INVARIANT=1 VLLM_ATTENTION_BACKEND=FLASH_ATTN python3 torchtitan/experiments/rl/unified/simple_rl_multiprocess.py ``` ### Before (Main) ```bash File "/home/lucaskabela/.conda/envs/vllm/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/home/lucaskabela/torchtitan/torchtitan/experiments/rl/unified/simple_rl_multiprocess.py", line 66, in main init_batch_invariance() TypeError: init_batch_invariance() missing 1 required positional argument: 'attention_backend' ``` And after patching with the enum: ``` File "/home/lucaskabela/pytorch/torch/nn/modules/module.py", line 1779, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/lucaskabela/pytorch/torch/nn/modules/module.py", line 1790, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: VLLMCompatibleFlashAttention.forward() got an unexpected keyword argument 'enable_gqa' ``` ### After ```bash Adding requests: 100%|█████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 2935.95it/s] Processed prompts: 100%|███████████████| 40/40 [00:01<00:00, 27.61it/s, est. speed input: 193.29 toks/s, output: 552.25 toks/s] [2026-02-09 16:11:59] INFO generator.py:446: [actor=<root>.<torchtitan.experiments.rl.unified.actors.generator.Generator generator{'gpus': 0/1}>] os.getpid()=300001 Generating finish generate (policy v0)... [2026-02-09 16:11:59] INFO trainer.py:101: [actor=<root>.<torchtitan.experiments.rl.unified.actors.trainer.Trainer trainer{'gpus': 1/2}>] os.getpid()=300426 Trainer starts to train 0 on traj: [2026-02-09 16:11:59] INFO trainer.py:101: [actor=<root>.<torchtitan.experiments.rl.unified.actors.trainer.Trainer trainer{'gpus': 0/2}>] os.getpid()=299553 Trainer starts to train 0 on traj: NCCL version 2.28.9+cuda12.9 ✓ vLLM-TorchTitan bitwise determinism verified: 20 tokens match exactly ✓ vLLM-TorchTitan bitwise determinism verified: 20 tokens match exactly ```
… vLLM nightly (pytorch#2219) See title; running with the latest version of vLLM nightly ran into a few issues as these APIs have changed and within torch titan some paths have changed ### Test plan ``` VLLM_BATCH_INVARIANT=1 VLLM_FLASH_ATTN_VERSION=3 python -m torchtitan.experiments.rl.vllm_compat.simple_rl ``` ### Results ``` ✓ vLLM-TorchTitan bitwise determinism verified: 100 tokens match exactly Step 2 | Loss: -0.0033 | Reward: +0.213 | Samples: 160 Sample: Natalia sold 48 clips in April. In May, she sold half as many as she sold in Ap... Converted to 311 vLLM weights ```
… nightly (pytorch#2226) NOTE: This PR should be landed on top of pytorch#2219 Few changes seem to be needed to get working; namely: 1) Update to use `spawn` method for CUDA fork 2) Modify attention import to be correct after refactor 3) Modify attention dimensions to be correct after refactor ### Test Plan Follow steps in the readme then run ``` python torchtitan/experiments/rl/unified/infer.py --model-ckpt-path torchtitan/experiments/rl/example_checkpoint/Qwen3-0.6B ``` ### Output ``` [2026-01-13 15:20:58] INFO infer.py:113: Generation complete Prompt: Hello, my name is Generated text: " Josh and I'm in the middle of a project to develop a hybrid mobile app. I'm looking for guidance on how to go about using modular frameworks. I want to use React and Vue. I need to decide on the framework to use. Can you help me choose the right framework and suggest some best practices for using them?\n\nAdditionally, I want to know what are the best practices for using a modular framework in the context of a web application? Also, what are the best practices for using a" ```
… vLLM nightly (pytorch#2219) See title; running with the latest version of vLLM nightly ran into a few issues as these APIs have changed and within torch titan some paths have changed ### Test plan ``` VLLM_BATCH_INVARIANT=1 VLLM_FLASH_ATTN_VERSION=3 python -m torchtitan.experiments.rl.vllm_compat.simple_rl ``` ### Results ``` ✓ vLLM-TorchTitan bitwise determinism verified: 100 tokens match exactly Step 2 | Loss: -0.0033 | Reward: +0.213 | Samples: 160 Sample: Natalia sold 48 clips in April. In May, she sold half as many as she sold in Ap... Converted to 311 vLLM weights ```
… nightly (pytorch#2226) NOTE: This PR should be landed on top of pytorch#2219 Few changes seem to be needed to get working; namely: 1) Update to use `spawn` method for CUDA fork 2) Modify attention import to be correct after refactor 3) Modify attention dimensions to be correct after refactor ### Test Plan Follow steps in the readme then run ``` python torchtitan/experiments/rl/unified/infer.py --model-ckpt-path torchtitan/experiments/rl/example_checkpoint/Qwen3-0.6B ``` ### Output ``` [2026-01-13 15:20:58] INFO infer.py:113: Generation complete Prompt: Hello, my name is Generated text: " Josh and I'm in the middle of a project to develop a hybrid mobile app. I'm looking for guidance on how to go about using modular frameworks. I want to use React and Vue. I need to decide on the framework to use. Can you help me choose the right framework and suggest some best practices for using them?\n\nAdditionally, I want to know what are the best practices for using a modular framework in the context of a web application? Also, what are the best practices for using a" ```
…LLM version (pytorch#2359) ## Summary We make a handful of QOL changes to make `simple_rl_multiprocess.py` work with recent vLLM. These are: 1) Update to `README.md` to add monarch requirement 2) `AttentionBackendEnum` forwarded in locations needed (similar to pytorch#2219) 3) Wiring through `enable_gqa` support in vllm_compat attention - this requires reshaping when `q.shape[1]` != `k.shape[1]` and using an output tensor explicitly with the proper shape With these changes, we are able to run the simple_rl_multiprocess script again ## Test Plan ```bash VLLM_BATCH_INVARIANT=1 VLLM_ATTENTION_BACKEND=FLASH_ATTN python3 torchtitan/experiments/rl/unified/simple_rl_multiprocess.py ``` ### Before (Main) ```bash File "/home/lucaskabela/.conda/envs/vllm/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/home/lucaskabela/torchtitan/torchtitan/experiments/rl/unified/simple_rl_multiprocess.py", line 66, in main init_batch_invariance() TypeError: init_batch_invariance() missing 1 required positional argument: 'attention_backend' ``` And after patching with the enum: ``` File "/home/lucaskabela/pytorch/torch/nn/modules/module.py", line 1779, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/lucaskabela/pytorch/torch/nn/modules/module.py", line 1790, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: VLLMCompatibleFlashAttention.forward() got an unexpected keyword argument 'enable_gqa' ``` ### After ```bash Adding requests: 100%|█████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 2935.95it/s] Processed prompts: 100%|███████████████| 40/40 [00:01<00:00, 27.61it/s, est. speed input: 193.29 toks/s, output: 552.25 toks/s] [2026-02-09 16:11:59] INFO generator.py:446: [actor=<root>.<torchtitan.experiments.rl.unified.actors.generator.Generator generator{'gpus': 0/1}>] os.getpid()=300001 Generating finish generate (policy v0)... [2026-02-09 16:11:59] INFO trainer.py:101: [actor=<root>.<torchtitan.experiments.rl.unified.actors.trainer.Trainer trainer{'gpus': 1/2}>] os.getpid()=300426 Trainer starts to train 0 on traj: [2026-02-09 16:11:59] INFO trainer.py:101: [actor=<root>.<torchtitan.experiments.rl.unified.actors.trainer.Trainer trainer{'gpus': 0/2}>] os.getpid()=299553 Trainer starts to train 0 on traj: NCCL version 2.28.9+cuda12.9 ✓ vLLM-TorchTitan bitwise determinism verified: 20 tokens match exactly ✓ vLLM-TorchTitan bitwise determinism verified: 20 tokens match exactly ```
… vLLM nightly (pytorch#2219) See title; running with the latest version of vLLM nightly ran into a few issues as these APIs have changed and within torch titan some paths have changed ### Test plan ``` VLLM_BATCH_INVARIANT=1 VLLM_FLASH_ATTN_VERSION=3 python -m torchtitan.experiments.rl.vllm_compat.simple_rl ``` ### Results ``` ✓ vLLM-TorchTitan bitwise determinism verified: 100 tokens match exactly Step 2 | Loss: -0.0033 | Reward: +0.213 | Samples: 160 Sample: Natalia sold 48 clips in April. In May, she sold half as many as she sold in Ap... Converted to 311 vLLM weights ```
… nightly (pytorch#2226) NOTE: This PR should be landed on top of pytorch#2219 Few changes seem to be needed to get working; namely: 1) Update to use `spawn` method for CUDA fork 2) Modify attention import to be correct after refactor 3) Modify attention dimensions to be correct after refactor ### Test Plan Follow steps in the readme then run ``` python torchtitan/experiments/rl/unified/infer.py --model-ckpt-path torchtitan/experiments/rl/example_checkpoint/Qwen3-0.6B ``` ### Output ``` [2026-01-13 15:20:58] INFO infer.py:113: Generation complete Prompt: Hello, my name is Generated text: " Josh and I'm in the middle of a project to develop a hybrid mobile app. I'm looking for guidance on how to go about using modular frameworks. I want to use React and Vue. I need to decide on the framework to use. Can you help me choose the right framework and suggest some best practices for using them?\n\nAdditionally, I want to know what are the best practices for using a modular framework in the context of a web application? Also, what are the best practices for using a" ```
…LLM version (pytorch#2359) ## Summary We make a handful of QOL changes to make `simple_rl_multiprocess.py` work with recent vLLM. These are: 1) Update to `README.md` to add monarch requirement 2) `AttentionBackendEnum` forwarded in locations needed (similar to pytorch#2219) 3) Wiring through `enable_gqa` support in vllm_compat attention - this requires reshaping when `q.shape[1]` != `k.shape[1]` and using an output tensor explicitly with the proper shape With these changes, we are able to run the simple_rl_multiprocess script again ## Test Plan ```bash VLLM_BATCH_INVARIANT=1 VLLM_ATTENTION_BACKEND=FLASH_ATTN python3 torchtitan/experiments/rl/unified/simple_rl_multiprocess.py ``` ### Before (Main) ```bash File "/home/lucaskabela/.conda/envs/vllm/lib/python3.12/asyncio/base_events.py", line 691, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/home/lucaskabela/torchtitan/torchtitan/experiments/rl/unified/simple_rl_multiprocess.py", line 66, in main init_batch_invariance() TypeError: init_batch_invariance() missing 1 required positional argument: 'attention_backend' ``` And after patching with the enum: ``` File "/home/lucaskabela/pytorch/torch/nn/modules/module.py", line 1779, in _wrapped_call_impl return self._call_impl(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/lucaskabela/pytorch/torch/nn/modules/module.py", line 1790, in _call_impl return forward_call(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: VLLMCompatibleFlashAttention.forward() got an unexpected keyword argument 'enable_gqa' ``` ### After ```bash Adding requests: 100%|█████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 2935.95it/s] Processed prompts: 100%|███████████████| 40/40 [00:01<00:00, 27.61it/s, est. speed input: 193.29 toks/s, output: 552.25 toks/s] [2026-02-09 16:11:59] INFO generator.py:446: [actor=<root>.<torchtitan.experiments.rl.unified.actors.generator.Generator generator{'gpus': 0/1}>] os.getpid()=300001 Generating finish generate (policy v0)... [2026-02-09 16:11:59] INFO trainer.py:101: [actor=<root>.<torchtitan.experiments.rl.unified.actors.trainer.Trainer trainer{'gpus': 1/2}>] os.getpid()=300426 Trainer starts to train 0 on traj: [2026-02-09 16:11:59] INFO trainer.py:101: [actor=<root>.<torchtitan.experiments.rl.unified.actors.trainer.Trainer trainer{'gpus': 0/2}>] os.getpid()=299553 Trainer starts to train 0 on traj: NCCL version 2.28.9+cuda12.9 ✓ vLLM-TorchTitan bitwise determinism verified: 20 tokens match exactly ✓ vLLM-TorchTitan bitwise determinism verified: 20 tokens match exactly ```
See title; running with the latest version of vLLM nightly ran into a few issues as these APIs have changed and within torch titan some paths have changed
Test plan
Results