Skip to content

Commit 7f1e63a

Browse files
bottlerfacebook-github-bot
authored andcommitted
Take care with single integers on gpu
Summary: Pytorch seems to be becoming stricter about integer tensors of shape `(1,)` on GPU and not allowing them to be used as `int`s. For example the following no longer works on pytorch master, foo = torch.tensor([3, 5, 3], device="cuda:0") torch.arange(10) + foo[0] because this is the sum of tensors on different devices. Here fix tests which recently broke because of this. Reviewed By: nikhilaravi Differential Revision: D21929745 fbshipit-source-id: 25374f70468d1c895372766f1a9dd61df0833957
1 parent d0e7426 commit 7f1e63a

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

pytorch3d/structures/meshes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ def __init__(self, verts=None, faces=None, textures=None):
329329
self._num_verts_per_mesh = torch.tensor(
330330
[len(v) for v in self._verts_list], device=self.device
331331
)
332-
self._V = self._num_verts_per_mesh.max()
332+
self._V = int(self._num_verts_per_mesh.max())
333333
self._num_faces_per_mesh = torch.tensor(
334334
[len(f) for f in self._faces_list], device=self.device
335335
)
336-
self._F = self._num_faces_per_mesh.max()
336+
self._F = int(self._num_faces_per_mesh.max())
337337
self.valid = torch.tensor(
338338
[
339339
len(v) > 0 and len(f) > 0
@@ -370,7 +370,7 @@ def __init__(self, verts=None, faces=None, textures=None):
370370
# as long as the faces index correspond to the right vertices.
371371

372372
self.valid = self._num_faces_per_mesh > 0
373-
self._F = self._num_faces_per_mesh.max()
373+
self._F = int(self._num_faces_per_mesh.max())
374374
if len(self._num_faces_per_mesh.unique()) == 1:
375375
self.equisized = True
376376

pytorch3d/structures/pointclouds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def __init__(self, points, normals=None, features=None):
189189
num_points_per_cloud = torch.tensor(
190190
[len(p) for p in self._points_list], device=self.device
191191
)
192-
self._P = num_points_per_cloud.max()
192+
self._P = int(num_points_per_cloud.max())
193193
self.valid = torch.tensor(
194194
[len(p) > 0 for p in self._points_list],
195195
dtype=torch.bool,

tests/test_point_mesh_distance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def test_edge_point_distance(self):
385385
start = edges_first_idx[i]
386386
end = edges_first_idx[i + 1] if i < N - 1 else edges_packed.shape[0]
387387

388-
min_idx = idx_cuda.cpu()[start:end] - points_first_idx[i]
388+
min_idx = idx_cuda.cpu()[start:end] - points_first_idx[i].cpu()
389389
iidx = torch.arange(edges.shape[0], device=device)
390390
min_dist = dists_temp[iidx, min_idx]
391391

@@ -583,7 +583,7 @@ def test_point_face_distance(self):
583583
start = points_first_idx[i]
584584
end = points_first_idx[i + 1] if i < N - 1 else points_packed.shape[0]
585585

586-
min_idx = idx_cuda.cpu()[start:end] - faces_first_idx[i]
586+
min_idx = idx_cuda.cpu()[start:end] - faces_first_idx[i].cpu()
587587
iidx = torch.arange(points.shape[0], device=device)
588588
min_dist = dists_temp[iidx, min_idx]
589589

@@ -666,7 +666,7 @@ def test_face_point_distance(self):
666666
start = faces_first_idx[i]
667667
end = faces_first_idx[i + 1] if i < N - 1 else faces_packed.shape[0]
668668

669-
min_idx = idx_cuda.cpu()[start:end] - points_first_idx[i]
669+
min_idx = idx_cuda.cpu()[start:end] - points_first_idx[i].cpu()
670670
iidx = torch.arange(tris.shape[0], device=device)
671671
min_dist = dists_temp[iidx, min_idx]
672672

0 commit comments

Comments
 (0)