Skip to content

fix(ppo): prevent NaN padding from corrupting policy loss - #614

Closed
ot4562-glitch wants to merge 2 commits into
CarperAI:mainfrom
ot4562-glitch:fix/ppo-ignored-logprob-nan
Closed

ot4562-glitch wants to merge 2 commits into
CarperAI:mainfrom
ot4562-glitch:fix/ppo-ignored-logprob-nan

Conversation

@ot4562-glitch

Copy link
Copy Markdown

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.

@ot4562-glitch ot4562-glitch closed this by deleting the head repository Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant