Fix: skip KL backward graph when coefficient is zero - #2258
Open
Dodojordi wants to merge 1 commit into
Open
Conversation
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.
Summary
When
--use-kl-lossis enabled withkl_loss_coef == 0, slime stillconstructs the reference KL autograd graph and adds the following term to the
actor loss:
This is not only unnecessary computation: it can turn an otherwise valid
policy loss and its gradients into NaN. Multiplying by a zero coefficient does
not make a non-finite KL value harmless, because
0 * NaNis stillNaN.This PR keeps KL metric reporting for observability while preventing the
zero-weight KL path from participating in actor backward or contaminating the
training loss.
Motivation
Users may want to keep reference KL metrics for observability without using
KL as a training objective.
The current implementation couples metric reporting with optimization. Even
when the coefficient is zero, it retains the KL computation graph and inserts
the zero-weight term into the total loss. If the KL calculation produces a
non-finite value under extreme log-probability differences, this can propagate
NaNs through actor backward and interrupt training despite KL regularization
being disabled.
It is therefore important to make zero mean metric-only, rather than
"compute the gradient and multiply it by zero."
Changes
When
kl_loss_coef == 0:metric path.
kl_lossas before.This prevents a non-finite metric-only KL value from poisoning the actor loss
or policy gradients.
When
kl_loss_coef != 0, the existing differentiable behavior is preserved.Behavior
use_kl_loss=Falseuse_kl_loss=True,kl_loss_coef=0use_kl_loss=True,kl_loss_coef!=0use_unbiased_kl=True,kl_loss_coef=0Reference-model execution and
kl_lossreporting remain enabled.Validation
A focused local regression test verified that:
Additional checks passed:
Example