Repository navigation
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4653
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ✅ You can merge normally! (1 Unrelated Failure)As of commit 396bcc2 with merge base 2bb989f ( BROKEN TRUNK - The following job failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
@coder-jayp thanks for the review! All three are in 6b55393:
|
coder-jayp
left a comment
There was a problem hiding this comment.
Thanks for the contribution!
LGTM. The paper defaults, updated docstrings, and tests look great and pass locally.
|
@coder-jayp thanks for the review! the one red check is the Examples tests job, Dr.CI marks it as broken trunk (it fails on the merge base too). |
|
Correcting what I said about the red check: it is The log ends with "Credentials could not be loaded, please check your action inputs" followed by "Executing the custom container implementation failed. Please contact your self hosted runner administrator", so this is runner infrastructure on the 1 November run rather than anything in this PR. A re-run should clear it. I cannot trigger one from a fork. |
|
@theap06 could you take a look when you get a chance? coder-jayp approved it and it still merges cleanly w/ main. |
Sorry for taking some time to get back to you! The PR looks great. I just ran some experiments for single and multi-turn for rlhf. The loss and gradients are in line with the paper Thanks for the PR! |
theap06
left a comment
There was a problem hiding this comment.
Thanks for the PR! @coder-jayp thanks for reviewing the pr!
|
@arun-elr can you rebase onto main to re-trigger ci? |
GSPO (https://arxiv.org/abs/2507.18071) uses one importance ratio per sequence, the geometric mean of the token ratios, in the GRPO clipped objective. GSPOLoss subclasses GRPOLoss and overrides only _log_weight, using the paper's GSPO-token form so the existing objective and aggregation are reused. kl_mask_threshold is rejected because the KL mask is applied after the sequence ratio is computed. Refs pytorch#4652
clip_epsilon=(3e-4, 4e-4) and aggregation="prompt_mean", as in the GSPO paper. With GRPO's 0.2 the sequence ratio rarely reaches the bounds. Document both in a Keyword Args section and test the defaults.
6b55393 to
396bcc2
Compare
|
@theap06 rebased cleanly onto latest |
Description
Adds
GSPOLoss, GSPO as aGRPOLosssubclass, in the same shape asCISPOLoss.s_i = exp(mean_t(log pi_theta - log pi_old)), in the GRPO clipped objective.GSPOLossoverrides only_log_weightand uses the paper's GSPO-token form: each valid token's log weight issg[log s_i] + (log pi_theta(y_i,t) - sg[log pi_theta(y_i,t)]). The clipped objective, entropy, KL terms and aggregation come fromGRPOLossunchanged. Withaggregation="prompt_mean"and an advantage that is constant over each sequence, this gives the paper's sequence-level objective.masking_strategy, across turns for"rlhf".kl_mask_thresholdraises aValueError, becauseGRPOLoss.forwardapplies the KL mask after the sequence ratio is computed.torchrl.objectives.llmand listed indocs/source/reference/llms.rst.Open questions from #4652, happy to adjust:
GSPOLosssubclass, or an option onGRPOLoss.GSPOLossnow defaults to the paper's values,clip_epsilon=(3e-4, 4e-4)andaggregation="prompt_mean"(Nonealso gives"prompt_mean"), instead of theGRPOLossdefaults (0.2,"token_mean"), with a test of the defaults. Happy to revert if you prefer theGRPOLossdefaults.sota-implementations/grposhould get a config option to pick the loss, with smoke test coverage and a benchmark.Motivation and Context
close #4652
verl (
actor_rollout_ref.actor.policy_loss.loss_mode=gspo) and TRL (GRPOConfig(importance_sampling_level="sequence")) support GSPO. TorchRL has GRPO, DAPO and CISPO, all with per-token ratios.Types of changes
Testing
CPU, Python 3.12, torch 2.11.0+cpu. New tests in
test/llm/test_llm_objectives.py::TestLosses, using the existing_FixedLogProbPolicyhelpers:test_gspo_matches_sequence_objective: loss and gradient match the sequence-level objective written directly from the paper, for rows with different padding, with a wide clip and with the paper's clip. Padded tokens get zero gradient.test_gspo_clips_whole_sequence: just past each clip bound, every token gradient of a sequence is zero when the advantage sign is the clipped one, and nonzero for the opposite sign.test_gspo_token_advantages: with per-token advantages[1, -1], the token gradients are[-0.5, 0.5].test_gspo_half_precision_long_sequence: a 32768-token fp16 sequence gives a finite loss.test_gspo_rejects_kl_mask.The 2 failures and the error (
test_grpo,test_failure_missing_entries,test_sft_assistant_only) also happen on main without this change in the same environment: they importtransformers, which is not installed there. ReplacingGSPOLoss._log_weightwith GRPO's per-token ratio, or letting the gradient flow through the sequence mean, or summing in fp16, makes the new tests fail.Checklist