Repository navigation
Conversation
Member
|
Hello! Thanks for this! This is a useful improvement alongside #4026. With 256 examples and two samples per label on an RTX 3090, additional peak tensor memory dropped from about 209 MiB to 3 MiB, and forward/backward took roughly half the time. I've inlined Similar to #4026, I've removed the redundant SGD test and float64 parametrization. The original ties case had zero gradients throughout, so I've replaced it with an explicit check of the hinge gradient at zero.
|
tomaarsen
enabled auto-merge (squash)
September 16, 2026 15:29
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.
BatchAllTripletLossbuilds aB × B × Bloss tensor before masking invalid anchor-positive pairs. With two samples per label, onlyBof thoseB²pairs are valid.Select valid anchor-positive pairs first, then compare each with the batch's negatives. This preserves all valid triplets, the hinge and the positive-triplet denominator. Intermediate storage scales with
B × P, wherePis the number of valid anchor-positive pairs; it is quadratic for paired labels, but can still be cubic for large label groups.Measured: CPU PyTorch 2.2.2, one thread, 256 examples × 32 dimensions, two examples per label: median loss forward/backward 245 → 2.39 ms over five runs. Unique additional storage saved for backward falls 101.3 → 0.99 MB, excluding inputs. This measures the loss only, not encoder throughput or peak GPU memory. Losses match within tolerance and gradients are bitwise identical across nine benchmark configurations.
Validation: 25 tests pass locally and on Linux PyTorch 2.8. The new memory regression fails on upstream; reference comparisons cover uneven groups, ties, missing positives/negatives, both distance metrics and dtypes, and three SGD updates. Official pre-commit hooks pass. Linux run on the exact commit.