Repository navigation
fix(kl): apply the reward KL penalty only when ref_log_prob is present - #639
Open
flyingdoubleG wants to merge 1 commit into
Open
flyingdoubleG wants to merge 1 commit into
flyingdoubleG wants to merge 1 commit into
Conversation
KLFn.apply_kl_penalty_to_reward read batch["ref_log_prob"] before checking whether the key exists, so the no-reference branch (rewards unchanged, kl_coef 0) was unreachable and a batch without reference log probs raised KeyError instead, e.g. with a KL penalty configured for an algorithm that does not use a reference model and computes advantages in the trainer (on_policy_distill, jsd). Read the log probs inside the check, and add tests for both branches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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
KLFn.apply_kl_penalty_to_reward(trinity/algorithm/kl_fn/kl_fn.py) reads the reference log probs before checking that they exist:So the intended fallback (rewards unchanged,
kl_coef0) never runs; a batch withoutref_log_probraisesKeyErrorinstead. The trainer calls this for every algorithm withcompute_advantage_in_trainer, including ones that do not use a reference model (on_policy_distill,jsd), so this happens when a KL penalty function is configured for them.The change moves the two reads inside the check; behaviour with
ref_log_probpresent is unchanged.Tests:
tests/algorithm/kl_fn_test.py::test_kl_penalty_to_reward_without_reference(fails before withKeyError: 'ref_log_prob') and::test_kl_penalty_to_reward_with_reference; the file passes (13 tests, CPU).Checklist
🤖 Generated with Claude Code