-
Notifications
You must be signed in to change notification settings - Fork 2.1k
π¦βπ₯ 6x faster GRPO with multi-step optimization #2899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
@@ -224,6 +228,14 @@ class GRPOConfig(TrainingArguments): | |||
"training speed." | |||
}, | |||
) | |||
num_iterations: int = field( | |||
default=1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we set another default? eg 4?
trl/trainer/grpo_trainer.py
Outdated
@@ -638,6 +731,10 @@ def _prepare_inputs(self, inputs: dict[str, Union[torch.Tensor, Any]]) -> dict[s | |||
logits_to_keep = completion_ids.size(1) # we only need to compute the logits for the completion tokens | |||
|
|||
with torch.inference_mode(): | |||
old_per_token_logps = self._get_per_token_logps( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if num_iterations == 1
, I think we do a redundant forward pass here as the same thing will be recalculated at the optimization step. Would it be better to only calculate this if num_iterations > 1
, and then at the optimization step ?
For example:
if num_iterations==1:
old_per_token_logps = per_token_logps.detach()
else:
old_per_token_logps = inputs["old_per_token_logps"]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
β¦into multi-step-grpi
clearer when one realizes that the integers are the indices of the prompts! |
I'll add it, thanks for the feedback |
@qgallouedec I think we should consider adding another term to calculate |
Not sure to get why you want to sync the ref model every step. It is equivalent to have no ref model, the KL will be always 0 |
Sorry, my mistake. I meant that setting ref_model_sync_steps = num_iterations might help stabilize the multi-step update. However, this would result in the loss of regularization provided by the original reference model. |
* Add num_updates and epsilon parameters to GRPOConfig and GRPOTrainer * test sampler * update the loss computation * fix eval sampler * should work now * buffer inputs with grad accum * optimize when num_iterations == 1 * test * minor comment removal and fix log metric * beta position * clarify comment [ci skip] * clarify sampler doc [ci skip] * fix collision with eval logging * clarify
@qgallouedec Can num_iterations not be used together with num_train_epochs? I found that setting num_train_epochs=2, the larger the num_iterations, the more iterations there are and the slower the training. |
Hi Team, Attaching algorithm from DeepseekMath paper - Please see the PPO algorithm - |
Thanks @SonuDixit can you open a dedicated issue for this? Just copy-paste you comment |
|
* Add num_updates and epsilon parameters to GRPOConfig and GRPOTrainer * test sampler * update the loss computation * fix eval sampler * should work now * buffer inputs with grad accum * optimize when num_iterations == 1 * test * minor comment removal and fix log metric * beta position * clarify comment [ci skip] * clarify sampler doc [ci skip] * fix collision with eval logging * clarify
What does this PR do?
This PR implements the multi-step trick. It allows the generation to be reused several times, speeding up training.
It requires to implement the importance sampling and the clipping logic.
Results with various mu (note that mu=1 matches the current implementation in main):
For reproduction:
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines.
Who can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.