[MLX] argmax/argmin(keepdim=True) without `dim` fails at runtime with a rank mismatch

Offen
#25,520 0 Kommentare 0 Reaktionen 2 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@vineetha00 arbeitet bereits daran.

Seit 24.11.2026.

  • #25522 von @vineetha00 — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
25/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
macos, python
Bereich
backend

Rechercherichtung

The bug is in the dim is None branch of _argmax_handler and _argmin_handler in backends/mlx/ops.py, which reads keepdim but hardcodes keepdims=False. Check the existing argmax/argmin tests, which only cover dim=None with keepdim=False. Done when keepdim=True with no dim returns a tensor with x.ndim dimensions of size 1. Linked PR #25522 is already open against this issue, so coordinate there before starting.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug module: mlx triaged

torch.argmax(x, keepdim=True) and torch.argmin(x, keepdim=True) with no dim export and are delegated to MLX, but the program fails when it runs. PyTorch returns a tensor with x.ndim dimensions of size 1. The MLX handler flattens the input and emits the arg-reduction with keepdims=False, so the delegate produces a 0-d tensor and the output cannot be written.

For x = torch.tensor([[1., 5.], [7., 2.]]):

Call PyTorch / expected MLX delegate / actual
torch.argmax(x, keepdim=True) tensor([[2]]), shape [1, 1] runtime error
torch.argmin(x, keepdim=True) tensor([[0]]), shape [1, 1] runtime error
[tensor_impl.cpp:113] Attempted to change the tensor rank which is immutable: old=2, new=0
[MLXBackend.cpp:677] MLX execute failed: write_output: failed to resize output tensor
[method.cpp:1786] CALL_DELEGATE execute failed at instruction 0: 0x1

Reproduced on ExecuTorch 8c3f75dfb1d70ad12161ab3c42b06b7e7a65e8ce.

Environment: Apple Silicon MacBook Air, macOS 15.1, Xcode 16.2, Python 3.12, built with install_executorch.py.

import torch
from executorch.backends.mlx.partitioner import MLXPartitioner
from executorch.exir import to_edge_transform_and_lower
from executorch.extension.pybindings.portable_lib import (
    _load_for_executorch_from_buffer,
)


class Model(torch.nn.Module):
    def forward(self, x):
        return torch.argmax(x, keepdim=True)


inputs = (torch.tensor([[1.0, 5.0], [7.0, 2.0]]),)
model = Model().eval()
print(model(*inputs))  # tensor([[2]])
ep = torch.export.export(model, inputs)
edge = to_edge_transform_and_lower(ep, partitioner=[MLXPartitioner()])
runtime = _load_for_executorch_from_buffer(edge.to_executorch().buffer)
print(runtime.forward(inputs))  # RuntimeError

In backends/mlx/ops.py, the dim is None branch of _argmax_handler and _argmin_handler reads keepdim but hardcodes keepdims=False. The existing tests cover dim=None only with keepdim=False, and the shared test model drops keepdim when dim is None.

Found by reading the op handlers for accepted-but-unused arguments, after #25332 and #25363. I have a fix with tests and will open a PR.

Investigated and written with assistance from Anthropic Claude.

cc @metascroy

Vorherrschende Sprache
Python
Sterne
5.2k
Forks
1.3k
Ø Merge
1 T. 7 Std.
Gemergte PRs (30 T.)
644

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus pytorch/executorch

Alle Issues in pytorch/executorch

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.