forked from facebookresearch/faiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexIVFPQ.h
284 lines (204 loc) · 8.66 KB
/
IndexIVFPQ.h
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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the CC-by-NC license found in the
* LICENSE file in the root directory of this source tree.
*/
// Copyright 2004-present Facebook. All Rights Reserved.
// -*- c++ -*-
#ifndef FAISS_INDEX_IVFPQ_H
#define FAISS_INDEX_IVFPQ_H
#include <vector>
#include "IndexIVF.h"
#include "IndexPQ.h"
namespace faiss {
/** Inverted file with Product Quantizer encoding. Each residual
* vector is encoded as a product quantizer code.
*/
struct IndexIVFPQ: IndexIVF {
bool by_residual; ///< Encode residual or plain vector?
int use_precomputed_table; ///< if by_residual, build precompute tables
size_t code_size; ///< code size per vector in bytes
ProductQuantizer pq; ///< produces the codes
bool do_polysemous_training; ///< reorder PQ centroids after training?
PolysemousTraining *polysemous_training; ///< if NULL, use default
// search-time parameters
size_t scan_table_threshold; ///< use table computation or on-the-fly?
size_t max_codes; ///< max nb of codes to visit to do a query
int polysemous_ht; ///< Hamming thresh for polysemous filtering
std::vector < std::vector<uint8_t> > codes; // binary codes, size nlist
/// if use_precompute_table
/// size nlist * pq.M * pq.ksub
std::vector <float> precomputed_table;
IndexIVFPQ (
Index * quantizer, size_t d, size_t nlist,
size_t M, size_t nbits_per_idx);
virtual void set_typename () override;
virtual void add_with_ids (
idx_t n, const float *x,
const long *xids = nullptr) override;
/// same as add_core, also:
/// - output 2nd level residuals if residuals_2 != NULL
/// - use precomputed list numbers if precomputed_idx != NULL
void add_core_o (idx_t n, const float *x,
const long *xids, float *residuals_2,
const long *precomputed_idx = nullptr);
virtual void search (
idx_t n, const float *x, idx_t k,
float *distances, idx_t *labels) const override;
virtual void reset () override;
virtual long remove_ids (const IDSelector & sel) override;
/// trains the product quantizer
virtual void train_residual(idx_t n, const float *x) override;
/// same as train_residual, also output 2nd level residuals
void train_residual_o (idx_t n, const float *x, float *residuals_2);
/** Reconstruct a subset of the indexed vectors
*
* @param i0 first vector to reconstruct
* @param ni nb of vectors to reconstruct
* @param recons output array of reconstructed vectors, size ni * d
*/
virtual void reconstruct_n (idx_t i0, idx_t ni, float *recons)
const override;
virtual void reconstruct (idx_t key, float * recons)
const override;
/** Find exact duplicates in the dataset.
*
* the duplicates are returned in pre-allocated arrays (see the
* max sizes).
*
* @params lims limits between groups of duplicates
* (max size ntotal / 2 + 1)
* @params ids ids[lims[i]] : ids[lims[i+1]-1] is a group of
* duplicates (max size ntotal)
* @return n number of groups found
*/
size_t find_duplicates (idx_t *ids, size_t *lims) const;
// map a vector to a binary code knowning the index
void encode (long key, const float * x, uint8_t * code) const;
/// same as encode, for multiple points at once
void encode_multiple (size_t n, const long *keys,
const float * x, uint8_t * codes) const;
/** search a set of vectors, that are pre-quantized by the IVF
* quantizer. Fill in the corresponding heaps with the query
* results.
*
* @param nx nb of vectors to query
* @param qx query vectors, size nx * d
* @param keys coarse quantization indices, size nx * nprobe
* @param coarse_dis
* distances to coarse centroids, size nx * nprobe
* @param res heaps for all the results, gives the nprobe
* @param store_pairs store inv list index + inv list offset
* instead in upper/lower 32 bit of result,
* instead of ids (used for reranking).
*/
virtual void search_knn_with_key (
size_t nx,
const float * qx,
const long * keys,
const float * coarse_dis,
float_maxheap_array_t* res,
bool store_pairs = false) const;
/// build precomputed table
void precompute_table ();
/// used to implement merging
virtual void merge_from_residuals (IndexIVF &other) override;
/** copy a subset of the entries index to the other index
*
* if subset_type == 0: copies ids in [a1, a2)
* if subset_type == 1: copies ids if id % a1 == a2
*/
void copy_subset_to (IndexIVFPQ & other, int subset_type,
long a1, long a2) const;
IndexIVFPQ ();
};
/// statistics are robust to internal threading, but not if
/// IndexIVFPQ::search is called by multiple threads
struct IndexIVFPQStats {
size_t nq; // nb of queries run
size_t nlist; // nb of inverted lists scanned
size_t ncode; // nb of codes visited
size_t nrefine; // nb of refines (IVFPQR)
size_t n_hamming_pass;
// nb of passed Hamming distance tests (for polysemous)
// timings measured with the CPU RTC
// on all threads
size_t assign_cycles;
size_t search_cycles;
size_t refine_cycles; // only for IVFPQR
// single thread (double-counted with search_cycles)
size_t init_query_cycles;
size_t init_list_cycles;
size_t scan_cycles;
size_t heap_cycles;
IndexIVFPQStats () {reset (); }
void reset ();
};
// global var that collects them all
extern IndexIVFPQStats indexIVFPQ_stats;
/** Index with an additional level of PQ refinement */
struct IndexIVFPQR: IndexIVFPQ {
ProductQuantizer refine_pq; ///< 3rd level quantizer
std::vector <uint8_t> refine_codes; ///< corresponding codes
/// factor between k requested in search and the k requested from the IVFPQ
float k_factor;
IndexIVFPQR (
Index * quantizer, size_t d, size_t nlist,
size_t M, size_t nbits_per_idx,
size_t M_refine, size_t nbits_per_idx_refine);
virtual void set_typename () override;
virtual void reset() override;
virtual long remove_ids (const IDSelector & sel) override;
/// trains the two product quantizers
virtual void train_residual (idx_t n, const float *x) override;
virtual void add_with_ids (idx_t n, const float *x, const long *xids)
override;
/// same as add_with_ids, but optionally use the precomputed list ids
void add_core (idx_t n, const float *x, const long *xids,
const long *precomputed_idx = nullptr);
virtual void reconstruct_n (idx_t i0, idx_t ni, float *recons)
const override;
virtual void search (
idx_t n, const float *x, idx_t k,
float *distances, idx_t *labels) const override;
virtual void merge_from_residuals (IndexIVF &other) override;
IndexIVFPQR();
};
/** Index with 32-bit ids and flat tables. Must be constructed from an
* exisiting IndexIVFPQ. Cannot be copy-constructed/assigned. The
* actual data is stored in the compact_* tables, the ids and codes
* tables are not used. */
struct IndexIVFPQCompact: IndexIVFPQ {
explicit IndexIVFPQCompact (const IndexIVFPQ &other);
/// how were the compact tables allocated?
enum Alloc_type_t {
Alloc_type_none, ///< alloc from outside
Alloc_type_new, ///< was allocated with new
Alloc_type_mmap ///< was mmapped
};
Alloc_type_t alloc_type;
uint32_t *limits; ///< size nlist + 1
uint32_t *compact_ids; ///< size ntotal
uint8_t *compact_codes; ///< size ntotal * code_size
// file and buffer this was mmapped (will be unmapped when object
// is deleted)
char * mmap_buffer;
long mmap_length;
virtual void search_knn_with_key (
size_t nx,
const float * qx,
const long * keys,
const float * coarse_dis,
float_maxheap_array_t * res,
bool store_pairs = false) const override;
/// the three following functions will fail at runtime
virtual void add (idx_t, const float *) override;
virtual void reset () override;
virtual void train (idx_t, const float *) override;
virtual ~IndexIVFPQCompact ();
IndexIVFPQCompact ();
};
} // namespace faiss
#endif