-
Notifications
You must be signed in to change notification settings - Fork 24
/
test.py
236 lines (221 loc) · 10 KB
/
test.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
from time import time
import unittest
from datasketch import WeightedMinHashGenerator, WeightedMinHash
import libMHCUDA
import numpy
from scipy.sparse import csr_matrix
from scipy.stats import gamma, uniform
class MHCUDATests(unittest.TestCase):
def test_calc_tiny(self):
v1 = [1, 0, 0, 0, 3, 4, 5, 0, 0, 0, 0, 6, 7, 8, 0, 0, 0, 0, 0, 0, 9, 10, 4]
v2 = [2, 0, 0, 0, 4, 3, 8, 0, 0, 0, 0, 4, 7, 10, 0, 0, 0, 0, 0, 0, 9, 0, 0]
bgen = WeightedMinHashGenerator(len(v1))
gen = libMHCUDA.minhash_cuda_init(len(v1), 128, devices=1, verbosity=2)
libMHCUDA.minhash_cuda_assign_vars(gen, bgen.rs, bgen.ln_cs, bgen.betas)
m = csr_matrix(numpy.array([v1, v2], dtype=numpy.float32))
hashes = libMHCUDA.minhash_cuda_calc(gen, m)
libMHCUDA.minhash_cuda_fini(gen)
self.assertEqual(hashes.shape, (2, 128, 2))
true_hashes = numpy.array([bgen.minhash(v1).hashvalues,
bgen.minhash(v2).hashvalues], dtype=numpy.uint32)
self.assertEqual(true_hashes.shape, (2, 128, 2))
try:
self.assertTrue((hashes == true_hashes).all())
except AssertionError as e:
print("---- TRUE ----")
print(true_hashes)
print("---- FALSE ----")
print(hashes)
raise e from None
def _test_calc_big(self, devices):
numpy.random.seed(0)
data = numpy.random.randint(0, 100, (6400, 130))
mask = numpy.random.randint(0, 5, data.shape)
data *= (mask >= 4)
del mask
bgen = WeightedMinHashGenerator(data.shape[-1])
gen = libMHCUDA.minhash_cuda_init(data.shape[-1], 128, devices=devices, verbosity=2)
libMHCUDA.minhash_cuda_assign_vars(gen, bgen.rs, bgen.ln_cs, bgen.betas)
m = csr_matrix(data, dtype=numpy.float32)
print(m.nnz / (m.shape[0] * m.shape[1]))
ts = time()
hashes = libMHCUDA.minhash_cuda_calc(gen, m)
print("libMHCUDA:", time() - ts)
libMHCUDA.minhash_cuda_fini(gen)
self.assertEqual(hashes.shape, (len(data), 128, 2))
ts = time()
true_hashes = numpy.array([bgen.minhash(line).hashvalues for line in data],
dtype=numpy.uint32)
print("datasketch:", time() - ts)
self.assertEqual(true_hashes.shape, (len(data), 128, 2))
try:
self.assertTrue((hashes == true_hashes).all())
except AssertionError as e:
for r in range(hashes.shape[0]):
if (hashes[r] != true_hashes[r]).any():
print("first invalid row:", r)
print(hashes[r])
print(true_hashes[r])
break
raise e from None
def test_calc_big(self):
self._test_calc_big(1)
def test_calc_big_2gpus(self):
self._test_calc_big(3)
def test_random_vars(self):
gen = libMHCUDA.minhash_cuda_init(1000, 128, devices=1, verbosity=2)
rs, ln_cs, betas = libMHCUDA.minhash_cuda_retrieve_vars(gen)
libMHCUDA.minhash_cuda_fini(gen)
self.assertEqual(rs.shape, (128, 1000))
self.assertEqual(ln_cs.shape, (128, 1000))
self.assertEqual(betas.shape, (128, 1000))
cs = numpy.exp(ln_cs)
a, loc, scale = gamma.fit(rs)
self.assertTrue(1.97 < a < 2.03)
self.assertTrue(-0.01 < loc < 0.01)
self.assertTrue(0.98 < scale < 1.02)
a, loc, scale = gamma.fit(cs)
self.assertTrue(1.97 < a < 2.03)
self.assertTrue(-0.01 < loc < 0.01)
self.assertTrue(0.98 < scale < 1.02)
bmin, bmax = uniform.fit(betas)
self.assertTrue(0 <= bmin < 0.001)
self.assertTrue(0.999 <= bmax <= 1)
def test_integration(self):
numpy.random.seed(1)
data = numpy.random.randint(0, 100, (6400, 130))
mask = numpy.random.randint(0, 5, data.shape)
data *= (mask >= 4)
del mask
gen = libMHCUDA.minhash_cuda_init(data.shape[-1], 128, seed=1, verbosity=1)
m = csr_matrix(data, dtype=numpy.float32)
print(m.nnz / (m.shape[0] * m.shape[1]))
hashes = libMHCUDA.minhash_cuda_calc(gen, m)
libMHCUDA.minhash_cuda_fini(gen)
self.assertEqual(hashes.shape, (len(data), 128, 2))
h1 = WeightedMinHash(0, hashes[0])
h2 = WeightedMinHash(0, hashes[1])
cudamh = h1.jaccard(h2)
print(cudamh)
truemh = numpy.amin(data[:2], axis=0).sum() / numpy.amax(data[:2], axis=0).sum()
print(truemh)
self.assertTrue(abs(truemh - cudamh) < 0.005)
def test_slice(self):
numpy.random.seed(0)
data = numpy.random.randint(0, 100, (6400, 130))
mask = numpy.random.randint(0, 5, data.shape)
data *= (mask >= 4)
del mask
gen = libMHCUDA.minhash_cuda_init(data.shape[-1], 128, verbosity=2)
m = csr_matrix(data, dtype=numpy.float32)
hashes = libMHCUDA.minhash_cuda_calc(gen, m)
hashes2 = libMHCUDA.minhash_cuda_calc(
gen, m, row_start=3200, row_finish=4800)
libMHCUDA.minhash_cuda_fini(gen)
self.assertTrue((hashes[3200:4800] == hashes2).all())
def test_backwards(self):
v1 = [1, 0, 0, 0, 3, 4, 5, 0, 0, 0, 0, 6, 7, 8, 0, 0, 0, 0, 0, 0, 9, 10, 4]
v2 = [2, 0, 0, 0, 4, 3, 8, 0, 0, 0, 0, 4, 7, 10, 0, 0, 0, 0, 0, 0, 9, 0, 0]
gen = libMHCUDA.minhash_cuda_init(len(v1), 128, devices=1, verbosity=2)
rs, ln_cs, betas = libMHCUDA.minhash_cuda_retrieve_vars(gen)
bgen = WeightedMinHashGenerator.__new__(WeightedMinHashGenerator)
bgen.dim = len(v1)
bgen.rs = rs
bgen.ln_cs = ln_cs
bgen.betas = betas
bgen.sample_size = 128
bgen.seed = None
m = csr_matrix(numpy.array([v1, v2], dtype=numpy.float32))
hashes = libMHCUDA.minhash_cuda_calc(gen, m)
libMHCUDA.minhash_cuda_fini(gen)
self.assertEqual(hashes.shape, (2, 128, 2))
true_hashes = numpy.array([bgen.minhash(v1).hashvalues,
bgen.minhash(v2).hashvalues], dtype=numpy.uint32)
self.assertEqual(true_hashes.shape, (2, 128, 2))
try:
self.assertTrue((hashes == true_hashes).all())
except AssertionError as e:
print("---- TRUE ----")
print(true_hashes)
print("---- FALSE ----")
print(hashes)
raise e from None
def test_deferred(self):
v1 = [1, 0, 0, 0, 3, 4, 5, 0, 0, 0, 0, 6, 7, 8, 0, 0, 0, 0, 0, 0, 9, 10, 4]
v2 = [2, 0, 0, 0, 4, 3, 8, 0, 0, 0, 0, 4, 7, 10, 0, 0, 0, 0, 0, 0, 9, 0, 0]
gen = libMHCUDA.minhash_cuda_init(len(v1), 128, devices=1, verbosity=2)
vars = libMHCUDA.minhash_cuda_retrieve_vars(gen)
libMHCUDA.minhash_cuda_fini(gen)
gen = libMHCUDA.minhash_cuda_init(
len(v1), 128, devices=1, deferred=True, verbosity=2)
libMHCUDA.minhash_cuda_assign_vars(gen, *vars)
bgen = WeightedMinHashGenerator.__new__(WeightedMinHashGenerator)
bgen.dim = len(v1)
bgen.rs, bgen.ln_cs, bgen.betas = vars
bgen.sample_size = 128
bgen.seed = None
m = csr_matrix(numpy.array([v1, v2], dtype=numpy.float32))
hashes = libMHCUDA.minhash_cuda_calc(gen, m)
libMHCUDA.minhash_cuda_fini(gen)
self.assertEqual(hashes.shape, (2, 128, 2))
true_hashes = numpy.array([bgen.minhash(v1).hashvalues,
bgen.minhash(v2).hashvalues], dtype=numpy.uint32)
self.assertEqual(true_hashes.shape, (2, 128, 2))
try:
self.assertTrue((hashes == true_hashes).all())
except AssertionError as e:
print("---- TRUE ----")
print(true_hashes)
print("---- FALSE ----")
print(hashes)
raise e from None
def test_float(self):
v1 = [
0, 1.0497366, 0.8494359, 0.66231006, 0.66231006, 0.8494359,
0, 0.66231006, 0.33652836, 0, 0, 0.5359344,
0.8494359, 0.66231006, 1.0497366, 0.33652836, 0.66231006, 0.8494359,
0.6800841, 0.33652836]
gen = libMHCUDA.minhash_cuda_init(len(v1), 128, devices=1, seed=7, verbosity=2)
vars = libMHCUDA.minhash_cuda_retrieve_vars(gen)
bgen = WeightedMinHashGenerator.__new__(WeightedMinHashGenerator)
bgen.dim = len(v1)
bgen.rs, bgen.ln_cs, bgen.betas = vars
bgen.sample_size = 128
bgen.seed = None
m = csr_matrix(numpy.array(v1, dtype=numpy.float32))
hashes = libMHCUDA.minhash_cuda_calc(gen, m).astype(numpy.int32)
libMHCUDA.minhash_cuda_fini(gen)
self.assertEqual(hashes.shape, (1, 128, 2))
true_hashes = numpy.array([bgen.minhash(v1).hashvalues], dtype=numpy.int32)
self.assertEqual(true_hashes.shape, (1, 128, 2))
try:
self.assertTrue((hashes == true_hashes).all())
except AssertionError as e:
print("---- TRUE ----")
print(true_hashes)
print("---- FALSE ----")
print(hashes)
raise e from None
def test_split(self):
def run_test(v):
k = sum([len(part) for part in v])
bgen = WeightedMinHashGenerator(len(k))
gen = libMHCUDA.minhash_cuda_init(len(k), 128, devices=4, verbosity=2)
libMHCUDA.minhash_cuda_assign_vars(gen, bgen.rs, bgen.ln_cs, bgen.betas)
m = csr_matrix(numpy.array(v, dtype=numpy.float32))
hashes = None
try:
hashes = libMHCUDA.minhash_cuda_calc(gen, m)
finally:
self.assertIsNotNone(hashes)
self.assertEqual(hashes.shape, (1, 128, 2))
libMHCUDA.minhash_cuda_fini(gen)
# here we try to break minhashcuda with unbalanced partitions
run_test([[2], [1], [1], [1]])
run_test([[1] * 50, [1], [1], [1]])
run_test([[1], [1] * 50, [1], [1]])
run_test([[1], [1], [1] * 50, [1]])
run_test([[1], [1], [1], [1] * 50])
run_test([[1] * 3, [1] * 10, [1] * 5, [1] * 2])
if __name__ == "__main__":
unittest.main()