Repository navigation
[BugFix] [LLM] Detach clipped importance ratio in CISPOLoss to preserve token policy gradients - #4665
davrens101 wants to merge 1 commit into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4665
Note: Links to docs will display an error until the docs builds have been completed.
|
|
| Prefix | Label Applied | Example |
|---|---|---|
[Algorithm] |
new algo | [Algorithm] Add new RL objective |
[BE] |
BE | [BE] Improve error messages |
[Benchmark] or [Benchmarks] |
Benchmarks | [Benchmark] Add collector benchmark |
[BugFix] |
BugFix | [BugFix] Fix memory leak in collector |
[Example] or [Examples] |
Examples | [Example] Add training script |
[Feature] |
Feature | [Feature] Add new optimizer |
[Doc] or [Docs] |
Documentation | [Doc] Update installation guide |
[Refactor] |
Refactoring | [Refactor] Clean up module imports |
[CI] |
CI | [CI] Fix workflow permissions |
[Test] or [Tests] |
Tests | [Tests] Add unit tests for buffer |
[Trainer] or [Trainers] |
Trainers | [Trainer] Add trainer config |
[Environment] or [Environments] |
Environments | [Environments] Add Gymnasium support |
[Data] |
Data | [Data] Fix replay buffer sampling |
[LLM] |
llm/ | [LLM] Add reward model integration |
[Minor] |
small change | [Minor] Fix typo in error message |
[Performance] or [Perf] |
Performance | [Performance] Optimize tensor ops |
[BC-Breaking] |
bc breaking | [BC-Breaking] Remove deprecated API |
[Deprecation] |
Deprecation | [Deprecation] Mark old function |
[Algorithm] or [Algorithms] |
new algo | [Algorithm] Add new objective |
[Quality] |
Quality | [Quality] Fix typos and add codespell |
[Versioning] |
versioning | [Versioning] Bump release version |
[WIP] |
WIP | [WIP] Draft implementation |
Note: Common variations like singular/plural are supported (e.g., [Doc] or [Docs]).
coder-jayp
left a comment
There was a problem hiding this comment.
LGTM! Thanks for your contribution!
Tested locally — gradients for clamped tokens were indeed getting zeroed out on main because of autograd through clamp. The fix matches Eq. 4 from the paper, and the regression tests pass cleanly without breaking any of the other LLM objectives.
Problem
Fixes #4664.
CISPOLosscomputed its policy objective by multiplying the clamped importance ratio by the advantage, then taking gradients through that product:Autograd through
torch.clampzeroes out gradients for all tokens whose importance ratio falls outside[1 - ε, 1 + ε]. This contradicts the defining mathematical design of CISPO (MiniMax-M1 paper, eq. 4, https://arxiv.org/html/2506.13585v1#S3.E4), which requires detaching the clipped ratio and multiplying it by the current log-probability:The previous implementation caused clipped tokens to contribute exactly zero gradient, destroying the policy updates for all tokens that CISPO was designed to continue updating with bounded weights.
Solution
_compute_policy_objectivehook intorchrl/objectives/llm/grpo.pyto acceptcur_log_prob(computed in_log_weightand stored as_cur_log_prob).GRPOLoss, the objective computation remains unchanged (standard PPO-style surrogate).CISPOLoss, detach the clamped ratio and multiply bycur_log_prob * advantage:CISPOLossto document the detached importance ratio and non-vanishing out-of-interval gradients.test_cispo_detached_importance_gradientsintest/llm/test_llm_objectives.pychecking loss values and exact parameter gradients against paper equation 4 across ratios below, inside, and above the clipping interval, with both positive and negative advantages and sequence padding masks.test_cispo_no_min_clip_objectivetest which previously pinned the erroneous non-detached forward value.Verification
nn.Lineartoken policy withLLMMaskedCategoricalwas evaluated on token 0 (current[0.0, 0.0].[-0.64, +0.64](max error[-0.80, +0.80](max error[-0.96, +0.96](max errortorch.compile(backend="eager").test/llm/test_llm_objectives.py -k "cispo or grpo or dapo": 15 passed, 2 skipped, 70 deselected.test/llm/test_llm_objectives.py: 82 passed, 1 skipped, 4 deselected, 40 warnings.ufmt checkandflake8pass cleanly on changed files.Review and assistance
I supervised and checked the AI-assisted work as it progressed. AI assistance was used for investigation, reproduction, implementation, and verification. No maintainer review or approval is claimed. CLA acceptance remains an account-holder action.