-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhnsw_wrapper.cc
370 lines (316 loc) · 11.5 KB
/
hnsw_wrapper.cc
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// hnsw_wrapper.cpp
#include <iostream>
#include "hnswlib/hnswlib.h"
#include "hnsw_wrapper.h"
#include <thread>
#include <atomic>
#include <vector>
static std::vector<std::vector<float>> convertTo2DVector(const float* flat_vectors, int rows, int cols);
/*
* replacement for the openmp '#pragma omp parallel for' directive
* only handles a subset of functionality (no reductions etc)
* Process ids from start (inclusive) to end (EXCLUSIVE)
*
* The method is borrowed from nmslib
*/
template <class Function>
inline void ParallelFor(size_t start, size_t end, size_t numThreads, Function fn)
{
if (numThreads <= 0)
{
numThreads = std::thread::hardware_concurrency();
}
if (numThreads == 1)
{
for (size_t id = start; id < end; id++)
{
fn(id, 0);
}
}
else
{
std::vector<std::thread> threads;
std::atomic<size_t> current(start);
// keep track of exceptions in threads
// https://stackoverflow.com/a/32428427/1713196
std::exception_ptr lastException = nullptr;
std::mutex lastExceptMutex;
for (size_t threadId = 0; threadId < numThreads; ++threadId)
{
threads.push_back(std::thread([&, threadId]
{
while (true) {
size_t id = current.fetch_add(1);
if (id >= end) {
break;
}
try {
fn(id, threadId);
} catch (...) {
std::unique_lock<std::mutex> lastExcepLock(lastExceptMutex);
lastException = std::current_exception();
/*
* This will work even when current is the largest value that
* size_t can fit, because fetch_add returns the previous value
* before the increment (what will result in overflow
* and produce 0 instead of current + 1).
*/
current = end;
break;
}
} }));
}
for (auto &thread : threads)
{
thread.join();
}
if (lastException)
{
std::rethrow_exception(lastException);
}
}
}
class CustomFilterFunctor : public hnswlib::BaseFilterFunctor
{
std::function<bool(hnswlib::labeltype)> filter;
public:
explicit CustomFilterFunctor(const std::function<bool(hnswlib::labeltype)> &f)
{
filter = f;
}
bool operator()(hnswlib::labeltype id)
{
return filter(id);
}
};
HnswIndex *newIndex(spaceType space_type, const int dim, size_t max_elements, int M, int ef_construction, int rand_seed, int allow_replace_deleted)
{
HnswIndex *index = new HnswIndex;
bool normalize = false;
hnswlib::SpaceInterface<float> *space;
if (space_type == l2)
{
space = new hnswlib::L2Space(dim);
}
else if (space_type == ip)
{
space = new hnswlib::InnerProductSpace(dim);
}
else if (space_type == cosine)
{
space = new hnswlib::InnerProductSpace(dim);
normalize = true;
}
else
{
throw std::runtime_error("Space name must be one of l2, ip, or cosine.");
}
hnswlib::HierarchicalNSW<float> *appr_alg = new hnswlib::HierarchicalNSW<float>(space, max_elements, M, ef_construction, rand_seed, static_cast<bool>(allow_replace_deleted));
index->hnsw = (void *)appr_alg;
index->dim = dim;
index->normalize = normalize;
index->space = (void *)space;
index->space_type = space_type;
return index;
}
// set efConstruction value.
void setEf(HnswIndex *index, size_t ef)
{
((hnswlib::HierarchicalNSW<float> *)(index->hnsw))->ef_ = ef;
}
// Returns index file size in size_t.
size_t indexFileSize(HnswIndex *index)
{
return ((hnswlib::HierarchicalNSW<float> *)(index->hnsw))->indexFileSize();
}
// Save index to a file.
void saveIndex(HnswIndex *index, char *location)
{
((hnswlib::HierarchicalNSW<float> *)(index->hnsw))->saveIndex(location);
}
HnswIndex *loadIndex(char *location, spaceType space_type, int dim, size_t max_elements, int allow_replace_deleted)
{
HnswIndex *index = new HnswIndex;
bool normalize = false;
hnswlib::SpaceInterface<float> *space;
if (space_type == l2)
{
space = new hnswlib::L2Space(dim);
}
else if (space_type == ip)
{
space = new hnswlib::InnerProductSpace(dim);
}
else if (space_type == cosine)
{
space = new hnswlib::InnerProductSpace(dim);
normalize = true;
}
else
{
throw std::runtime_error("Space name must be one of l2, ip, or cosine.");
}
hnswlib::HierarchicalNSW<float> *appr_alg = new hnswlib::HierarchicalNSW<float>(space, location, false, max_elements, static_cast<bool>(allow_replace_deleted));
index->hnsw = (void *)appr_alg;
index->dim = dim;
index->normalize = normalize;
index->space = (void *)space;
index->space_type = space_type;
return index;
}
void normalize_vector(int dim, float *data, float *norm_array)
{
float norm = 0.0f;
for (int i = 0; i < dim; i++)
norm += data[i] * data[i];
norm = 1.0f / (sqrtf(norm) + 1e-30f);
for (int i = 0; i < dim; i++)
norm_array[i] = data[i] * norm;
}
int addPoints(HnswIndex *index, const float *flat_vectors, int rows, size_t *labels, int num_threads, int replace_deleted)
{
// avoid using threads when the number of additions is small:
if (rows <= num_threads * 4)
{
num_threads = 1;
}
std::vector<std::vector<float>> vectors = convertTo2DVector(flat_vectors, rows, index->dim);
try {
if (index->normalize == false) {
ParallelFor(0, rows, num_threads, [&](size_t row, size_t threadId) {
size_t id = *(labels + row);
((hnswlib::HierarchicalNSW<float> *)(index->hnsw))->addPoint(vectors[row].data(), id, static_cast<bool>(replace_deleted));
});
return 0;
}
std::vector<float> norm_array(num_threads * (index->dim));
ParallelFor(0, rows, num_threads, [&](size_t row, size_t threadId){
// normalize vector:
size_t start_idx = threadId * (index->dim);
normalize_vector((index->dim), vectors[row].data(), (norm_array.data() + start_idx));
size_t id = *(labels + row);
((hnswlib::HierarchicalNSW<float> *)(index->hnsw))->addPoint((void*)(norm_array.data() + start_idx), id, static_cast<bool>(replace_deleted));
});
} catch (const std::exception& e) {
std::cerr << "[hnsw] Exception caught: " << e.what() << std::endl;
return 1; // Error code for C
}
return 0;
}
void markDeleted(HnswIndex *index, size_t label)
{
((hnswlib::HierarchicalNSW<float> *)(index->hnsw))->markDelete(label);
}
void unmarkDeleted(HnswIndex *index, size_t label)
{
((hnswlib::HierarchicalNSW<float> *)(index->hnsw))->unmarkDelete(label);
}
void resizeIndex(HnswIndex *index, size_t new_size)
{
((hnswlib::HierarchicalNSW<float> *)(index->hnsw))->resizeIndex(new_size);
}
size_t getMaxElements(HnswIndex *index)
{
return ((hnswlib::HierarchicalNSW<float> *)(index->hnsw))->max_elements_;
}
size_t getCurrentCount(HnswIndex *index)
{
return ((hnswlib::HierarchicalNSW<float> *)(index->hnsw))->cur_element_count;
}
SearchResult *searchKnn(HnswIndex *index, const float *flat_vectors, int rows, int k, int num_threads)
{
//CustomFilterFunctor idFilter(filter);
//CustomFilterFunctor *p_idFilter = filter ? &idFilter : nullptr;
// avoid using threads when the number of searches is small:
if (rows <= num_threads * 4)
{
num_threads = 1;
}
std::vector<std::vector<float>> vectors = convertTo2DVector(flat_vectors, rows, index->dim);
SearchResult *searchResult = new SearchResult;
if (!searchResult) {
return nullptr; // Allocation failure
}
searchResult->label = new hnswlib::labeltype[rows * k];
searchResult->dist = new float[rows * k];
if (!searchResult->label || !searchResult->dist) {
delete[] searchResult->label;
delete[] searchResult->dist;
delete searchResult;
return nullptr; // Allocation failure
}
if (index->normalize == false) {
ParallelFor(0, rows, num_threads, [&](size_t row, size_t threadId) {
std::priority_queue<std::pair<float, hnswlib::labeltype>> result =
((hnswlib::HierarchicalNSW<float> *)index->hnsw)->searchKnn(vectors[row].data(), k, nullptr);
if (result.size() != k)
throw std::runtime_error("Cannot return the results in a contiguous 2D array. Probably ef or M is too small");
for (int i = k - 1; i >= 0; i--) {
auto& result_tuple = result.top();
*(searchResult->dist + row * k + i) = result_tuple.first;
*(searchResult->label + row * k + i) = result_tuple.second;
result.pop();
}
});
} else {
std::vector<float> norm_array(num_threads * (index->dim));
ParallelFor(0, rows, num_threads, [&](size_t row, size_t threadId) {
size_t start_idx = threadId * (index->dim);
normalize_vector((index->dim), vectors[row].data(), (norm_array.data() + start_idx));
std::priority_queue<std::pair<float, hnswlib::labeltype>> result =
((hnswlib::HierarchicalNSW<float> *)index->hnsw)->searchKnn((void*)(norm_array.data() + start_idx), k, nullptr);
if (result.size() != k)
throw std::runtime_error("Cannot return the results in a contiguous 2D array. Probably ef or M is too small");
for (int i = k - 1; i >= 0; i--) {
auto& result_tuple = result.top();
*(searchResult->dist + row * k + i) = result_tuple.first;
*(searchResult->label + row * k + i) = result_tuple.second;
result.pop();
}
});
}
return searchResult;
}
int getAllowReplaceDeleted(HnswIndex *index) {
return ((hnswlib::HierarchicalNSW<float> *)index->hnsw)->allow_replace_deleted_;
}
void getDataByLabel(HnswIndex *index, const size_t label, float* data) {
auto vec = ((hnswlib::HierarchicalNSW<float> *)index->hnsw)->getDataByLabel<float>(label);
data = vec.data();
}
void freeHNSW(HnswIndex *index)
{
hnswlib::HierarchicalNSW<float> *ptr = (hnswlib::HierarchicalNSW<float> *)index->hnsw;
delete ptr;
if (index->space_type == l2)
{
hnswlib::L2Space *space = (hnswlib::L2Space *)(index->space);
delete space;
}
else if (index->space_type == ip || index->space_type == cosine)
{
hnswlib::InnerProductSpace *space = (hnswlib::InnerProductSpace *)(index->space);
delete space;
}
else
{
throw std::runtime_error("Space name must be one of l2, ip, or cosine.");
}
delete index;
index = nullptr;
}
void freeResult(SearchResult *result)
{
delete[] result->label;
delete[] result->dist;
delete result;
}
static std::vector<std::vector<float>> convertTo2DVector(const float* flat_vectors, int rows, int cols) {
std::vector<std::vector<float>> vectors(rows, std::vector<float>(cols));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
vectors[i][j] = flat_vectors[i * cols + j];
}
}
return vectors;
}