Skip to content

[MLX] Honor descending in sort/argsort lowering - #25419

Open
doge-th wants to merge 1 commit into
pytorch:mainfrom
doge-th:fix-mlx-sort-descending
Open

doge-th wants to merge 1 commit into
pytorch:mainfrom
doge-th:fix-mlx-sort-descending

Conversation

@doge-th

@doge-th doge-th commented Nov 21, 2026 •

Copy link
Copy Markdown

Fixes #25363.

_sort_handler and _argsort_handler accept up to three args but read only x and dim, so torch.sort(..., descending=True) and torch.argsort(..., descending=True) were silently delegated as ascending sorts. This is the same class of bug as #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 — the same reverse trick _topk_handler already uses, so no schema or runtime changes are needed. Ascending lowering is unchanged.

SortTest and ArgsortTest gain descending=True configs (values / indices / both) with node-count expectations asserting the tmp-sort + reverse-slice sequence; CI previously had no descending=True coverage, which is why this went unnoticed.

Verification

Python-side, on Apple Silicon (macOS 27.2, torch 2.15.0):

  • Exported + partitioned/lowered + serialized each model variant, then parsed the delegate payload: all descending variants emit exactly SortNode/ArgsortNode → SliceNode(axis, start=D-1, stop=-(D+1), step=-1), ascending variants are byte-identical in structure to before (no SliceNode).
  • Algorithm check (pip mlx simulating the interpreter: ascending argsort + reverse-slice): matches torch.sort/torch.argsorteagerdescending=Truevalues and indices exactly across shapes/dims, andmxnegative-step slicing matchesflip` semantics.
  • ruff check clean on the touched lines.

The C++ op_test_runner could not be built locally (MLX JIT kernel generation requires the full Xcode Metal toolchain, only CLT is available on this machine), so the added descending=True op-test configs will be exercised by the MLX CI job. Pre-existing failures in test_partitioner.py/test_serialization_dedup.py were confirmed unrelated via stash A/B (identical counts on unpatched main).

Authored with AI assistance (ZCode, GLM model).

cc @nil-is-all

_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.
@pytorch-bot

pytorch-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🔗 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.

⚠️ 18 Awaiting Approval

As of commit 25ed8c6 with merge base f63b372 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Nov 21, 2026

Copy link
Copy Markdown

Hi @doge-th!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@linux-foundation-easycla

linux-foundation-easycla Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: doge-th / name: doge-th (25ed8c6)

@doge-th

doge-th commented Nov 21, 2026

Copy link
Copy Markdown
Author

@pytorchbot label "release notes: none"

@pytorch-bot pytorch-bot Bot added the release notes: none Do not include this in the release notes label Nov 21, 2026
@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Nov 21, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community: contribution PRs coming from community (excluding hardware partners) release notes: none Do not include this in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MLX] sort/argsort(descending=True) silently returns ascending order

1 participant