Skip to content

Commit 4c48beb

Browse files
kylelofacebook-github-bot
authored andcommitted
Fix scatter_ error in cubify (#1067)
Summary: Error Reproduction: python=3.8.12 pytorch=1.9.1 pytorch3d=0.6.1 cudatoolkit=11.1.74 test.py: ```python import torch from pytorch3d.ops import cubify voxels = torch.Tensor([[[[0,1], [0,0]], [[0,1], [0,0]]]]).float() meshes = cubify(voxels, 0.5, device="cpu") ``` The error appears when `device="cpu"` and `pytorch=1.9.1` (works fine with pytorch=1.10.2) Error message: ```console /home/kyle/anaconda3/envs/adapt-net/lib/python3.8/site-packages/torch/_tensor.py:575: UserWarning: floor_divide is deprecated, and will be removed in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor'). (Triggered internally at /opt/conda/conda-bld/pytorch_1631630839582/work/aten/src/ATen/native/BinaryOps.cpp:467.) return torch.floor_divide(self, other) Traceback (most recent call last): File "test.py", line 5, in <module> meshes = cubify(voxels, 0.5, device="cpu") File "/home/kyle/anaconda3/envs/adapt-net/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 28, in decorate_context return func(*args, **kwargs) File "/home/kyle/Desktop/pytorch3d/pytorch3d/ops/cubify.py", line 227, in cubify idleverts.scatter_(0, grid_faces.flatten(), 0) RuntimeError: Expected index [60] to be smaller than self [27] apart from dimension 0 and to be smaller size than src [27] ``` Pull Request resolved: #1067 Reviewed By: nikhilaravi Differential Revision: D34893567 Pulled By: bottler fbshipit-source-id: aa95980f7319302044141f7821ef48129cfa37a6
1 parent 4db9fc1 commit 4c48beb

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

pytorch3d/ops/cubify.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ def cubify(voxels, thresh, device=None, align: str = "topleft") -> Meshes:
223223
grid_faces += nyxz[:, 0].view(-1, 1) * num_verts
224224
idleverts = torch.ones(num_verts * N, dtype=torch.uint8, device=device)
225225

226-
idleverts.scatter_(0, grid_faces.flatten(), 0)
226+
indices = grid_faces.flatten()
227+
if device.type == "cpu":
228+
indices = torch.unique(indices)
229+
idleverts.scatter_(0, indices, 0)
227230
grid_faces -= nyxz[:, 0].view(-1, 1) * num_verts
228231
split_size = torch.bincount(nyxz[:, 0], minlength=N)
229232
faces_list = list(torch.split(grid_faces, split_size.tolist(), 0))

0 commit comments

Comments
 (0)