@@ -61,21 +61,18 @@ def test_cpu_gpu_equality(self):
6161class TestBallPartial (unittest .TestCase ):
6262 @run_if_cuda
6363 def test_simple_gpu (self ):
64- x = torch .tensor ([[10 , 0 , 0 ], [0.1 , 0 , 0 ], [10 , 0 , 0 ], [0.1 , 0 , 0 ]]).to (torch .float ).cuda ()
64+ x = torch .tensor ([[10 , 0 , 0 ], [0.1 , 0 , 0 ], [0.2 , 0 , 0 ], [0.1 , 0 , 0 ]]).to (torch .float ).cuda ()
6565 y = torch .tensor ([[0 , 0 , 0 ]]).to (torch .float ).cuda ()
66- batch_x = torch .from_numpy (np .asarray ([0 , 0 , 1 , 1 ])).long ().cuda ()
66+ batch_x = torch .from_numpy (np .asarray ([0 , 0 , 0 , 1 ])).long ().cuda ()
6767 batch_y = torch .from_numpy (np .asarray ([0 ])).long ().cuda ()
6868
69- batch_x = torch .from_numpy (np .asarray ([0 , 0 , 1 , 1 ])).long ().cuda ()
70- batch_y = torch .from_numpy (np .asarray ([0 ])).long ().cuda ()
71-
72- idx , dist2 = ball_query (1.0 , 2 , x , y , mode = "PARTIAL_DENSE" , batch_x = batch_x , batch_y = batch_y )
69+ idx , dist2 = ball_query (0.2 , 4 , x , y , mode = "PARTIAL_DENSE" , batch_x = batch_x , batch_y = batch_y )
7370
7471 idx = idx .detach ().cpu ().numpy ()
7572 dist2 = dist2 .detach ().cpu ().numpy ()
7673
77- idx_answer = np .asarray ([[1 , - 1 ]])
78- dist2_answer = np .asarray ([[0.0100 , - 1.0000 ]]).astype (np .float32 )
74+ idx_answer = np .asarray ([[1 , 2 , - 1 , - 1 ]])
75+ dist2_answer = np .asarray ([[0.0100 , 0.04 , - 1 , - 1 ]]).astype (np .float32 )
7976
8077 npt .assert_array_almost_equal (idx , idx_answer )
8178 npt .assert_array_almost_equal (dist2 , dist2_answer )
@@ -98,30 +95,29 @@ def test_simple_cpu(self):
9895 npt .assert_array_almost_equal (idx , idx_answer )
9996 npt .assert_array_almost_equal (dist2 , dist2_answer )
10097
101-
10298 def test_breaks (self ):
10399 x = torch .tensor ([[10 , 0 , 0 ], [0.1 , 0 , 0 ], [10 , 0 , 0 ], [10.1 , 0 , 0 ]]).to (torch .float )
104100 y = torch .tensor ([[0 , 0 , 0 ]]).to (torch .float )
105101
106102 batch_x = torch .from_numpy (np .asarray ([0 , 0 , 1 , 1 ])).long ()
107103 batch_y = torch .from_numpy (np .asarray ([0 ])).long ()
108-
104+
109105 with self .assertRaises (RuntimeError ):
110106 idx , dist2 = ball_query (1.0 , 2 , x , y , mode = "PARTIAL_DENSE" , batch_x = batch_x , batch_y = batch_y )
111107
112- def test_random_cpu (self ):
108+ def test_random_cpu (self , cuda = False ):
113109 a = torch .randn (100 , 3 ).to (torch .float )
114110 b = torch .randn (50 , 3 ).to (torch .float )
115111 batch_a = torch .tensor ([0 for i in range (a .shape [0 ] // 2 )] + [1 for i in range (a .shape [0 ] // 2 , a .shape [0 ])])
116112 batch_b = torch .tensor ([0 for i in range (b .shape [0 ] // 2 )] + [1 for i in range (b .shape [0 ] // 2 , b .shape [0 ])])
117113 R = 1
118114
119- idx , dist = ball_query (R , 15 , a , b , mode = "PARTIAL_DENSE" , batch_x = batch_a , batch_y = batch_b , sort = True )
120- idx1 , dist = ball_query (R , 15 , a , b , mode = "PARTIAL_DENSE" , batch_x = batch_a , batch_y = batch_b , sort = True )
115+ idx , dist = ball_query (R , 15 , a , b , mode = "PARTIAL_DENSE" , batch_x = batch_a , batch_y = batch_b , sort = True , )
116+ idx1 , dist = ball_query (R , 15 , a , b , mode = "PARTIAL_DENSE" , batch_x = batch_a , batch_y = batch_b , sort = True , )
121117 torch .testing .assert_allclose (idx1 , idx )
122118 with self .assertRaises (AssertionError ):
123- idx , dist = ball_query (R , 15 , a , b , mode = "PARTIAL_DENSE" , batch_x = batch_a , batch_y = batch_b , sort = False )
124- idx1 , dist = ball_query (R , 15 , a , b , mode = "PARTIAL_DENSE" , batch_x = batch_a , batch_y = batch_b , sort = False )
119+ idx , dist = ball_query (R , 15 , a , b , mode = "PARTIAL_DENSE" , batch_x = batch_a , batch_y = batch_b , sort = False , )
120+ idx1 , dist = ball_query (R , 15 , a , b , mode = "PARTIAL_DENSE" , batch_x = batch_a , batch_y = batch_b , sort = False , )
125121 torch .testing .assert_allclose (idx1 , idx )
126122
127123 self .assertEqual (idx .shape [0 ], b .shape [0 ])
@@ -136,6 +132,28 @@ def test_random_cpu(self):
136132 if p >= 0 and p < len (batch_a ):
137133 assert p in idx3_sk [i ]
138134
135+ @run_if_cuda
136+ def test_random_gpu (self ):
137+ a = torch .randn (100 , 3 ).to (torch .float ).cuda ()
138+ b = torch .randn (50 , 3 ).to (torch .float ).cuda ()
139+ batch_a = torch .tensor (
140+ [0 for i in range (a .shape [0 ] // 2 )] + [1 for i in range (a .shape [0 ] // 2 , a .shape [0 ])]
141+ ).cuda ()
142+ batch_b = torch .tensor (
143+ [0 for i in range (b .shape [0 ] // 2 )] + [1 for i in range (b .shape [0 ] // 2 , b .shape [0 ])]
144+ ).cuda ()
145+ R = 1
146+
147+ idx , dist = ball_query (R , 15 , a , b , mode = "PARTIAL_DENSE" , batch_x = batch_a , batch_y = batch_b , sort = False ,)
148+
149+ # Comparison to see if we have the same result
150+ tree = KDTree (a .cpu ().detach ().numpy ())
151+ idx3_sk = tree .query_radius (b .cpu ().detach ().numpy (), r = R )
152+ i = np .random .randint (len (batch_b ))
153+ for p in idx [i ].cpu ().detach ().numpy ():
154+ if p >= 0 and p < len (batch_a ):
155+ assert p in idx3_sk [i ]
156+
139157
140158if __name__ == "__main__" :
141159 unittest .main ()
0 commit comments