-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy.hpp
308 lines (246 loc) · 8.18 KB
/
policy.hpp
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#ifndef _CERTIFIEDCOSINE_POLICY
#define _CERTIFIEDCOSINE_POLICY
/**
* Policies control when the lookup procedure is done. They are "notified"
* anytime something new is expanded or anytime a new proof has been
* constructed.
*
* Policies implement a few basic methods which constrol how the lookup is performed
* typename float_t
* The float type that this policy supports (either float or double)
*
* bool expand(int id, float_t score)
* Everytime that a new vector is expanded, this will be called with the vectors id (index in the embedding
* matrix) and the score representing the cosine similarity with the query. Returning true will STOP the current search.
* Presummaly this should correspond with exceeding a budget. Note: score is in terms of cosine similarity. A value of
* 1 corresponds with a "distance" of 0, and -1 is the furthest possible point. (distance = 1 - score)
*
* float_t proof_distance() const
* The size of $\mathcal{N}_{\hat{v}}$ which controls how large the neighborhood around the query $q$ is that we
* are trying to construct a proof for. In the case of certifiying the top-1 nearest neighbor, then this should just
* return the $q^T \hat{v}$. For certifying the top-k nearest neighbors, then this should return $q^T v^{(k)}$. Note:
* this the distances is returned in terms of cosine similiary. So 1 corresponds with the _smallest_ possible
* $\mathcal{N}_{\hat{v}}$ and -1 is the largest possible area.
*/
#include <assert.h>
#include "constants.hpp"
#include "utils.hpp"
#include <unordered_set>
namespace certified_cosine {
template <typename float_T>
struct OneBestPolicy {
typedef float_T float_t;
int id = -1;
float_t distance = Consts<float_t>::infWorseScore;
inline bool expand(int id, float_t score) {
if (compareScore(score, distance)) {
distance = score;
this->id = id;
}
return false;
}
inline float_t proof_distance() const {
// the distance that this proving system is looking for atm
return distance;
}
bool got_proof() const {
// this just keeps looking until it found the best item,
// so when it terminates, that means that it must have constructed the proof
return true;
}
};
template <typename float_T>
struct NBestPolicy {
typedef float_T float_t;
struct located {
float_t score;
int id;
located() : id(-1) {}
located(int id, float_t score) : score(score), id(id) {}
float_t get_value() const { return score; }
};
int n;
IntervalHeap<located> items;
inline bool add_to_items(int id, float_t score) {
for (auto &a : items) {
if (a.id == id) return false; // then already added
}
assert(false);
items.insert(located(id, score));
return true;
}
inline bool expand(int id, float_t score) {
if (items.size() < n) {
add_to_items(id, score);
return false;
} else {
if (compareScore(score, items.min().score)) {
if (add_to_items(id, score)) items.remove_min();
}
return false;
}
}
inline float_t proof_distance() const {
assert(items.size() > 0);
if (items.size() < n) return Consts<float_t>::infWorseScore;
return items.min().score;
}
bool got_proof() const { return true; }
NBestPolicy(int n) : n(n) {}
};
template <typename float_T>
struct NBestSingleProof : NBestPolicy<float_T> {
typedef float_T float_t;
inline float_t proof_distance() const {
assert(this->items.size() > 0);
return this->items.max().score;
}
NBestSingleProof(int n) : NBestPolicy<float_t>(n) {}
};
template <typename parent>
struct CountExpandPolicy : parent {
typedef typename parent::float_t float_t;
int count = 0;
inline bool expand(int id, float_t score) {
count++;
return parent::expand(id, score);
}
};
template <typename float_T>
struct CountingTillBest {
typedef float_T float_t;
int count = 0;
int count_located = -1;
int id = -1;
float_t distance = Consts<float_t>::infWorseScore;
inline bool expand(int id, float_t score) {
count++;
if (compareScore(score, distance)) {
distance = score;
this->id = id;
count_located = count;
}
return false;
}
inline float_t proof_distance() const { return distance; }
bool got_proof() const { return true; }
};
template <typename float_T>
struct CountingNBestPolicy {
typedef float_T float_t;
struct located {
float_t score;
int id;
located() : id(-1) {}
located(int id, float_t score) : score(score), id(id) {}
float_t get_value() const { return score; }
};
int n;
IntervalHeap<located> items;
bool add_to_items(int id, float_t score) {
for (auto &a : items) {
if (a.id == id) return false; // then already added
}
items.insert(located(id, score));
return true;
}
int count = 0;
int count_located = -1;
inline bool expand(int id, float_t score) {
count++;
if (items.size() < n) {
if (add_to_items(id, score)) count_located = count;
return false;
} else {
if (compareScore(score, items.min().score)) {
if (add_to_items(id, score)) {
items.remove_min();
count_located = count;
}
}
return false;
}
}
inline float_t proof_distance() const {
assert(items.size() > 0);
if (items.size() < n) return Consts<float_t>::infWorseScore;
return items.min().score;
}
bool got_proof() const { return true; }
CountingNBestPolicy(int n) : n(n) {}
};
template <typename parent>
struct LimitExpand : parent {
typedef typename parent::float_t float_t;
int limit;
inline bool expand(int id, float_t score) { return parent::expand(id, score) || this->count > limit; }
bool got_proof() const { return this->count <= limit; }
LimitExpand(int a) : limit(a) {}
LimitExpand(int a, int b) : limit(a), parent(b) {}
};
template <typename parent>
struct ApproximatePolicy : parent {
// make the proof distance have a shrinking radius based off how long we have
// run, so that we don't spend too long. In this case we are not constructing
// the "true" proof, but rather for a smaller area
typedef typename parent::float_t float_t;
inline float_t proof_distance() const {
float_t p = parent::proof_distance();
return ((float_t)1.0) - (((float_t)1.0) - p) * (((float_t)1.0) - ((float_t)this->count) / (this->limit + 500));
}
ApproximatePolicy(int a) : parent(a) {}
ApproximatePolicy(int a, int b) : parent(a, b) {}
};
template <typename parent>
struct ProveBest : parent {
typedef typename parent::float_t float_t;
inline float_t proof_distance() const {
assert(this->items.size() > 0);
// return the max score instead of the min score, as we are only going to
// prove the 1 best in this case instead of all of the points
return this->items.max().score;
}
ProveBest() : parent() {}
ProveBest(int a) : parent(a) {}
ProveBest(int a, int b) : parent(a, b) {}
};
template <typename float_T>
struct WrappedPolicy {
typedef float_T float_t;
private:
struct WrappedBased {
virtual bool expand(int id, float_t score) = 0;
virtual float_t proof_distance() const = 0;
virtual bool got_proof() const = 0;
};
template <typename T>
struct WrappedT : WrappedBased {
T *t;
WrappedT(T *t) : t(t) {}
bool expand(int id, float_t score) override { return t->expand(id, score); }
float_t proof_distance() override { return t->proof_distance(); }
bool got_proof() override { return t->got_proof(); }
};
uint8_t wrapped[sizeof(WrappedT<WrappedBased>)];
public:
template <typename T>
WrappedPolicy(T *t) {
new (wrapped) WrappedT<T>(t);
}
bool expand(int id, float_t score) { return ((WrappedBased *)wrapped)->expand(id, score); }
float_t proof_distance() const { return ((WrappedBased *)wrapped)->proof_distance(); }
bool got_proof() const { return ((WrappedBased *)wrapped)->got_proof(); }
};
template <typename parent>
struct DebugPolicy : parent {
typedef typename parent::float_t float_t;
std::unordered_set<int> opened;
bool expand(int id, float_t score) {
// ensure that we do not repeat looking at the same vertex
assert(!opened.count(id));
opened.emplace(id);
return parent::expand(id, score);
}
};
} // namespace certified_cosine
#endif