[MLX] sum/mean(dim=None, keepdim=True) fails at runtime with a rank mismatch
Les mainteneurs répondent en général sous 1 jour
Évaluation
- Difficulté
- 2/5
- Temps estimé
- 1-3 heures
- Accessibilité débutants
- 25/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- À l'abandon
- Stack technique
- python
- Domaine
- machine-learning
Piste de recherche
Start in normalize_reduction_dim in backends/mlx/ops.py, which ignores keepdim when dim is None. Run the reproduction script to confirm the rank-0 output for sum and mean. A PR (#25719) is already open against this, so check it before starting. Done means keepdim=True returns shape [1, 1] for a 2x2 input, matching PyTorch.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Describe the bug
On the MLX backend, torch.sum(x, dim=None, keepdim=True) and torch.mean(x, dim=None, keepdim=True) export and lower fine, then fail at runtime. PyTorch keeps every dimension as size 1 (a 2x2 input gives shape [1, 1]), but the MLX delegate produces a 0-d result, so writing the output fails with a rank mismatch.
Cause: normalize_reduction_dim in backends/mlx/ops.py only reads keepdim when dim is a list or an int. When dim is None it returns keepdim=False without looking at the argument. Export records these calls as aten.sum.dim_IntList(x, None, True) and aten.mean.dim(x, None, True), so the flag is there and gets dropped.
To reproduce
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 M(torch.nn.Module):
def forward(self, x):
return torch.sum(x, dim=None, keepdim=True) # same with torch.mean
x = torch.tensor([[1.0, 2.0], [4.0, 8.0]])
print("PyTorch:", M()(x).tolist(), list(M()(x).shape))
ep = torch.export.export(M(), (x,))
edge = to_edge_transform_and_lower(ep, partitioner=[MLXPartitioner()])
out = _load_for_executorch_from_buffer(edge.to_executorch().buffer).forward((x,))[0]
print("MLX: ", out.tolist(), list(out.shape))
Expected (PyTorch)
sum: [[15.0]] shape=[1, 1]
mean: [[3.75]] shape=[1, 1]
Actual (MLX)
Both ops fail the same way:
Failed to execute method forward, error: 0x1: Attempted to change the tensor rank which is immutable: old=2, new=0; MLX execute failed: write_output: failed to resize output tensor; CALL_DELEGATE execute failed
Notes
- Same family as #25520 (argmax/argmin drop
keepdimwhendimis omitted), but a different code path: this one is in the shared helper. - I have a fix with tests and will open a PR right after filing this.
Investigated and written with assistance from Anthropic Claude.
Versions
- ExecuTorch: main at 8c3f75dfb1, built from source with the
mlx-releasepreset - macOS 15.1, Apple Silicon (MacBook Air)
- Python 3.12.16
- Xcode 16.2
- Langage dominant
- Python
- Étoiles
- 5.2k
- Forks
- 1.3k
- Merge moyen
- 1 j 7 h
- PR mergées (30 j)
- 644
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de pytorch/executorch
-
to_edge_transform_and_lower ignores some ops_to_not_decompose requests: multi-output ops, and non-core ops without a decompositionPeut-être pris @Eureka-Zhang l’a pris il y a 7 jours. Ouvertemodule: exir
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
pytorch/executorch#25620 ·
Les mainteneurs répondent en général sous 1 jour
-
Qualcomm AI Engine Direct - fp16a8w quantization crashes on layer_normPeut-être pris @psiddh l’a pris il y a 21 jours. Ouvertebug module: qnn partner: qualcomm
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
pytorch/executorch#24434 · 1 personne assignée ·
Les mainteneurs répondent en général sous 1 jour
-
enhancement triaged
Difficulté 2/5 Une demi-journée Accessibilité débutants 68/100
pytorch/executorch#21640 ·
Les mainteneurs répondent en général sous 1 jour
-
quantize_per_channel and dequantize_per_tensor.tensor abort the process on a float32 scalePeut-être pris @AxelNoun l’a pris aujourd’hui. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
pytorch/executorch#25873 ·
Les mainteneurs répondent en général sous 1 jour
-
XNNPACK INT4 linear returns incorrect per-channel outputs and fails groupwise FP16 on Apple M4 MaxPeut-être pris Une pull request liée à cette issue est ouverte ou déjà fusionnée. Ouvertebug module: quantization module: xnnpack
Difficulté 4/5 3-5 jours Accessibilité débutants 43/100
pytorch/executorch#25854 ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de pytorch/executorch
Issues similaires
-
Container fails to start when GitHub Releases is unavailable: uv run re-downloads the tvl wheelOuverte
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
trezor/trezor-user-env#421 ·
-
Support new version of pythonOuverte
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
-
Difficulté 1/5 1-3 heures Accessibilité débutants 91/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
tangentcode/tangentswarm#167 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
Les mainteneurs répondent en général sous 1 jour