fix(algorithms): honour the configured KL clamps in the reward-side KL - #3853
Open
tianyi-zhang-02 wants to merge 1 commit into
Open
fix(algorithms): honour the configured KL clamps in the reward-side KL#3853tianyi-zhang-02 wants to merge 1 commit into
tianyi-zhang-02 wants to merge 1 commit into
Conversation
Under use_kl_in_reward, GeneralizedAdvantageEstimator and ReinforcePlusPlusAdvantageEstimator both call calculate_kl without input_clamp_value / output_clamp_value, so the user's configured values are silently replaced by the function defaults (20.0 / 10.0). They already read reference_policy_kl_penalty and reference_policy_kl_type off the same ClippedPGLossConfig that carries the two clamps, and ClippedPGLossFn passes both through -- so setting kl_output_clamp_value moves the loss-side KL and leaves the reward-side one alone. Same config, same approximation, two different bounds, no error. Tests are CPU-only. Note what they had to do to be non-vacuous: both estimators normalize the advantage globally at the end, so a KL that is constant across the batch normalizes away to zeros whatever the clamp does. The reference has to diverge by a different amount at each position for the clamp to be observable at all. Mutation-tested: dropping the two kwargs from either call site, and hardcoding a clamp to its old default, each turn one of these red. Signed-off-by: Tianyi Zhang <zhangtianyi975@gmail.com> Signed-off-by: Tianyi Zhang <123608656+tianyi-zhang-02@users.noreply.github.com>
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.
What does this PR do ?
Makes the reward-side KL use the clamp values that are configured for it, instead of falling back to
calculate_kl's defaults.Issues
None to close.
Usage
No new config.
loss_fn.kl_input_clamp_value/loss_fn.kl_output_clamp_valuenow reach the reward-side KL as well as the loss-side one.Before your PR is "Ready for review"
Pre checks:
Additional Information
GeneralizedAdvantageEstimatorandReinforcePlusPlusAdvantageEstimatorboth take aClippedPGLossConfigand read three things off it:The same config also carries
kl_input_clamp_valueandkl_output_clamp_value, andClippedPGLossFnpasses both intocalculate_kl. The estimators did not, so withuse_kl_in_rewardthe configured bounds were silently replaced by the function's own defaults, 20.0 and 10.0.Same config, same KL approximation, two different bounds depending on which side of the algorithm reads it — and no error either way.
On the tests. Getting them to assert anything took one non-obvious step worth flagging for review: both estimators normalize the advantage globally at the end, so a KL that is constant across the batch normalizes away to all-zeros no matter what the clamp does. My first version of these tests passed with the fix reverted for exactly that reason. The reference logprobs have to diverge by a different amount at each position before the clamp is observable at all.
Mutation-tested: dropping the two kwargs from either call site, and hardcoding a clamp back to its old default, each turn one of these red.
Adjacent to my #3512, which wraps these returns in an
AdvantageResult— the tests accept a bare tensor, a(advantages, returns)tuple, or the dataclass, so they survive that rebase whichever lands first. :)