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

オープン
#25,363 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@2648782102 がすでに取り組んでいます。

2026年11月20日 から。

  • #25364 @2648782102 による — オープン
  • #25419 @doge-th による — マージされずにクローズ

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
76/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, pytorch

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
Python
スター
5.2k
フォーク
1.3k
平均マージ
1日 7時間
マージ済み PR(30日)
644

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pytorch/executorch のほかの issue

pytorch/executorch の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。