Repository navigation
[rl][combo] Refactor simple RL loop with torchtitan components - #2443
Conversation
There was a problem hiding this comment.
We can even put it directly in trainer.py for now until we have a more generic loss abstraction
tianyu-l
left a comment
There was a problem hiding this comment.
beautiful work, thank you
| 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)), | ||
| ) | ||
| ) |
There was a problem hiding this comment.
-
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?
-
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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). |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
we are using the loss in util, can we delete it, or vice versa?
| return pg_loss | ||
|
|
||
|
|
||
| def compute_policy_gradient_loss_vllm( |
There was a problem hiding this comment.
is this repeated from utils/trainer?
| # 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, | ||
| ) |
There was a problem hiding this comment.
this seems repeated from the trainer, i guess i dont understand the difference between simple_rl and trainer
felipemello1
left a comment
There was a problem hiding this comment.
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 |
619b669 to
dc2dc8a
Compare
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
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
…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
…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
This PR is a combination of all the 4 different PRs:
#2244
#2221
#2194
#2191
Current status:
Next Step: