Skip to content

[RL] Changes to enable compilation for trainer - #2568

Merged
Lucaskabela merged 2 commits into
pytorch:mainfrom
Lucaskabela:lucaskabela/enable_trainer_compile_03_10
Mar 23, 2026
Merged

Lucaskabela merged 2 commits into
pytorch:mainfrom
Lucaskabela:lucaskabela/enable_trainer_compile_03_10

Conversation

@Lucaskabela

@Lucaskabela Lucaskabela commented Mar 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

In this PR, we enable naive, JIT style torch.compile for the RL policy trainer. This is the first step towards speeding up the trainer model. Changes are:

  1. Wiring through compilation config:
  • Added TrainerCompileConfig dataclass with enable (bool) and backend (str, default "eager") fields
  • Added compile field to PolicyTrainer.Config with compile and aot_eager backend
  • Added apply_compile() method that calls .compile(backend=..., fullgraph=True, dynamic=True) on each transformer
    layer -> This is crticial, as torch.compile() results in weight name change which breaks the weight transfer and dynamism needed for seqlen
  1. config_registry.py — Enable compile by default in configs
  • Both rl_grpo_qwen3_0_6b and rl_grpo_qwen3_debug configs now set
    compile=TrainerCompileConfig(enable=True, backend="aot_eager")
  1. vllm_compat_attention.py — Deleted to consolidate on new attention backend

Test Plan

python torchtitan/experiments/rl/simple_grpo_sum_digits.py --module rl --config rl_grpo_qwen3_0_6b --hf_assets_path=torchtitan/experiments/rl/example_checkpoint/Qwen3-0.6B

Results in the same losses w/ and w/o compile, but different on main due to changing backend - the timing is now like:

Main

