forked from pygae/clifford
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_CUDA.py
386 lines (309 loc) · 13.8 KB
/
test_CUDA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from __future__ import absolute_import, division
from __future__ import print_function, unicode_literals
from past.builtins import range
import numpy as np
from numpy import exp, float64, testing
import unittest
import itertools
from nose.plugins.skip import SkipTest
import math
import time
import clifford as cf
from clifford.tools.g3c import *
from clifford.tools.g3c.cuda import *
from clifford import g3c
from clifford.tools.g3c.cost_functions import val_object_cost_function, \
object_set_cost_matrix
from clifford.tools.g3c.rotor_estimation import sequential_object_rotor_estimation_convergence_detection
from clifford.tools.g3c.model_matching import REFORM_cuda, iterative_model_match_sequential
@SkipTest
class CUDATESTS(unittest.TestCase):
@classmethod
def setUpClass(self):
layout = g3c.layout
self.layout = layout
self.stuff = g3c.stuff
def test_sequential_rotor_estimation_kernel(self):
n_mvs = 1000
query_model = [random_line() for i in range(n_mvs)]
r = random_rotation_translation_rotor()
reference_model = [(r*a*~r).normal() for a in query_model]
query_model_array = np.array(query_model)
reference_model_array = np.array(reference_model)
n_samples = 100
n_objects_per_sample = 100
output = np.zeros((n_samples, 32))
mv_d_array = np.zeros(output.shape)
print('Starting kernel')
t = time.time()
# blockdim = 64
# griddim = int(math.ceil(n_mvs / blockdim))
# sequential_rotor_estimation_kernel[griddim, blockdim](reference_model_array, query_model_array, output)
output, cost_array = sequential_rotor_estimation_cuda(reference_model_array, query_model_array, n_samples,
n_objects_per_sample)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
# Now do the non cuda kernel
t = time.time()
for i in range(output.shape[0]):
mv_d_array[i, :] = sequential_object_rotor_estimation_convergence_detection(reference_model,
query_model)[0].value
print(time.time() - t)
print(cost_array)
np.testing.assert_almost_equal(output, mv_d_array, 5)
def test_square_root_of_rotor_kernel(self):
n_mvs = 500
mv_a_list = [random_line() for i in range(n_mvs)]
mv_b_list = [random_line() for i in range(n_mvs)]
rotor_list = [rotor_between_objects(C1,C2) for C1,C2 in zip(mv_a_list,mv_b_list)]
rotor_list_array = np.array(rotor_list)
rotor_root_array = np.zeros(rotor_list_array.shape)
mv_d_array = np.zeros(rotor_list_array.shape)
print('Starting kernel')
t = time.time()
blockdim = 64
griddim = int(math.ceil(n_mvs / blockdim))
square_root_of_rotor_kernel[griddim, blockdim](rotor_list_array, rotor_root_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
# Now do the non cuda kernel
t = time.time()
for i in range(rotor_list_array.shape[0]):
mv_d_array[i, :] = square_roots_of_rotor(rotor_list[i])[0].value
print(time.time() - t)
np.testing.assert_almost_equal(rotor_root_array, mv_d_array)
def test_rotor_between_lines(self):
# Make a big array of data
n_mvs = 1000
mv_a_array = np.array([random_line() for i in range(n_mvs)])
mv_b_array = np.array([random_line() for i in range(n_mvs)])
mv_c_array = np.zeros(mv_b_array.shape)
mv_d_array = np.zeros(mv_b_array.shape)
print('Starting kernel')
t = time.time()
blockdim = 64
griddim = int(math.ceil(n_mvs / blockdim))
rotor_between_lines_kernel[griddim, blockdim](mv_a_array, mv_b_array, mv_c_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
# Now do the non cuda kernel
t = time.time()
for i in range(mv_a_array.shape[0]):
mv_d_array[i, :] = val_rotor_between_lines(mv_a_array[i, :], mv_b_array[i, :])
print(time.time() - t)
np.testing.assert_almost_equal(mv_c_array, mv_d_array)
def test_normalise_mvs_kernel(self):
n_mvs = 500
mv_a_array = np.pi*np.array([random_line() for i in range(n_mvs)])
mv_d_array = np.zeros(mv_a_array.shape)
mv_b_array = mv_a_array.copy()
print('Starting kernel')
t = time.time()
blockdim = 64
griddim = int(math.ceil(n_mvs / blockdim))
normalise_mvs_kernel[griddim, blockdim](mv_a_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
# Now do the non cuda kernel
t = time.time()
for i in range(mv_a_array.shape[0]):
mv_a = cf.MultiVector(self.layout, mv_b_array[i, :])
mv_d_array[i, :] = mv_a.normal().value
print(time.time() - t)
np.testing.assert_almost_equal(mv_a_array, mv_d_array)
def test_rotor_between_objects(self):
# Make a big array of data
n_mvs = 1000
generator_list = [random_point_pair, random_line, random_circle, \
random_sphere, random_plane]
for generator in generator_list:
mv_a_array = np.array([generator() for i in range(n_mvs)], dtype=np.double)
mv_b_array = np.array([generator() for i in range(n_mvs)], dtype=np.double)
mv_c_array = np.zeros(mv_b_array.shape, dtype=np.double)
mv_d_array = np.zeros(mv_b_array.shape, dtype=np.double)
print('Starting kernel')
t = time.time()
blockdim = 64
griddim = int(math.ceil(n_mvs / blockdim))
rotor_between_objects_kernel[griddim, blockdim](mv_a_array, mv_b_array, mv_c_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
# Now do the non cuda kernel
t = time.time()
for i in range(mv_a_array.shape[0]):
mv_a = cf.MultiVector(self.layout, mv_a_array[i, :])
mv_b = cf.MultiVector(self.layout, mv_b_array[i, :])
mv_d_array[i, :] = rotor_between_objects(mv_a, mv_b).value
print(time.time() - t)
np.testing.assert_almost_equal(mv_c_array, mv_d_array)
def test_dorst_norm_val(self):
# Make a big array of data
n_mvs = 500
mv_a_list = [random_line() for i in range(n_mvs)]
mv_b_list = [random_line() for i in range(n_mvs)]
c_list = [1 + b*a for a,b in zip(mv_a_list, mv_b_list)]
sigma_list = [c*~c for c in c_list]
mv_sigma_array = np.array(sigma_list, dtype=np.double)
mv_c_array = np.zeros(mv_sigma_array.shape[0], dtype=np.double)
mv_d_array = np.zeros(mv_sigma_array.shape[0], dtype=np.double)
print('Starting kernel')
t = time.time()
blockdim = 64
griddim = int(math.ceil(n_mvs / blockdim))
dorst_norm_val_kernel[griddim, blockdim](mv_sigma_array, mv_c_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
# Now do the non cuda kernel
t = time.time()
for i in range(len(mv_a_list)):
sigma = sigma_list[i]
k_value = dorst_norm_val(sigma.value)
mv_d_array[i] = k_value
print(time.time() - t)
np.testing.assert_almost_equal(mv_c_array, mv_d_array)
def test_gp(self):
n_mvs = 500
mv_a_array = np.array([self.layout.randomMV().value for i in range(n_mvs)])
mv_b_array = np.array([self.layout.randomMV().value for i in range(n_mvs)])
mv_c_array = np.zeros(mv_b_array.shape)
mv_d_array = np.zeros(mv_b_array.shape)
print('Starting kernel')
t = time.time()
blockdim = 64
griddim = int(math.ceil(n_mvs/blockdim))
gp_kernel[griddim, blockdim](mv_a_array, mv_b_array, mv_c_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
# Now do the non cuda kernel
t = time.time()
for i in range(mv_a_array.shape[0]):
mv_d_array[i,:] = self.layout.gmt_func(mv_a_array[i,:],mv_b_array[i,:])
print(time.time() - t)
np.testing.assert_almost_equal(mv_c_array, mv_d_array)
def test_ip(self):
n_mvs = 500
mv_a_array = np.array([self.layout.randomMV().value for i in range(n_mvs)])
mv_b_array = np.array([self.layout.randomMV().value for i in range(n_mvs)])
mv_c_array = np.zeros(mv_b_array.shape)
mv_d_array = np.zeros(mv_b_array.shape)
print('Starting kernel')
t = time.time()
blockdim = 64
griddim = int(math.ceil(n_mvs / blockdim))
ip_kernel[griddim, blockdim](mv_a_array, mv_b_array, mv_c_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
# Now do the non cuda kernel
t = time.time()
for i in range(mv_a_array.shape[0]):
mv_d_array[i, :] = self.layout.imt_func(mv_a_array[i, :], mv_b_array[i, :])
print(time.time() - t)
np.testing.assert_almost_equal(mv_c_array, mv_d_array)
def test_adjoint(self):
n_mvs = 1000
mv_a_array = np.array([self.layout.randomMV().value for i in range(n_mvs)])
mv_d_array = np.zeros(mv_a_array.shape)
mv_c_array = np.zeros(mv_a_array.shape)
blockdim = 64
griddim = int(math.ceil(n_mvs / blockdim))
adjoint_kernel[griddim, blockdim](mv_a_array, mv_c_array)
for i in range(mv_a_array.shape[0]):
mv_d_array[i, :] = self.layout.adjoint_func(mv_a_array[i, :])
np.testing.assert_almost_equal(mv_c_array, mv_d_array, 5)
def test_rotor_cost(self):
# Make a big array of data
n_mvs = 500
mv_a_array = np.array([random_line() for i in range(n_mvs)])
mv_b_array = np.array([random_line() for i in range(n_mvs)])
mv_c_array = np.zeros(n_mvs)
mv_d_array = np.zeros(n_mvs)
# Multiply together a load of them and see how long it takes
print('Starting kernel')
t = time.time()
blockdim = 64
griddim = int(math.ceil(n_mvs / blockdim))
cost_line_to_line_kernel[griddim, blockdim](mv_a_array, mv_b_array, mv_c_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
# Now do the non cuda kernel
t = time.time()
for i in range(mv_a_array.shape[0]):
mv_d_array[i] = val_object_cost_function(mv_a_array[i, :], mv_b_array[i, :])
print(time.time() - t)
np.testing.assert_almost_equal(mv_c_array, mv_d_array, 5)
def test_line_set_cost(self):
n_mvs = 50
mv_a_array = [random_line() for i in range(n_mvs)]
mv_b_array = [random_line() for i in range(n_mvs)]
print(mv_a_array)
print('Starting kernel')
t = time.time()
mv_c_array = line_set_cost_cuda_mvs(mv_a_array, mv_b_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
t = time.time()
mv_d_array = object_set_cost_matrix(mv_a_array, mv_b_array, object_type='generic')
print(time.time() - t)
try:
np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3)
except:
print(mv_c_array[0,:])
print(mv_d_array[0,:])
np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3)
def test_object_set_cost(self):
n_mvs = 100
generator_list = [random_point_pair, random_line, random_circle, \
random_sphere, random_plane]
for generator in generator_list:
mv_a_array = [generator() for i in range(n_mvs)]
mv_b_array = [generator() for i in range(n_mvs)]
print(mv_a_array)
print('Starting kernel')
t = time.time()
mv_c_array = object_set_cost_cuda_mvs(mv_a_array, mv_b_array)
end_time = time.time() - t
print('Kernel finished')
print(end_time)
t = time.time()
mv_d_array = object_set_cost_matrix(mv_a_array, mv_b_array, object_type='generic')
print(time.time() - t)
try:
np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3)
except:
print(mv_c_array[0,:])
print(mv_d_array[0,:])
np.testing.assert_almost_equal(mv_c_array, mv_d_array, 3)
def test_REFORM_cuda(self):
object_generator = random_line
n_objects_per_cluster = 20
objects_per_sample = 10
n_samples = 100
iterations = 100
n_runs = 5
error_count = 0
for i in range(n_runs):
# Make a cluster
cluster_objects = generate_random_object_cluster(n_objects_per_cluster, object_generator,
max_cluster_trans=0.5, max_cluster_rot=np.pi / 3)
# Rotate and translate the cluster
disturbance_rotor = random_rotation_translation_rotor(maximum_translation=2, maximum_angle=np.pi / 8)
target = [apply_rotor(c, disturbance_rotor).normal() for c in cluster_objects]
labels, costs, r_est = REFORM_cuda(target, cluster_objects, n_samples, objects_per_sample,
iterations, mutation_probability=None)
try:
assert np.sum(labels == range(n_objects_per_cluster)) == n_objects_per_cluster
except:
print(disturbance_rotor)
print(r_est)
error_count += 1
print('Correct fraction: ', 1.0 - error_count / n_runs)