Skip to content

Commit fc6a6b8

Browse files
bottlermeta-codesync[bot]
authored andcommitted
separate multigpu tests
Reviewed By: MichaelRamamonjisoa Differential Revision: D83477594 fbshipit-source-id: 5ea67543e288e9a06ee5141f436e879aa5cfb7f3
1 parent 7711bf3 commit fc6a6b8

File tree

2 files changed

+167
-77
lines changed

2 files changed

+167
-77
lines changed

tests/test_pointclouds.py

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Pointclouds,
1818
)
1919

20-
from .common_testing import needs_multigpu, TestCaseMixin
20+
from .common_testing import TestCaseMixin
2121

2222

2323
class TestPointclouds(TestCaseMixin, unittest.TestCase):
@@ -703,82 +703,6 @@ def test_to(self):
703703
self.assertEqual(cuda_device, cloud.device)
704704
self.assertIsNot(cloud, converted_cloud)
705705

706-
@needs_multigpu
707-
def test_to_list(self):
708-
cloud = self.init_cloud(5, 100, 10)
709-
device = torch.device("cuda:1")
710-
711-
new_cloud = cloud.to(device)
712-
self.assertTrue(new_cloud.device == device)
713-
self.assertTrue(cloud.device == torch.device("cuda:0"))
714-
for attrib in [
715-
"points_padded",
716-
"points_packed",
717-
"normals_padded",
718-
"normals_packed",
719-
"features_padded",
720-
"features_packed",
721-
"num_points_per_cloud",
722-
"cloud_to_packed_first_idx",
723-
"padded_to_packed_idx",
724-
]:
725-
self.assertClose(
726-
getattr(new_cloud, attrib)().cpu(), getattr(cloud, attrib)().cpu()
727-
)
728-
for i in range(len(cloud)):
729-
self.assertClose(
730-
cloud.points_list()[i].cpu(), new_cloud.points_list()[i].cpu()
731-
)
732-
self.assertClose(
733-
cloud.normals_list()[i].cpu(), new_cloud.normals_list()[i].cpu()
734-
)
735-
self.assertClose(
736-
cloud.features_list()[i].cpu(), new_cloud.features_list()[i].cpu()
737-
)
738-
self.assertTrue(all(cloud.valid.cpu() == new_cloud.valid.cpu()))
739-
self.assertTrue(cloud.equisized == new_cloud.equisized)
740-
self.assertTrue(cloud._N == new_cloud._N)
741-
self.assertTrue(cloud._P == new_cloud._P)
742-
self.assertTrue(cloud._C == new_cloud._C)
743-
744-
@needs_multigpu
745-
def test_to_tensor(self):
746-
cloud = self.init_cloud(5, 100, 10, lists_to_tensors=True)
747-
device = torch.device("cuda:1")
748-
749-
new_cloud = cloud.to(device)
750-
self.assertTrue(new_cloud.device == device)
751-
self.assertTrue(cloud.device == torch.device("cuda:0"))
752-
for attrib in [
753-
"points_padded",
754-
"points_packed",
755-
"normals_padded",
756-
"normals_packed",
757-
"features_padded",
758-
"features_packed",
759-
"num_points_per_cloud",
760-
"cloud_to_packed_first_idx",
761-
"padded_to_packed_idx",
762-
]:
763-
self.assertClose(
764-
getattr(new_cloud, attrib)().cpu(), getattr(cloud, attrib)().cpu()
765-
)
766-
for i in range(len(cloud)):
767-
self.assertClose(
768-
cloud.points_list()[i].cpu(), new_cloud.points_list()[i].cpu()
769-
)
770-
self.assertClose(
771-
cloud.normals_list()[i].cpu(), new_cloud.normals_list()[i].cpu()
772-
)
773-
self.assertClose(
774-
cloud.features_list()[i].cpu(), new_cloud.features_list()[i].cpu()
775-
)
776-
self.assertTrue(all(cloud.valid.cpu() == new_cloud.valid.cpu()))
777-
self.assertTrue(cloud.equisized == new_cloud.equisized)
778-
self.assertTrue(cloud._N == new_cloud._N)
779-
self.assertTrue(cloud._P == new_cloud._P)
780-
self.assertTrue(cloud._C == new_cloud._C)
781-
782706
def test_split(self):
783707
clouds = self.init_cloud(5, 100, 10)
784708
split_sizes = [2, 3]

