Repository navigation
[torchlib] Implement aten::cdist and aten::_cdist_forward - #3171
Open
Qiming Bao (14H034160212) wants to merge 1 commit into
Open
Qiming Bao (14H034160212) wants to merge 1 commit into
Qiming Bao (14H034160212) wants to merge 1 commit into
Conversation
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.
aten_cdistis still aNotImplementedErrorstub, sotorch.onnx.export(..., dynamo=True)fails on any model that callstorch.cdist:(#1647 was closed, but
cdistwas not implemented.)This PR implements it for
aten::cdistandaten::_cdist_forward, following PyTorch's semantics:p == 2: uses the matmul formsqrt(max(|x1|² + |x2|²ᵀ − 2·x1·x2ᵀ, 0)), as PyTorch does, forcompute_mode=1, and for the default mode when P or R is greater than 25 or not known statically. This avoids materializing the[*, P, R, M]difference tensor for large inputs. Otherwise, and forcompute_mode=2, it uses the exact difference form.p: reduces|x1[..., :, None, :] − x2[..., None, :, :]|over the last axis withReduceL1(p=1),ReduceL2,ReduceMax/ReduceMin(±inf), a non-zero count (p=0) orPow/ReduceSum.Tests:
cdisttoops_test_data.py. All 120 non-empty OpInfo subtests pass (p ∈ {1, 1.5, 2, 2.5, 3, inf}, both compute modes, broadcasting batch dims). Size-0 samples are skipped with afixme, likeflipandpixel_unshuffle: ORT returns the wrong shape when reducing a size-0 input.torch.onnx.export(dynamo=True)+ ONNX Runtime: 146/146 cases match eager (p ∈ {0, 1, 1.5, 2, 3, inf} × three compute modes × small/large/broadcast shapes × float32/float64, plus dynamic P and R).ruff check/ruff format(0.15.8) clean.