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

Aperta
#25,363 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@2648782102 ci sta già lavorando.

Dal 20/11/2026.

  • #25364 di @2648782102 — aperta
  • #25419 di @doge-th — chiusa senza merge

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch

Direzione di ricerca

Start with _sort_handler and _argsort_handler in backends/mlx/ops.py, then check the MLX SortTest and ArgsortTest cases. Run the reported op test harness with descending=True and compare values and indices with PyTorch. Done means descending results are correct, or those calls remain undelegated, with tests covering both operations.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

good first issue module: mlx

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 ba0d826cc8, 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

Lingua principale
Python
Stelle
5.2k
Fork
1.3k
Merge medio
1g 7h
PR unite (30g)
644

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pytorch/executorch

Tutte le issue di pytorch/executorch

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.