Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/hoss/faiss/IndexIVFFlat.h
1 /**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 // -*- c++ -*-
9
10 #ifndef FAISS_INDEX_IVF_FLAT_H
11 #define FAISS_INDEX_IVF_FLAT_H
12
13 #include <unordered_map>
14
15 #include "IndexIVF.h"
16
17
18 namespace faiss {
19
20 /** Inverted file with stored vectors. Here the inverted file
21 * pre-selects the vectors to be searched, but they are not otherwise
22 * encoded, the code array just contains the raw float entries.
23 */
24 struct IndexIVFFlat: IndexIVF {
25
26 IndexIVFFlat (
27 Index * quantizer, size_t d, size_t nlist_,
28 MetricType = METRIC_L2);
29
30 /// same as add_with_ids, with precomputed coarse quantizer
31 virtual void add_core (idx_t n, const float * x, const long *xids,
32 const long *precomputed_idx);
33
34 /// implemented for all IndexIVF* classes
35 void add_with_ids(idx_t n, const float* x, const long* xids) override;
36
37 void encode_vectors(idx_t n, const float* x,
38 const idx_t *list_nos,
39 uint8_t * codes) const override;
40
41
42 InvertedListScanner *get_InvertedListScanner (bool store_pairs)
43 const override;
44
45 /** Update a subset of vectors.
46 *
47 * The index must have a direct_map
48 *
49 * @param nv nb of vectors to update
50 * @param idx vector indices to update, size nv
51 * @param v vectors of new values, size nv*d
52 */
53 virtual void update_vectors (int nv, idx_t *idx, const float *v);
54
55 void reconstruct_from_offset (long list_no, long offset,
56 float* recons) const override;
57
58 IndexIVFFlat () {}
59 };
60
61
62 struct IndexIVFFlatDedup: IndexIVFFlat {
63
64 /** Maps ids stored in the index to the ids of vectors that are
65 * the same. When a vector is unique, it does not appear in the
66 * instances map */
67 std::unordered_multimap <idx_t, idx_t> instances;
68
69 IndexIVFFlatDedup (
70 Index * quantizer, size_t d, size_t nlist_,
71 MetricType = METRIC_L2);
72
73 /// also dedups the training set
74 void train(idx_t n, const float* x) override;
75
76 /// implemented for all IndexIVF* classes
77 void add_with_ids(idx_t n, const float* x, const long* xids) override;
78
79 void search_preassigned (idx_t n, const float *x, idx_t k,
80 const idx_t *assign,
81 const float *centroid_dis,
82 float *distances, idx_t *labels,
83 bool store_pairs,
84 const IVFSearchParameters *params=nullptr
85 ) const override;
86
87 long remove_ids(const IDSelector& sel) override;
88
89 /// not implemented
90 void range_search(
91 idx_t n,
92 const float* x,
93 float radius,
94 RangeSearchResult* result) const override;
95
96 /// not implemented
97 void update_vectors (int nv, idx_t *idx, const float *v) override;
98
99
100 /// not implemented
101 void reconstruct_from_offset (long list_no, long offset,
102 float* recons) const override;
103
104 IndexIVFFlatDedup () {}
105
106
107 };
108
109
110
111 } // namespace faiss
112
113 #endif
faiss::IndexIVF
Definition: IndexIVF.h:90
faiss::IndexIVFFlatDedup::train
void train(idx_t n, const float *x) override
also dedups the training set
Definition: IndexIVFFlat.cpp:243
faiss::IndexIVFFlatDedup::reconstruct_from_offset
void reconstruct_from_offset(long list_no, long offset, float *recons) const override
not implemented
Definition: IndexIVFFlat.cpp:471
faiss::IndexIVFFlat::encode_vectors
void encode_vectors(idx_t n, const float *x, const idx_t *list_nos, uint8_t *codes) const override
Definition: IndexIVFFlat.cpp:82
faiss::Index::assign
void assign(idx_t n, const float *x, idx_t *labels, idx_t k=1)
Definition: Index.cpp:34
faiss::IDSelector
Definition: AuxIndexStructures.h:54
faiss::IndexIVFFlat
Definition: IndexIVFFlat.h:24
faiss::IVFSearchParameters
Definition: IndexIVF.h:60
faiss::Index::d
int d
vector dimension
Definition: Index.h:66
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:62
faiss::IndexIVFFlatDedup::add_with_ids
void add_with_ids(idx_t n, const float *x, const long *xids) override
implemented for all IndexIVF* classes
Definition: IndexIVFFlat.cpp:270
faiss::IndexIVFFlatDedup::search_preassigned
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
Definition: IndexIVFFlat.cpp:326
faiss::IndexIVFFlat::add_with_ids
void add_with_ids(idx_t n, const float *x, const long *xids) override
implemented for all IndexIVF* classes
Definition: IndexIVFFlat.cpp:36
faiss::IndexIVFFlat::reconstruct_from_offset
void reconstruct_from_offset(long list_no, long offset, float *recons) const override
Definition: IndexIVFFlat.cpp:226
faiss::IndexIVFFlat::update_vectors
virtual void update_vectors(int nv, idx_t *idx, const float *v)
Definition: IndexIVFFlat.cpp:190
faiss::IndexIVFFlatDedup::range_search
void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result) const override
not implemented
Definition: IndexIVFFlat.cpp:456
faiss::InvertedListScanner
Definition: IndexIVF.h:272
faiss::IndexIVFFlatDedup::update_vectors
void update_vectors(int nv, idx_t *idx, const float *v) override
not implemented
Definition: IndexIVFFlat.cpp:465
faiss::RangeSearchResult
Definition: AuxIndexStructures.h:31
faiss::Index
Definition: Index.h:61
faiss::Level1Quantizer::quantizer
Index * quantizer
quantizer that maps vectors to inverted lists
Definition: IndexIVF.h:32
faiss::IndexIVFFlatDedup::remove_ids
long remove_ids(const IDSelector &sel) override
Dataset manipulation functions.
Definition: IndexIVFFlat.cpp:381
faiss::MetricType
MetricType
Some algorithms support both an inner product version and a L2 search version.
Definition: Index.h:44
faiss::IndexIVFFlatDedup
Definition: IndexIVFFlat.h:62
faiss::IndexIVFFlat::get_InvertedListScanner
InvertedListScanner * get_InvertedListScanner(bool store_pairs) const override
get a scanner for this index (store_pairs means ignore labels)
Definition: IndexIVFFlat.cpp:166
faiss::IndexIVFFlat::add_core
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
Definition: IndexIVFFlat.cpp:41
faiss::IndexIVFFlatDedup::instances
std::unordered_multimap< idx_t, idx_t > instances
Definition: IndexIVFFlat.h:67