Repository navigation
fix(kto): sum chosen/rejected rewards per sample when chunk_size > 1 - #1495
Merged
vaibhavjindal merged 1 commit intoSep 27, 2026
Merged
Conversation
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
approved these changes
Sep 27, 2026
vaibhavjindal
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for making this change!
vaibhavjindal
enabled auto-merge
September 27, 2026 08:33
kwy404
added a commit
to kwy404/kwy404
that referenced
this pull request
Sep 27, 2026
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.
Summary
Root cause: in
LigerFusedLinearKTOFunction.preference_loss_fn,rewardshas shape(chunk_size,), but it is multiplied bypreference_labels_chunk.unsqueeze(1), which has shape(chunk_size, 1). Broadcasting turns the product into a(chunk_size, chunk_size)matrix, sochosen_rewards_sumbecomesnum_chosen_in_chunk * sum(rewards)andrejected_rewards_sumbecomesnum_rejected_in_chunk * sum(rewards). With the defaultchunk_size=1the matrix is 1x1 and the result is right, which is why the existing tests did not catch it. Withchunk_size > 1and 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=1returns chosen 0.193 and rejected -1.435, whilechunk_size=4returns -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_suminfused_linear_unpaired_preference.py; the two reward lines inkto_loss.pystill had the oldunsqueeze(1).Testing Done
Added
test_kto_rewards_sum_with_chunk_size, which checks that the chosen and rejected reward sums withchunk_size2 and 4 matchchunk_size=1. On main it fails (chosen sum -85.45 withchunk_size=2and -170.90 withchunk_size=4, vs -38.35 withchunk_size=1) and it passes with the fix.python -m pytest test/chunked_loss/test_kto_loss.py(54 passed)make checkstylechecks:ruff check .andruff 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)