Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/matthijs/github_faiss/faiss/MetaIndexes.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 // Copyright 2004-present Facebook. All Rights Reserved.
10 // -*- c++ -*-
11
12 #ifndef META_INDEXES_H
13 #define META_INDEXES_H
14
15
16 #include <vector>
17 #include <unordered_map>
18
19
20 #include "Index.h"
21
22
23 namespace faiss {
24
25 /** Index that translates search results to ids */
26 struct IndexIDMap : Index {
27 Index * index; ///! the sub-index
28 bool own_fields; ///! whether pointers are deleted in destructo
29 std::vector<long> id_map;
30
31 explicit IndexIDMap (Index *index);
32
33 /// Same as add_core, but stores xids instead of sequential ids
34 /// @param xids if non-null, ids to store for the vectors (size n)
35 void add_with_ids(idx_t n, const float* x, const long* xids) override;
36
37 /// this will fail. Use add_with_ids
38 void add(idx_t n, const float* x) override;
39
40 void search(
41 idx_t n,
42 const float* x,
43 idx_t k,
44 float* distances,
45 idx_t* labels) const override;
46
47 void train(idx_t n, const float* x) override;
48
49 void reset() override;
50
51 /// remove ids adapted to IndexFlat
52 long remove_ids(const IDSelector& sel) override;
53
54 ~IndexIDMap() override;
55 IndexIDMap () {own_fields=false; index=nullptr; }
56 };
57
58 /** same as IndexIDMap but also provides an efficient reconstruction
59 implementation via a 2-way index */
60 struct IndexIDMap2 : IndexIDMap {
61
62 std::unordered_map<idx_t, idx_t> rev_map;
63
64 explicit IndexIDMap2 (Index *index);
65
66 /// make the rev_map from scratch
67 void construct_rev_map ();
68
69 void add_with_ids(idx_t n, const float* x, const long* xids) override;
70
71 long remove_ids(const IDSelector& sel) override;
72
73 void reconstruct (idx_t key, float * recons) const override;
74
75 ~IndexIDMap2() override {}
76 IndexIDMap2 () {}
77 };
78
79
80 /** Index that concatenates the results from several sub-indexes
81 *
82 */
83 struct IndexShards : Index {
84
85 std::vector<Index*> shard_indexes;
86 bool own_fields; /// should the sub-indexes be deleted along with this?
87 bool threaded;
88 bool successive_ids;
89
90 /**
91 * @param threaded do we use one thread per sub_index or do
92 * queries sequentially?
93 * @param successive_ids should we shift the returned ids by
94 * the size of each sub-index or return them
95 * as they are?
96 */
97 explicit IndexShards (idx_t d, bool threaded = false,
98 bool successive_ids = true);
99
100 void add_shard (Index *);
101
102 // update metric_type and ntotal. Call if you changes something in
103 // the shard indexes.
104 void sync_with_shard_indexes ();
105
106 Index *at(int i) {return shard_indexes[i]; }
107
108 /// supported only for sub-indices that implement add_with_ids
109 void add(idx_t n, const float* x) override;
110
111 void add_with_ids(idx_t n, const float* x, const long* xids) override;
112
113 void search(
114 idx_t n,
115 const float* x,
116 idx_t k,
117 float* distances,
118 idx_t* labels) const override;
119
120 void train(idx_t n, const float* x) override;
121
122 void reset() override;
123
124 ~IndexShards() override;
125 };
126
127 /** splits input vectors in segments and assigns each segment to a sub-index
128 * used to distribute a MultiIndexQuantizer
129 */
130
131 struct IndexSplitVectors: Index {
132 bool own_fields;
133 bool threaded;
134 std::vector<Index*> sub_indexes;
135 idx_t sum_d; /// sum of dimensions seen so far
136
137 explicit IndexSplitVectors (idx_t d, bool threaded = false);
138
139 void add_sub_index (Index *);
140 void sync_with_sub_indexes ();
141
142 void add(idx_t n, const float* x) override;
143
144 void search(
145 idx_t n,
146 const float* x,
147 idx_t k,
148 float* distances,
149 idx_t* labels) const override;
150
151 void train(idx_t n, const float* x) override;
152
153 void reset() override;
154
155 ~IndexSplitVectors() override;
156 };
157
158
159
160 }
161
162
163 #endif
faiss::IndexShards::train
void train(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:385
faiss::IndexIDMap2
Definition: MetaIndexes.h:60
faiss::IndexShards::IndexShards
IndexShards(idx_t d, bool threaded=false, bool successive_ids=true)
Definition: MetaIndexes.cpp:355
faiss::IndexIDMap::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: MetaIndexes.cpp:69
faiss::IndexShards::add_with_ids
void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: MetaIndexes.cpp:425
faiss::IDSelector
Definition: AuxIndexStructures.h:52
faiss::IndexShards::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: MetaIndexes.cpp:486
faiss::IndexIDMap2::add_with_ids
void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: MetaIndexes.cpp:130
faiss::IndexIDMap
Definition: MetaIndexes.h:26
faiss::IndexSplitVectors::reset
void reset() override
removes all elements from the database.
Definition: MetaIndexes.cpp:715
faiss::IndexIDMap::add
void add(idx_t n, const float *x) override
this will fail. Use add_with_ids
Definition: MetaIndexes.cpp:40
faiss::IndexSplitVectors
Definition: MetaIndexes.h:131
faiss::Index::d
int d
vector dimension
Definition: Index.h:64
faiss::IndexIDMap::id_map
std::vector< long > id_map
! whether pointers are deleted in destructo
Definition: MetaIndexes.h:29
faiss::IndexSplitVectors::train
void train(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:711
faiss::IndexShards::add
void add(idx_t n, const float *x) override
supported only for sub-indices that implement add_with_ids
Definition: MetaIndexes.cpp:408
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:62
faiss::IndexIDMap2::construct_rev_map
void construct_rev_map()
make the rev_map from scratch
Definition: MetaIndexes.cpp:139
faiss::IndexSplitVectors::add
void add(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:610
faiss::IndexIDMap2::remove_ids
long remove_ids(const IDSelector &sel) override
remove ids adapted to IndexFlat
Definition: MetaIndexes.cpp:148
faiss::IndexShards::threaded
bool threaded
should the sub-indexes be deleted along with this?
Definition: MetaIndexes.h:87
faiss::IndexShards::reset
void reset() override
removes all elements from the database.
Definition: MetaIndexes.cpp:478
faiss::IndexIDMap::reset
void reset() override
removes all elements from the database.
Definition: MetaIndexes.cpp:53
faiss::IndexSplitVectors::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: MetaIndexes.cpp:654
faiss::IndexIDMap::remove_ids
long remove_ids(const IDSelector &sel) override
remove ids adapted to IndexFlat
Definition: MetaIndexes.cpp:95
faiss::IndexIDMap2::reconstruct
void reconstruct(idx_t key, float *recons) const override
Definition: MetaIndexes.cpp:156
faiss::IndexIDMap::train
void train(idx_t n, const float *x) override
Definition: MetaIndexes.cpp:47
faiss::Index
Definition: Index.h:60
faiss::IndexShards
Definition: MetaIndexes.h:83
faiss::IndexIDMap::add_with_ids
void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: MetaIndexes.cpp:60
faiss::IndexSplitVectors::IndexSplitVectors
IndexSplitVectors(idx_t d, bool threaded=false)
sum of dimensions seen so far
Definition: MetaIndexes.cpp:580
faiss::IndexIDMap::own_fields
bool own_fields
! the sub-index
Definition: MetaIndexes.h:28