[MLX] argmax/argmin(keepdim=True) without `dim` fails at runtime with a rank mismatch
Mantenedores costumam responder em até 1 dia
Avaliação
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Facilidade para iniciantes
- 25/100
Direção de pesquisa
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.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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
- Linguagem predominante
- Python
- Estrelas
- 5.2k
- Forks
- 1.3k
- Merge médio
- 1d 7h
- PRs com merge (30d)
- 644
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Tem um modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de pytorch/executorch
-
to_edge_transform_and_lower ignores some ops_to_not_decompose requests: multi-output ops, and non-core ops without a decompositionTalvez já em andamento @Eureka-Zhang assumiu há 7 dias. Abertamodule: exir
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 66/100
pytorch/executorch#25620 ·
Mantenedores costumam responder em até 1 dia
-
Qualcomm AI Engine Direct - fp16a8w quantization crashes on layer_normTalvez já em andamento @psiddh assumiu há 21 dias. Abertabug module: qnn partner: qualcomm
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
pytorch/executorch#24434 · 1 responsável ·
Mantenedores costumam responder em até 1 dia
-
enhancement triaged
Dificuldade 2/5 Meio dia Facilidade para iniciantes 68/100
pytorch/executorch#21640 ·
Mantenedores costumam responder em até 1 dia
-
quantize_per_channel and dequantize_per_tensor.tensor abort the process on a float32 scaleTalvez já em andamento @AxelNoun assumiu hoje. Aberta
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 35/100
pytorch/executorch#25873 ·
Mantenedores costumam responder em até 1 dia
-
XNNPACK INT4 linear returns incorrect per-channel outputs and fails groupwise FP16 on Apple M4 MaxTalvez já em andamento Um pull request vinculado a esta issue está aberto ou já foi mesclado. Abertabug module: quantization module: xnnpack
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 43/100
pytorch/executorch#25854 ·
Mantenedores costumam responder em até 1 dia
Todas as issues de pytorch/executorch
Issues semelhantes
-
Container fails to start when GitHub Releases is unavailable: uv run re-downloads the tvl wheelAberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
trezor/trezor-user-env#421 ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 84/100
-
Dificuldade 1/5 1-3 horas Facilidade para iniciantes 91/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 82/100
tangentcode/tangentswarm#167 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
Mantenedores costumam responder em até 1 dia