-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
test_lowlevel_ivf.cpp
549 lines (433 loc) · 16.5 KB
/
test_lowlevel_ivf.cpp
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <random>
#include <thread>
#include <vector>
#include <gtest/gtest.h>
#include <faiss/AutoTune.h>
#include <faiss/IVFlib.h>
#include <faiss/IndexBinaryIVF.h>
#include <faiss/IndexIVF.h>
#include <faiss/IndexPreTransform.h>
#include <faiss/index_factory.h>
using namespace faiss;
namespace {
// dimension of the vectors to index
int d = 32;
// nb of training vectors
size_t nt = 5000;
// size of the database points per window step
size_t nb = 1000;
// nb of queries
size_t nq = 200;
int k = 10;
std::mt19937 rng;
std::vector<float> make_data(size_t n) {
std::vector<float> database(n * d);
std::uniform_real_distribution<> distrib;
for (size_t i = 0; i < n * d; i++) {
database[i] = distrib(rng);
}
return database;
}
std::unique_ptr<Index> make_trained_index(
const char* index_type,
MetricType metric_type) {
auto index =
std::unique_ptr<Index>(index_factory(d, index_type, metric_type));
auto xt = make_data(nt);
index->train(nt, xt.data());
ParameterSpace().set_index_parameter(index.get(), "nprobe", 4);
return index;
}
std::vector<idx_t> search_index(Index* index, const float* xq) {
std::vector<idx_t> I(k * nq);
std::vector<float> D(k * nq);
index->search(nq, xq, k, D.data(), I.data());
return I;
}
/*************************************************************
* Test functions for a given index type
*************************************************************/
void test_lowlevel_access(const char* index_key, MetricType metric) {
std::unique_ptr<Index> index = make_trained_index(index_key, metric);
auto xb = make_data(nb);
index->add(nb, xb.data());
/** handle the case if we have a preprocessor */
const IndexPreTransform* index_pt =
dynamic_cast<const IndexPreTransform*>(index.get());
int dt = index->d;
const float* xbt = xb.data();
std::unique_ptr<float[]> del_xbt;
if (index_pt) {
dt = index_pt->index->d;
xbt = index_pt->apply_chain(nb, xb.data());
if (xbt != xb.data()) {
del_xbt.reset((float*)xbt);
}
}
IndexIVF* index_ivf = ivflib::extract_index_ivf(index.get());
/** Test independent encoding
*
* Makes it possible to do additions on a custom inverted list
* implementation. From a set of vectors, computes the inverted
* list ids + the codes corresponding to each vector.
*/
std::vector<idx_t> list_nos(nb);
std::vector<uint8_t> codes(index_ivf->code_size * nb);
index_ivf->quantizer->assign(nb, xbt, list_nos.data());
index_ivf->encode_vectors(nb, xbt, list_nos.data(), codes.data());
// compare with normal IVF addition
const InvertedLists* il = index_ivf->invlists;
for (int list_no = 0; list_no < index_ivf->nlist; list_no++) {
InvertedLists::ScopedCodes ivf_codes(il, list_no);
InvertedLists::ScopedIds ivf_ids(il, list_no);
size_t list_size = il->list_size(list_no);
for (int i = 0; i < list_size; i++) {
const uint8_t* ref_code = ivf_codes.get() + i * il->code_size;
const uint8_t* new_code = codes.data() + ivf_ids[i] * il->code_size;
EXPECT_EQ(memcmp(ref_code, new_code, il->code_size), 0);
}
}
/** Test independent search
*
* Manually scans through inverted lists, computing distances and
* ordering results organized in a heap.
*/
// sample some example queries and get reference search results.
auto xq = make_data(nq);
auto ref_I = search_index(index.get(), xq.data());
// handle preprocessing
const float* xqt = xq.data();
std::unique_ptr<float[]> del_xqt;
if (index_pt) {
xqt = index_pt->apply_chain(nq, xq.data());
if (xqt != xq.data()) {
del_xqt.reset((float*)xqt);
}
}
// quantize the queries to get the inverted list ids to visit.
int nprobe = index_ivf->nprobe;
std::vector<idx_t> q_lists(nq * nprobe);
std::vector<float> q_dis(nq * nprobe);
index_ivf->quantizer->search(nq, xqt, nprobe, q_dis.data(), q_lists.data());
// object that does the scanning and distance computations.
std::unique_ptr<InvertedListScanner> scanner(
index_ivf->get_InvertedListScanner());
for (int i = 0; i < nq; i++) {
std::vector<idx_t> I(k, -1);
float default_dis = metric == METRIC_L2 ? HUGE_VAL : -HUGE_VAL;
std::vector<float> D(k, default_dis);
scanner->set_query(xqt + i * dt);
for (int j = 0; j < nprobe; j++) {
int list_no = q_lists[i * nprobe + j];
if (list_no < 0)
continue;
scanner->set_list(list_no, q_dis[i * nprobe + j]);
// here we get the inverted lists from the InvertedLists
// object but they could come from anywhere
scanner->scan_codes(
il->list_size(list_no),
InvertedLists::ScopedCodes(il, list_no).get(),
InvertedLists::ScopedIds(il, list_no).get(),
D.data(),
I.data(),
k);
if (j == 0) {
// all results so far come from list_no, so let's check if
// the distance function works
for (int jj = 0; jj < k; jj++) {
int vno = I[jj];
if (vno < 0)
break; // heap is not full yet
// we have the codes from the addition test
float computed_D = scanner->distance_to_code(
codes.data() + vno * il->code_size);
EXPECT_FLOAT_EQ(computed_D, D[jj]);
}
}
}
// re-order heap
if (metric == METRIC_L2) {
maxheap_reorder(k, D.data(), I.data());
} else {
minheap_reorder(k, D.data(), I.data());
}
// check that we have the same results as the reference search
for (int j = 0; j < k; j++) {
EXPECT_EQ(I[j], ref_I[i * k + j]);
}
}
}
} // anonymous namespace
/*************************************************************
* Test entry points
*************************************************************/
TEST(TestLowLevelIVF, IVFFlatL2) {
test_lowlevel_access("IVF32,Flat", METRIC_L2);
}
TEST(TestLowLevelIVF, PCAIVFFlatL2) {
test_lowlevel_access("PCAR16,IVF32,Flat", METRIC_L2);
}
TEST(TestLowLevelIVF, IVFFlatIP) {
test_lowlevel_access("IVF32,Flat", METRIC_INNER_PRODUCT);
}
TEST(TestLowLevelIVF, IVFSQL2) {
test_lowlevel_access("IVF32,SQ8", METRIC_L2);
}
TEST(TestLowLevelIVF, IVFSQIP) {
test_lowlevel_access("IVF32,SQ8", METRIC_INNER_PRODUCT);
}
TEST(TestLowLevelIVF, IVFPQL2) {
test_lowlevel_access("IVF32,PQ4np", METRIC_L2);
}
TEST(TestLowLevelIVF, IVFPQIP) {
test_lowlevel_access("IVF32,PQ4np", METRIC_INNER_PRODUCT);
}
/*************************************************************
* Same for binary (a bit simpler)
*************************************************************/
namespace {
int nbit = 256;
// here d is used the number of ints -> d=32 means 128 bits
std::vector<uint8_t> make_data_binary(size_t n) {
std::vector<uint8_t> database(n * nbit / 8);
std::uniform_int_distribution<> distrib;
for (size_t i = 0; i < n * d; i++) {
database[i] = distrib(rng);
}
return database;
}
std::unique_ptr<IndexBinary> make_trained_index_binary(const char* index_type) {
auto index = std::unique_ptr<IndexBinary>(
index_binary_factory(nbit, index_type));
auto xt = make_data_binary(nt);
index->train(nt, xt.data());
return index;
}
void test_lowlevel_access_binary(const char* index_key) {
std::unique_ptr<IndexBinary> index = make_trained_index_binary(index_key);
IndexBinaryIVF* index_ivf = dynamic_cast<IndexBinaryIVF*>(index.get());
assert(index_ivf);
index_ivf->nprobe = 4;
auto xb = make_data_binary(nb);
index->add(nb, xb.data());
std::vector<idx_t> list_nos(nb);
index_ivf->quantizer->assign(nb, xb.data(), list_nos.data());
/* For binary there is no test for encoding because binary vectors
* are copied verbatim to the inverted lists */
const InvertedLists* il = index_ivf->invlists;
/** Test independent search
*
* Manually scans through inverted lists, computing distances and
* ordering results organized in a heap.
*/
// sample some example queries and get reference search results.
auto xq = make_data_binary(nq);
std::vector<idx_t> I_ref(k * nq);
std::vector<int32_t> D_ref(k * nq);
index->search(nq, xq.data(), k, D_ref.data(), I_ref.data());
// quantize the queries to get the inverted list ids to visit.
int nprobe = index_ivf->nprobe;
std::vector<idx_t> q_lists(nq * nprobe);
std::vector<int32_t> q_dis(nq * nprobe);
// quantize queries
index_ivf->quantizer->search(
nq, xq.data(), nprobe, q_dis.data(), q_lists.data());
// object that does the scanning and distance computations.
std::unique_ptr<BinaryInvertedListScanner> scanner(
index_ivf->get_InvertedListScanner());
for (int i = 0; i < nq; i++) {
std::vector<idx_t> I(k, -1);
uint32_t default_dis = 1 << 30;
std::vector<int32_t> D(k, default_dis);
scanner->set_query(xq.data() + i * index_ivf->code_size);
for (int j = 0; j < nprobe; j++) {
int list_no = q_lists[i * nprobe + j];
if (list_no < 0)
continue;
scanner->set_list(list_no, q_dis[i * nprobe + j]);
// here we get the inverted lists from the InvertedLists
// object but they could come from anywhere
scanner->scan_codes(
il->list_size(list_no),
InvertedLists::ScopedCodes(il, list_no).get(),
InvertedLists::ScopedIds(il, list_no).get(),
D.data(),
I.data(),
k);
if (j == 0) {
// all results so far come from list_no, so let's check if
// the distance function works
for (int jj = 0; jj < k; jj++) {
int vno = I[jj];
if (vno < 0)
break; // heap is not full yet
// we have the codes from the addition test
float computed_D = scanner->distance_to_code(
xb.data() + vno * il->code_size);
EXPECT_EQ(computed_D, D[jj]);
}
}
}
// re-order heap
heap_reorder<CMax<int32_t, idx_t>>(k, D.data(), I.data());
// check that we have the same results as the reference search
for (int j = 0; j < k; j++) {
// here the order is not guaranteed to be the same
// so we scan through ref results
// EXPECT_EQ (I[j], I_ref[i * k + j]);
EXPECT_LE(D[j], D_ref[i * k + k - 1]);
if (D[j] < D_ref[i * k + k - 1]) {
int j2 = 0;
while (j2 < k) {
if (I[j] == I_ref[i * k + j2])
break;
j2++;
}
EXPECT_LT(j2, k); // it was found
if (j2 < k) {
EXPECT_EQ(D[j], D_ref[i * k + j2]);
}
}
}
}
}
} // anonymous namespace
TEST(TestLowLevelIVF, IVFBinary) {
test_lowlevel_access_binary("BIVF32");
}
namespace {
void test_threaded_search(const char* index_key, MetricType metric) {
std::unique_ptr<Index> index = make_trained_index(index_key, metric);
auto xb = make_data(nb);
index->add(nb, xb.data());
/** handle the case if we have a preprocessor */
const IndexPreTransform* index_pt =
dynamic_cast<const IndexPreTransform*>(index.get());
int dt = index->d;
const float* xbt = xb.data();
std::unique_ptr<float[]> del_xbt;
if (index_pt) {
dt = index_pt->index->d;
xbt = index_pt->apply_chain(nb, xb.data());
if (xbt != xb.data()) {
del_xbt.reset((float*)xbt);
}
}
IndexIVF* index_ivf = ivflib::extract_index_ivf(index.get());
/** Test independent search
*
* Manually scans through inverted lists, computing distances and
* ordering results organized in a heap.
*/
// sample some example queries and get reference search results.
auto xq = make_data(nq);
auto ref_I = search_index(index.get(), xq.data());
// handle preprocessing
const float* xqt = xq.data();
std::unique_ptr<float[]> del_xqt;
if (index_pt) {
xqt = index_pt->apply_chain(nq, xq.data());
if (xqt != xq.data()) {
del_xqt.reset((float*)xqt);
}
}
// quantize the queries to get the inverted list ids to visit.
int nprobe = index_ivf->nprobe;
std::vector<idx_t> q_lists(nq * nprobe);
std::vector<float> q_dis(nq * nprobe);
index_ivf->quantizer->search(nq, xqt, nprobe, q_dis.data(), q_lists.data());
// now run search in this many threads
int nproc = 3;
for (int i = 0; i < nq; i++) {
// one result table per thread
std::vector<idx_t> I(k * nproc, -1);
float default_dis = metric == METRIC_L2 ? HUGE_VAL : -HUGE_VAL;
std::vector<float> D(k * nproc, default_dis);
auto search_function = [index_ivf,
&I,
&D,
dt,
i,
nproc,
xqt,
nprobe,
&q_dis,
&q_lists](int rank) {
const InvertedLists* il = index_ivf->invlists;
// object that does the scanning and distance computations.
std::unique_ptr<InvertedListScanner> scanner(
index_ivf->get_InvertedListScanner());
idx_t* local_I = I.data() + rank * k;
float* local_D = D.data() + rank * k;
scanner->set_query(xqt + i * dt);
for (int j = rank; j < nprobe; j += nproc) {
int list_no = q_lists[i * nprobe + j];
if (list_no < 0)
continue;
scanner->set_list(list_no, q_dis[i * nprobe + j]);
scanner->scan_codes(
il->list_size(list_no),
InvertedLists::ScopedCodes(il, list_no).get(),
InvertedLists::ScopedIds(il, list_no).get(),
local_D,
local_I,
k);
}
};
// start the threads. Threads are numbered rank=0..nproc-1 (a la MPI)
// thread rank takes care of inverted lists
// rank, rank+nproc, rank+2*nproc,...
std::vector<std::thread> threads;
for (int rank = 0; rank < nproc; rank++) {
threads.emplace_back(search_function, rank);
}
// join threads, merge heaps
for (int rank = 0; rank < nproc; rank++) {
threads[rank].join();
if (rank == 0)
continue; // nothing to merge
// merge into first result
if (metric == METRIC_L2) {
maxheap_addn(
k,
D.data(),
I.data(),
D.data() + rank * k,
I.data() + rank * k,
k);
} else {
minheap_addn(
k,
D.data(),
I.data(),
D.data() + rank * k,
I.data() + rank * k,
k);
}
}
// re-order heap
if (metric == METRIC_L2) {
maxheap_reorder(k, D.data(), I.data());
} else {
minheap_reorder(k, D.data(), I.data());
}
// check that we have the same results as the reference search
for (int j = 0; j < k; j++) {
EXPECT_EQ(I[j], ref_I[i * k + j]);
}
}
}
} // namespace
TEST(TestLowLevelIVF, ThreadedSearch) {
test_threaded_search("IVF32,Flat", METRIC_L2);
}