Repository navigation
Conversation
_sort_handler and _argsort_handler read only x and dim, so torch.sort(..., descending=True) and torch.argsort(..., descending=True) were silently delegated as ascending sorts, the same class of bug as pytorch#25332 (topk ignoring largest). When descending is set, each output is now emitted as an ascending SortNode/ArgsortNode into a tmp slot followed by a full-reverse SliceNode (step=-1, start=dim_size-1, stop=-(dim_size+1)) along dim, reusing the reverse trick _topk_handler already uses so no schema or runtime changes are needed. SortTest and ArgsortTest gain descending=True configs with node-count expectations covering values, indices and both.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25419
Note: Links to docs will display an error until the docs builds have been completed.
|
|
Hi @doge-th! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
|
|
@pytorchbot label "release notes: none" |
Fixes #25363.
_sort_handlerand_argsort_handleraccept up to three args but read onlyxanddim, sotorch.sort(..., descending=True)andtorch.argsort(..., descending=True)were silently delegated as ascending sorts. This is the same class of bug as #25332 (topkignoringlargest).When
descendingis set, each output is now emitted as an ascendingSortNode/ArgsortNodeinto a tmp slot followed by a full-reverseSliceNode(step=-1,start=dim_size-1,stop=-(dim_size+1)) alongdim— the same reverse trick_topk_handleralready uses, so no schema or runtime changes are needed. Ascending lowering is unchanged.SortTestandArgsortTestgaindescending=Trueconfigs (values / indices / both) with node-count expectations asserting the tmp-sort + reverse-slice sequence; CI previously had nodescending=Truecoverage, which is why this went unnoticed.Verification
Python-side, on Apple Silicon (macOS 27.2, torch 2.15.0):
SortNode/ArgsortNode→SliceNode(axis, start=D-1, stop=-(D+1), step=-1), ascending variants are byte-identical in structure to before (noSliceNode).mlxsimulating the interpreter: ascendingargsort+ reverse-slice): matchestorch.sort/torch.argsorteagerdescending=Truevalues and indices exactly across shapes/dims, andmxnegative-step slicing matchesflip` semantics.ruff checkclean on the touched lines.The C++
op_test_runnercould not be built locally (MLX JIT kernel generation requires the full Xcode Metal toolchain, only CLT is available on this machine), so the addeddescending=Trueop-test configs will be exercised by the MLX CI job. Pre-existing failures intest_partitioner.py/test_serialization_dedup.pywere confirmed unrelated via stash A/B (identical counts on unpatched main).Authored with AI assistance (ZCode, GLM model).
cc @nil-is-all