tests/test_pointclouds_multigpu.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
import unittest
8+
9+
import numpy as np
10+
import torch
11+
from pytorch3d.structures.pointclouds import Pointclouds
12+
13+
from .common_testing import needs_multigpu, TestCaseMixin
14+
15+
16+
class TestPointclouds(TestCaseMixin, unittest.TestCase):
17+
def setUp(self) -> None:
18+
np.random.seed(42)
19+
torch.manual_seed(42)
20+
21+
@staticmethod
22+
def init_cloud(
23+
num_clouds: int = 3,
24+
max_points: int = 100,
25+
channels: int = 4,
26+
lists_to_tensors: bool = False,
27+
with_normals: bool = True,
28+
with_features: bool = True,
29+
min_points: int = 0,
30+
requires_grad: bool = False,
31+
):
32+
"""
33+
Function to generate a Pointclouds object of N meshes with
34+
random number of points.
35+
36+
Args:
37+
num_clouds: Number of clouds to generate.
38+
channels: Number of features.
39+
max_points: Max number of points per cloud.
40+
lists_to_tensors: Determines whether the generated clouds should be
41+
constructed from lists (=False) or
42+
tensors (=True) of points/normals/features.
43+
with_normals: bool whether to include normals
44+
with_features: bool whether to include features
45+
min_points: Min number of points per cloud
46+
47+
Returns:
48+
Pointclouds object.
49+
"""
50+
device = torch.device("cuda:0")
51+
p = torch.randint(low=min_points, high=max_points, size=(num_clouds,))
52+
if lists_to_tensors:
53+
p.fill_(p[0])
54+
55+
points_list = [
56+
torch.rand(
57+
(i, 3), device=device, dtype=torch.float32, requires_grad=requires_grad
58+
)
59+
for i in p
60+
]
61+
normals_list, features_list = None, None
62+
if with_normals:
63+
normals_list = [
64+
torch.rand(
65+
(i, 3),
66+
device=device,
67+
dtype=torch.float32,
68+
requires_grad=requires_grad,
69+
)
70+
for i in p
71+
]
72+
if with_features:
73+
features_list = [
74+
torch.rand(
75+
(i, channels),
76+
device=device,
77+
dtype=torch.float32,
78+
requires_grad=requires_grad,
79+
)
80+
for i in p
81+
]
82+
83+
if lists_to_tensors:
84+
points_list = torch.stack(points_list)
85+
if with_normals:
86+
normals_list = torch.stack(normals_list)
87+
if with_features:
88+
features_list = torch.stack(features_list)
89+
90+
return Pointclouds(points_list, normals=normals_list, features=features_list)
91+
92+
@needs_multigpu
93+
def test_to_list(self):
94+
cloud = self.init_cloud(5, 100, 10)
95+
device = torch.device("cuda:1")
96+
97+
new_cloud = cloud.to(device)
98+
self.assertTrue(new_cloud.device == device)
99+
self.assertTrue(cloud.device == torch.device("cuda:0"))
100+
for attrib in [
101+
"points_padded",
102+
"points_packed",
103+
"normals_padded",
104+
"normals_packed",
105+
"features_padded",
106+
"features_packed",
107+
"num_points_per_cloud",
108+
"cloud_to_packed_first_idx",
109+
"padded_to_packed_idx",
110+
]:
111+
self.assertClose(
112+
getattr(new_cloud, attrib)().cpu(), getattr(cloud, attrib)().cpu()
113+
)
114+
for i in range(len(cloud)):
115+
self.assertClose(
116+
cloud.points_list()[i].cpu(), new_cloud.points_list()[i].cpu()
117+
)
118+
self.assertClose(
119+
cloud.normals_list()[i].cpu(), new_cloud.normals_list()[i].cpu()
120+
)
121+
self.assertClose(
122+
cloud.features_list()[i].cpu(), new_cloud.features_list()[i].cpu()
123+
)
124+
self.assertTrue(all(cloud.valid.cpu() == new_cloud.valid.cpu()))
125+
self.assertTrue(cloud.equisized == new_cloud.equisized)
126+
self.assertTrue(cloud._N == new_cloud._N)
127+
self.assertTrue(cloud._P == new_cloud._P)
128+
self.assertTrue(cloud._C == new_cloud._C)
129+
130+
@needs_multigpu
131+
def test_to_tensor(self):
132+
cloud = self.init_cloud(5, 100, 10, lists_to_tensors=True)
133+
device = torch.device("cuda:1")
134+
135+
new_cloud = cloud.to(device)
136+
self.assertTrue(new_cloud.device == device)
137+
self.assertTrue(cloud.device == torch.device("cuda:0"))
138+
for attrib in [
139+
"points_padded",
140+
"points_packed",
141+
"normals_padded",
142+
"normals_packed",
143+
"features_padded",
144+
"features_packed",
145+
"num_points_per_cloud",
146+
"cloud_to_packed_first_idx",
147+
"padded_to_packed_idx",
148+
]:
149+
self.assertClose(
150+
getattr(new_cloud, attrib)().cpu(), getattr(cloud, attrib)().cpu()
151+
)
152+
for i in range(len(cloud)):
153+
self.assertClose(
154+
cloud.points_list()[i].cpu(), new_cloud.points_list()[i].cpu()
155+
)
156+
self.assertClose(
157+
cloud.normals_list()[i].cpu(), new_cloud.normals_list()[i].cpu()
158+
)
159+
self.assertClose(
160+
cloud.features_list()[i].cpu(), new_cloud.features_list()[i].cpu()
161+
)
162+
self.assertTrue(all(cloud.valid.cpu() == new_cloud.valid.cpu()))
163+
self.assertTrue(cloud.equisized == new_cloud.equisized)
164+
self.assertTrue(cloud._N == new_cloud._N)
165+
self.assertTrue(cloud._P == new_cloud._P)
166+
self.assertTrue(cloud._C == new_cloud._C)

0 commit comments

Comments
 (0)