isl-org / isl-org/Open3D

inconsistent behavior of as_tuple in nonzero method between open3d and pytorch

Open
#7,166 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
14k
Forks
2.6k
Avg merge
5d 18h
Merged PRs (30d)
6

Description

### Checklist

- [x] I have searched for [similar issues](https://github.com/isl-org/Open3D/issues).
- [x] For Python issues, I have tested with the [latest development wheel](https://www.open3d.org/docs/latest/getting_started.html#development-version-pip).
- [x] I have checked the [release documentation](https://www.open3d.org/docs/release/) and the [latest documentation](https://www.open3d.org/docs/latest/) (for `main` branch).

### Describe the issue

Seems that the behavior of as_tuple in nonzero method is not consistent with pytorch, and it is reversed from the meaning of as_tuple.

For instance, a.nonzero(as_tuple=True) retur a tensor and a.nonzero(as_tuple=False) return a list of tensors.

### Steps to reproduce the bug

```python
import open3d.core as o3c

a = o3c.Tensor([[3, 0, 0], [0, 4, 0], [5, 6, 0]])

print("a = \n{}\n".format(a))
print("a.nonzero() = \n{}\n".format(a.nonzero()))
print("a.nonzero(as_tuple = True) = \n{}".format(a.nonzero(as_tuple=True)))

import torch

a_tr = torch.Tensor([[3, 0, 0], [0, 4, 0], [5, 6, 0]])

print()
print("a_tr = \n{}\n".format(a_tr))
print("a_tr.nonzero() = \n{}\n".format(a_tr.nonzero()))
print("a_tr.nonzero(as_tuple = True) = \n{}".format(a_tr.nonzero(as_tuple=True)))
```

### Error message

```python
a =
[[3 0 0],
[0 4 0],
[5 6 0]]
Tensor[shape={3, 3}, stride={3, 1}, Int64, CPU:0, 0x55d13980e480]

a.nonzero() =
[[0 1 2 2]
Tensor[shape={4}, stride={1}, Int64, CPU:0, 0x55d139807770], [0 1 0 1]
Tensor[shape={4}, stride={1}, Int64, CPU:0, 0x55d134c006a0]]

a.nonzero(as_tuple = True) =
[[0 1 2 2],
[0 1 0 1]]
Tensor[shape={2, 4}, stride={4, 1}, Int64, CPU:0, 0x55d139b75c00]

a_tr =
tensor([[3., 0., 0.],
[0., 4., 0.],
[5., 6., 0.]])

a_tr.nonzero() =
tensor([[0, 0],
[1, 1],
[2, 0],
[2, 1]])

a_tr.nonzero(as_tuple = True) =
(tensor([0, 1, 2, 2]), tensor([0, 1, 0, 1]))
```

### Expected behavior

_No response_

### Open3D, Python and System information

```markdown
- Operating system: Ubuntu 24.04
- Python version: Python 3.12
- Open3D version: output from python: 0.19
- System architecture: x86
- Is this a remote workstation?: no
- How did you install Open3D?: pip
```

### Additional information

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.