Skip to content

[rl] batch invariant mode under same parallelism - #2683

Merged
wwwjn merged 39 commits into
mainfrom
rl-batch-invariant
Apr 6, 2026
Merged

wwwjn merged 39 commits into
mainfrom
rl-batch-invariant

Conversation

@wwwjn

@wwwjn wwwjn commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

What changed

  • Currently only support both trainer and generator using bfloat16. (working on consolidating the details of dtype conversion, but currently FA3 kernel only support bf16/f16)
  • Tested with compile("eager") and cudagraph("piecewise"), the bitwise identity are still preserved.

Test

TP=2 on both trainer and generator:

  • Verified after enabling batch-invariant mode, run-to-run deterministic is also so enabled as prerequisite .
[actor=<root>] Eval: Accuracy=20% (4/20) Format=20% (4/20)
[actor=<root>] ================================================================================
[actor=<root>] Starting RL training for 10 steps
[actor=<root>] ================================================================================
[actor=<root>] Weight sync: push=0.198s, total=0.827s
[actor=<root>] Step  0 | Loss: -0.0041 | Reward: -0.100 | Correct: 18/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 52.1s
[actor=<root>] Weight sync: push=0.203s, total=0.400s
[actor=<root>] Step  1 | Loss: -0.0047 | Reward: -0.350 | Correct: 13/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.8s
[actor=<root>] Weight sync: push=0.197s, total=0.384s
[actor=<root>] Step  2 | Loss: -0.0067 | Reward: -0.300 | Correct: 14/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.6s
[actor=<root>] Weight sync: push=0.196s, total=0.414s
[actor=<root>] Step  3 | Loss: -0.0064 | Reward: -0.100 | Correct: 18/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.3s
[actor=<root>] Weight sync: push=0.204s, total=0.413s
[actor=<root>] Step  4 | Loss: -0.0084 | Reward: -0.400 | Correct: 12/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.7s
[actor=<root>] Weight sync: push=0.211s, total=0.413s
[actor=<root>] Step  5 | Loss: -0.0120 | Reward: +0.300 | Correct: 26/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.1s
[actor=<root>] Weight sync: push=0.186s, total=0.385s
[actor=<root>] Step  6 | Loss: -0.0051 | Reward: -0.350 | Correct: 13/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.6s
[actor=<root>] Weight sync: push=0.223s, total=0.426s
[actor=<root>] Step  7 | Loss: -0.0058 | Reward: -0.600 | Correct:  8/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.9s
[actor=<root>] Weight sync: push=0.206s, total=0.408s
[actor=<root>] Step  8 | Loss: -0.0075 | Reward: -0.250 | Correct: 15/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.5s
[actor=<root>] Weight sync: push=0.200s, total=0.402s
[actor=<root>] Step  9 | Loss: -0.0124 | Reward: +0.100 | Correct: 22/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.8s
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=35% (7/20) Format=50% (10/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=20% (4/20) Format=20% (4/20)
[actor=<root>] Post-training: Accuracy=35% (7/20) Format=50% (10/20)
[actor=<root>] ================================================================================

Config for bit-wise identity:

  • RLTrainer.debug is set from config_registry. When batch_invariant_mode=True, we check deterministic must be true, and dtype on both trainer and generator needs to be bfloat16 in __post__init
    • PolicyTrainer.debug is set by RLTrainer.debug
      • PolicyTrainer model's inner_attn (VarlenAttention) has a batch_invariant flag, this flag is set by PolicyTrainer
    • Generator.debug is set by RLTrainer.debug
      • Generator model's inner_attn (VLLMAttentionWrapper) enables batch_invariant mode by checking is_batch_invariant_mode_enabled() global variable.

Attention Part

Attention changes (for both VarlenAttention in core torchtitan, and VLLMAttentionWrapper in RL):

  • If the hardware is Hopper, always use FA3 attention. If not, use FA2 for Blackwell hardware.
  • If we are using FA3 attention, then batch-invariant is enabled by setting num_split=1
  • If we are using FA2 attention, batch-invariant is automatically supported.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 24, 2026
@wwwjn wwwjn changed the title [WIP][rl] batch invariant mode [WIP][rl] batch invariant mode under same parallelism Mar 25, 2026
@wwwjn

wwwjn commented Mar 26, 2026

Copy link
Copy Markdown
Contributor Author

Need to rebase onto #2649 and clean up the varlen patch in trainer side in this PR

@wwwjn
wwwjn force-pushed the rl-batch-invariant branch from 254d3da to c69a7ac Compare March 26, 2026 22:15
Comment thread torchtitan/models/qwen3/__init__.py Resolved
@wwwjn wwwjn changed the title [WIP][rl] batch invariant mode under same parallelism [rl] batch invariant mode under same parallelism Mar 27, 2026
@wwwjn
wwwjn marked this pull request as ready for review March 27, 2026 16:10

@Lucaskabela Lucaskabela left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rebase on #2649 to see what these changes look like post rebase?

Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/models/common/attention.py Resolved
@wwwjn
wwwjn force-pushed the rl-batch-invariant branch from 55d4280 to ee39957 Compare March 30, 2026 20:03
Comment thread torchtitan/models/common/attention.py Resolved
@wwwjn
wwwjn force-pushed the rl-batch-invariant branch from 35eb9ea to f52e7f2 Compare March 31, 2026 23:30
Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/experiments/rl/README.md Resolved
Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/config/configs.py Resolved
Comment thread torchtitan/models/common/attention.py Resolved
Comment thread torchtitan/models/common/attention.py Resolved
@wwwjn
wwwjn force-pushed the rl-batch-invariant branch from 80e97da to 0cc91e2 Compare April 6, 2026 18:58
@wwwjn
wwwjn merged commit 52e34d3 into main Apr 6, 2026
22 of 33 checks passed
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
## What changed
- Currently only support both trainer and generator using bfloat16.
(working on consolidating the details of dtype conversion, but currently
FA3 kernel only support bf16/f16)
- Tested with compile("eager") and cudagraph("piecewise"), the bitwise
identity are still preserved.

## Test
TP=2 on both trainer and generator:
- Verified after enabling batch-invariant mode, run-to-run deterministic
is also so enabled as prerequisite .
```
[actor=<root>] Eval: Accuracy=20% (4/20) Format=20% (4/20)
[actor=<root>] ================================================================================
[actor=<root>] Starting RL training for 10 steps
[actor=<root>] ================================================================================
[actor=<root>] Weight sync: push=0.198s, total=0.827s
[actor=<root>] Step  0 | Loss: -0.0041 | Reward: -0.100 | Correct: 18/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 52.1s
[actor=<root>] Weight sync: push=0.203s, total=0.400s
[actor=<root>] Step  1 | Loss: -0.0047 | Reward: -0.350 | Correct: 13/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.8s
[actor=<root>] Weight sync: push=0.197s, total=0.384s
[actor=<root>] Step  2 | Loss: -0.0067 | Reward: -0.300 | Correct: 14/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.6s
[actor=<root>] Weight sync: push=0.196s, total=0.414s
[actor=<root>] Step  3 | Loss: -0.0064 | Reward: -0.100 | Correct: 18/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.3s
[actor=<root>] Weight sync: push=0.204s, total=0.413s
[actor=<root>] Step  4 | Loss: -0.0084 | Reward: -0.400 | Correct: 12/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.7s
[actor=<root>] Weight sync: push=0.211s, total=0.413s
[actor=<root>] Step  5 | Loss: -0.0120 | Reward: +0.300 | Correct: 26/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.1s
[actor=<root>] Weight sync: push=0.186s, total=0.385s
[actor=<root>] Step  6 | Loss: -0.0051 | Reward: -0.350 | Correct: 13/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.6s
[actor=<root>] Weight sync: push=0.223s, total=0.426s
[actor=<root>] Step  7 | Loss: -0.0058 | Reward: -0.600 | Correct:  8/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.9s
[actor=<root>] Weight sync: push=0.206s, total=0.408s
[actor=<root>] Step  8 | Loss: -0.0075 | Reward: -0.250 | Correct: 15/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.5s
[actor=<root>] Weight sync: push=0.200s, total=0.402s
[actor=<root>] Step  9 | Loss: -0.0124 | Reward: +0.100 | Correct: 22/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.8s
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=35% (7/20) Format=50% (10/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=20% (4/20) Format=20% (4/20)
[actor=<root>] Post-training: Accuracy=35% (7/20) Format=50% (10/20)
[actor=<root>] ================================================================================
```


## Config for bit-wise identity:
- RLTrainer.debug is set from config_registry. When
`batch_invariant_mode=True`, we check `deterministic` must be true, and
dtype on both trainer and generator needs to be `bfloat16` in
`__post__init`
    - PolicyTrainer.debug is set by RLTrainer.debug
- PolicyTrainer model's inner_attn (VarlenAttention) has a
`batch_invariant` flag, this flag is set by PolicyTrainer
    - Generator.debug is set by RLTrainer.debug
- Generator model's inner_attn (VLLMAttentionWrapper) enables
`batch_invariant` mode by checking `is_batch_invariant_mode_enabled()`
global variable.


## Attention Part
Attention changes (for both VarlenAttention in core torchtitan, and
VLLMAttentionWrapper in RL):
- If the hardware is Hopper, always use FA3 attention. If not, use FA2
for Blackwell hardware.
- If we are using FA3 attention, then batch-invariant is enabled by
setting `num_split=1`
- If we are using FA2 attention, batch-invariant is automatically
supported.
jemitche1 pushed a commit to jemitche1/torchtitan that referenced this pull request Apr 14, 2026
## What changed
- Currently only support both trainer and generator using bfloat16.
(working on consolidating the details of dtype conversion, but currently
FA3 kernel only support bf16/f16)
- Tested with compile("eager") and cudagraph("piecewise"), the bitwise
identity are still preserved.

## Test
TP=2 on both trainer and generator:
- Verified after enabling batch-invariant mode, run-to-run deterministic
is also so enabled as prerequisite .
```
[actor=<root>] Eval: Accuracy=20% (4/20) Format=20% (4/20)
[actor=<root>] ================================================================================
[actor=<root>] Starting RL training for 10 steps
[actor=<root>] ================================================================================
[actor=<root>] Weight sync: push=0.198s, total=0.827s
[actor=<root>] Step  0 | Loss: -0.0041 | Reward: -0.100 | Correct: 18/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 52.1s
[actor=<root>] Weight sync: push=0.203s, total=0.400s
[actor=<root>] Step  1 | Loss: -0.0047 | Reward: -0.350 | Correct: 13/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.8s
[actor=<root>] Weight sync: push=0.197s, total=0.384s
[actor=<root>] Step  2 | Loss: -0.0067 | Reward: -0.300 | Correct: 14/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.6s
[actor=<root>] Weight sync: push=0.196s, total=0.414s
[actor=<root>] Step  3 | Loss: -0.0064 | Reward: -0.100 | Correct: 18/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.3s
[actor=<root>] Weight sync: push=0.204s, total=0.413s
[actor=<root>] Step  4 | Loss: -0.0084 | Reward: -0.400 | Correct: 12/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.7s
[actor=<root>] Weight sync: push=0.211s, total=0.413s
[actor=<root>] Step  5 | Loss: -0.0120 | Reward: +0.300 | Correct: 26/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.1s
[actor=<root>] Weight sync: push=0.186s, total=0.385s
[actor=<root>] Step  6 | Loss: -0.0051 | Reward: -0.350 | Correct: 13/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.6s
[actor=<root>] Weight sync: push=0.223s, total=0.426s
[actor=<root>] Step  7 | Loss: -0.0058 | Reward: -0.600 | Correct:  8/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.9s
[actor=<root>] Weight sync: push=0.206s, total=0.408s
[actor=<root>] Step  8 | Loss: -0.0075 | Reward: -0.250 | Correct: 15/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.5s
[actor=<root>] Weight sync: push=0.200s, total=0.402s
[actor=<root>] Step  9 | Loss: -0.0124 | Reward: +0.100 | Correct: 22/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.8s
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=35% (7/20) Format=50% (10/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=20% (4/20) Format=20% (4/20)
[actor=<root>] Post-training: Accuracy=35% (7/20) Format=50% (10/20)
[actor=<root>] ================================================================================
```


## Config for bit-wise identity:
- RLTrainer.debug is set from config_registry. When
`batch_invariant_mode=True`, we check `deterministic` must be true, and
dtype on both trainer and generator needs to be `bfloat16` in
`__post__init`
    - PolicyTrainer.debug is set by RLTrainer.debug
- PolicyTrainer model's inner_attn (VarlenAttention) has a
`batch_invariant` flag, this flag is set by PolicyTrainer
    - Generator.debug is set by RLTrainer.debug
- Generator model's inner_attn (VLLMAttentionWrapper) enables
`batch_invariant` mode by checking `is_batch_invariant_mode_enabled()`
global variable.


## Attention Part
Attention changes (for both VarlenAttention in core torchtitan, and
VLLMAttentionWrapper in RL):
- If the hardware is Hopper, always use FA3 attention. If not, use FA2
for Blackwell hardware.
- If we are using FA3 attention, then batch-invariant is enabled by
setting `num_split=1`
- If we are using FA2 attention, batch-invariant is automatically
supported.
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
## What changed
- Currently only support both trainer and generator using bfloat16.
(working on consolidating the details of dtype conversion, but currently
FA3 kernel only support bf16/f16)
- Tested with compile("eager") and cudagraph("piecewise"), the bitwise
identity are still preserved.

## Test
TP=2 on both trainer and generator:
- Verified after enabling batch-invariant mode, run-to-run deterministic
is also so enabled as prerequisite .
```
[actor=<root>] Eval: Accuracy=20% (4/20) Format=20% (4/20)
[actor=<root>] ================================================================================
[actor=<root>] Starting RL training for 10 steps
[actor=<root>] ================================================================================
[actor=<root>] Weight sync: push=0.198s, total=0.827s
[actor=<root>] Step  0 | Loss: -0.0041 | Reward: -0.100 | Correct: 18/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 52.1s
[actor=<root>] Weight sync: push=0.203s, total=0.400s
[actor=<root>] Step  1 | Loss: -0.0047 | Reward: -0.350 | Correct: 13/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.8s
[actor=<root>] Weight sync: push=0.197s, total=0.384s
[actor=<root>] Step  2 | Loss: -0.0067 | Reward: -0.300 | Correct: 14/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.6s
[actor=<root>] Weight sync: push=0.196s, total=0.414s
[actor=<root>] Step  3 | Loss: -0.0064 | Reward: -0.100 | Correct: 18/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.3s
[actor=<root>] Weight sync: push=0.204s, total=0.413s
[actor=<root>] Step  4 | Loss: -0.0084 | Reward: -0.400 | Correct: 12/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 46.7s
[actor=<root>] Weight sync: push=0.211s, total=0.413s
[actor=<root>] Step  5 | Loss: -0.0120 | Reward: +0.300 | Correct: 26/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.1s
[actor=<root>] Weight sync: push=0.186s, total=0.385s
[actor=<root>] Step  6 | Loss: -0.0051 | Reward: -0.350 | Correct: 13/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.6s
[actor=<root>] Weight sync: push=0.223s, total=0.426s
[actor=<root>] Step  7 | Loss: -0.0058 | Reward: -0.600 | Correct:  8/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.9s
[actor=<root>] Weight sync: push=0.206s, total=0.408s
[actor=<root>] Step  8 | Loss: -0.0075 | Reward: -0.250 | Correct: 15/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 44.5s
[actor=<root>] Weight sync: push=0.200s, total=0.402s
[actor=<root>] Step  9 | Loss: -0.0124 | Reward: +0.100 | Correct: 22/40 | Avg tokens: 100 | Logprob diff: mean=0.0000e+00, max=0.0000e+00 | Time: 47.8s
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=35% (7/20) Format=50% (10/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=20% (4/20) Format=20% (4/20)
[actor=<root>] Post-training: Accuracy=35% (7/20) Format=50% (10/20)
[actor=<root>] ================================================================================
```


## Config for bit-wise identity:
- RLTrainer.debug is set from config_registry. When
`batch_invariant_mode=True`, we check `deterministic` must be true, and
dtype on both trainer and generator needs to be `bfloat16` in
`__post__init`
    - PolicyTrainer.debug is set by RLTrainer.debug
- PolicyTrainer model's inner_attn (VarlenAttention) has a
`batch_invariant` flag, this flag is set by PolicyTrainer
    - Generator.debug is set by RLTrainer.debug
- Generator model's inner_attn (VLLMAttentionWrapper) enables
`batch_invariant` mode by checking `is_batch_invariant_mode_enabled()`
global variable.


## Attention Part
Attention changes (for both VarlenAttention in core torchtitan, and
VLLMAttentionWrapper in RL):
- If the hardware is Hopper, always use FA3 attention. If not, use FA2
for Blackwell hardware.
- If we are using FA3 attention, then batch-invariant is enabled by
setting `num_split=1`
- If we are using FA2 attention, batch-invariant is automatically
supported.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants