`MaskedTensor` reductions crash on explicit `dim=None`, `dim=()`, or `dim=[]`

Open Beginner friendly
#208,071 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch

Research direction

Start in torch/masked/maskedtensor/reductions.py at _torch_reduce and _multidim_any, then inspect nearby MaskedTensor reduction tests. Reproduce the failures with explicit dim=None, dim=(), and dim=[]; compare them with mt.sum(). Done means all three calls return the same 0-D MaskedTensor result, with regression tests covering each form.

Written by the indexing model from the issue text.

Description

bot-triaged module: masked operators module: reductions triaged
Description

Calling MaskedTensor reduction methods (e.g., mt.sum, mt.mean, mt.prod) with explicit dim=None, dim=(), or dim=[] crashes instead of reducing over all dimensions like eager Tensor and torch.masked.<op>.

In torch/masked/maskedtensor/reductions.py, _torch_reduce (line 114) only routes to _torch_reduce_all when len(args) == 1 and len(kwargs) == 0. Passing dim=None, dim=(), or dim=[] enters _torch_reduce_dim, which calls _multidim_any(mask, dim, keepdim) at line 107 without canonicalizing dim. In _multidim_any (lines 33–38), sorted(dim, reverse=True) raises TypeError for dim=None, and for dim=() or dim=[] the loop performs zero iterations, leaving mask unreduced while result_data is reduced to a 0-D scalar and triggering ValueError: data.dim() must equal mask.dim().

Reproducer
import torch
from torch.masked import masked_tensor

d = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
m = torch.tensor([[True, False], [True, True]])
mt = masked_tensor(d, m)

# Works on eager tensor and torch.masked.sum:
print(d.sum(dim=None), torch.masked.sum(mt, dim=None))

# Crashes on MaskedTensor:
mt.sum(dim=None)  # TypeError: 'NoneType' object is not iterable
mt.sum(dim=())    # ValueError: data.dim() must equal mask.dim()
mt.sum(dim=[])    # ValueError: data.dim() must equal mask.dim()
Actual vs Expected
  • Actual: mt.sum(dim=None) raises TypeError: 'NoneType' object is not iterable; mt.sum(dim=()) and mt.sum(dim=[]) raise ValueError: data.dim() must equal mask.dim().
  • Expected: All three reduce across all dimensions and return a 0-D MaskedTensor matching mt.sum().
Version

PyTorch 2.12.0

Dominant language
Python
Stars
106k
Forks
30.7k
Avg merge
7h 8m
Merged PRs (30d)
10

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pytorch/pytorch

All issues in pytorch/pytorch

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.