-
Notifications
You must be signed in to change notification settings - Fork 2.2k
📎 Fix is_clipped to compute the effective clip_ratio #3175
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. |
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.
Thanks @pandong2011! A small remark
@qgallouedec if the situation is per_token_loss1 == per_token_loss2 or advantage == 0, your code will trigger the clip operation. If you really and definitely want to make such a modification, it should be is_clipped = (per_token_loss == -per_token_loss2) & (per_token_loss1 != per_token_loss2). However, I don't recommend doing this because it will obscure your judgment process, especially the influence of the "advantage" on the judgment. |
Ah yes good point |
what is clip_radio? |
Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com> Co-authored-by: Quentin Gallouédec <gallouedec.quentin@gmail.com>
…ggingface#3131) Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com> Co-authored-by: Quentin Gallouédec <gallouedec.quentin@gmail.com> log answer key to wandb all Table HTML logging table bump patch hmm formatting html esacape reward isnt string [Liger] Liger KTO support (huggingface#2812) Co-authored-by: Kashif Rasul <kashif.rasul@gmail.com> Co-authored-by: Quentin Gallouédec <gallouedec.quentin@gmail.com> 🏃 Migrate CI to self-hosted runners (huggingface#3174) ❤️🩹 [CI] fix transformers dev CI failure (huggingface#3176) Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com> ⏯️ Fix: handle None inputs when resuming GRPO Trainer from checkpoint (huggingface#3148) Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com> 📎 Fix is_clipped to compute the effective clip_ratio (huggingface#3175) Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com> Co-authored-by: Quentin Gallouédec <gallouedec.quentin@gmail.com> Fix breaking typo for flash_attention reducing_memory_usage.md (huggingface#3190) Show unique prompts in GRPO WandB tables (huggingface#3191) 🐗 [CI] Fix trufflehog false positives (huggingface#3192) [GRPO] Improve completion length logging (huggingface#3188) preliminary openai compatible endpoint early concept, needs refining dedupe debug print some slop to work on unslop, missing hist almost valid pseudocode middle-ware monkey patch in mp.Pool()... remove unused More accurate .md need gpu renting lambda again much nicer small aider-chat and datasets conflict risky reqs change should work, but hacky some insights, but monkeypatching probably wont suffice refactor: Rewrite test script to use SWE-bench dataset with MultiProcessAider refactor: Remove logging statements from test.py one step closer finally, the correct abstraction doc todo unslop unslop undo accidental black cleaner abstraction new abstraction
Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com> Co-authored-by: Quentin Gallouédec <gallouedec.quentin@gmail.com>
What does this PR do?
Below, let me briefly explain my motives and reasons, the code mainly uses the part marked in the figure.

if (coef_1 < (1 - self.epsilon_low) and advantage > 0) or (coef_1 > (1 + self.epsilon_high) and advantage < 0)
then coef_1 * advantage < (1 - self.epsilon_low) * advantage
coef_1 * advantage < (1 + self.epsilon_high) * advantage
min(per_token_loss1, per_token_loss2) = per_token_loss1
From the perspective of loss, it is actually not clipped.
clip_ratio = (is_clipped * completion_mask).sum() / completion_mask.sum()
And this code also calculates an effective clip_ratio
original code:
is_clipped = (coef_1 < (1 - self.epsilon_low)) | (coef_1 > (1 + self.epsilon_high))
Therefore, we must combine the sign of the advantage with the original code to calculate is_clipped, thereby computing an effective clip_ratio
As follows
is_clipped = (coef_1 < (1 - self.epsilon_low) and advantage < 0) or (coef_1 > (1 + self.epsilon_high) and advantage > 0)
if (coef_1 < (1 - self.epsilon_low) and advantage < 0) or (coef_1 > (1 + self.epsilon_high) and advantage > 0)
then coef_1 * advantage > (1 - self.epsilon_low) * advantage
coef_1 * advantage > (1 + self.epsilon_high) * advantage
min(per_token_loss1, per_token_loss2) = per_token_loss2
Therefore, by modifying is_clipped as described above, the effective clip_ratio can be correctly calculated