Skip to content

[rl][combo] Refactor simple RL loop with torchtitan components - #2443

Merged
wwwjn merged 4 commits into
mainfrom
refactor-rl
Feb 28, 2026
Merged

wwwjn merged 4 commits into
mainfrom
refactor-rl

Conversation

@wwwjn

@wwwjn wwwjn commented Feb 26, 2026 •

Copy link
Copy Markdown
Contributor

This PR is a combination of all the 4 different PRs:
#2244
#2221
#2194
#2191

Current status:

  • Applied the same parallelism on trainer / generator, eg, Trainer (TP=2) and Generator (TP=2)
    • NOTE: now we have a strong assumption that the trainer and generator must have the same parallelism, and are collocated. This is because we unwrap the DTensor and assume we can always wrap it back with the same device mesh and placement.
  • We should be able to remove this constrain once we have more powerful weight sync. cc @daniellepintz
  • Weight transfer: Simply unwrap DTensor in trainer before sending
  • Collocated trainer and generator

Next Step:

  • Patch batch-invariant kernel, test if the trainer / generator is batch-invariant
  • Supports different parallelism on trainer / generator side
  • CI as a guard

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Feb 26, 2026
@wwwjn wwwjn changed the title [rl [rl] Refactor simple RL loop with torchtitan components Feb 26, 2026
@wwwjn wwwjn changed the title [rl] Refactor simple RL loop with torchtitan components [rl][combo] Refactor simple RL loop with torchtitan components Feb 26, 2026
@wwwjn
wwwjn requested review from Lucaskabela, acisseJZhong and tianyu-l and removed request for tianyu-l February 26, 2026 00:09

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 can even put it directly in trainer.py for now until we have a more generic loss abstraction

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

beautiful work, thank you

Comment on lines +186 to 211
episodes = []
for output in all_outputs:
prompt_token_ids = output.prompt_token_ids

completions = []
for sample in output.outputs:
per_token_log_probs = [
list(logprob_dict.values())[0].logprob
for logprob_dict in sample.logprobs
]
completions.append(
Episode.Completion(
text=sample.text,
token_ids=sample.token_ids,
token_log_probs=per_token_log_probs,
)
)

episodes.append(
Episode(
policy_version=self.policy_version,
prompt_token_ids=prompt_token_ids,
completions=completions,
rewards=torch.zeros(len(completions)),
)
)

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.

  1. We are adding to Episode a list[Completions]. I think i dont follow why there would be multiple samples in one output. Isnt it a single generation/completion?

  2. its ok to keep this for now, but IMO we should add a todo to remove "Episode" from here. Episode is an RL abstraction. Different users may have different definitions.

the generator should only output the completion, i.e. tokens, logprobs, and stop reason. Everything downstream is the controller problem.

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.

I think i dont follow why there would be multiple samples in one output. Isnt it a single generation/completion?

This is part of GRPO algorithm, for each prompt, will generate n_samples_per_prompt completions and use this group to calculate reward for each completion.

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.

IMO we should add a todo to remove "Episode" from here. Episode is an RL abstraction. Different users may have different definitions

Do you mean we should let orchestrator to assemble the Episode?

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.

Do you mean we should let orchestrator to assemble the Episode?

yes, think of the multiturn case, we do:

while True:
     completion = generation(msg_history) # currently this returns an episode
     msg_history.append(completion)
     env_output = env(msg_history)
     msg_history.append(env_output)

episode = make_episode(msg_history)

a generators job is only to generate. The controller will put things together.

@felipemello1 felipemello1 Feb 28, 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.

This is part of GRPO algorithm, for each prompt, will generate n_samples_per_prompt

i agree, i guess i dont understand the nested for loop. Why would 1 episode have N completions?

# first loop of n_samples_per_prompt
for output in all_outputs:
      prompt_token_ids = output.prompt_token_ids
      completions = []

	  # second loop that adds N completions to 1 Episode
      for sample in output.outputs:
			completions.append(
                        Episode.Completion(...

model_mode: Indicates which model to use. Train inferece unified model, batch invariant Torchtitan model,
or plain Torchtitan model
config: PolicyTrainer.Config for model/optimizer/parallelism settings.
num_samples_per_prompt: Number of completions per prompt (from sampling config).

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.

this is only used to calculate the advantage, and only necessary to compute batch//num_samples_per_prompt.

A better option, that avoid this division, would be to keep the advantage calculation close to where the group is created. e.g.

episodes = generate()
episodes = compute_rewards(episodes)
episodes = compute_advantages(episodes)

now the trainer can just do episode.advantage.

the trainer should look like a regular SFT/pretraining. It doesnt need to know about RL

return advantages


def policy_gradient_loss(

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 using the loss in util, can we delete it, or vice versa?

return pg_loss


def compute_policy_gradient_loss_vllm(

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.

is this repeated from utils/trainer?

Comment on lines 895 to 919
# Normalize rewards for stability (mean=0, std=1)
reward_mean = rewards.mean()
reward_std = rewards.std()
if reward_std > 1e-8:
rewards_normalized = (rewards - reward_mean) / reward_std
else:
rewards_normalized = rewards - reward_mean

# Compute advantages using GRPO
if use_stable_grpo:
advantages = compute_grpo_advantages_stable(rewards_normalized, group_size)
else:
advantages = compute_grpo_advantages(
rewards_normalized, group_size, beta=grpo_beta
)

# Compute loss using current policy
loss, loss_metrics = compute_policy_gradient_loss_vllm(
model,
vllm_token_ids,
vllm_token_log_probs,
prompt_token_ids,
advantages,
kl_coef=0.1,
)

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.

this seems repeated from the trainer, i guess i dont understand the difference between simple_rl and trainer

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

hey, thanks for all of the work here! Its a lot

I left a bunch of comments, but i understand that we want to land and then refactor. Its up to you if you want to address them or mark as todo.

Great job!

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

yayyy!

@wwwjn

wwwjn commented Feb 28, 2026

Copy link
Copy Markdown
Contributor Author

hey, thanks for all of the work here! Its a lot

I left a bunch of comments, but i understand that we want to land and then refactor. Its up to you if you want to address them or mark as todo.

Great job!

Thanks, comments under rl/unified are addressed. But i didn't address comments on rl/vllm_compat

ghstack-source-id: d49aef3
Pull Request resolved: #2191

config sys for simple_grpo v1

ghstack-source-id: d49aef3
Pull Request resolved: #2192

config sys for simple_grpo v2

ghstack-source-id: d49aef3
Pull Request resolved: #2193
ghstack-source-id: 19d7ad4
Pull Request resolved: #2194
ghstack-source-id: f826258
Pull Request resolved: #2221
@wwwjn
wwwjn force-pushed the refactor-rl branch 2 times, most recently from 619b669 to dc2dc8a Compare February 28, 2026 00:54
ghstack-source-id: b92ef4b
Pull Request resolved: #2244
@wwwjn
wwwjn merged commit d27ac1c into main Feb 28, 2026
21 of 22 checks passed
danielvegamyhre pushed a commit that referenced this pull request Mar 6, 2026
This PR is a combination of all the 4 different PRs: 
#2244
#2221
#2194
#2191

## Current status:

- Applied **the same parallelism** on trainer / generator, eg, Trainer
(TP=2) and Generator (TP=2)
- NOTE: now we have a strong assumption that the trainer and generator
must have the same parallelism, and are collocated. This is because we
unwrap the DTensor and assume we can always wrap it back with the same
device mesh and placement.
- We should be able to remove this constrain once we have more powerful
weight sync. cc @daniellepintz
- Weight transfer: Simply unwrap DTensor in trainer before sending
- Collocated trainer and generator

## Next Step:
- Patch batch-invariant kernel, test if the trainer / generator is
batch-invariant
- Supports different parallelism on trainer / generator side
- CI as a guard
acisseJZhong pushed a commit that referenced this pull request Mar 11, 2026
This PR is a combination of all the 4 different PRs: 
#2244
#2221
#2194
#2191

## Current status:

- Applied **the same parallelism** on trainer / generator, eg, Trainer
(TP=2) and Generator (TP=2)
- NOTE: now we have a strong assumption that the trainer and generator
must have the same parallelism, and are collocated. This is because we
unwrap the DTensor and assume we can always wrap it back with the same
device mesh and placement.
- We should be able to remove this constrain once we have more powerful
weight sync. cc @daniellepintz
- Weight transfer: Simply unwrap DTensor in trainer before sending
- Collocated trainer and generator

## Next Step:
- Patch batch-invariant kernel, test if the trainer / generator is
batch-invariant
- Supports different parallelism on trainer / generator side
- CI as a guard
TXacs pushed a commit to McmillanTAC/torchtitan that referenced this pull request Apr 13, 2026
…ch#2443)

This PR is a combination of all the 4 different PRs: 
pytorch#2244
pytorch#2221
pytorch#2194
pytorch#2191

## Current status:

- Applied **the same parallelism** on trainer / generator, eg, Trainer
(TP=2) and Generator (TP=2)
- NOTE: now we have a strong assumption that the trainer and generator
must have the same parallelism, and are collocated. This is because we
unwrap the DTensor and assume we can always wrap it back with the same
device mesh and placement.
- We should be able to remove this constrain once we have more powerful
weight sync. cc @daniellepintz
- Weight transfer: Simply unwrap DTensor in trainer before sending
- Collocated trainer and generator

## Next Step:
- Patch batch-invariant kernel, test if the trainer / generator is
batch-invariant
- Supports different parallelism on trainer / generator side
- CI as a guard
ACharacterInASimulation pushed a commit to ACharacterInASimulation/torchtitan that referenced this pull request Apr 21, 2026
…ch#2443)

This PR is a combination of all the 4 different PRs: 
pytorch#2244
pytorch#2221
pytorch#2194
pytorch#2191

## Current status:

- Applied **the same parallelism** on trainer / generator, eg, Trainer
(TP=2) and Generator (TP=2)
- NOTE: now we have a strong assumption that the trainer and generator
must have the same parallelism, and are collocated. This is because we
unwrap the DTensor and assume we can always wrap it back with the same
device mesh and placement.
- We should be able to remove this constrain once we have more powerful
weight sync. cc @daniellepintz
- Weight transfer: Simply unwrap DTensor in trainer before sending
- Collocated trainer and generator

## Next Step:
- Patch batch-invariant kernel, test if the trainer / generator is
batch-invariant
- Supports different parallelism on trainer / generator side
- CI as a guard
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.

6 participants