-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
🩺 Dr. GRPO loss #3256
🩺 Dr. GRPO loss #3256
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. |
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.
LGTM with some nits and a question about what BNPO refers to
difficulty bias. | ||
applied. The [Dr. GRPO paper](https://huggingface.co/papers/2503.14476) recommends not scaling the rewards, | ||
as scaling by the standard deviation introduces a question-level difficulty bias. | ||
loss_type (`str`, *optional*, defaults to `"bnpo"`): |
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.
What is bnpo
? Would be good to have a reference to where it's defined (I thought we had DAPO as the default loss)
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.
In fact, I realized while doing this PR that it wasn't exactly DAPO that was being used, but a variant of BNPO as defined here :

Let me try to clarify here. Losses per token are normalized by
- GRPO: the length of the sequence
- DAPO: the average sequence length in the group
- BNPO: the average sequence length in the batch
- TRL's BNPO: the average sequence length in the local batch*; this is what I call
bnpo
in the code, but it's not 100% correct - Dr GRPO: by the maximum possible length of the completion
*a batch is made up of num_devices * gradient_accumulations
local batches
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.
Special cases:
When
per_device_batch_size==num_generations
, TRL's BNPO is equivalent to DAPOper_device_batch_size==1
, TRL's BNPO is equivalent to GRPOgradient_accumualtion_steps==1
andnum_devices=1
, TRL's BNPO is equivalent to the actual BNPO.
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.
@qgallouedec Thanks for the comprehensive support! A minor comment for your future consideration: Dr. GRPO does not constrain the constant normalizer to be MAX_LEN (although it's easier to just use that). This can affect the update scale (related to your recent tweet https://x.com/QGallouedec/status/1908741708021457357). In fact, different constant of x
in the setting in your tweet can be absorbed into the constant normalizer we propose in the paper, and MAX_LEN is a convenient example.
slightly vary depending on the local batch size, despite a constant effective batch size. | ||
- `"drgrpo"`: Token-level losses are aggregated by normalizing with a global constant. This method was | ||
introduced in the [Dr. GRPO paper](https://huggingface.co/papers/2503.14476) to eliminate length bias. | ||
The value of the constant corresponds to `max_completion_length`. |
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 I understand correctly, @edbeeching was trying something slightly different in #3231 that did local scaling per batch instead of a global constant. Do you know if there's much difference between the two?
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.
They are roughly equivalent, I have closed my PR in favor of this one.
Co-authored-by: lewtun <lewis.c.tunstall@gmail.com>
What does this PR do?
This PR supersedes #3231 #3138
Closes #3178
Before submitting
Pull Request section?
to it if that's the case.
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.