forked from facebookresearch/faiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexBinaryHash.cpp
492 lines (391 loc) · 12 KB
/
IndexBinaryHash.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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// Copyright 2004-present Facebook. All Rights Reserved
// -*- c++ -*-
#include <faiss/IndexBinaryHash.h>
#include <cstdio>
#include <memory>
#include <faiss/utils/hamming.h>
#include <faiss/utils/utils.h>
#include <faiss/impl/AuxIndexStructures.h>
#include <faiss/impl/FaissAssert.h>
namespace faiss {
void IndexBinaryHash::InvertedList::add (
idx_t id, size_t code_size, const uint8_t *code)
{
ids.push_back(id);
vecs.insert(vecs.end(), code, code + code_size);
}
IndexBinaryHash::IndexBinaryHash(int d, int b):
IndexBinary(d), b(b), nflip(0)
{
is_trained = true;
}
IndexBinaryHash::IndexBinaryHash(): b(0), nflip(0)
{
is_trained = true;
}
void IndexBinaryHash::reset()
{
invlists.clear();
ntotal = 0;
}
void IndexBinaryHash::add(idx_t n, const uint8_t *x)
{
add_with_ids(n, x, nullptr);
}
void IndexBinaryHash::add_with_ids(idx_t n, const uint8_t *x, const idx_t *xids)
{
uint64_t mask = ((uint64_t)1 << b) - 1;
// simplistic add function. Cannot really be parallelized.
for (idx_t i = 0; i < n; i++) {
idx_t id = xids ? xids[i] : ntotal + i;
const uint8_t * xi = x + i * code_size;
idx_t hash = *((uint64_t*)xi) & mask;
invlists[hash].add(id, code_size, xi);
}
ntotal += n;
}
namespace {
/** Enumerate all bit vectors of size nbit with up to maxflip 1s
* test in P127257851 P127258235
*/
struct FlipEnumerator {
int nbit, nflip, maxflip;
uint64_t mask, x;
FlipEnumerator (int nbit, int maxflip): nbit(nbit), maxflip(maxflip) {
nflip = 0;
mask = 0;
x = 0;
}
bool next() {
if (x == mask) {
if (nflip == maxflip) {
return false;
}
// increase Hamming radius
nflip++;
mask = (((uint64_t)1 << nflip) - 1);
x = mask << (nbit - nflip);
return true;
}
int i = __builtin_ctzll(x);
if (i > 0) {
x ^= (uint64_t)3 << (i - 1);
} else {
// nb of LSB 1s
int n1 = __builtin_ctzll(~x);
// clear them
x &= ((uint64_t)(-1) << n1);
int n2 = __builtin_ctzll(x);
x ^= (((uint64_t)1 << (n1 + 2)) - 1) << (n2 - n1 - 1);
}
return true;
}
};
using idx_t = Index::idx_t;
struct RangeSearchResults {
int radius;
RangeQueryResult &qres;
inline void add (float dis, idx_t id) {
if (dis < radius) {
qres.add (dis, id);
}
}
};
struct KnnSearchResults {
// heap params
idx_t k;
int32_t * heap_sim;
idx_t * heap_ids;
using C = CMax<int, idx_t>;
inline void add (float dis, idx_t id) {
if (dis < heap_sim[0]) {
heap_pop<C> (k, heap_sim, heap_ids);
heap_push<C> (k, heap_sim, heap_ids, dis, id);
}
}
};
template<class HammingComputer, class SearchResults>
void
search_single_query_template(const IndexBinaryHash & index, const uint8_t *q,
SearchResults &res,
size_t &n0, size_t &nlist, size_t &ndis)
{
size_t code_size = index.code_size;
uint64_t mask = ((uint64_t)1 << index.b) - 1;
uint64_t qhash = *((uint64_t*)q) & mask;
HammingComputer hc (q, code_size);
FlipEnumerator fe(index.b, index.nflip);
// loop over neighbors that are at most at nflip bits
do {
uint64_t hash = qhash ^ fe.x;
auto it = index.invlists.find (hash);
if (it == index.invlists.end()) {
continue;
}
const IndexBinaryHash::InvertedList &il = it->second;
size_t nv = il.ids.size();
if (nv == 0) {
n0++;
} else {
const uint8_t *codes = il.vecs.data();
for (size_t i = 0; i < nv; i++) {
int dis = hc.hamming (codes);
res.add(dis, il.ids[i]);
codes += code_size;
}
ndis += nv;
nlist++;
}
} while(fe.next());
}
template<class SearchResults>
void
search_single_query(const IndexBinaryHash & index, const uint8_t *q,
SearchResults &res,
size_t &n0, size_t &nlist, size_t &ndis)
{
#define HC(name) search_single_query_template<name>(index, q, res, n0, nlist, ndis);
switch(index.code_size) {
case 4: HC(HammingComputer4); break;
case 8: HC(HammingComputer8); break;
case 16: HC(HammingComputer16); break;
case 20: HC(HammingComputer20); break;
case 32: HC(HammingComputer32); break;
default:
if (index.code_size % 8 == 0) {
HC(HammingComputerM8);
} else {
HC(HammingComputerDefault);
}
}
#undef HC
}
} // anonymous namespace
void IndexBinaryHash::range_search(idx_t n, const uint8_t *x, int radius,
RangeSearchResult *result) const
{
size_t nlist = 0, ndis = 0, n0 = 0;
#pragma omp parallel if(n > 100) reduction(+: ndis, n0, nlist)
{
RangeSearchPartialResult pres (result);
#pragma omp for
for (size_t i = 0; i < n; i++) { // loop queries
RangeQueryResult & qres = pres.new_result (i);
RangeSearchResults res = {radius, qres};
const uint8_t *q = x + i * code_size;
search_single_query (*this, q, res, n0, nlist, ndis);
}
pres.finalize ();
}
indexBinaryHash_stats.nq += n;
indexBinaryHash_stats.n0 += n0;
indexBinaryHash_stats.nlist += nlist;
indexBinaryHash_stats.ndis += ndis;
}
void IndexBinaryHash::search(idx_t n, const uint8_t *x, idx_t k,
int32_t *distances, idx_t *labels) const
{
using HeapForL2 = CMax<int32_t, idx_t>;
size_t nlist = 0, ndis = 0, n0 = 0;
#pragma omp parallel for if(n > 100) reduction(+: nlist, ndis, n0)
for (size_t i = 0; i < n; i++) {
int32_t * simi = distances + k * i;
idx_t * idxi = labels + k * i;
heap_heapify<HeapForL2> (k, simi, idxi);
KnnSearchResults res = {k, simi, idxi};
const uint8_t *q = x + i * code_size;
search_single_query (*this, q, res, n0, nlist, ndis);
}
indexBinaryHash_stats.nq += n;
indexBinaryHash_stats.n0 += n0;
indexBinaryHash_stats.nlist += nlist;
indexBinaryHash_stats.ndis += ndis;
}
size_t IndexBinaryHash::hashtable_size() const
{
return invlists.size();
}
void IndexBinaryHash::display() const
{
for (auto it = invlists.begin(); it != invlists.end(); ++it) {
printf("%ld: [", it->first);
const std::vector<idx_t> & v = it->second.ids;
for (auto x: v) {
printf("%ld ", 0 + x);
}
printf("]\n");
}
}
void IndexBinaryHashStats::reset()
{
memset ((void*)this, 0, sizeof (*this));
}
IndexBinaryHashStats indexBinaryHash_stats;
/*******************************************************
* IndexBinaryMultiHash implementation
******************************************************/
IndexBinaryMultiHash::IndexBinaryMultiHash(int d, int nhash, int b):
IndexBinary(d),
storage(new IndexBinaryFlat(d)), own_fields(true),
maps(nhash), nhash(nhash), b(b), nflip(0)
{
FAISS_THROW_IF_NOT(nhash * b <= d);
}
IndexBinaryMultiHash::IndexBinaryMultiHash():
storage(nullptr), own_fields(true),
nhash(0), b(0), nflip(0)
{}
IndexBinaryMultiHash::~IndexBinaryMultiHash()
{
if (own_fields) {
delete storage;
}
}
void IndexBinaryMultiHash::reset()
{
storage->reset();
ntotal = 0;
for(auto map: maps) {
map.clear();
}
}
void IndexBinaryMultiHash::add(idx_t n, const uint8_t *x)
{
storage->add(n, x);
// populate maps
uint64_t mask = ((uint64_t)1 << b) - 1;
for(idx_t i = 0; i < n; i++) {
const uint8_t *xi = x + i * code_size;
int ho = 0;
for(int h = 0; h < nhash; h++) {
uint64_t hash = *(uint64_t*)(xi + (ho >> 3)) >> (ho & 7);
hash &= mask;
maps[h][hash].push_back(i + ntotal);
ho += b;
}
}
ntotal += n;
}
namespace {
template <class HammingComputer, class SearchResults>
static
void verify_shortlist(
const IndexBinaryFlat & index,
const uint8_t * q,
const std::unordered_set<Index::idx_t> & shortlist,
SearchResults &res)
{
size_t code_size = index.code_size;
size_t nlist = 0, ndis = 0, n0 = 0;
HammingComputer hc (q, code_size);
const uint8_t *codes = index.xb.data();
for (auto i: shortlist) {
int dis = hc.hamming (codes + i * code_size);
res.add(dis, i);
}
}
template<class SearchResults>
void
search_1_query_multihash(const IndexBinaryMultiHash & index, const uint8_t *xi,
SearchResults &res,
size_t &n0, size_t &nlist, size_t &ndis)
{
std::unordered_set<idx_t> shortlist;
int b = index.b;
uint64_t mask = ((uint64_t)1 << b) - 1;
int ho = 0;
for(int h = 0; h < index.nhash; h++) {
uint64_t qhash = *(uint64_t*)(xi + (ho >> 3)) >> (ho & 7);
qhash &= mask;
const IndexBinaryMultiHash::Map & map = index.maps[h];
FlipEnumerator fe(index.b, index.nflip);
// loop over neighbors that are at most at nflip bits
do {
uint64_t hash = qhash ^ fe.x;
auto it = map.find (hash);
if (it != map.end()) {
const std::vector<idx_t> & v = it->second;
for (auto i: v) {
shortlist.insert(i);
}
nlist++;
} else {
n0++;
}
} while(fe.next());
ho += b;
}
ndis += shortlist.size();
// verify shortlist
#define HC(name) verify_shortlist<name> (*index.storage, xi, shortlist, res)
switch(index.code_size) {
case 4: HC(HammingComputer4); break;
case 8: HC(HammingComputer8); break;
case 16: HC(HammingComputer16); break;
case 20: HC(HammingComputer20); break;
case 32: HC(HammingComputer32); break;
default:
if (index.code_size % 8 == 0) {
HC(HammingComputerM8);
} else {
HC(HammingComputerDefault);
}
}
#undef HC
}
} // anonymous namespace
void IndexBinaryMultiHash::range_search(idx_t n, const uint8_t *x, int radius,
RangeSearchResult *result) const
{
size_t nlist = 0, ndis = 0, n0 = 0;
#pragma omp parallel if(n > 100) reduction(+: ndis, n0, nlist)
{
RangeSearchPartialResult pres (result);
#pragma omp for
for (size_t i = 0; i < n; i++) { // loop queries
RangeQueryResult & qres = pres.new_result (i);
RangeSearchResults res = {radius, qres};
const uint8_t *q = x + i * code_size;
search_1_query_multihash (*this, q, res, n0, nlist, ndis);
}
pres.finalize ();
}
indexBinaryHash_stats.nq += n;
indexBinaryHash_stats.n0 += n0;
indexBinaryHash_stats.nlist += nlist;
indexBinaryHash_stats.ndis += ndis;
}
void IndexBinaryMultiHash::search(idx_t n, const uint8_t *x, idx_t k,
int32_t *distances, idx_t *labels) const
{
using HeapForL2 = CMax<int32_t, idx_t>;
size_t nlist = 0, ndis = 0, n0 = 0;
#pragma omp parallel for if(n > 100) reduction(+: nlist, ndis, n0)
for (size_t i = 0; i < n; i++) {
int32_t * simi = distances + k * i;
idx_t * idxi = labels + k * i;
heap_heapify<HeapForL2> (k, simi, idxi);
KnnSearchResults res = {k, simi, idxi};
const uint8_t *q = x + i * code_size;
search_1_query_multihash (*this, q, res, n0, nlist, ndis);
}
indexBinaryHash_stats.nq += n;
indexBinaryHash_stats.n0 += n0;
indexBinaryHash_stats.nlist += nlist;
indexBinaryHash_stats.ndis += ndis;
}
size_t IndexBinaryMultiHash::hashtable_size() const
{
size_t tot = 0;
for (auto map: maps) {
tot += map.size();
}
return tot;
}
}