Repository navigation
[MRG] Fix catastrophic cancellation in batched Euclidean distance - #932
Open
xihaian251 wants to merge 1 commit into
Open
xihaian251 wants to merge 1 commit into
xihaian251 wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #932 +/- ##
=======================================
Coverage 97.09% 97.09%
=======================================
Files 130 130
Lines 27229 27299 +70
=======================================
+ Hits 26437 26507 +70
Misses 792 792 🚀 New features to boost your workflow:
|
This branch has not been deployed
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.
ot.batch.dist_batchcomputes squared Euclidean costs with the Gram expansion‖x‖² + ‖y‖² − 2⟨x,y⟩and no non-negativity guard. With a large common coordinate offset, cancellation makes the result inaccurate: negative squared distances andNaNEuclidean distances are produced, and OT costs/plans can be corrupted.For any reference
c,‖(x−c)−(y−c)‖² = ‖x−y‖². The fix shifts both inputs by a common per-batch reference point (a static sliceX[:, 0:1, :], falling back toYfor empty source batches) before the Gram expansion, then clamps residual round-off at zero. Subtracting close values avoids theO(u·d·c²)absolute error of the unshifted expansion. Clipping alone is insufficient: it returns0instead of6on the reproduction above, leaves a self-distance of256on a float32/1e4 batch, and still produces a negative OT value insolve_batch.Downstream, a two-problem 5×5 OT batch at offset
1e8gives mastervalue_linear = [4.2e−13, −12.8]and plans differing from the Oracle by up to 0.2; with the fix it gives[3.985493198744215, 3.347919393456945](Oracle[3.985493198744212, 3.347919393456947]) and identical plans.Six regression cases fail on master and pass with the patch: the fixed counterexample, float32 and float64 large-offset properties, exact translation invariance for representable shifts, the Torch large-offset gradient, and an end-to-end
solve_batchvalue.Benchmarks with one thread, 5 warmups and 15 repetitions show a typical 6–20% overhead on medium/large CPU inputs (NumPy 70.2 → 79.3 ms at
(32,256,256,64)); complexity remainsO(B·n·m·d).NumPy, PyTorch CPU and JAX (eager,
jit,grad) were verified locally. PyTorch CUDA and TensorFlow were not tested and are not claimed as validated. Summing ordinary Euclidean distances that contain zero self-distances producesNaNgradients becausesqrtis not differentiable at zero; this is identical on master and on this patch and is out of scope. The fix targets shared coordinate offsets and does not guarantee machine precision for arbitrary layouts (for example, when the first source sample is a far outlier).Both PRs modifying these files were integrated locally in both merge orders with #926: production code and release notes merge cleanly, the test-file append/append collision was resolved by keeping both test blocks, 139 batch tests passed in each order, and #926's 13 envelope tests and VJP numerics were unchanged. These are local integration checks, not upstream CI results.
RELEASES.mdis updated to satisfy the repository's mandatory release-notes check. A clean WSL source build ran 82 batch tests (19 skipped because Torch was not installed there) and 2 doctests; a NumPy/Torch/JAX runtime ran 129 batch tests. Ruff reported no new findings, andruff format --checkandgit diff --checkwere clean.