Repository navigation
fix(kl): make the adaptive KL coefficient update work - #645
Open
David-Wu1119 wants to merge 1 commit into
Open
David-Wu1119 wants to merge 1 commit into
David-Wu1119 wants to merge 1 commit into
Conversation
`KLFn.update_kl_coef` clipped the proportional error with `torch.clip` on a Python float, which raises `TypeError` (torch.clip only accepts tensors). It is called from `apply_kl_penalty_to_reward` on every trainer step, so any run with a reward KL penalty and `kl_penalty_fn_args.adaptive: true` crashed at the first step instead of adapting the coefficient. Clip with `min`/`max`, matching verl's AdaptiveKLController (`np.clip(current_kl / target - 1, -0.2, 0.2)`). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Bug.
KLFn.update_kl_coef(trinity/algorithm/kl_fn/kl_fn.py) clips the proportional error withtorch.clipon a Python float:torch.cliponly accepts tensors, so this raisesTypeError: clip() received an invalid combination of arguments - got (float, float, float).apply_kl_penalty_to_rewardcalls it on every trainer step for algorithms that compute advantages in the trainer, so a run with a reward KL penalty andkl_penalty_fn_args: {adaptive: true, target_kl: ..., horizon: ...}crashes at the first step. The adaptive KL option (also exposed as the "Adaptive KL Penalty" checkbox in the config manager) cannot work.Fix. Clip with
min/max. This matches verl'sAdaptiveKLController.update:np.clip(current_kl / target - 1, -0.2, 0.2), thenvalue *= 1 + proportional_error * n_steps / horizon.Tests. New
tests/algorithm/kl_fn_test.py::KLFnTest::test_adaptive_kl_penalty_updates_coefrunsapply_kl_penalty_to_rewardon aDataProtowith a known KL above and below the target and checks the updated coefficient against verl's formula. It fails onmainwith theTypeErrorand passes with this change.tests/algorithmandtests/common/rewardspass (108 tests, CPU).pre-commitpasses on the changed files. No GPU or model-weight tests were run.The change does not overlap #639, which edits the reference-log-prob reads in
apply_kl_penalty_to_reward; both PRs add tests tokl_fn_test.py.Checklist
Please check the following items before code is ready to be reviewed.
tests/algorithmandtests/common/rewardswere run; GPU and Ray tests were not)🤖 Generated with Claude Code