Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/tmp/faiss/IndexBinary.h
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD+Patents license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 // -*- c++ -*-
10
11 #ifndef FAISS_INDEX_BINARY_H
12 #define FAISS_INDEX_BINARY_H
13
14 #include <cstdio>
15 #include <typeinfo>
16 #include <string>
17 #include <sstream>
18
19 #include "FaissAssert.h"
20 #include "Index.h"
21
22
23 namespace faiss {
24
25
26 /// Forward declarations see AuxIndexStructures.h
27 struct IDSelector;
28 struct RangeSearchResult;
29
30 /** Abstract structure for a binary index.
31 *
32 * Supports adding vertices and searching them.
33 *
34 * All queries are symmetric because there is no distinction between codes and
35 * vectors.
36 */
37 struct IndexBinary {
38 typedef long idx_t; ///< all indices are this type
39
40 int d; ///< vector dimension
41 int code_size; ///< number of bytes per vector ( = d / 8 )
42 idx_t ntotal; ///< total nb of indexed vectors
43 bool verbose; ///< verbosity level
44
45 /// set if the Index does not require training, or if training is done already
46 bool is_trained;
47
48 /// type of metric this index uses for search
49 MetricType metric_type;
50
51 explicit IndexBinary(idx_t d = 0, MetricType metric = METRIC_L2)
52 : d(d),
53 code_size(d / 8),
54 ntotal(0),
55 verbose(false),
56 is_trained(true),
57 metric_type(metric) {
58 FAISS_THROW_IF_NOT(d % 8 == 0);
59 }
60
61 virtual ~IndexBinary();
62
63
64 /** Perform training on a representative set of vectors.
65 *
66 * @param n nb of training vectors
67 * @param x training vecors, size n * d / 8
68 */
69 virtual void train(idx_t n, const uint8_t *x);
70
71 /** Add n vectors of dimension d to the index.
72 *
73 * Vectors are implicitly assigned labels ntotal .. ntotal + n - 1
74 * @param x input matrix, size n * d / 8
75 */
76 virtual void add(idx_t n, const uint8_t *x) = 0;
77
78 /** Same as add, but stores xids instead of sequential ids.
79 *
80 * The default implementation fails with an assertion, as it is
81 * not supported by all indexes.
82 *
83 * @param xids if non-null, ids to store for the vectors (size n)
84 */
85 virtual void add_with_ids(idx_t n, const uint8_t *x, const long *xids);
86
87 /** Query n vectors of dimension d to the index.
88 *
89 * return at most k vectors. If there are not enough results for a
90 * query, the result array is padded with -1s.
91 *
92 * @param x input vectors to search, size n * d / 8
93 * @param labels output labels of the NNs, size n*k
94 * @param distances output pairwise distances, size n*k
95 */
96 virtual void search(idx_t n, const uint8_t *x, idx_t k,
97 int32_t *distances, idx_t *labels) const = 0;
98
99 /** Query n vectors of dimension d to the index.
100 *
101 * return all vectors with distance < radius. Note that many
102 * indexes do not implement the range_search (only the k-NN search
103 * is mandatory).
104 *
105 * @param x input vectors to search, size n * d / 8
106 * @param radius search radius
107 * @param result result table
108 */
109 virtual void range_search(idx_t n, const uint8_t *x, int radius,
110 RangeSearchResult *result) const;
111
112 /** Return the indexes of the k vectors closest to the query x.
113 *
114 * This function is identical to search but only returns labels of neighbors.
115 * @param x input vectors to search, size n * d / 8
116 * @param labels output labels of the NNs, size n*k
117 */
118 void assign(idx_t n, const uint8_t *x, idx_t *labels, idx_t k = 1);
119
120 /// Removes all elements from the database.
121 virtual void reset() = 0;
122
123 /** Removes IDs from the index. Not supported by all indexes.
124 */
125 virtual long remove_ids(const IDSelector& sel);
126
127 /** Reconstruct a stored vector.
128 *
129 * This function may not be defined for some indexes.
130 * @param key id of the vector to reconstruct
131 * @param recons reconstucted vector (size d / 8)
132 */
133 virtual void reconstruct(idx_t key, uint8_t *recons) const;
134
135
136 /** Reconstruct vectors i0 to i0 + ni - 1.
137 *
138 * This function may not be defined for some indexes.
139 * @param recons reconstucted vectors (size ni * d / 8)
140 */
141 virtual void reconstruct_n(idx_t i0, idx_t ni, uint8_t *recons) const;
142
143 /** Similar to search, but also reconstructs the stored vectors (or an
144 * approximation in the case of lossy coding) for the search results.
145 *
146 * If there are not enough results for a query, the resulting array
147 * is padded with -1s.
148 *
149 * @param recons reconstructed vectors size (n, k, d)
150 **/
151 virtual void search_and_reconstruct(idx_t n, const uint8_t *x, idx_t k,
152 int32_t *distances, idx_t *labels,
153 uint8_t *recons) const;
154
155 /** Display the actual class name and some more info. */
156 void display() const;
157 };
158
159
160 } // namespace faiss
161
162 #endif // FAISS_INDEX_BINARY_H
faiss::IndexBinary::search
virtual void search(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels) const =0
faiss::IndexBinary::reset
virtual void reset()=0
Removes all elements from the database.
faiss::IDSelector
Definition: AuxIndexStructures.h:54
faiss::IndexBinary::verbose
bool verbose
verbosity level
Definition: IndexBinary.h:43
faiss::IndexBinary::remove_ids
virtual long remove_ids(const IDSelector &sel)
Definition: IndexBinary.cpp:39
faiss::IndexBinary::is_trained
bool is_trained
set if the Index does not require training, or if training is done already
Definition: IndexBinary.h:46
faiss::IndexBinary::train
virtual void train(idx_t n, const uint8_t *x)
Definition: IndexBinary.cpp:20
faiss::IndexBinary::code_size
int code_size
number of bytes per vector ( = d / 8 )
Definition: IndexBinary.h:41
faiss::IndexBinary::d
int d
vector dimension
Definition: IndexBinary.h:40
faiss::IndexBinary::display
void display() const
Definition: IndexBinary.cpp:73
faiss::IndexBinary::reconstruct
virtual void reconstruct(idx_t key, uint8_t *recons) const
Definition: IndexBinary.cpp:44
faiss::IndexBinary::assign
void assign(idx_t n, const uint8_t *x, idx_t *labels, idx_t k=1)
Definition: IndexBinary.cpp:29
faiss::IndexBinary::range_search
virtual void range_search(idx_t n, const uint8_t *x, int radius, RangeSearchResult *result) const
Definition: IndexBinary.cpp:24
faiss::IndexBinary::add_with_ids
virtual void add_with_ids(idx_t n, const uint8_t *x, const long *xids)
Definition: IndexBinary.cpp:35
faiss::IndexBinary::search_and_reconstruct
virtual void search_and_reconstruct(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels, uint8_t *recons) const
Definition: IndexBinary.cpp:54
faiss::IndexBinary::reconstruct_n
virtual void reconstruct_n(idx_t i0, idx_t ni, uint8_t *recons) const
Definition: IndexBinary.cpp:48
faiss::RangeSearchResult
Definition: AuxIndexStructures.h:31
faiss::IndexBinary::ntotal
idx_t ntotal
total nb of indexed vectors
Definition: IndexBinary.h:42
faiss::IndexBinary
Definition: IndexBinary.h:37
faiss::IndexBinary::idx_t
long idx_t
all indices are this type
Definition: IndexBinary.h:38
faiss::IndexBinary::add
virtual void add(idx_t n, const uint8_t *x)=0
faiss::IndexBinary::metric_type
MetricType metric_type
type of metric this index uses for search
Definition: IndexBinary.h:49
faiss::MetricType
MetricType
Some algorithms support both an inner product version and a L2 search version.
Definition: Index.h:45