Skip to content

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

Description

@mergennachin

The MLX delegate accepts torch.sort(..., descending=True) and torch.argsort(..., descending=True) but ignores descending. Both ops are fully delegated and return ascending results.

For x = [4, -2, 7, 1]:

sort values sort indices argsort
PyTorch / expected [7, 4, 1, -2] [2, 0, 3, 1] [2, 0, 3, 1]
MLX delegate / actual [-2, 1, 4, 7] [1, 3, 0, 2] [1, 3, 0, 2]

_sort_handler and _argsort_handler both accept up to three args but only read x and dim. SortTest and ArgsortTest have no descending=True configs, so CI doesn't catch it. This is the same kind of bug as #25332 (topk ignoring largest). Either descending needs to affect the lowering, or descending=True should stay undelegated.

Repro with the MLX op test harness. It needs op_test_runner: cmake --preset mlx-release -DEXECUTORCH_BUILD_TESTS=ON && cmake --build cmake-out --target op_test_runner.

import torch
from executorch.backends.mlx.test.test_utils import OpTestCase


class Sort(torch.nn.Module):
    def forward(self, x):
        values, indices = torch.sort(x, descending=True)
        return values, indices


class Argsort(torch.nn.Module):
    def forward(self, x):
        return torch.argsort(x, descending=True)


class Case(OpTestCase):
    def __init__(self, name, model):
        self.name, self.model = name, model

    def create_model(self):
        return self.model

    def create_inputs(self):
        return (torch.tensor([4.0, -2.0, 7.0, 1.0]),)


for name, model in [("sort_descending", Sort()), ("argsort_descending", Argsort())]:
    print(name, "PASS" if Case(name, model).run_test() else "FAIL")

Both cases print MLX delegation verified and then fail the output comparison.

Environment: main at ba0d826, MLX pin 1f8e74e3, Release build, Apple M2 Ultra, macOS 27.2, Python 3.13, PyTorch 2.14.0.

Found while reviewing #25358, and verified with help from Claude Code.

cc @metascroy

Activity

  1. added
    module: mlxIssues related to MLX Backend: Metal-accelerated inference on Apple Silicon
    on Nov 20, 2026
  2. doge-th commented on Nov 21, 2026

    @doge-th

    I'd like to work on this. Plan: make _sort_handler/_argsort_handler honor descending (sort ascending, then flip along dim when requested), and add descending=True configs to SortTest/ArgsortTest so CI covers it. Will verify on Apple Silicon against current main.

  3. doge-th commented on Nov 21, 2026

    @doge-th

    Fix up in #25419 — the lowering now emits an ascending SortNode/ArgsortNode into a tmp slot followed by a full-reverse SliceNode (step=-1) along dim when descending is set, reusing _topk_handler's reverse trick, plus descending=True configs in SortTest/ArgsortTest.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomersmodule: mlxIssues related to MLX Backend: Metal-accelerated inference on Apple Silicon

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions