Repository navigation
Group batches by label for SupConLoss and the BatchTriplet losses - #663
Open
danielkorat wants to merge 1 commit into
Open
danielkorat wants to merge 1 commit into
danielkorat wants to merge 1 commit into
Conversation
Trainer.train_embeddings compared the loss instance against a tuple of loss classes, so the check was always False and GROUP_BY_LABEL was never set. SupConLoss and the four BatchTriplet losses trained on random batches, where a label can appear only once; SupConLoss then returns NaN and fitting the head fails with 'Input X contains NaN'. Compare args.loss, as get_dataset already does.
Contributor
Author
|
Hi @tomaarsen 👋 |
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!
Heads up, this PR was AI-generated.
Pull Request overview
GROUP_BY_LABELnever being applied forSupConLossand the fourBatchTripletlossesDetails
Since #554 (v1.1.0),
Trainer.train_embeddingschecks the loss like this:losshere is the instance returned byget_dataset, while the tuple holds classes, so the condition is alwaysFalseand these losses train on plain random batches. They mine positives inside each batch, so a label that appears only once in a batch leaves that sample without a positive. ForSupConLossthis is a 0/0, the loss becomes NaN, and the NaN weights then make the classification head fail. The fix comparesargs.loss, asget_datasetalready does a few lines below.Reproducer (6 classes, 4 samples each, batches of 8):
mainst_trainer.args.batch_samplerBatchSamplers.BATCH_SAMPLERBatchSamplers.GROUP_BY_LABELembedding_lossper step0.0, 0.0, 0.01.45, 1.55, 1.44trainer.train()ValueError: Input X contains NaN.The logged
0.0hides the problem: with the defaultlogging_nan_inf_filter=True, transformers leaves non-finite losses out of the logged value. With few classes (e.g. binary) a label rarely appears only once in a batch, so the bug mostly shows up as fewer in-batch positives than intended, without any error.Tests
test_trainer_groups_batches_by_label_for_in_batch_lossestrains each of the five losses on a 6-class dataset and asserts thatGROUP_BY_LABELis set. It fails for all five onmainand passes with the fix.pytest tests/test_trainer.py(Python 3.12.14, macOS, torch 2.14.1, transformers 5.18.0, sentence-transformers 6.1.0)make style && make quality(Python 3.9, black 25.11.0, as in the quality workflow)🤖 Generated with Claude Code