Skip to content

[fix] Patch gather_across_devices for in-batch negatives losses#3453

Merged
tomaarsen merged 7 commits into
huggingface:masterfrom
tomaarsen:fix/gather_across_negatives
Jul 24, 2025
Merged

[fix] Patch gather_across_devices for in-batch negatives losses#3453
tomaarsen merged 7 commits into
huggingface:masterfrom
tomaarsen:fix/gather_across_negatives

Conversation

@tomaarsen

Copy link
Copy Markdown
Member

Hello!

Pull Request overview

  • Patch gather_across_devices for in-batch negatives losses

Details

There was an issue in my gather_across_devices implementations 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):

PPPPNNNNNNNN

But in reality, they were gathered like so:

PNNPNNPNNPNN

I updated my labels so 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

  • Tom Aarsen

@tomaarsen
tomaarsen requested a review from Copilot July 21, 2025 11:03
@tomaarsen tomaarsen added the bug Something isn't working label Jul 21, 2025

This comment was marked as outdated.

@tomaarsen
tomaarsen requested a review from Copilot July 21, 2025 12:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread sentence_transformers/losses/CachedMultipleNegativesSymmetricRankingLoss.py Outdated
@tomaarsen
tomaarsen merged commit c9c86c5 into huggingface:master Jul 24, 2025
3 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants