Skip to content

fix(kto): sum chosen/rejected rewards per sample when chunk_size > 1 - #1495

Merged
vaibhavjindal merged 1 commit into
linkedin:mainfrom
kwy404:fix/kto-reward-sums-chunk-size
Sep 27, 2026
Merged

vaibhavjindal merged 1 commit into
linkedin:mainfrom
kwy404:fix/kto-reward-sums-chunk-size

Conversation

@kwy404

@kwy404 kwy404 commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Root cause: in LigerFusedLinearKTOFunction.preference_loss_fn, rewards has shape (chunk_size,), but it is multiplied by preference_labels_chunk.unsqueeze(1), which has shape (chunk_size, 1). Broadcasting turns the product into a (chunk_size, chunk_size) matrix, so chosen_rewards_sum becomes num_chosen_in_chunk * sum(rewards) and rejected_rewards_sum becomes num_rejected_in_chunk * sum(rewards). With the default chunk_size=1 the matrix is 1x1 and the result is right, which is why the existing tests did not catch it. With chunk_size > 1 and mixed labels in a chunk, both reward metrics are wrong. The loss and the gradients are not affected.

Example (B=4, labels [True, False, True, False], with a reference model): chunk_size=1 returns chosen 0.193 and rejected -1.435, while chunk_size=4 returns -2.485 for both (2 x the sum of all four rewards).

Fix: multiply by the 1D mask. This is the same change #1441 made for chosen_logps_sum / rejected_logps_sum in fused_linear_unpaired_preference.py; the two reward lines in kto_loss.py still had the old unsqueeze(1).

Testing Done

Added test_kto_rewards_sum_with_chunk_size, which checks that the chosen and rejected reward sums with chunk_size 2 and 4 match chunk_size=1. On main it fails (chosen sum -85.45 with chunk_size=2 and -170.90 with chunk_size=4, vs -38.35 with chunk_size=1) and it passes with the fix.

  • Hardware Type: NVIDIA GeForce GTX 1060 6GB, PyTorch 2.5.1 (the new test was also run CPU only)
  • python -m pytest test/chunked_loss/test_kto_loss.py (54 passed)
  • make checkstyle checks: ruff check . and ruff format --check . pass (ruff 0.14.11, as pinned in .pre-commit-config.yaml)
  • make test (not run; Triton is not available on this Windows host)
  • make test-convergence (not run; KTO is not covered by the convergence tests)

rewards has shape (chunk_size,), but it was multiplied by
preference_labels_chunk.unsqueeze(1) of shape (chunk_size, 1). That
broadcasts to a (chunk_size, chunk_size) matrix, so each sum became
num_labels_in_chunk * sum(rewards) and chosen/rejected rewards were
mixed together whenever a chunk held more than one sample. Multiply by
the 1D mask instead, as the base class already does for the logps sums.

@vaibhavjindal vaibhavjindal left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this change!

@vaibhavjindal
vaibhavjindal added this pull request to the merge queue Sep 27, 2026
Merged via the queue into linkedin:main with commit d76315b Sep 27, 2026
1 of 3 checks passed
kwy404 added a commit to kwy404/kwy404 that referenced this pull request Sep 27, 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.

2 participants