[MLX] argmax/argmin(keepdim=True) without `dim` fails at runtime with a rank mismatch
维护者通常 1 天内回复
评估
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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
- 主要语言
- Python
- 星标
- 5.2k
- 派生
- 1.3k
- 平均合并
- 1 天 7 小时
- 30 天内合并 PR
- 644
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pytorch/executorch 的其他 Issue
-
to_edge_transform_and_lower ignores some ops_to_not_decompose requests: multi-output ops, and non-core ops without a decomposition可能已有人在做 @Eureka-Zhang 于 7 天前认领。 未关闭module: exir
难度 2/5 1-3 小时 新手友好度 66/100
pytorch/executorch#25620 ·
维护者通常 1 天内回复
-
Qualcomm AI Engine Direct - fp16a8w quantization crashes on layer_norm可能已有人在做 @psiddh 于 21 天前认领。 未关闭bug module: qnn partner: qualcomm
难度 2/5 1-3 小时 新手友好度 74/100
pytorch/executorch#24434 · 已指派 1 人 ·
维护者通常 1 天内回复
-
enhancement triaged
难度 2/5 半天 新手友好度 68/100
pytorch/executorch#21640 ·
维护者通常 1 天内回复
-
quantize_per_channel and dequantize_per_tensor.tensor abort the process on a float32 scale可能已有人在做 @AxelNoun 今天认领。 未关闭
难度 3/5 1-2 天 新手友好度 35/100
pytorch/executorch#25873 ·
维护者通常 1 天内回复
-
XNNPACK INT4 linear returns incorrect per-channel outputs and fails groupwise FP16 on Apple M4 Max可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭bug module: quantization module: xnnpack
难度 4/5 3-5 天 新手友好度 43/100
pytorch/executorch#25854 ·
维护者通常 1 天内回复
查看 pytorch/executorch 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
trezor/trezor-user-env#421 ·
-
难度 2/5 1-3 小时 新手友好度 84/100
-
难度 1/5 1-3 小时 新手友好度 91/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
tangentcode/tangentswarm#167 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复