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

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

Los mantenedores suelen responder en 1 día

@vineetha00 ya está trabajando en esto.

Desde el 24/11/2026.

  • #25522 de @vineetha00 — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
macos, python
Área
backend

Línea de trabajo

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.

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

Descripción

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

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.