-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstop_condition.h
More file actions
276 lines (233 loc) · 8.67 KB
/
stop_condition.h
File metadata and controls
276 lines (233 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#pragma once
#include "space_l2.h"
#include "space_ip.h"
#include <assert.h>
#include <unordered_map>
namespace hnswlib {
template<typename DOCIDTYPE>
class BaseMultiVectorSpace : public SpaceInterface<float> {
public:
virtual DOCIDTYPE get_doc_id(const void *datapoint) = 0;
virtual void set_doc_id(void *datapoint, DOCIDTYPE doc_id) = 0;
};
template<typename DOCIDTYPE>
class MultiVectorL2Space : public BaseMultiVectorSpace<DOCIDTYPE> {
DISTFUNC<float> fstdistfunc_;
size_t data_size_;
size_t vector_size_;
size_t dim_;
public:
MultiVectorL2Space(size_t dim) {
fstdistfunc_ = L2Sqr;
#if defined(USE_SSE) || defined(USE_AVX) || defined(USE_AVX512)
#if defined(USE_AVX512)
if (AVX512Capable())
L2SqrSIMD16Ext = L2SqrSIMD16ExtAVX512;
else if (AVXCapable())
L2SqrSIMD16Ext = L2SqrSIMD16ExtAVX;
#elif defined(USE_AVX)
if (AVXCapable())
L2SqrSIMD16Ext = L2SqrSIMD16ExtAVX;
#endif
if (dim % 16 == 0)
fstdistfunc_ = L2SqrSIMD16Ext;
else if (dim % 4 == 0)
fstdistfunc_ = L2SqrSIMD4Ext;
else if (dim > 16)
fstdistfunc_ = L2SqrSIMD16ExtResiduals;
else if (dim > 4)
fstdistfunc_ = L2SqrSIMD4ExtResiduals;
#endif
dim_ = dim;
vector_size_ = dim * sizeof(float);
data_size_ = vector_size_ + sizeof(DOCIDTYPE);
}
size_t get_data_size() override {
return data_size_;
}
DISTFUNC<float> get_dist_func() override {
return fstdistfunc_;
}
void *get_dist_func_param() override {
return &dim_;
}
DOCIDTYPE get_doc_id(const void *datapoint) override {
return *(DOCIDTYPE *)((char *)datapoint + vector_size_);
}
void set_doc_id(void *datapoint, DOCIDTYPE doc_id) override {
*(DOCIDTYPE*)((char *)datapoint + vector_size_) = doc_id;
}
~MultiVectorL2Space() {}
};
template<typename DOCIDTYPE>
class MultiVectorInnerProductSpace : public BaseMultiVectorSpace<DOCIDTYPE> {
DISTFUNC<float> fstdistfunc_;
size_t data_size_;
size_t vector_size_;
size_t dim_;
public:
MultiVectorInnerProductSpace(size_t dim) {
fstdistfunc_ = InnerProductDistance;
#if defined(USE_AVX) || defined(USE_SSE) || defined(USE_AVX512)
#if defined(USE_AVX512)
if (AVX512Capable()) {
InnerProductSIMD16Ext = InnerProductSIMD16ExtAVX512;
InnerProductDistanceSIMD16Ext = InnerProductDistanceSIMD16ExtAVX512;
} else if (AVXCapable()) {
InnerProductSIMD16Ext = InnerProductSIMD16ExtAVX;
InnerProductDistanceSIMD16Ext = InnerProductDistanceSIMD16ExtAVX;
}
#elif defined(USE_AVX)
if (AVXCapable()) {
InnerProductSIMD16Ext = InnerProductSIMD16ExtAVX;
InnerProductDistanceSIMD16Ext = InnerProductDistanceSIMD16ExtAVX;
}
#endif
#if defined(USE_AVX)
if (AVXCapable()) {
InnerProductSIMD4Ext = InnerProductSIMD4ExtAVX;
InnerProductDistanceSIMD4Ext = InnerProductDistanceSIMD4ExtAVX;
}
#endif
if (dim % 16 == 0)
fstdistfunc_ = InnerProductDistanceSIMD16Ext;
else if (dim % 4 == 0)
fstdistfunc_ = InnerProductDistanceSIMD4Ext;
else if (dim > 16)
fstdistfunc_ = InnerProductDistanceSIMD16ExtResiduals;
else if (dim > 4)
fstdistfunc_ = InnerProductDistanceSIMD4ExtResiduals;
#endif
vector_size_ = dim * sizeof(float);
data_size_ = vector_size_ + sizeof(DOCIDTYPE);
}
size_t get_data_size() override {
return data_size_;
}
DISTFUNC<float> get_dist_func() override {
return fstdistfunc_;
}
void *get_dist_func_param() override {
return &dim_;
}
DOCIDTYPE get_doc_id(const void *datapoint) override {
return *(DOCIDTYPE *)((char *)datapoint + vector_size_);
}
void set_doc_id(void *datapoint, DOCIDTYPE doc_id) override {
*(DOCIDTYPE*)((char *)datapoint + vector_size_) = doc_id;
}
~MultiVectorInnerProductSpace() {}
};
template<typename DOCIDTYPE, typename dist_t>
class MultiVectorSearchStopCondition : public BaseSearchStopCondition<dist_t> {
size_t curr_num_docs_;
size_t num_docs_to_search_;
size_t ef_collection_;
std::unordered_map<DOCIDTYPE, size_t> doc_counter_;
std::priority_queue<std::pair<dist_t, DOCIDTYPE>> search_results_;
BaseMultiVectorSpace<DOCIDTYPE>& space_;
public:
MultiVectorSearchStopCondition(
BaseMultiVectorSpace<DOCIDTYPE>& space,
size_t num_docs_to_search,
size_t ef_collection = 10)
: space_(space) {
curr_num_docs_ = 0;
num_docs_to_search_ = num_docs_to_search;
ef_collection_ = std::max(ef_collection, num_docs_to_search);
}
void add_point_to_result(labeltype label, const void *datapoint, dist_t dist) override {
DOCIDTYPE doc_id = space_.get_doc_id(datapoint);
if (doc_counter_[doc_id] == 0) {
curr_num_docs_ += 1;
}
search_results_.emplace(dist, doc_id);
doc_counter_[doc_id] += 1;
}
void remove_point_from_result(labeltype label, const void *datapoint, dist_t dist) override {
DOCIDTYPE doc_id = space_.get_doc_id(datapoint);
doc_counter_[doc_id] -= 1;
if (doc_counter_[doc_id] == 0) {
curr_num_docs_ -= 1;
}
search_results_.pop();
}
bool should_stop_search(dist_t candidate_dist, dist_t lowerBound) override {
bool stop_search = candidate_dist > lowerBound && curr_num_docs_ == ef_collection_;
return stop_search;
}
bool should_consider_candidate(dist_t candidate_dist, dist_t lowerBound) override {
bool flag_consider_candidate = curr_num_docs_ < ef_collection_ || lowerBound > candidate_dist;
return flag_consider_candidate;
}
bool should_remove_extra() override {
bool flag_remove_extra = curr_num_docs_ > ef_collection_;
return flag_remove_extra;
}
void filter_results(std::vector<std::pair<dist_t, labeltype >> &candidates) override {
while (curr_num_docs_ > num_docs_to_search_) {
dist_t dist_cand = candidates.back().first;
dist_t dist_res = search_results_.top().first;
assert(dist_cand == dist_res);
DOCIDTYPE doc_id = search_results_.top().second;
doc_counter_[doc_id] -= 1;
if (doc_counter_[doc_id] == 0) {
curr_num_docs_ -= 1;
}
search_results_.pop();
candidates.pop_back();
}
}
~MultiVectorSearchStopCondition() {}
};
template<typename dist_t>
class EpsilonSearchStopCondition : public BaseSearchStopCondition<dist_t> {
float epsilon_;
size_t min_num_candidates_;
size_t max_num_candidates_;
size_t curr_num_items_;
public:
EpsilonSearchStopCondition(float epsilon, size_t min_num_candidates, size_t max_num_candidates) {
assert(min_num_candidates <= max_num_candidates);
epsilon_ = epsilon;
min_num_candidates_ = min_num_candidates;
max_num_candidates_ = max_num_candidates;
curr_num_items_ = 0;
}
void add_point_to_result(labeltype label, const void *datapoint, dist_t dist) override {
curr_num_items_ += 1;
}
void remove_point_from_result(labeltype label, const void *datapoint, dist_t dist) override {
curr_num_items_ -= 1;
}
bool should_stop_search(dist_t candidate_dist, dist_t lowerBound) override {
if (candidate_dist > lowerBound && curr_num_items_ == max_num_candidates_) {
// new candidate can't improve found results
return true;
}
if (candidate_dist > epsilon_ && curr_num_items_ >= min_num_candidates_) {
// new candidate is out of epsilon region and
// minimum number of candidates is checked
return true;
}
return false;
}
bool should_consider_candidate(dist_t candidate_dist, dist_t lowerBound) override {
bool flag_consider_candidate = curr_num_items_ < max_num_candidates_ || lowerBound > candidate_dist;
return flag_consider_candidate;
}
bool should_remove_extra() {
bool flag_remove_extra = curr_num_items_ > max_num_candidates_;
return flag_remove_extra;
}
void filter_results(std::vector<std::pair<dist_t, labeltype >> &candidates) override {
while (!candidates.empty() && candidates.back().first > epsilon_) {
candidates.pop_back();
}
while (candidates.size() > max_num_candidates_) {
candidates.pop_back();
}
}
~EpsilonSearchStopCondition() {}
};
} // namespace hnswlib