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

Đang mở
#25,363 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@2648782102 đang làm issue này rồi.

Từ ngày 20/11/2026.

  • #25364 của @2648782102 — đang mở
  • #25419 của @doge-th — đã đóng, không merge

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
76/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, pytorch
Lĩnh vực
backend, machine-learning

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Python
Star
5.2k
Fork
1.3k
Merge trung bình
1 ngày 7 giờ
Pull request đã merge (30 ngày)
644

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pytorch/executorch

Tất cả issue của pytorch/executorch

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.