Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/hoss/faiss/IndexBinaryIVF.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_BINARY_IVF_H
11 #define FAISS_INDEX_BINARY_IVF_H
12 
13 
14 #include <vector>
15 
16 #include "IndexBinary.h"
17 #include "IndexIVF.h"
18 #include "Clustering.h"
19 #include "Heap.h"
20 
21 
22 namespace faiss {
23 
24 struct BinaryInvertedListScanner;
25 
26 /** Index based on a inverted file (IVF)
27  *
28  * In the inverted file, the quantizer (an IndexBinary instance) provides a
29  * quantization index for each vector to be added. The quantization
30  * index maps to a list (aka inverted list or posting list), where the
31  * id of the vector is stored.
32  *
33  * Otherwise the object is similar to the IndexIVF
34  */
36  /// Acess to the actual data
38  bool own_invlists;
39 
40  size_t nprobe; ///< number of probes at query time
41  size_t max_codes; ///< max nb of codes to visit to do a query
42 
43  /** Select between using a heap or counting to select the k smallest values
44  * when scanning inverted lists.
45  */
46  bool use_heap = true;
47 
48  /// map for direct access to the elements. Enables reconstruct().
50  std::vector<long> direct_map;
51 
52  IndexBinary *quantizer; ///< quantizer that maps vectors to inverted lists
53  size_t nlist; ///< number of possible key values
54 
55  bool own_fields; ///< whether object owns the quantizer
56 
57  ClusteringParameters cp; ///< to override default clustering params
58  Index *clustering_index; ///< to override index used during clustering
59 
60  /** The Inverted file takes a quantizer (an IndexBinary) on input,
61  * which implements the function mapping a vector to a list
62  * identifier. The pointer is borrowed: the quantizer should not
63  * be deleted while the IndexBinaryIVF is in use.
64  */
65  IndexBinaryIVF(IndexBinary *quantizer, size_t d, size_t nlist);
66 
68 
69  ~IndexBinaryIVF() override;
70 
71  void reset() override;
72 
73  /// Trains the quantizer
74  void train(idx_t n, const uint8_t *x) override;
75 
76  void add(idx_t n, const uint8_t *x) override;
77 
78  void add_with_ids(idx_t n, const uint8_t *x, const long *xids) override;
79 
80  /// same as add_with_ids, with precomputed coarse quantizer
81  void add_core (idx_t n, const uint8_t * x, const long *xids,
82  const long *precomputed_idx);
83 
84  /** Search a set of vectors, that are pre-quantized by the IVF
85  * quantizer. Fill in the corresponding heaps with the query
86  * results. search() calls this.
87  *
88  * @param n nb of vectors to query
89  * @param x query vectors, size nx * d
90  * @param assign coarse quantization indices, size nx * nprobe
91  * @param centroid_dis
92  * distances to coarse centroids, size nx * nprobe
93  * @param distance
94  * output distances, size n * k
95  * @param labels output labels, size n * k
96  * @param store_pairs store inv list index + inv list offset
97  * instead in upper/lower 32 bit of result,
98  * instead of ids (used for reranking).
99  * @param params used to override the object's search parameters
100  */
101  void search_preassigned(idx_t n, const uint8_t *x, idx_t k,
102  const idx_t *assign,
103  const int32_t *centroid_dis,
104  int32_t *distances, idx_t *labels,
105  bool store_pairs,
106  const IVFSearchParameters *params=nullptr
107  ) const;
108 
109  virtual BinaryInvertedListScanner *get_InvertedListScanner (
110  bool store_pairs=false) const;
111 
112  /** assign the vectors, then call search_preassign */
113  virtual void search(idx_t n, const uint8_t *x, idx_t k,
114  int32_t *distances, idx_t *labels) const override;
115 
116  void reconstruct(idx_t key, uint8_t *recons) const override;
117 
118  /** Reconstruct a subset of the indexed vectors.
119  *
120  * Overrides default implementation to bypass reconstruct() which requires
121  * direct_map to be maintained.
122  *
123  * @param i0 first vector to reconstruct
124  * @param ni nb of vectors to reconstruct
125  * @param recons output array of reconstructed vectors, size ni * d / 8
126  */
127  void reconstruct_n(idx_t i0, idx_t ni, uint8_t *recons) const override;
128 
129  /** Similar to search, but also reconstructs the stored vectors (or an
130  * approximation in the case of lossy coding) for the search results.
131  *
132  * Overrides default implementation to avoid having to maintain direct_map
133  * and instead fetch the code offsets through the `store_pairs` flag in
134  * search_preassigned().
135  *
136  * @param recons reconstructed vectors size (n, k, d / 8)
137  */
138  void search_and_reconstruct(idx_t n, const uint8_t *x, idx_t k,
139  int32_t *distances, idx_t *labels,
140  uint8_t *recons) const override;
141 
142  /** Reconstruct a vector given the location in terms of (inv list index +
143  * inv list offset) instead of the id.
144  *
145  * Useful for reconstructing when the direct_map is not maintained and
146  * the inv list offset is computed by search_preassigned() with
147  * `store_pairs` set.
148  */
149  virtual void reconstruct_from_offset(long list_no, long offset,
150  uint8_t* recons) const;
151 
152 
153  /// Dataset manipulation functions
154 
155  long remove_ids(const IDSelector& sel) override;
156 
157  /** moves the entries from another dataset to self. On output,
158  * other is empty. add_id is added to all moved ids (for
159  * sequential ids, this would be this->ntotal */
160  virtual void merge_from(IndexBinaryIVF& other, idx_t add_id);
161 
162  size_t get_list_size(size_t list_no) const
163  { return invlists->list_size(list_no); }
164 
165  /** intialize a direct map
166  *
167  * @param new_maintain_direct_map if true, create a direct map,
168  * else clear it
169  */
170  void make_direct_map(bool new_maintain_direct_map=true);
171 
172  void replace_invlists(InvertedLists *il, bool own=false);
173 };
174 
175 
177 
178  using idx_t = Index::idx_t;
179 
180  /// from now on we handle this query.
181  virtual void set_query (const uint8_t *query_vector) = 0;
182 
183  /// following codes come from this inverted list
184  virtual void set_list (idx_t list_no, uint8_t coarse_dis) = 0;
185 
186  /// compute a single query-to-code distance
187  virtual uint32_t distance_to_code (const uint8_t *code) const = 0;
188 
189  /** compute the distances to codes. (distances, labels) should be
190  * organized as a min- or max-heap
191  *
192  * @param n number of codes to scan
193  * @param codes codes to scan (n * code_size)
194  * @param ids corresponding ids (ignored if store_pairs)
195  * @param distances heap distances (size k)
196  * @param labels heap labels (size k)
197  * @param k heap size
198  */
199  virtual size_t scan_codes (size_t n,
200  const uint8_t *codes,
201  const idx_t *ids,
202  int32_t *distances, idx_t *labels,
203  size_t k) const = 0;
204 
205  virtual ~BinaryInvertedListScanner () {}
206 
207 };
208 
209 
210 } // namespace faiss
211 
212 #endif // FAISS_INDEX_BINARY_IVF_H
virtual void set_query(const uint8_t *query_vector)=0
from now on we handle this query.
size_t nprobe
number of probes at query time
void add_core(idx_t n, const uint8_t *x, const long *xids, const long *precomputed_idx)
same as add_with_ids, with precomputed coarse quantizer
virtual void reconstruct_from_offset(long list_no, long offset, uint8_t *recons) const
virtual void set_list(idx_t list_no, uint8_t coarse_dis)=0
following codes come from this inverted list
bool maintain_direct_map
map for direct access to the elements. Enables reconstruct().
ClusteringParameters cp
to override default clustering params
void search_preassigned(idx_t n, const uint8_t *x, idx_t k, const idx_t *assign, const int32_t *centroid_dis, int32_t *distances, idx_t *labels, bool store_pairs, const IVFSearchParameters *params=nullptr) const
size_t nlist
number of possible key values
virtual size_t list_size(size_t list_no) const =0
get the size of a list
IndexBinary * quantizer
quantizer that maps vectors to inverted lists
bool own_fields
whether object owns the quantizer
virtual void merge_from(IndexBinaryIVF &other, idx_t add_id)
Index::idx_t idx_t
all indices are this type
Definition: IndexBinary.h:37
void make_direct_map(bool new_maintain_direct_map=true)
int d
vector dimension
Definition: IndexBinary.h:41
Index * clustering_index
to override index used during clustering
long idx_t
all indices are this type
Definition: Index.h:62
virtual uint32_t distance_to_code(const uint8_t *code) const =0
compute a single query-to-code distance
void train(idx_t n, const uint8_t *x) override
Trains the quantizer.
void assign(idx_t n, const uint8_t *x, idx_t *labels, idx_t k=1)
Definition: IndexBinary.cpp:28
void reset() override
Removes all elements from the database.
virtual size_t scan_codes(size_t n, const uint8_t *codes, const idx_t *ids, int32_t *distances, idx_t *labels, size_t k) const =0
size_t max_codes
max nb of codes to visit to do a query
void add_with_ids(idx_t n, const uint8_t *x, const long *xids) override
void add(idx_t n, const uint8_t *x) override
long remove_ids(const IDSelector &sel) override
Dataset manipulation functions.
virtual void search(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels) const override
void search_and_reconstruct(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels, uint8_t *recons) const override
void reconstruct_n(idx_t i0, idx_t ni, uint8_t *recons) const override
void reconstruct(idx_t key, uint8_t *recons) const override
InvertedLists * invlists
Acess to the actual data.