Skip to content

_unique_dim does not support zero size tensor #18408

@zasdfgbnm

Description

@zasdfgbnm

Unique without dim works well

>>> a = torch.empty(5, 0)

>>> a
tensor([], size=(5, 0))

>>> a.unique(return_inverse=True)
(tensor([]), tensor([], size=(5, 0), dtype=torch.int64))

But unique with dim does not work

>>> a.unique(dim=1, return_inverse=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/gaoxiang/anaconda3/lib/python3.7/site-packages/torch/tensor.py", line 351, in unique
    dim=dim
RuntimeError: cannot reshape tensor of 0 elements into shape [0, -1]

>>> a.unique(dim=0, return_inverse=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/gaoxiang/anaconda3/lib/python3.7/site-packages/torch/tensor.py", line 351, in unique
    dim=dim
RuntimeError: cannot reshape tensor of 0 elements into shape [-1, 0]

I suggest changing the behavior to:
if dim refers to a dimension with size 0, and other dimensions have size > 0, then the output and reverse_indicies should be the same empty tensor as input. If dim refers to a dimension with size > 0, or there are other 0 sized dimensions, then reject the input with an error because whether "nothing == nothing" is not defined.

Metadata

Metadata

Assignees

Labels

triagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions