Skip to content

fix(kl): make the adaptive KL coefficient update work - #645

Open
David-Wu1119 wants to merge 1 commit into
agentscope-ai:mainfrom
David-Wu1119:fix/adaptive-kl-coef-update
Open

David-Wu1119 wants to merge 1 commit into
agentscope-ai:mainfrom
David-Wu1119:fix/adaptive-kl-coef-update

Conversation

@David-Wu1119

@David-Wu1119 David-Wu1119 commented Nov 11, 2026 •

Copy link
Copy Markdown

Description

Bug. KLFn.update_kl_coef (trinity/algorithm/kl_fn/kl_fn.py) clips the proportional error with torch.clip on a Python float:

proportional_error = torch.clip(current_kl / target_kl - 1, -0.2, 0.2).item()

torch.clip only accepts tensors, so this raises TypeError: clip() received an invalid combination of arguments - got (float, float, float). apply_kl_penalty_to_reward calls it on every trainer step for algorithms that compute advantages in the trainer, so a run with a reward KL penalty and kl_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's AdaptiveKLController.update: np.clip(current_kl / target - 1, -0.2, 0.2), then value *= 1 + proportional_error * n_steps / horizon.

Tests. New tests/algorithm/kl_fn_test.py::KLFnTest::test_adaptive_kl_penalty_updates_coef runs apply_kl_penalty_to_reward on a DataProto with a known KL above and below the target and checks the updated coefficient against verl's formula. It fails on main with the TypeError and passes with this change. tests/algorithm and tests/common/rewards pass (108 tests, CPU). pre-commit passes 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 to kl_fn_test.py.

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has passed all tests (only the CPU tests in tests/algorithm and tests/common/rewards were run; GPU and Ray tests were not)
  • Docstrings have been added/updated in Google Style
  • Documentation has been updated
  • Code is ready for review

🤖 Generated with Claude Code

`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>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 05:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

No deployments
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.

2 participants