Repository navigation
fix(ppo): prevent NaN padding from corrupting policy loss - #614
Closed
ot4562-glitch wants to merge 2 commits into
Closed
ot4562-glitch wants to merge 2 commits into
ot4562-glitch wants to merge 2 commits into
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.
Bug
PPOConfig.loss builds log_ratio = (logprobs - old_logprobs) * mask. If a padded response position contains -inf in both log-probability streams (or NaN from an excluded padded position), the multiplication does not sanitize it: NaN * 0 remains NaN, so exp and PPO loss/statistics can be NaN even when all included response tokens are finite.
Change
Use torch.where(mask.bool(), logprobs - old_logprobs, 0) to select a zero log-ratio at excluded positions before exponentiation. Active-token PPO clipping is unchanged. Add CPU tests for -inf/-inf padded values, a NaN pad sentinel, valid gradient preservation, and the finite normal case.
Validation
PyTorch 2.10 CPU source-equivalent before/after calculation: source arithmetic returns NaN for masked -inf/-inf; changed arithmetic returns finite loss -1, zero gradient on padding, and unchanged gradients on valid tokens. Tests use the project's PPOConfig.loss method, but the full trlx dependency environment, model training, and upstream CI were not run here. This change does not address a completely empty mask.
QuotientGuard
This issue was surfaced for review in a 10-repository numerical audit using QuotientGuard (https://github.com/ot4562-glitch/QuotientGuard) synthetic coefficient observations. The QuotientGuard controls were not native trlx capture and did not automatically certify a bug (reconstruction_validation=false). The isolated PyTorch before/after behavior is the primary bug evidence. No new dependency.