Repository navigation
Expand file tree
/
Copy pathtest_rerank.py
More file actions
290 lines (243 loc) · 11.6 KB
/
Copy pathtest_rerank.py
File metadata and controls
290 lines (243 loc) · 11.6 KB
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
import pytest
import numpy as np
import deglib
from deglib.distances import FloatSpace, Metric
class TestRerankUnit:
def setup_method(self):
self.dims = 16
self.space = FloatSpace.create(self.dims, Metric.FP32_L2)
def test_single_query_single_candidate(self):
queries = np.zeros((1, self.dims), dtype=np.float32)
base = np.zeros((1, self.dims), dtype=np.float32)
candidates = np.array([[0]], dtype=np.uint32)
result = deglib.search.rerank(self.space, queries, candidates, base, k_top=1)
assert result.shape == (1, 1)
assert result[0, 0] == 0
def test_single_query_multiple_candidates(self):
queries = np.zeros((1, self.dims), dtype=np.float32)
base = np.zeros((3, self.dims), dtype=np.float32)
base[0, 0] = 1.0
base[1, 0] = 3.0
base[2, 0] = 0.5
candidates = np.array([[0, 1, 2]], dtype=np.uint32)
result = deglib.search.rerank(self.space, queries, candidates, base, k_top=2)
assert result.shape == (1, 2)
# Closest 2: idx 2 (dist=0.25), idx 0 (dist=1.0)
assert result[0, 0] == 2
assert result[0, 1] == 0
def test_return_distances(self):
queries = np.zeros((1, self.dims), dtype=np.float32)
base = np.zeros((3, self.dims), dtype=np.float32)
base[0, 0] = 1.0
base[1, 0] = 3.0
base[2, 0] = 0.5
candidates = np.array([[0, 1, 2]], dtype=np.uint32)
indices, distances = deglib.search.rerank(self.space, queries, candidates, base, k_top=2, return_distances=True)
assert indices.shape == (1, 2)
assert distances.shape == (1, 2)
assert indices[0, 0] == 2
assert indices[0, 1] == 0
np.testing.assert_allclose(distances[0, 0], 0.25, rtol=1e-5)
np.testing.assert_allclose(distances[0, 1], 1.0, rtol=1e-5)
def test_k_top_zero_returns_all(self):
queries = np.zeros((1, self.dims), dtype=np.float32)
base = np.zeros((3, self.dims), dtype=np.float32)
base[0, 0] = 1.0
base[1, 0] = 2.0
base[2, 0] = 3.0
candidates = np.array([[0, 1, 2]], dtype=np.uint32)
result = deglib.search.rerank(self.space, queries, candidates, base, k_top=0)
assert result.shape == (1, 3)
assert result[0, 0] == 0
assert result[0, 1] == 1
assert result[0, 2] == 2
def test_k_top_larger_than_candidates(self):
queries = np.zeros((1, self.dims), dtype=np.float32)
base = np.zeros((1, self.dims), dtype=np.float32)
base[0, 0] = 1.0
candidates = np.array([[0]], dtype=np.uint32)
result = deglib.search.rerank(self.space, queries, candidates, base, k_top=5)
assert result.shape == (1, 1)
assert result[0, 0] == 0
def test_uses_queries_as_targets_when_base_none(self):
queries = np.array(
[
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
],
dtype=np.float32,
)
candidates = np.array([[0, 1], [0, 1]], dtype=np.uint32)
result = deglib.search.rerank(self.space, queries, candidates, k_top=2)
assert result.shape == (2, 2)
# Query 0: idx 0 (dist=0), idx 1 (dist=1)
assert result[0, 0] == 0
assert result[0, 1] == 1
# Query 1: idx 1 (dist=0), idx 0 (dist=1)
assert result[1, 0] == 1
assert result[1, 1] == 0
def test_invalid_candidate_index_skipped(self):
queries = np.zeros((1, self.dims), dtype=np.float32)
base = np.zeros((1, self.dims), dtype=np.float32)
base[0, 0] = 1.0
# idx 99 is out of bounds, should be skipped
candidates = np.array([[0, 99]], dtype=np.uint32)
result = deglib.search.rerank(self.space, queries, candidates, base, k_top=2)
assert result.shape == (1, 2)
assert result[0, 0] == 0
assert result[0, 1] == np.iinfo(np.uint32).max # unfilled candidate is padded with uint32 max
def test_multiple_queries(self):
queries = np.array(
[
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
],
dtype=np.float32,
)
base = np.array(
[
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
],
dtype=np.float32,
)
candidates = np.array([[0, 1], [0, 1]], dtype=np.uint32)
result = deglib.search.rerank(self.space, queries, candidates, base, k_top=2)
assert result.shape == (2, 2)
# Query 0: idx 0 (dist=0), idx 1 (dist=2)
assert result[0, 0] == 0
assert result[0, 1] == 1
# Query 1: idx 1 (dist=32), idx 0 (dist=50)
assert result[1, 0] == 1
assert result[1, 1] == 0
def test_inner_product_metric(self):
space = FloatSpace.create(self.dims, Metric.FP32_InnerProduct)
query = np.zeros((1, self.dims), dtype=np.float32)
query[0, 0] = 1.0
base = np.zeros((3, self.dims), dtype=np.float32)
base[0, 0] = 1.0
base[1, 0] = 2.0
base[2, 0] = 0.5
candidates = np.array([[0, 1, 2]], dtype=np.uint32)
result = deglib.search.rerank(space, query, candidates, base, k_top=2)
assert result.shape == (1, 2)
# Inner product: higher is better → idx 1 (ip=2), idx 0 (ip=1)
assert result[0, 0] == 1
assert result[0, 1] == 0
def test_return_distances_all(self):
queries = np.zeros((1, self.dims), dtype=np.float32)
base = np.zeros((3, self.dims), dtype=np.float32)
base[0, 0] = 1.0
base[1, 0] = 3.0
base[2, 0] = 0.5
candidates = np.array([[0, 1, 2]], dtype=np.uint32)
indices, distances = deglib.search.rerank(self.space, queries, candidates, base, k_top=3, return_distances=True)
assert indices.shape == (1, 3)
assert distances.shape == (1, 3)
# Verify distances are correct and sorted ascending
np.testing.assert_allclose(distances[0, 0], 0.25, rtol=1e-5) # idx 2
np.testing.assert_allclose(distances[0, 1], 1.0, rtol=1e-5) # idx 0
np.testing.assert_allclose(distances[0, 2], 9.0, rtol=1e-5) # idx 1
def test_multi_threaded(self):
queries = np.array(
[
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
],
dtype=np.float32,
)
base = np.array(
[
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
],
dtype=np.float32,
)
candidates = np.array([[0, 1], [0, 1]], dtype=np.uint32)
result = deglib.search.rerank(self.space, queries, candidates, base, k_top=2, num_threads=4)
assert result.shape == (2, 2)
assert result[0, 0] == 0
assert result[0, 1] == 1
assert result[1, 0] == 1
assert result[1, 1] == 0
def test_unsorted(self):
queries = np.zeros((1, self.dims), dtype=np.float32)
base = np.zeros((3, self.dims), dtype=np.float32)
base[0, 0] = 1.0
base[1, 0] = 3.0
base[2, 0] = 0.5
candidates = np.array([[0, 1, 2]], dtype=np.uint32)
# Sorted returns idx 2 then idx 0
sorted_res = deglib.search.rerank(self.space, queries, candidates, base, k_top=2, unsorted=False)
assert sorted_res[0, 0] == 2
assert sorted_res[0, 1] == 0
# Unsorted returns top 2 without sorting
unsorted_res = deglib.search.rerank(self.space, queries, candidates, base, k_top=2, unsorted=True)
assert unsorted_res.shape == (1, 2)
assert set(unsorted_res[0]) == {0, 2}
class TestRerankerClass:
def setup_method(self):
self.dims = 16
self.space = FloatSpace.create(self.dims, Metric.FP32_L2)
def test_reranker_class(self):
queries = np.zeros((1, self.dims), dtype=np.float32)
base = np.zeros((3, self.dims), dtype=np.float32)
base[0, 0] = 1.0
base[1, 0] = 3.0
base[2, 0] = 0.5
candidates = np.array([[0, 1, 2]], dtype=np.uint32)
reranker = deglib.Reranker(self.space, base)
assert reranker.get_num_base_vectors() == 3
res = reranker.rerank(queries, candidates, k_top=2)
assert res.shape == (1, 2)
assert res[0, 0] == 2
assert res[0, 1] == 0
indices, distances = reranker.rerank(queries, candidates, k_top=2, return_distances=True)
assert indices.shape == (1, 2)
assert distances.shape == (1, 2)
np.testing.assert_allclose(distances[0, 0], 0.25, rtol=1e-5)
np.testing.assert_allclose(distances[0, 1], 1.0, rtol=1e-5)
def test_reranker_optimize_preserves_results(self):
rng = np.random.default_rng(0)
base = rng.random((64, self.dims), dtype=np.float32)
query = rng.random((1, self.dims), dtype=np.float32)
candidates = np.array([list(range(64))], dtype=np.uint32)
reranker = deglib.Reranker(self.space, base)
before = reranker.rerank(query, candidates, k_top=5)
# Prefetch auto-tuning must not change reranking results.
reranker.optimize(sample_count=16, num_candidates=32, k=5)
after = reranker.rerank(query, candidates, k_top=5)
np.testing.assert_array_equal(before, after)
def test_reranker_prefetch_defaults_and_override(self):
rng = np.random.default_rng(3)
base = rng.random((64, self.dims), dtype=np.float32)
query = rng.random((1, self.dims), dtype=np.float32)
candidates = np.array([list(range(64))], dtype=np.uint32)
reranker = deglib.Reranker(self.space, base)
# Prefetching must be on out of the box, otherwise out-of-cache reranking silently loses throughput.
assert reranker.get_po() > 0
assert reranker.get_pl() == 0
reranker.set_prefetch(12, 4)
assert reranker.get_po() == 12
assert reranker.get_pl() == 4
reranker.set_prefetch(0, -1)
assert reranker.get_po() == 0
assert reranker.get_pl() == 0
# Both configs must rank identically; prefetching is a speed knob only.
unprefetched = reranker.rerank(query, candidates, k_top=5)
reranker.set_prefetch(8, 0)
prefetched = reranker.rerank(query, candidates, k_top=5)
np.testing.assert_array_equal(unprefetched, prefetched)
def test_reranker_rejects_mismatched_base_vectors(self):
mismatched = np.zeros((3, self.dims + 1), dtype=np.float32)
with pytest.raises(ValueError):
deglib.Reranker(self.space, mismatched)
def test_reranker_matches_free_function(self):
queries = np.zeros((2, self.dims), dtype=np.float32)
base = np.zeros((4, self.dims), dtype=np.float32)
for i in range(4):
base[i, 0] = float(i)
candidates = np.array([[0, 1, 2, 3], [3, 2, 1, 0]], dtype=np.uint32)
via_class = deglib.Reranker(self.space, base).rerank(queries, candidates, k_top=3)
via_function = deglib.search.rerank(self.space, queries, candidates, base, k_top=3)
np.testing.assert_array_equal(via_class, via_function)