Skip to content

[BugFix] Normalize reward loss over valid pairs#3886

Open
fallintoplace wants to merge 1 commit into
pytorch:mainfrom
fallintoplace:fix/reward-loss-valid-count
Open

[BugFix] Normalize reward loss over valid pairs#3886
fallintoplace wants to merge 1 commit into
pytorch:mainfrom
fallintoplace:fix/reward-loss-valid-count

Conversation

@fallintoplace

Copy link
Copy Markdown

Summary

  • Track how many chosen/rejected pairs actually contribute to GPT2RewardModel.compute_reward_loss.
  • Normalize by that valid pair count instead of the original batch size when identical pairs are skipped.
  • Keep all-identical batches returning a scalar zero, while preserving an autograd connection to the reward tensors.

Root cause

The identical-sequence fix correctly skipped pairs with no preference signal, but the final loss still divided by the original batch size. Mixed batches with skipped samples therefore silently scaled the loss and gradients down.

Tests

  • /Users/hoangvu/.pyenv/versions/3.10.13/bin/python -m pytest test/test_rlhf.py::test_compute_reward_loss_identical_sequences test/test_rlhf.py::test_compute_reward_loss_normalizes_by_non_identical_sequences
  • /Users/hoangvu/.pyenv/versions/3.10.13/bin/python -m pytest test/test_rlhf.py (2 passed, 75 skipped; optional LLM dependencies unavailable locally)

@pytorch-bot

pytorch-bot Bot commented Jun 20, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3886

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

⏳ No Failures, 23 Pending

As of commit 798243e with merge base 92a3f7f (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Integrations/torch_geometric Integrations Modules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants