[torchlib] Register aten::sort.stable and implement aten::argsort - #3053
Open
Anish Mehta (anishmehta24) wants to merge 1 commit into
Open
Anish Mehta (anishmehta24) wants to merge 1 commit into
Anish Mehta (anishmehta24) wants to merge 1 commit into
Conversation
torch.sort(..., stable=True) and torch.argsort(..., stable=True) lower to the `.stable` overloads, for which no ONNX function was registered: aten_sort only covered aten::sort and aten_argsort was a NotImplementedError stub, so exporting either op failed with "No ONNX function found for aten.sort.stable". aten_sort now also registers aten::sort.stable (accepting the overload's `bool? stable`), and aten_argsort is implemented on top of it for aten::argsort and aten::argsort.stable, returning the indices. Stability is not something TopK can promise, so the flag is accepted and ignored as it already was for aten::sort. Adds the argsort OpInfo test, with the same float16 xfail as sort: float16 inputs have many exact ties and the order among them is unspecified. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D56yuT4mLP36Um1WF51fEA
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.
torch.sort(x, stable=True)andtorch.argsort(x, stable=True)lower to theaten::sort.stable/aten::argsort.stableoverloads, andtorch.argsort(x)toaten::argsort. None of them had an ONNX function:aten_sortwas registered foraten::sortonly, andaten_argsortwas aNotImplementedErrorstub, sotorch.onnx.export(..., dynamo=True)failed withRepro:
Changes
aten_sortnow also registersaten::sort.stableand accepts the overload'sbool? stablekeyword. TopK is the only ONNX op that sorts and gives no stability guarantee, so the flag is accepted and ignored, exactly as it already was foraten::sort; both overloads lower to the same graph.aten_argsortis implemented on top ofaten_sort(returning the indices) and registered foraten::argsortandaten::argsort.stable.TorchLibOpInfo("argsort", ...)added to the op tests, with the same float16 xfail assort: the 8000-element float16 sample has many exact ties whose relative order is unspecified (float32 passes).Validation
pytest tests/function_libs/torch_lib/ops_test.py -k sort: 6 passed, 2 xfailed (float16 sort/argsort), 348 subtests passed.sort(stable=True)values+indices, descending,argsortdefault / descending /stable=Trueall export and match torch eager, including on inputs with duplicate values.ruff check/ruff formatclean on the touched files.AI assistance: drafted with Claude Code and validated as above.
🤖 Generated with Claude Code
https://claude.ai/code/session_01D56yuT4mLP36Um1WF51fEA