fix(ppo): exclude padding tokens from entropy calculation#6121
Open
mukund1985 wants to merge 1 commit into
Open
fix(ppo): exclude padding tokens from entropy calculation#6121mukund1985 wants to merge 1 commit into
mukund1985 wants to merge 1 commit into
Conversation
INVALID_LOGPROB = 1.0 is used as a sentinel for padding positions. Without masking, each padding position contributes -1.0 to the `(-logprobs).sum()` entropy calculation, driving `objective/entropy` negative — which is mathematically impossible for a discrete distribution. Fix: apply `(~padding_mask).float()` before summing so only real tokens contribute to the entropy metric. Closes huggingface#2022
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.
Problem
objective/entropygoes negative during PPO training, which is mathematically impossible for a discrete distribution — Shannon entropy H(p) = −Σ p·log(p) ≥ 0 always.Root cause:
INVALID_LOGPROB = 1.0is used as a sentinel for padding positions. The current calculation:contributes
−1.0per padding position, driving the sum negative.Fixes #2022.
Solution
Mask padding tokens before summing:
padding_maskis already computed in the same scope. One-line change.Before submitting
AI writing disclosure
Note
Low Risk
Logging-only metric fix; no change to PPO loss, gradients, or reward computation.
Overview
Fixes
objective/entropyreporting in PPO when padded response positions were counted in the rollout logprob sum.Rollout
logprobsare masked withINVALID_LOGPROB = 1.0on padding, so(-logprobs).sum(1)added −1.0 per pad token and could make the logged entropy negative.mean_entropynow multiplies by(~padding_mask).float()before summing, using the samepadding_maskalready built for KL/rewards.Training policy loss still uses per-step entropy from logits inside micro-batches; only this end-of-step
objective/entropymetric changes. Fixes #2022.Reviewed by Cursor Bugbot for commit b9622c0. Bugbot is set up for automated code reviews on this repo. Configure here.