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

Abierto
#25,363 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@2648782102 ya está trabajando en esto.

Desde el 20/11/2026.

  • #25364 de @2648782102 — abierto
  • #25419 de @doge-th — cerrado sin fusionar

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
76/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python, pytorch

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
Python
Estrellas
5.2k
Forks
1.3k
Merge medio
1 d 7 h
PR fusionados (30 d)
644

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de pytorch/executorch

Todos los issues de pytorch/executorch

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.