[fix] Patch gather_across_devices for in-batch negatives losses#3453
Merged
tomaarsen merged 7 commits intoJul 24, 2025
Conversation
There was an issue with the offset/label update not being correct
This fully removes any interleaving
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a critical bug in the gather_across_devices functionality for multiple negatives losses where anchors were being incorrectly aligned with hard negatives instead of their corresponding positives during distributed training.
- Fixes tensor gathering order to maintain proper anchor-positive alignment across devices
- Updates all four multiple negatives loss classes with the corrected gathering logic
- Improves code comments and documentation for better clarity
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| MultipleNegativesSymmetricRankingLoss.py | Fixed gathering logic and improved comments for symmetric ranking loss |
| MultipleNegativesRankingLoss.py | Applied same gathering fix to standard ranking loss |
| CachedMultipleNegativesSymmetricRankingLoss.py | Updated cached symmetric loss with corrected gathering and better documentation |
| CachedMultipleNegativesRankingLoss.py | Applied fix to cached ranking loss with improved comments |
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.
Hello!
Pull Request overview
gather_across_devicesfor in-batch negatives lossesDetails
There was an issue in my
gather_across_devicesimplementations that would ruin performance if trained with one or multiple negatives. See #3442 (comment) by @Batwu for more details.In short: my code assumed that the positives and negatives were gathered across devices like so (assuming 2 negative columns and 4 devices):
But in reality, they were gathered like so:
I updated my
labelsso that the anchors align with the corresponding positives, but it wasn't working correctly because of the above issue. It would have resulted in training anchors to align with their hard negatives instead: awful!Huge props to @Batwu , who found this issue after I completely missed it. In my tests, I only tested with
natural-questions, a dataset without negatives. I've now also done tests with multiple negatives.If it wasn't for your comment, the next release would have shipped with a serious regression.
cc @NohTow