feat: add TripletMarginLoss#5175
Open
Bellman281 wants to merge 4 commits into
Open
Conversation
Codecov Report❌ Patch coverage is
❌ Your project check has failed because the head coverage (66.11%) is below the target coverage (80.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #5175 +/- ##
==========================================
+ Coverage 66.03% 66.11% +0.07%
==========================================
Files 1206 1217 +11
Lines 183117 183581 +464
==========================================
+ Hits 120927 121371 +444
- Misses 62190 62210 +20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Removed merge conflict markers and cleaned up imports.
Charles23R
approved these changes
Jul 17, 2026
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.
Checklist
cargo run-checkscommand has been executed.Related Issues/PRs
Part of the PyTorch loss-parity effort, following
SoftMarginLoss(#5149),MarginRankingLoss(#5151), andHingeEmbeddingLoss.Changes
Adds
TripletMarginLoss/TripletMarginLossConfig, matchingtorch.nn.TripletMarginLoss:for each triplet,
max(0, ||a − p||_p − ||a − n||_p + margin)(defaultsmargin = 1.0,p = 2.0). Distances useburn::tensor::linalg::lp_norm, and the loss composes existingops so autodiff is free. Follows the
MarginRankingLossconfig/module pattern; added to thebook's loss table.
Testing
Known-answer test over
forward_no_reduction,Mean, andSumagainst PyTorch referencevalues (3 triplets exercising a normal, a partial, and a clamped-to-zero case), plus a display
test.
cargo run-checkspasses.