-
Notifications
You must be signed in to change notification settings - Fork 128
/
nmslib_wrapper.cpp
386 lines (309 loc) · 14.6 KB
/
nmslib_wrapper.cpp
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
#include "jni_util.h"
#include "nmslib_wrapper.h"
#include "nmslib_stream_support.h"
#include "commons.h"
#include "init.h"
#include "index.h"
#include "params.h"
#include "knnquery.h"
#include "knnqueue.h"
#include "methodfactory.h"
#include "spacefactory.h"
#include "space.h"
#include <jni.h>
#include <string>
#include "hnswquery.h"
#include "method/hnsw.h"
std::string TranslateSpaceType(const std::string &spaceType);
// We do not use label functionality of nmslib so we pass default label. Setting as a const allows us to avoid a few
// allocations
const similarity::LabelType DEFAULT_LABEL = -1;
void knn_jni::nmslib_wrapper::CreateIndex(knn_jni::JNIUtilInterface *jniUtil, JNIEnv *env, jintArray idsJ,
jlong vectorsAddressJ, jint dimJ,
jobject output, jobject parametersJ) {
if (idsJ == nullptr) {
throw std::runtime_error("IDs cannot be null");
}
if (vectorsAddressJ <= 0) {
throw std::runtime_error("VectorsAddress cannot be less than 0");
}
if (dimJ <= 0) {
throw std::runtime_error("Vectors dimensions cannot be less than or equal to 0");
}
if (output == nullptr) {
throw std::runtime_error("Index output stream cannot be null");
}
if (parametersJ == nullptr) {
throw std::runtime_error("Parameters cannot be null");
}
// Handle parameters
auto parametersCpp = jniUtil->ConvertJavaMapToCppMap(env, parametersJ);
std::vector<std::string> indexParameters;
// Algorithm parameters will be in a sub map
if (parametersCpp.find(knn_jni::PARAMETERS) != parametersCpp.end()) {
jobject subParametersJ = parametersCpp[knn_jni::PARAMETERS];
auto subParametersCpp = jniUtil->ConvertJavaMapToCppMap(env, subParametersJ);
if (subParametersCpp.find(knn_jni::EF_CONSTRUCTION) != subParametersCpp.end()) {
auto efConstruction = jniUtil->ConvertJavaObjectToCppInteger(env, subParametersCpp[knn_jni::EF_CONSTRUCTION]);
indexParameters.push_back(knn_jni::EF_CONSTRUCTION_NMSLIB + "=" + std::to_string(efConstruction));
}
if (subParametersCpp.find(knn_jni::M) != subParametersCpp.end()) {
auto m = jniUtil->ConvertJavaObjectToCppInteger(env, subParametersCpp[knn_jni::M]);
indexParameters.push_back(knn_jni::M_NMSLIB + "=" + std::to_string(m));
}
jniUtil->DeleteLocalRef(env, subParametersJ);
}
if (parametersCpp.find(knn_jni::INDEX_THREAD_QUANTITY) != parametersCpp.end()) {
auto indexThreadQty = jniUtil->ConvertJavaObjectToCppInteger(env, parametersCpp[knn_jni::INDEX_THREAD_QUANTITY]);
indexParameters.push_back(knn_jni::INDEX_THREAD_QUANTITY + "=" + std::to_string(indexThreadQty));
}
jniUtil->DeleteLocalRef(env, parametersJ);
// Get space type for this index
jobject spaceTypeJ = knn_jni::GetJObjectFromMapOrThrow(parametersCpp, knn_jni::SPACE_TYPE);
std::string spaceTypeCpp(jniUtil->ConvertJavaObjectToCppString(env, spaceTypeJ));
spaceTypeCpp = TranslateSpaceType(spaceTypeCpp);
std::unique_ptr<similarity::Space<float>> space;
space.reset(similarity::SpaceFactoryRegistry<float>::Instance().CreateSpace(spaceTypeCpp, similarity::AnyParams()));
// Get number of ids and vectors and dimension
auto *inputVectors = reinterpret_cast<std::vector<float> *>(vectorsAddressJ);
int dim = (int) dimJ;
// The number of vectors can be int here because a lucene segment number of total docs never crosses INT_MAX value
int numVectors = (int) (inputVectors->size() / (uint64_t) dim);
if (numVectors == 0) {
throw std::runtime_error("Number of vectors cannot be 0");
}
int numIds = jniUtil->GetJavaIntArrayLength(env, idsJ);
if (numIds != numVectors) {
throw std::runtime_error("Number of IDs does not match number of vectors");
}
// Read dataset
similarity::ObjectVector dataset;
dataset.reserve(numVectors);
int *idsCpp;
try {
// Read in data set
idsCpp = jniUtil->GetIntArrayElements(env, idsJ, nullptr);
size_t vectorSizeInBytes = dim * sizeof(float);
// vectorPointer needs to be unsigned long long, this will ensure that out of range doesn't happen for this pointer
// when the values of numVectors * dim becomes very large.
// Example: for 10M vectors of 1536 dim vectorPointer max value will be ~15.3B which is already > range of ints.
// keeping it unsigned long long we will never go above the range.
unsigned long long vectorPointer = 0;
// Allocate a large buffer that will contain all the vectors. Allocating the objects in one large buffer as
// opposed to individually will prevent heap fragmentation. We have observed that allocating individual
// objects causes RSS to rise throughout the lifetime of a process
// (see https://github.com/opensearch-project/k-NN/issues/772 and
// https://github.com/opensearch-project/k-NN/issues/72). This is because, in typical systems, small
// allocations will reside on some kind of heap managed by an allocator. Once freed, the allocator does not
// always return the memory to the OS. If the heap gets fragmented, this will cause the allocator
// to ask for more memory, causing RSS to grow. On large allocations (> 128 kb), most allocators will
// internally use mmap. Once freed, unmap will be called, which will immediately return memory to the OS
// which in turn prevents RSS from growing out of control. Wrap with a smart pointer so that buffer will be
// freed once variable goes out of scope. For reference, the code that specifies the layout of the buffer can be
// found: https://github.com/nmslib/nmslib/blob/v2.1.1/similarity_search/include/object.h#L61-L75
std::unique_ptr<char[]> objectBuffer
(new char[(similarity::ID_SIZE + similarity::LABEL_SIZE + similarity::DATALENGTH_SIZE + vectorSizeInBytes)
* numVectors]);
char *ptr = objectBuffer.get();
for (int i = 0; i < numVectors; i++) {
dataset.push_back(new similarity::Object(ptr));
memcpy(ptr, &idsCpp[i], similarity::ID_SIZE);
ptr += similarity::ID_SIZE;
memcpy(ptr, &DEFAULT_LABEL, similarity::LABEL_SIZE);
ptr += similarity::LABEL_SIZE;
memcpy(ptr, &vectorSizeInBytes, similarity::DATALENGTH_SIZE);
ptr += similarity::DATALENGTH_SIZE;
memcpy(ptr, &(inputVectors->at(vectorPointer)), vectorSizeInBytes);
ptr += vectorSizeInBytes;
vectorPointer += dim;
}
JNIReleaseElements release_int_array_elements {[=](){
jniUtil->ReleaseIntArrayElements(env, idsJ, idsCpp, JNI_ABORT);
}};
// Releasing the vectorsAddressJ memory as that is not required once we have created the index.
// This is not the ideal approach, please refer this gh issue for long term solution:
// https://github.com/opensearch-project/k-NN/issues/1600
//commons::freeVectorData(vectorsAddressJ);
delete inputVectors;
std::unique_ptr<similarity::Index<float>> index;
index.reset(similarity::MethodFactoryRegistry<float>::Instance().CreateMethod(false,
"hnsw",
spaceTypeCpp,
*(space),
dataset));
index->CreateIndex(similarity::AnyParams(indexParameters));
knn_jni::stream::NativeEngineIndexOutputMediator mediator {jniUtil, env, output};
knn_jni::stream::NmslibOpenSearchIOWriter writer {&mediator};
if (auto hnswFloatIndex = dynamic_cast<similarity::Hnsw<float> *>(index.get())) {
hnswFloatIndex->SaveIndexWithStream(writer);
} else {
throw std::runtime_error("We only support similarity::Hnsw<float> in NMSLIB.");
}
for (auto it : dataset) {
delete it;
}
} catch (...) {
for (auto it : dataset) {
delete it;
}
throw;
}
}
jlong knn_jni::nmslib_wrapper::LoadIndex(knn_jni::JNIUtilInterface *jniUtil, JNIEnv *env, jstring indexPathJ,
jobject parametersJ) {
if (indexPathJ == nullptr) {
throw std::runtime_error("Index path cannot be null");
}
if (parametersJ == nullptr) {
throw std::runtime_error("Parameters cannot be null");
}
std::string indexPathCpp(jniUtil->ConvertJavaStringToCppString(env, indexPathJ));
auto parametersCpp = jniUtil->ConvertJavaMapToCppMap(env, parametersJ);
// Get space type for this index
jobject spaceTypeJ = knn_jni::GetJObjectFromMapOrThrow(parametersCpp, knn_jni::SPACE_TYPE);
std::string spaceTypeCpp(jniUtil->ConvertJavaObjectToCppString(env, spaceTypeJ));
spaceTypeCpp = TranslateSpaceType(spaceTypeCpp);
// Parse query params
std::vector<std::string> queryParams;
auto it = parametersCpp.find("efSearch");
if (it != parametersCpp.end()) {
auto efSearch = std::to_string(jniUtil->ConvertJavaObjectToCppInteger(env, it->second));
queryParams.push_back("efSearch=" + efSearch);
}
// Load index
knn_jni::nmslib_wrapper::IndexWrapper *indexWrapper = nullptr;
try {
indexWrapper = new knn_jni::nmslib_wrapper::IndexWrapper(spaceTypeCpp);
indexWrapper->index->LoadIndex(indexPathCpp);
indexWrapper->index->SetQueryTimeParams(similarity::AnyParams(queryParams));
} catch (...) {
delete indexWrapper;
throw;
}
return (jlong) indexWrapper;
}
jlong knn_jni::nmslib_wrapper::LoadIndexWithStream(knn_jni::JNIUtilInterface *jniUtil,
JNIEnv *env,
jobject readStream,
jobject parametersJ) {
if (readStream == nullptr) {
throw std::runtime_error("Read stream cannot be null");
}
if (parametersJ == nullptr) {
throw std::runtime_error("Parameters cannot be null");
}
auto parametersCpp = jniUtil->ConvertJavaMapToCppMap(env, parametersJ);
// Get space type for this index
jobject spaceTypeJ = knn_jni::GetJObjectFromMapOrThrow(parametersCpp, knn_jni::SPACE_TYPE);
std::string spaceTypeCpp(jniUtil->ConvertJavaObjectToCppString(env, spaceTypeJ));
spaceTypeCpp = TranslateSpaceType(spaceTypeCpp);
// Parse query params
std::vector<std::string> queryParams;
auto it = parametersCpp.find("efSearch");
if (it != parametersCpp.end()) {
auto efSearch = std::to_string(jniUtil->ConvertJavaObjectToCppInteger(env, it->second));
queryParams.push_back("efSearch=" + efSearch);
}
// Create a mediator locally.
// Note that `indexInput` is `IndexInputWithBuffer` type.
knn_jni::stream::NativeEngineIndexInputMediator mediator{jniUtil, env, readStream};
knn_jni::stream::NmslibOpenSearchIOReader ioReader {&mediator};
// Load index
knn_jni::nmslib_wrapper::IndexWrapper *indexWrapper = nullptr;
try {
indexWrapper = new knn_jni::nmslib_wrapper::IndexWrapper(spaceTypeCpp);
indexWrapper->index->SetQueryTimeParams(similarity::AnyParams(queryParams));
if (auto hnswFloatIndex = dynamic_cast<similarity::Hnsw<float> *>(indexWrapper->index.get())) {
hnswFloatIndex->LoadIndexWithStream(ioReader);
} else {
throw std::runtime_error("We only support similarity::Hnsw<float> in NMSLIB.");
}
} catch (...) {
delete indexWrapper;
throw;
}
return (jlong) indexWrapper;
}
jobjectArray knn_jni::nmslib_wrapper::QueryIndex(knn_jni::JNIUtilInterface *jniUtil, JNIEnv *env, jlong indexPointerJ,
jfloatArray queryVectorJ, jint kJ, jobject methodParamsJ) {
if (queryVectorJ == nullptr) {
throw std::runtime_error("Query Vector cannot be null");
}
if (indexPointerJ == 0) {
throw std::runtime_error("Invalid pointer to index");
}
auto *indexWrapper = reinterpret_cast<knn_jni::nmslib_wrapper::IndexWrapper *>(indexPointerJ);
int dim = jniUtil->GetJavaFloatArrayLength(env, queryVectorJ);
float *rawQueryvector = jniUtil->GetFloatArrayElements(env, queryVectorJ, nullptr); // Have to call release on this
std::unique_ptr<const similarity::Object> queryObject;
try {
queryObject.reset(new similarity::Object(-1, -1, dim * sizeof(float), rawQueryvector));
} catch (...) {
jniUtil->ReleaseFloatArrayElements(env, queryVectorJ, rawQueryvector, JNI_ABORT);
throw;
}
jniUtil->ReleaseFloatArrayElements(env, queryVectorJ, rawQueryvector, JNI_ABORT);
std::unordered_map<std::string, jobject> methodParams;
if (methodParamsJ != nullptr) {
methodParams = jniUtil->ConvertJavaMapToCppMap(env, methodParamsJ);
}
int queryEfSearch = knn_jni::commons::getIntegerMethodParameter(env, jniUtil, methodParams, EF_SEARCH, -1);
std::unique_ptr<similarity::KNNQuery<float>> query;
std::unique_ptr<similarity::KNNQueue<float>> neighbors;
if (queryEfSearch == -1) {
query.reset(new similarity::KNNQuery<float>(*(indexWrapper->space), queryObject.get(), kJ));
} else {
query.reset(new similarity::HNSWQuery<float>(*(indexWrapper->space), queryObject.get(), kJ, queryEfSearch));
}
indexWrapper->index->Search(query.get());
neighbors.reset(query->Result()->Clone());
int resultSize = neighbors->Size();
jclass resultClass = jniUtil->FindClass(env, "org/opensearch/knn/index/query/KNNQueryResult");
jmethodID allArgs = jniUtil->FindMethod(env, "org/opensearch/knn/index/query/KNNQueryResult", "<init>");
jobjectArray results = jniUtil->NewObjectArray(env, resultSize, resultClass, nullptr);
jobject result;
float distance;
long id;
for (int i = 0; i < resultSize; ++i) {
distance = neighbors->TopDistance();
id = neighbors->Pop()->id();
result = jniUtil->NewObject(env, resultClass, allArgs, id, distance);
jniUtil->SetObjectArrayElement(env, results, i, result);
}
return results;
}
void knn_jni::nmslib_wrapper::Free(jlong indexPointerJ) {
auto *indexWrapper = reinterpret_cast<knn_jni::nmslib_wrapper::IndexWrapper *>(indexPointerJ);
delete indexWrapper;
}
void knn_jni::nmslib_wrapper::InitLibrary() {
similarity::initLibrary();
}
std::string TranslateSpaceType(const std::string &spaceType) {
if (spaceType == knn_jni::L2) {
return spaceType;
}
if (spaceType == knn_jni::L1) {
return spaceType;
}
if (spaceType == knn_jni::LINF) {
return spaceType;
}
if (spaceType == knn_jni::COSINESIMIL) {
return spaceType;
}
if (spaceType == knn_jni::INNER_PRODUCT) {
return knn_jni::NEG_DOT_PRODUCT;
}
throw std::runtime_error("Invalid spaceType");
}