Skip to content

Conversation

pandong2011
Copy link
Contributor

@pandong2011 pandong2011 commented Mar 29, 2025

What does this PR do?

Below, let me briefly explain my motives and reasons, the code mainly uses the part marked in the figure.
image

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

@HuggingFaceDocBuilderDev

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.

Copy link
Member

@qgallouedec qgallouedec left a 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

@pandong2011
Copy link
Contributor Author

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

@qgallouedec
Copy link
Member

Ah yes good point

@qgallouedec
Copy link
Member

what is clip_radio?

@qgallouedec qgallouedec changed the title Fix is_clipped to compute the effective clip_radio 📎 Fix is_clipped to compute the effective clip_ratio Mar 31, 2025
@qgallouedec qgallouedec merged commit 5b586da into huggingface:main Mar 31, 2025
7 of 9 checks passed
kashif pushed a commit to kashif/trl that referenced this pull request Mar 31, 2025
Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com>
Co-authored-by: Quentin Gallouédec <gallouedec.quentin@gmail.com>
BjarniHaukur pushed a commit to ASSERT-KTH/trl that referenced this pull request Apr 15, 2025
…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
yxliu-TAMU pushed a commit to mincheolseong/ECEN743-GRPO-Project-Proposal that referenced this pull request Apr 20, 2025
Co-authored-by: Quentin Gallouédec <45557362+qgallouedec@users.noreply.github.com>
Co-authored-by: Quentin Gallouédec <gallouedec.quentin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants