`MaskedTensor` reductions crash on explicit `dim=None`, `dim=()`, or `dim=[]`
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
- Domain
- machine-learning
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
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)raisesTypeError: 'NoneType' object is not iterable;mt.sum(dim=())andmt.sum(dim=[])raiseValueError: data.dim() must equal mask.dim(). - Expected: All three reduce across all dimensions and return a 0-D
MaskedTensormatchingmt.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
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pytorch/pytorch
-
`torch._refs.round` crashes with `OverflowError` on float tensors when `|decimals| >= 19`Possibly taken @Sun-Wukhan claimed this 1 day ago. Openbot-triaged module: decompositions module: edge cases module: int overflow module: primTorch oncall: pt2 triage review
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
bot-triaged module: decompositions module: empty tensor module: norms and normalization oncall: pt2 triaged
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
bot-triaged module: correctness (silent) module: cpu module: loss module: NaNs and Infs triage review
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
bot-triaged module: inductor module: performance oncall: pt2 triaged
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
pytorch/pytorch#208232 · 1 comment ·
Maintainers usually reply within 1 day
-
bot-triaged enhancement module: fft triaged
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Maintainers usually reply within 1 day
Similar issues
-
docs(contributing): the two things that silently block a PRPossibly taken A pull request linked to this issue is open or already merged. Openavailable documentation good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 95/100
Nixitov/Riptide-Native#944 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
JTHCode/secretguard#24 ·
Maintainers usually reply within 1 day
-
area: middleware documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
good first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 ·
Maintainers usually reply within 1 day