10 #include "IndexIVFFlat.h"
16 #include "FaissAssert.h"
17 #include "IndexFlat.h"
18 #include "AuxIndexStructures.h"
28 IndexIVFFlat::IndexIVFFlat (Index * quantizer,
30 IndexIVF (quantizer, d, nlist, sizeof(float) * d, metric)
32 code_size =
sizeof(float) * d;
42 const long *precomputed_idx)
48 "cannot have direct map and add with ids");
52 if (precomputed_idx) {
53 idx = precomputed_idx;
55 long * idx0 =
new long [n];
61 for (
size_t i = 0; i < n; i++) {
62 long id = xids ? xids[i] :
ntotal + i;
63 long list_no = idx [i];
67 const float *xi = x + i *
d;
69 list_no,
id, (
const uint8_t*) xi);
72 direct_map.push_back (list_no << 32 | offset);
76 printf(
"IndexIVFFlat::add_core: added %ld / %ld vectors\n",
84 uint8_t * codes)
const
94 template<MetricType metric,
bool store_pairs,
class C>
98 IVFFlatScanner(
size_t d): d(d) {}
101 void set_query (
const float *query)
override {
106 void set_list (idx_t list_no,
float )
override {
107 this->list_no = list_no;
110 float distance_to_code (
const uint8_t *code)
const override {
111 const float *yj = (
float*)code;
112 float dis = metric == METRIC_INNER_PRODUCT ?
113 fvec_inner_product (xi, yj, d) :
fvec_L2sqr (xi, yj, d);
117 size_t scan_codes (
size_t list_size,
118 const uint8_t *codes,
120 float *simi, idx_t *idxi,
121 size_t k)
const override
123 const float *list_vecs = (
const float*)codes;
125 for (
size_t j = 0; j < list_size; j++) {
126 const float * yj = list_vecs + d * j;
127 float dis = metric == METRIC_INNER_PRODUCT ?
128 fvec_inner_product (xi, yj, d) :
fvec_L2sqr (xi, yj, d);
129 if (C::cmp (simi[0], dis)) {
130 heap_pop<C> (k, simi, idxi);
131 long id = store_pairs ? (list_no << 32 | j) : ids[j];
132 heap_push<C> (k, simi, idxi, dis, id);
139 void scan_codes_range (
size_t list_size,
140 const uint8_t *codes,
143 RangeQueryResult & res)
const override
145 const float *list_vecs = (
const float*)codes;
146 for (
size_t j = 0; j < list_size; j++) {
147 const float * yj = list_vecs + d * j;
148 float dis = metric == METRIC_INNER_PRODUCT ?
149 fvec_inner_product (xi, yj, d) :
fvec_L2sqr (xi, yj, d);
150 if (C::cmp (radius, dis)) {
151 long id = store_pairs ? (list_no << 32 | j) : ids[j];
166 (
bool store_pairs)
const
168 if (metric_type == METRIC_INNER_PRODUCT) {
170 return new IVFFlatScanner<
173 return new IVFFlatScanner<
176 }
else if (metric_type == METRIC_L2) {
178 return new IVFFlatScanner<
181 return new IVFFlatScanner<
195 std::vector<idx_t>
assign (n);
198 for (
size_t i = 0; i < n; i++) {
199 idx_t id = new_ids[i];
200 FAISS_THROW_IF_NOT_MSG (0 <=
id &&
id <
ntotal,
201 "id to update out of range");
203 long dm = direct_map[id];
204 long ofs = dm & 0xffffffff;
209 direct_map[id2] = (il << 32) | ofs;
210 invlists->update_entry (il, ofs, id2,
218 long dm = (il << 32) | l;
236 IndexIVFFlatDedup::IndexIVFFlatDedup (
237 Index * quantizer,
size_t d,
size_t nlist_,
245 std::unordered_map<uint64_t, idx_t> map;
246 float * x2 =
new float [n *
d];
250 for (
long i = 0; i < n; i++) {
252 if (map.count(hash) &&
253 !memcmp (x2 + map[hash] * d, x + i * d,
code_size)) {
257 memcpy (x2 + n2 * d, x + i * d,
code_size);
262 printf (
"IndexIVFFlatDedup::train: train on %ld points after dedup "
263 "(was %ld points)\n", n2, n);
271 idx_t na,
const float* x,
const long* xids)
276 FAISS_THROW_IF_NOT_MSG (
278 "IVFFlatDedup not implemented with direct_map");
279 long * idx =
new long [na];
283 long n_add = 0, n_dup = 0;
285 for (
size_t i = 0; i < na; i++) {
286 idx_t
id = xids ? xids[i] :
ntotal + i;
287 long list_no = idx [i];
292 const float *xi = x + i *
d;
299 for (
long o = 0; o < n; o++) {
300 if (!memcmp (codes.get() + o *
code_size,
312 std::pair<idx_t, idx_t> pair (id2,
id);
319 printf(
"IndexIVFFlat::add_with_ids: added %ld / %ld vectors"
320 " (out of which %ld are duplicates)\n",
327 idx_t n,
const float *x, idx_t k,
329 const float *centroid_dis,
330 float *distances, idx_t *labels,
334 FAISS_THROW_IF_NOT_MSG (
335 !store_pairs,
"store_pairs not supported in IVFDedup");
338 distances, labels,
false,
341 std::vector <idx_t> labels2 (k);
342 std::vector <float> dis2 (k);
344 for (
long i = 0; i < n; i++) {
345 idx_t *labels1 = labels + i * k;
346 float *dis1 = distances + i * k;
359 auto range =
instances.equal_range (labels1[rp]);
360 float dis = dis1[rp];
361 labels2[j] = labels1[rp];
364 for (
auto it = range.first; j < k && it != range.second; ++it) {
365 labels2[j] = it->second;
371 memcpy (labels1 + j0, labels2.data() + j0,
372 sizeof(labels1[0]) * (k - j0));
373 memcpy (dis1 + j0, dis2.data() + j0,
374 sizeof(dis2[0]) * (k - j0));
383 std::unordered_map<idx_t, idx_t> replace;
384 std::vector<std::pair<idx_t, idx_t> > toadd;
386 if (sel.is_member(it->first)) {
388 if (!sel.is_member(it->second)) {
390 if (replace.count(it->first) == 0) {
391 replace[it->first] = it->second;
393 std::pair<idx_t, idx_t> new_entry (
394 replace[it->first], it->second);
395 toadd.push_back(new_entry);
400 if (sel.is_member(it->second)) {
408 instances.insert (toadd.begin(), toadd.end());
413 "direct map remove not implemented");
415 std::vector<long> toremove(
nlist);
417 #pragma omp parallel for
418 for (
long i = 0; i <
nlist; i++) {
422 if (sel.is_member (idsi[j])) {
423 if (replace.count(idsi[j]) == 0) {
440 toremove[i] = l0 - l;
444 for (
long i = 0; i <
nlist; i++) {
445 if (toremove[i] > 0) {
446 nremove += toremove[i];
462 FAISS_THROW_MSG (
"not implemented");
467 FAISS_THROW_MSG (
"not implemented");
475 FAISS_THROW_MSG (
"not implemented");
void train(idx_t n, const float *x) override
also dedups the training set
virtual void search_preassigned(idx_t n, const float *x, idx_t k, const idx_t *assign, const float *centroid_dis, float *distances, idx_t *labels, bool store_pairs, const IVFSearchParameters *params=nullptr) const
float fvec_L2sqr(const float *x, const float *y, size_t d)
Squared L2 distance between two vectors.
void reconstruct_from_offset(long list_no, long offset, float *recons) const override
not implemented
void encode_vectors(idx_t n, const float *x, const idx_t *list_nos, uint8_t *codes) const override
void assign(idx_t n, const float *x, idx_t *labels, idx_t k=1)
virtual size_t list_size(size_t list_no) const =0
get the size of a list
virtual idx_t get_single_id(size_t list_no, size_t offset) const
long idx_t
all indices are this type
void add_with_ids(idx_t n, const float *x, const long *xids) override
implemented for all IndexIVF* classes
void train(idx_t n, const float *x) override
Trains the quantizer and calls train_residual to train sub-quantizers.
virtual const uint8_t * get_single_code(size_t list_no, size_t offset) const
void search_preassigned(idx_t n, const float *x, idx_t k, const idx_t *assign, const float *centroid_dis, float *distances, idx_t *labels, bool store_pairs, const IVFSearchParameters *params=nullptr) const override
void add_with_ids(idx_t n, const float *x, const long *xids) override
implemented for all IndexIVF* classes
uint64_t hash_bytes(const uint8_t *bytes, long n)
virtual size_t add_entry(size_t list_no, idx_t theid, const uint8_t *code)
add one entry to an inverted list
idx_t ntotal
total nb of indexed vectors
bool verbose
verbosity level
void reconstruct_from_offset(long list_no, long offset, float *recons) const override
virtual void update_vectors(int nv, idx_t *idx, const float *v)
void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result) const override
not implemented
void update_vectors(int nv, idx_t *idx, const float *v) override
not implemented
InvertedLists * invlists
Acess to the actual data.
Index * quantizer
quantizer that maps vectors to inverted lists
bool is_trained
set if the Index does not require training, or if training is done already
bool maintain_direct_map
map for direct access to the elements. Enables reconstruct().
long remove_ids(const IDSelector &sel) override
Dataset manipulation functions.
size_t nlist
number of possible key values
size_t code_size
code size per vector in bytes
MetricType
Some algorithms support both an inner product version and a L2 search version.
InvertedListScanner * get_InvertedListScanner(bool store_pairs) const override
get a scanner for this index (store_pairs means ignore labels)
virtual void add_core(idx_t n, const float *x, const long *xids, const long *precomputed_idx)
same as add_with_ids, with precomputed coarse quantizer
std::unordered_multimap< idx_t, idx_t > instances