[MLX] sum/mean(dim=None, keepdim=True) fails at runtime with a rank mismatch

未關閉
#25,718 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@vineetha00 已經在處理了。

開始於 2026年11月25日。

  • #25719 來自 @vineetha00 —— 未關閉

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
25/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
python

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

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 keepdim when dim is 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-release preset
  • macOS 15.1, Apple Silicon (MacBook Air)
  • Python 3.12.16
  • Xcode 16.2
主要語言
Python
星號
5.2k
分支
1.3k
平均合併
1 天 7 小時
30 天內合併 PR
644

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

pytorch/executorch 的其他 Issue

查看 pytorch/executorch 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。