[Fix][Relax][Frontend][Torch] Emit int64 indices for sort and argsort - #20254
Open
hiyufan wants to merge 1 commit into
Open
[Fix][Relax][Frontend][Torch] Emit int64 indices for sort and argsort#20254hiyufan wants to merge 1 commit into
hiyufan wants to merge 1 commit into
Conversation
`torch.sort` and `torch.argsort` return int64 indices, but the Relax frontend emitted int32. `relax.op.argsort` defaults to `dtype="int32"`, and both call sites in `base_fx_graph_translator.py` took that default. `_topk` in the same file already overrides the identical int32 default on `relax.op.topk` to match PyTorch, and every other index-producing op the frontend supports (argmax, argmin, max.dim, median.dim, bucketize) comes out as int64. sort and argsort were the only exceptions, so a single imported graph could carry two different index dtypes for the same kind of value. The sorted values and the index values themselves were already correct; only the index dtype diverged. Pass `dtype="int64"` at both call sites, update the expectations that had the int32 result written into them, and add a `test_sort` to the exported-program tests, which had no coverage for `torch.sort`. Co-authored-by: Claude <noreply@anthropic.com>
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.
Problem
torch.sortandtorch.argsortreturnint64indices. The Relax PyTorch frontend emitsint32for them.relax.op.argsortdefaults todtype="int32", and both call sites inbase_fx_graph_translator.pytake that default:To be precise about the scope: the sorted values and the index values are already correct. Only the index dtype diverges from PyTorch.
Why this looks like an oversight rather than a deliberate choice
relax.op.topkhas the samedtype="int32"default, and_topkin this very file explicitly overrides it:So the frontend already establishes that the relax-level default has to be overridden to match PyTorch. Every index-producing torch op the frontend supports comes out as
int64— exceptsortandargsort:torch.sortint64int32torch.argsortint64int32torch.topkint64int64(explicitdtype=)torch.argmax/torch.argminint64int64torch.max(dim=)/torch.median(dim=)int64int64torch.bucketizeint64int64A single imported graph that uses both
topkandsorttherefore carries two different index dtypes for the same kind of value.Reproduce
Before / after:
Fix
Pass
dtype="int64"at bothrelax.op.argsortcall sites, matching what_topkalready does.Verification
Built with LLVM and ran the imported graph through the VM against PyTorch on
sort(dim=1),sort(descending=True),sort(dim=0)andargsort(dim=1):int32vs torchint64int64— matches torch on every caseTest suites,
tests/python/relax/test_frontend_from_fx.py+tests/python/relax/test_frontend_from_exported_program.py:main: 24 failed, 412 passed, 3 skippedThe 24 failures are pre-existing on
mainin my environment (test_dtypesand friends) and are identical before and after; the one additional pass is the newtest_sort.ruff format --checkandruff checkare clean on the touched files.Tests
test_argsort(both frontend test files) andtest_sort(test_frontend_from_fx.py) — these had theint32result written into them.test_sorttotest_frontend_from_exported_program.py; that path had no coverage fortorch.sort.This change was prepared with AI assistance (Claude). I have reviewed and verified it, and can speak to it in review.