fix(training): align contrastive pretraining anchors with their pairs - #437
Open
ChrisW09 wants to merge 1 commit into
Open
fix(training): align contrastive pretraining anchors with their pairs#437ChrisW09 wants to merge 1 commit into
ChrisW09 wants to merge 1 commit into
Conversation
The pair tensor is cat([positives of a0..aN, negatives of a0..aN]) but the anchor tensor was built with repeat_interleave(k * len(pairs)), i.e. [a0 x 2k, a1 x 2k, ...]. The layouts do not line up: row j of the pairs belongs to anchor j//k, while the anchor at row j is j//(2k). With the defaults (both pair types enabled) 12 of 16 (anchor, pair, label) triples referenced the wrong anchor for N=4, k=2 -- e.g. CosineEmbeddingLoss pushed an anchor away from an embedding that was actually a positive neighbour of a different anchor. The objective was effectively scrambled noise; it only lined up when exactly one of use_positive/use_negative was enabled. Anchors are now repeated anchor-major in blocks of k and tiled once per pair type, in both the pooled and unpooled branches. Fixes #414 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Fixes #414
Problem
In
ContrastivePretrainer.contrastive_loss(both the pooled and unpooled branches) the pair tensor is built asbut the anchor tensor is
These layouts do not line up. Row
jof the pairs belongs to anchorj // k, while the anchor at rowjise_{j // (2k)}. With the defaults (use_positiveanduse_negativeboth True), 12 of 16 triples reference the wrong anchor for N=4, k=2 —CosineEmbeddingLossends up pushing an anchor away from an embedding that was actually a positive neighbour of a different anchor. The pretraining objective is essentially scrambled noise; it only lines up when exactly one pair type is enabled.Fix
embeddings.repeat_interleave(k_neighbors, dim=0).repeat(len(pairs), 1)— anchor-major blocks ofk, tiled once per pair type — in both branches.Tests
Two tests appended to
tests/test_training_pretraining.py:mainand passes with the fix.🤖 Generated with Claude Code