generated from fastai/nbdev_template
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
ScaleRL: Add CISPO Loss #4495
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
Merged
Merged
ScaleRL: Add CISPO Loss #4495
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
44577a1
CISPO Loss
pramodith 13512d2
fix coef_1 calculation
pramodith 0ced03b
Add paper index.
pramodith ecd2c22
bugs
pramodith b8de22d
clean up.
pramodith 7472f9e
precommit
pramodith 4e9e47f
Update docs.
pramodith 442943a
recommended value
pramodith 6b30fb0
nits
pramodith 3de5ce8
nits
pramodith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1816,19 +1816,25 @@ def _compute_loss(self, model, inputs): | |||||
| f"Unknown importance sampling level: {self.importance_sampling_level}. Possible values are 'token' " | ||||||
| "and 'sequence'." | ||||||
| ) | ||||||
|
|
||||||
| coef_1 = torch.exp(log_importance_weights) | ||||||
|
|
||||||
| # From here, log_importance_weights (and all subsequent tensors, coef_1, coef_2, etc.) shape depends on | ||||||
| # importance_sampling_level: "token" level: (B, T); "sequence" level: (B, 1) | ||||||
| if self.loss_type in ["grpo", "bnpo", "dr_grpo", "dapo"]: | ||||||
| clamped_ratios = torch.clamp(coef_1, max=self.epsilon_high).detach() | ||||||
| per_token_loss = -clamped_ratios * advantages.unsqueeze(1) * per_token_logps | ||||||
|
|
||||||
| coef_1 = torch.exp(log_importance_weights) | ||||||
| coef_2 = torch.clamp(coef_1, 1 - self.epsilon_low, 1 + self.epsilon_high) | ||||||
| else: | ||||||
| coef_2 = torch.clamp(coef_1, 1 - self.epsilon_low, 1 + self.epsilon_high) | ||||||
| # Two-sided clipping | ||||||
| if self.args.delta is not None: | ||||||
| coef_1 = torch.clamp(coef_1, max=self.args.delta) | ||||||
|
|
||||||
| # Two-sided clipping | ||||||
| if self.args.delta is not None: | ||||||
| coef_1 = torch.clamp(coef_1, max=self.args.delta) | ||||||
| per_token_loss1 = coef_1 * advantages.unsqueeze(1) | ||||||
| per_token_loss2 = coef_2 * advantages.unsqueeze(1) | ||||||
| per_token_loss = -torch.min(per_token_loss1, per_token_loss2) | ||||||
|
|
||||||
| per_token_loss1 = coef_1 * advantages.unsqueeze(1) | ||||||
| per_token_loss2 = coef_2 * advantages.unsqueeze(1) | ||||||
| per_token_loss = -torch.min(per_token_loss1, per_token_loss2) | ||||||
| if entropy_mask is not None: | ||||||
| per_token_loss = per_token_loss * entropy_mask | ||||||
|
|
||||||
|
|
@@ -1847,7 +1853,7 @@ def _compute_loss(self, model, inputs): | |||||
| elif self.loss_type == "dr_grpo": | ||||||
| loss = (per_token_loss * completion_mask).sum() / (per_token_loss.size(0) * self.max_completion_length) | ||||||
| loss = loss / self.current_gradient_accumulation_steps | ||||||
| elif self.loss_type == "dapo": | ||||||
| elif self.loss_type in ["cispo", "dapo"]: | ||||||
| normalizer = inputs["num_items_in_batch"] / self.accelerator.num_processes | ||||||
| loss = (per_token_loss * completion_mask).sum() / normalizer | ||||||
| else: | ||||||
|
|
@@ -1871,23 +1877,30 @@ def masked_batch_mean(x): | |||||
| mean_entropy = masked_batch_mean(entropies) | ||||||
| self._metrics[mode]["entropy"].append(self.accelerator.gather(mean_entropy).nanmean().item()) | ||||||
|
|
||||||
| # Compute the clipped probability ratios | ||||||
| is_low_clipped = (coef_1 < 1 - self.epsilon_low) & (advantages.unsqueeze(1) < 0) | ||||||
| is_high_clipped = (coef_1 > 1 + self.epsilon_high) & (advantages.unsqueeze(1) > 0) | ||||||
| is_region_clipped = is_low_clipped | is_high_clipped | ||||||
|
|
||||||
| low_clip = masked_batch_mean(is_low_clipped.float()) | ||||||
| high_clip = masked_batch_mean(is_high_clipped.float()) | ||||||
| clip_ratio = masked_batch_mean(is_region_clipped.float()) | ||||||
|
|
||||||
| gathered_low_clip = self.accelerator.gather(low_clip) | ||||||
| self._metrics[mode]["clip_ratio/low_mean"].append(gathered_low_clip.nanmean().item()) | ||||||
| self._metrics[mode]["clip_ratio/low_min"].append(nanmin(gathered_low_clip).item()) | ||||||
| gathered_high_clip = self.accelerator.gather(high_clip) | ||||||
| self._metrics[mode]["clip_ratio/high_mean"].append(gathered_high_clip.nanmean().item()) | ||||||
| self._metrics[mode]["clip_ratio/high_max"].append(nanmax(gathered_high_clip).item()) | ||||||
| gathered_clip_ratio = self.accelerator.gather(clip_ratio) | ||||||
| self._metrics[mode]["clip_ratio/region_mean"].append(gathered_clip_ratio.nanmean().item()) | ||||||
| if self.loss_type != "cispo": | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit, again (explicit better than implicit
Suggested change
|
||||||
| # Compute the clipped probability ratios | ||||||
| is_low_clipped = (coef_1 < 1 - self.epsilon_low) & (advantages.unsqueeze(1) < 0) | ||||||
| is_high_clipped = (coef_1 > 1 + self.epsilon_high) & (advantages.unsqueeze(1) > 0) | ||||||
| is_region_clipped = is_low_clipped | is_high_clipped | ||||||
|
|
||||||
| low_clip = masked_batch_mean(is_low_clipped.float()) | ||||||
| high_clip = masked_batch_mean(is_high_clipped.float()) | ||||||
| clip_ratio = masked_batch_mean(is_region_clipped.float()) | ||||||
|
|
||||||
| gathered_low_clip = self.accelerator.gather(low_clip) | ||||||
| self._metrics[mode]["clip_ratio/low_mean"].append(gathered_low_clip.nanmean().item()) | ||||||
| self._metrics[mode]["clip_ratio/low_min"].append(nanmin(gathered_low_clip).item()) | ||||||
| gathered_high_clip = self.accelerator.gather(high_clip) | ||||||
| self._metrics[mode]["clip_ratio/high_mean"].append(gathered_high_clip.nanmean().item()) | ||||||
| self._metrics[mode]["clip_ratio/high_max"].append(nanmax(gathered_high_clip).item()) | ||||||
| gathered_clip_ratio = self.accelerator.gather(clip_ratio) | ||||||
| self._metrics[mode]["clip_ratio/region_mean"].append(gathered_clip_ratio.nanmean().item()) | ||||||
| elif self.loss_type == "cispo": | ||||||
| is_cispo_clipped = (coef_1 > self.epsilon_high) & (advantages.unsqueeze(1) > 0) | ||||||
| cispo_clip_ratio = masked_batch_mean(is_cispo_clipped.float()) | ||||||
| gathered_cispo_clip_ratio = self.accelerator.gather(cispo_clip_ratio) | ||||||
| self._metrics[mode]["cispo_clip_ratio"].append(gathered_cispo_clip_ratio.nanmean().item()) | ||||||
|
|
||||||
| return loss | ||||||
|
|
||||||
| def prediction_step(self, model, inputs, prediction_loss_only, ignore_keys: list[str] | None = None): | ||||||
|
|
||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
maybe in the documentation of
epsilon_highwe can mention that this is the value used for epsilon_max when used with CISPO loss. and that the paper recommends =5.0