[actor=<root>] Step  9 | Loss: +0.0041 | Reward: +0.450 | Correct: 29/40 | Avg tokens: 100 | Logprob diff: mean=-1.1657e-01, max=1.1326e+01 | Time: 21.1s
[actor=<root>]   Timing: generate=2.98s  grade=0.01s  advantage=0.0003s  train=15.02s  sync=3.14s (push=0.13s)
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] Training timing (10 steps in 219.2s, avg 21.92s/step):
[actor=<root>]   generate: 25.1s total, 2.51s avg, 11%
[actor=<root>]   grade:    0.1s total, 0.01s avg, 0%
[actor=<root>]   train:    155.9s total, 15.59s avg, 71%
[actor=<root>]   sync:     38.1s total, 3.81s avg, 17%
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=55% (11/20) Format=100% (20/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=40% (8/20) Format=50% (10/20)
[actor=<root>] Post-training: Accuracy=55% (11/20) Format=100% (20/20)
[actor=<root>] ================================================================================

Changes

[actor=<root>] Step  9 | Loss: -0.0072 | Reward: +0.300 | Correct: 26/40 | Avg tokens: 100 | Logprob diff: mean=-1.0181e-01, max=1.2256e+01 | Time: 12.9s
[actor=<root>]   Timing: generate=2.53s  grade=0.01s  advantage=0.0002s  train=7.12s  sync=3.24s (push=0.12s)
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] Training timing (10 steps in 170.0s, avg 17.00s/step):
[actor=<root>]   generate: 24.0s total, 2.40s avg, 14%
[actor=<root>]   grade:    0.1s total, 0.01s avg, 0%
[actor=<root>]   train:    113.3s total, 11.33s avg, 67%
[actor=<root>]   sync:     32.7s total, 3.27s avg, 19%
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=60% (12/20) Format=90% (18/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=40% (8/20) Format=50% (10/20)
[actor=<root>] Post-training: Accuracy=60% (12/20) Format=90% (18/20)=
(vllm) [[email protected] ~/torchtitan (lucaskabela/enable_trainer_compile_03_10)]$ pytho

So we save ~40s of runtime e2e via compilation in this manner without affecting our logits/accuracy

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 13, 2026
@Lucaskabela
Lucaskabela marked this pull request as ready for review March 13, 2026 19:19

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.

if we go with pytorch varlen, do we still need to worry about this file? cc @wwwjn

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is post rebase - yes we do need these changes for the following reasons:

  1. Moving torch.autograd.Function out of the inner context - this is so compile can trace it
  2. _flash_attn_varlen_fwd custom op - this is so torch can trace it; without custom op, we don't know how shape will propogate through this

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.

if ur going with pytorch varlen i think u can directly call varlen_attn (which is already a custom op) pytorch's varlen calls the upstream flash attention impl instead of vllm's flash attention

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry I misunderstood this question (I thought this was related to the deletion of vllm_attention directory).

We will need to move the autograd.Function out but yes we won't need the custom op in that case

@Lucaskabela
Lucaskabela requested a review from wwwjn March 13, 2026 20:39
@Lucaskabela
Lucaskabela force-pushed the lucaskabela/enable_trainer_compile_03_10 branch from 8a62589 to be75f04 Compare March 13, 2026 22:14
@Lucaskabela
Lucaskabela force-pushed the lucaskabela/enable_trainer_compile_03_10 branch from be75f04 to 520d314 Compare March 13, 2026 23:50
@Lucaskabela
Lucaskabela requested a review from tianyu-l March 14, 2026 00:28
@Lucaskabela
Lucaskabela marked this pull request as draft March 17, 2026 17:34
@Lucaskabela
Lucaskabela force-pushed the lucaskabela/enable_trainer_compile_03_10 branch 4 times, most recently from b0e8401 to d032ea8 Compare March 19, 2026 21:02
@Lucaskabela
Lucaskabela marked this pull request as ready for review March 19, 2026 21:06
@Lucaskabela
Lucaskabela force-pushed the lucaskabela/enable_trainer_compile_03_10 branch from d032ea8 to 76c42cc Compare March 19, 2026 21:42
logger = logging.getLogger(__name__)


def parallelize_qwen3(

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.

@wwwjn could you work with @sanketpurandare to land PP + DTensor so that we can remove this ad hoc function?

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.

Sure, let me follow up with @sanketpurandare

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Do we have an estimated timeline on this? In the meanwhile I don't think this should be blocking here but it is good to be aware of to coordinate :)

@acisseJZhong acisseJZhong Mar 21, 2026 •

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.

also wondering what is the timeline for the PP full dtensor PR? would also like to have that to unblock Verl integration. cc @sanketpurandare

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.

Thanks! Do we have an estimated timeline on this? In the meanwhile I don't think this should be blocking here but it is good to be aware of to coordinate :)

DTensors for PP Stack is ready (RFC):PR1, PR2, PR3
I think the main blocker now is these PR needs review, and might not have a good ETA for this to be landed now. Removing this function here should be a separate concern.

return model


def apply_compile(model: nn.Module, compile_config: CompileConfig):

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.

we switched to a central impl in #2615, but I understand that this is doing model.compile() not torch.compile(model)

@fegin can we universally switch to model.compile style (and it should still work with DCP)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Similar comment here - we can switch to that definition once ready/as a follow up :) shouldn't be blocking (imo)

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.

@Lucaskabela
Lucaskabela marked this pull request as draft March 20, 2026 16:16
@Lucaskabela

Copy link
Copy Markdown
Contributor Author

Converting to draft to rebase on the attention changes

@Lucaskabela
Lucaskabela force-pushed the lucaskabela/enable_trainer_compile_03_10 branch 2 times, most recently from 6b10848 to 2a05c1f Compare March 21, 2026 00:51
@Lucaskabela
Lucaskabela requested a review from tianyu-l March 21, 2026 00:52
@Lucaskabela
Lucaskabela marked this pull request as ready for review March 21, 2026 00:52
with utils.set_default_dtype(TORCH_DTYPE_MAP[config.training.dtype]):
model = model_spec.model.build()

# Replace attention with vLLM compatible attention for RL training.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If we need to keep this definition around then we will need the custom op changes there to support the compilation

Additionally, deleting keeps us grounded on one source of truth to help avoid confusions so I think it best to just remove this code if we are switching to varlen

logger = logging.getLogger(__name__)


def parallelize_qwen3(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Do we have an estimated timeline on this? In the meanwhile I don't think this should be blocking here but it is good to be aware of to coordinate :)

return model


def apply_compile(model: nn.Module, compile_config: CompileConfig):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Similar comment here - we can switch to that definition once ready/as a follow up :) shouldn't be blocking (imo)

@Lucaskabela
Lucaskabela force-pushed the lucaskabela/enable_trainer_compile_03_10 branch from 2a05c1f to 1a8fb5c Compare March 21, 2026 01:02
@Lucaskabela
Lucaskabela force-pushed the lucaskabela/enable_trainer_compile_03_10 branch 2 times, most recently from 4fa4dac to 2196a7d Compare March 21, 2026 01:13

@acisseJZhong acisseJZhong 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.

Results in the same losses as on main - the timing is now like:

loss seems to differ based on your paste?

return model


def apply_compile(model: nn.Module, compile_config: CompileConfig):

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.

Follows the same pattern as torchtitan/models/llama3/parallelize.py.
"""
# NOTE: we MUST use `.compile()` instead of `torch.compile()` here, because
# compatibility with weight naming between this model and vLLM definition.

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 you put more detail on why torch.compile() wouldn't work here?

also, can we all switch to model.compile() in this case?

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.

It's OK to not change all pretrain compile in this PR. But please create an issue to track.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

.compile() is the general guidance due to subtle interactions with nn.Module - I will investigate this more though but IIRC this is a known issue with compile

tensor_parallel_degree=1,
data_parallel_replicate_degree=1,
),
compile=CompileConfig(enable=True, backend="aot_eager"),

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.

nit: can we also introduce the enable field to GeneratorCompileConfig just for symmetry? Or is that we by default turn on generator compile, but no default on for trainer?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is a bit more complicated than that unfortunately - the compile integration in generator is quite different than trainer, so there is not a simple "enable" flag - we need both a backend turned on and a cudagraph option.

There is an "enforce_eager" option that we can wire through to compile, but on the PR for enabling the GeneratorCompile I was discouraged from wiring this through as it adds complexity

@tianyu-l tianyu-l 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.

please address remaining comments

# Explicit positions avoid dynamic rope_cache[0:seqlen] slice in RoPE,
# which breaks torch.compile with symbolic shapes.
seq_len = full_tensor.shape[1]
positions = torch.arange(seq_len, device=device).unsqueeze(0)

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.

A separate question: It seems the full sequence here is always per episode, with batch size 1, and we have a for loop over all the episodes. Wouldn't this be very inefficient?
https://github.com/pytorch/torchtitan/blob/main/torchtitan/experiments/rl/actors/trainer.py#L296

cc @joecummings @felipemello1

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.

@Lucaskabela please create an issue to track

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes absolutely - we can make a significant improvement by batching in the future but need to be careful about our logits

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.

We are missing a "batcher" concept in the main RL loop

Follows the same pattern as torchtitan/models/llama3/parallelize.py.
"""
# NOTE: we MUST use `.compile()` instead of `torch.compile()` here, because
# compatibility with weight naming between this model and vLLM definition.

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.

It's OK to not change all pretrain compile in this PR. But please create an issue to track.

@Lucaskabela Lucaskabela left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Re logits changing: That is expected now, since this PR was rebased to change the backend (i.e removing the code in trainer). There is no impact on the loss between compile=False and True, but between main and this PR there will be

# Explicit positions avoid dynamic rope_cache[0:seqlen] slice in RoPE,
# which breaks torch.compile with symbolic shapes.
seq_len = full_tensor.shape[1]
positions = torch.arange(seq_len, device=device).unsqueeze(0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes absolutely - we can make a significant improvement by batching in the future but need to be careful about our logits

tensor_parallel_degree=1,
data_parallel_replicate_degree=1,
),
compile=CompileConfig(enable=True, backend="aot_eager"),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is a bit more complicated than that unfortunately - the compile integration in generator is quite different than trainer, so there is not a simple "enable" flag - we need both a backend turned on and a cudagraph option.

There is an "enforce_eager" option that we can wire through to compile, but on the PR for enabling the GeneratorCompile I was discouraged from wiring this through as it adds complexity

@Lucaskabela
Lucaskabela force-pushed the lucaskabela/enable_trainer_compile_03_10 branch from 2196a7d to 88e2191 Compare March 23, 2026 15:41
@Lucaskabela
Lucaskabela merged commit 555a4f4 into pytorch:main Mar 23, 2026
20 of 31 checks passed
Lucaskabela added a commit that referenced this pull request Mar 23, 2026
Minor fix in #2568 to move
apply_compile to distributed/compile caused a bug due to missing
`.named_children()` in this integration. So, update to fix (avoid
crashing) for compile on the trainer
mori360 pushed a commit to mori360/torchtitan that referenced this pull request Mar 23, 2026
## Summary

In this PR, we enable naive, JIT style torch.compile for the RL policy
trainer. This is the first step towards speeding up the trainer model.
Changes are:

1. Wiring through compilation config:
- Added TrainerCompileConfig dataclass with enable (bool) and backend
(str, default "eager") fields
- Added compile field to PolicyTrainer.Config with compile and aot_eager
backend
- Added apply_compile() method that calls .compile(backend=...,
fullgraph=True, dynamic=True) on each transformer
layer -> This is crticial, as `torch.compile()` results in weight name
change which breaks the weight transfer and dynamism needed for seqlen

  2. config_registry.py — Enable compile by default in configs
  - Both rl_grpo_qwen3_0_6b and rl_grpo_qwen3_debug configs now set
  compile=TrainerCompileConfig(enable=True, backend="aot_eager")

3. vllm_compat_attention.py — Deleted to consolidate on new attention
backend
 
## Test Plan
```bash
python torchtitan/experiments/rl/simple_grpo_sum_digits.py --module rl --config rl_grpo_qwen3_0_6b --hf_assets_path=torchtitan/experiments/rl/example_checkpoint/Qwen3-0.6B
```

Results in the same losses w/ and w/o compile, but different on main due
to changing backend - the timing is now like:
### Main
```bash
[actor=<root>] Step  9 | Loss: +0.0041 | Reward: +0.450 | Correct: 29/40 | Avg tokens: 100 | Logprob diff: mean=-1.1657e-01, max=1.1326e+01 | Time: 21.1s
[actor=<root>]   Timing: generate=2.98s  grade=0.01s  advantage=0.0003s  train=15.02s  sync=3.14s (push=0.13s)
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] Training timing (10 steps in 219.2s, avg 21.92s/step):
[actor=<root>]   generate: 25.1s total, 2.51s avg, 11%
[actor=<root>]   grade:    0.1s total, 0.01s avg, 0%
[actor=<root>]   train:    155.9s total, 15.59s avg, 71%
[actor=<root>]   sync:     38.1s total, 3.81s avg, 17%
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=55% (11/20) Format=100% (20/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=40% (8/20) Format=50% (10/20)
[actor=<root>] Post-training: Accuracy=55% (11/20) Format=100% (20/20)
[actor=<root>] ================================================================================
```
### Changes
```bash
[actor=<root>] Step  9 | Loss: -0.0072 | Reward: +0.300 | Correct: 26/40 | Avg tokens: 100 | Logprob diff: mean=-1.0181e-01, max=1.2256e+01 | Time: 12.9s
[actor=<root>]   Timing: generate=2.53s  grade=0.01s  advantage=0.0002s  train=7.12s  sync=3.24s (push=0.12s)
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] Training timing (10 steps in 170.0s, avg 17.00s/step):
[actor=<root>]   generate: 24.0s total, 2.40s avg, 14%
[actor=<root>]   grade:    0.1s total, 0.01s avg, 0%
[actor=<root>]   train:    113.3s total, 11.33s avg, 67%
[actor=<root>]   sync:     32.7s total, 3.27s avg, 19%
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=60% (12/20) Format=90% (18/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=40% (8/20) Format=50% (10/20)
[actor=<root>] Post-training: Accuracy=60% (12/20) Format=90% (18/20)=
(vllm) [[email protected] ~/torchtitan (lucaskabela/enable_trainer_compile_03_10)]$ pytho
```

So we save ~40s of runtime e2e via compilation in this manner without
affecting our logits/accuracy
pytorch-bot Bot pushed a commit that referenced this pull request Mar 27, 2026
## Summary

In this PR, we enable naive, JIT style torch.compile for the RL policy
trainer. This is the first step towards speeding up the trainer model.
Changes are:

1. Wiring through compilation config:
- Added TrainerCompileConfig dataclass with enable (bool) and backend
(str, default "eager") fields
- Added compile field to PolicyTrainer.Config with compile and aot_eager
backend
- Added apply_compile() method that calls .compile(backend=...,
fullgraph=True, dynamic=True) on each transformer
layer -> This is crticial, as `torch.compile()` results in weight name
change which breaks the weight transfer and dynamism needed for seqlen

  2. config_registry.py — Enable compile by default in configs
  - Both rl_grpo_qwen3_0_6b and rl_grpo_qwen3_debug configs now set
  compile=TrainerCompileConfig(enable=True, backend="aot_eager")

3. vllm_compat_attention.py — Deleted to consolidate on new attention
backend
 
## Test Plan
```bash
python torchtitan/experiments/rl/simple_grpo_sum_digits.py --module rl --config rl_grpo_qwen3_0_6b --hf_assets_path=torchtitan/experiments/rl/example_checkpoint/Qwen3-0.6B
```

Results in the same losses w/ and w/o compile, but different on main due
to changing backend - the timing is now like:
### Main
```bash
[actor=<root>] Step  9 | Loss: +0.0041 | Reward: +0.450 | Correct: 29/40 | Avg tokens: 100 | Logprob diff: mean=-1.1657e-01, max=1.1326e+01 | Time: 21.1s
[actor=<root>]   Timing: generate=2.98s  grade=0.01s  advantage=0.0003s  train=15.02s  sync=3.14s (push=0.13s)
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] Training timing (10 steps in 219.2s, avg 21.92s/step):
[actor=<root>]   generate: 25.1s total, 2.51s avg, 11%
[actor=<root>]   grade:    0.1s total, 0.01s avg, 0%
[actor=<root>]   train:    155.9s total, 15.59s avg, 71%
[actor=<root>]   sync:     38.1s total, 3.81s avg, 17%
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=55% (11/20) Format=100% (20/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=40% (8/20) Format=50% (10/20)
[actor=<root>] Post-training: Accuracy=55% (11/20) Format=100% (20/20)
[actor=<root>] ================================================================================
```
### Changes
```bash
[actor=<root>] Step  9 | Loss: -0.0072 | Reward: +0.300 | Correct: 26/40 | Avg tokens: 100 | Logprob diff: mean=-1.0181e-01, max=1.2256e+01 | Time: 12.9s
[actor=<root>]   Timing: generate=2.53s  grade=0.01s  advantage=0.0002s  train=7.12s  sync=3.24s (push=0.12s)
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] Training timing (10 steps in 170.0s, avg 17.00s/step):
[actor=<root>]   generate: 24.0s total, 2.40s avg, 14%
[actor=<root>]   grade:    0.1s total, 0.01s avg, 0%
[actor=<root>]   train:    113.3s total, 11.33s avg, 67%
[actor=<root>]   sync:     32.7s total, 3.27s avg, 19%
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=60% (12/20) Format=90% (18/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=40% (8/20) Format=50% (10/20)
[actor=<root>] Post-training: Accuracy=60% (12/20) Format=90% (18/20)=
(vllm) [[email protected] ~/torchtitan (lucaskabela/enable_trainer_compile_03_10)]$ pytho
```

So we save ~40s of runtime e2e via compilation in this manner without
affecting our logits/accuracy
pytorch-bot Bot pushed a commit that referenced this pull request Mar 27, 2026
Minor fix in #2568 to move
apply_compile to distributed/compile caused a bug due to missing
`.named_children()` in this integration. So, update to fix (avoid
crashing) for compile on the trainer
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
## Summary

In this PR, we enable naive, JIT style torch.compile for the RL policy
trainer. This is the first step towards speeding up the trainer model.
Changes are:

1. Wiring through compilation config:
- Added TrainerCompileConfig dataclass with enable (bool) and backend
(str, default "eager") fields
- Added compile field to PolicyTrainer.Config with compile and aot_eager
backend
- Added apply_compile() method that calls .compile(backend=...,
fullgraph=True, dynamic=True) on each transformer
layer -> This is crticial, as `torch.compile()` results in weight name
change which breaks the weight transfer and dynamism needed for seqlen

  2. config_registry.py — Enable compile by default in configs
  - Both rl_grpo_qwen3_0_6b and rl_grpo_qwen3_debug configs now set
  compile=TrainerCompileConfig(enable=True, backend="aot_eager")

3. vllm_compat_attention.py — Deleted to consolidate on new attention
backend
 
## Test Plan
```bash
python torchtitan/experiments/rl/simple_grpo_sum_digits.py --module rl --config rl_grpo_qwen3_0_6b --hf_assets_path=torchtitan/experiments/rl/example_checkpoint/Qwen3-0.6B
```

Results in the same losses w/ and w/o compile, but different on main due
to changing backend - the timing is now like:
### Main
```bash
[actor=<root>] Step  9 | Loss: +0.0041 | Reward: +0.450 | Correct: 29/40 | Avg tokens: 100 | Logprob diff: mean=-1.1657e-01, max=1.1326e+01 | Time: 21.1s
[actor=<root>]   Timing: generate=2.98s  grade=0.01s  advantage=0.0003s  train=15.02s  sync=3.14s (push=0.13s)
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] Training timing (10 steps in 219.2s, avg 21.92s/step):
[actor=<root>]   generate: 25.1s total, 2.51s avg, 11%
[actor=<root>]   grade:    0.1s total, 0.01s avg, 0%
[actor=<root>]   train:    155.9s total, 15.59s avg, 71%
[actor=<root>]   sync:     38.1s total, 3.81s avg, 17%
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=55% (11/20) Format=100% (20/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=40% (8/20) Format=50% (10/20)
[actor=<root>] Post-training: Accuracy=55% (11/20) Format=100% (20/20)
[actor=<root>] ================================================================================
```
### Changes
```bash
[actor=<root>] Step  9 | Loss: -0.0072 | Reward: +0.300 | Correct: 26/40 | Avg tokens: 100 | Logprob diff: mean=-1.0181e-01, max=1.2256e+01 | Time: 12.9s
[actor=<root>]   Timing: generate=2.53s  grade=0.01s  advantage=0.0002s  train=7.12s  sync=3.24s (push=0.12s)
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] Training timing (10 steps in 170.0s, avg 17.00s/step):
[actor=<root>]   generate: 24.0s total, 2.40s avg, 14%
[actor=<root>]   grade:    0.1s total, 0.01s avg, 0%
[actor=<root>]   train:    113.3s total, 11.33s avg, 67%
[actor=<root>]   sync:     32.7s total, 3.27s avg, 19%
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=60% (12/20) Format=90% (18/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=40% (8/20) Format=50% (10/20)
[actor=<root>] Post-training: Accuracy=60% (12/20) Format=90% (18/20)=
(vllm) [[email protected] ~/torchtitan (lucaskabela/enable_trainer_compile_03_10)]$ pytho
```

So we save ~40s of runtime e2e via compilation in this manner without
affecting our logits/accuracy
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
Minor fix in pytorch#2568 to move
apply_compile to distributed/compile caused a bug due to missing
`.named_children()` in this integration. So, update to fix (avoid
crashing) for compile on the trainer
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
## Summary

In this PR, we enable naive, JIT style torch.compile for the RL policy
trainer. This is the first step towards speeding up the trainer model.
Changes are:

1. Wiring through compilation config:
- Added TrainerCompileConfig dataclass with enable (bool) and backend
(str, default "eager") fields
- Added compile field to PolicyTrainer.Config with compile and aot_eager
backend
- Added apply_compile() method that calls .compile(backend=...,
fullgraph=True, dynamic=True) on each transformer
layer -> This is crticial, as `torch.compile()` results in weight name
change which breaks the weight transfer and dynamism needed for seqlen

  2. config_registry.py — Enable compile by default in configs
  - Both rl_grpo_qwen3_0_6b and rl_grpo_qwen3_debug configs now set
  compile=TrainerCompileConfig(enable=True, backend="aot_eager")

3. vllm_compat_attention.py — Deleted to consolidate on new attention
backend
 
## Test Plan
```bash
python torchtitan/experiments/rl/simple_grpo_sum_digits.py --module rl --config rl_grpo_qwen3_0_6b --hf_assets_path=torchtitan/experiments/rl/example_checkpoint/Qwen3-0.6B
```

Results in the same losses w/ and w/o compile, but different on main due
to changing backend - the timing is now like:
### Main
```bash
[actor=<root>] Step  9 | Loss: +0.0041 | Reward: +0.450 | Correct: 29/40 | Avg tokens: 100 | Logprob diff: mean=-1.1657e-01, max=1.1326e+01 | Time: 21.1s
[actor=<root>]   Timing: generate=2.98s  grade=0.01s  advantage=0.0003s  train=15.02s  sync=3.14s (push=0.13s)
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] Training timing (10 steps in 219.2s, avg 21.92s/step):
[actor=<root>]   generate: 25.1s total, 2.51s avg, 11%
[actor=<root>]   grade:    0.1s total, 0.01s avg, 0%
[actor=<root>]   train:    155.9s total, 15.59s avg, 71%
[actor=<root>]   sync:     38.1s total, 3.81s avg, 17%
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=55% (11/20) Format=100% (20/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=40% (8/20) Format=50% (10/20)
[actor=<root>] Post-training: Accuracy=55% (11/20) Format=100% (20/20)
[actor=<root>] ================================================================================
```
### Changes
```bash
[actor=<root>] Step  9 | Loss: -0.0072 | Reward: +0.300 | Correct: 26/40 | Avg tokens: 100 | Logprob diff: mean=-1.0181e-01, max=1.2256e+01 | Time: 12.9s
[actor=<root>]   Timing: generate=2.53s  grade=0.01s  advantage=0.0002s  train=7.12s  sync=3.24s (push=0.12s)
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] Training timing (10 steps in 170.0s, avg 17.00s/step):
[actor=<root>]   generate: 24.0s total, 2.40s avg, 14%
[actor=<root>]   grade:    0.1s total, 0.01s avg, 0%
[actor=<root>]   train:    113.3s total, 11.33s avg, 67%
[actor=<root>]   sync:     32.7s total, 3.27s avg, 19%
[actor=<root>] --------------------------------------------------------------------------------
[actor=<root>] RL Training complete
[actor=<root>] Evaluating post-training performance...
[actor=<root>] Eval: Accuracy=60% (12/20) Format=90% (18/20)
[actor=<root>] ================================================================================
[actor=<root>] Pre-training:  Accuracy=40% (8/20) Format=50% (10/20)
[actor=<root>] Post-training: Accuracy=60% (12/20) Format=90% (18/20)=
(vllm) [[email protected] ~/torchtitan (lucaskabela/enable_trainer_compile_03_10)]$ pytho
```

So we save ~40s of runtime e2e via compilation in this manner without
affecting our logits/accuracy
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
Minor fix in pytorch#2568 to move
apply_compile to distributed/compile caused a bug due to missing
`.named_children()` in this integration. So, update to fix (avoid
crashing) for compile on the trainer
@Lucaskabela
Lucaskabela deleted the lucaskabela/enable_trainer_compile_03_10 branch May 6, 2026 19:29
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.

[RL][Feature Request] Tun on torch.compile + cudagraphs for trainer definition

5 participants