forked from stanis-morozov/ip-nsw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
467 lines (404 loc) · 17 KB
/
main.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include <iostream>
#include <fstream>
#include <string>
#include <unordered_set>
#include <utility>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <chrono>
#include "hnswlib.h"
#include <algorithm>
#include <ctime>
#include <cassert>
#include <omp.h>
const int defaultEfConstruction = 1024;
const int defaultEfSearch = 128;
const int defaultM = 32;
const int defaultTopK = 10;
int loadFvecs(float*& data, int numItems, std::string inputPath) {
std::ifstream fin(inputPath, std::ios::binary);
if (!fin) {
std::cout << "cannot open file " << inputPath << std::endl;
assert(false);
}
int dimension;
fin.read((char*)&dimension, 4);
data = new float[numItems* dimension];
fin.read((char*)data, sizeof(float) * dimension);
int dim;
for (int i = 1; i < numItems; ++i) {
fin.read((char*)&dim, 4);
assert(dim == dimension);
fin.read((char*)(data + i * dimension), sizeof(float) * dimension);
}
fin.close();
return dimension;
}
std::vector<std::priority_queue<std::pair<float, labeltype >>> loadLSHBOX(std::string inputPath) {
std::vector<std::priority_queue<std::pair<float, labeltype >>> answers;
std::ifstream fin(inputPath.c_str());
unsigned numQueries;
unsigned K;
fin >> numQueries >> K;
answers.resize(numQueries);
unsigned qId;
unsigned id;
float dist;
int index = 0;
for (int q = 0; q < numQueries; ++q) {
fin >> qId;
assert(qId == q);
for (int i = 0; i < K; ++i) {
fin >> id >> dist;
answers[q].emplace(dist, id);
}
}
fin.close();
return answers;
}
void printHelpMessage()
{
std::cerr << "Usage: main [OPTIONS]" << std::endl;
std::cerr << "This tool supports two modes: to construct the graph index from the database and to retrieve top K maximum inner product vectors using the constructed index. Each mode has its own set of parameters." << std::endl;
std::cerr << std::endl;
std::cerr << " --mode " << "\"database\" or \"query\". Use \"database\" for" << std::endl;
std::cerr << " " << "constructing index and \"query\" for top K" << std::endl;
std::cerr << " " << "maximum inner product retrieval" << std::endl;
std::cerr << std::endl;
std::cerr << "Database mode supports the following options:" << std::endl;
std::cerr << " --database " << "Database filename. Database should be stored in binary format." << std::endl;
std::cerr << " " << "Vectors are written consecutive and numbers are" << std::endl;
std::cerr << " " << "represented in 4-bytes floating poing format (float in C/C++)" << std::endl;
std::cerr << " --databaseSize " << "Number of vectors in the database" << std::endl;
std::cerr << " --dimension " << "Dimension of vectors" << std::endl;
std::cerr << " --outputGraph " << "Filename for the output index graph" << std::endl;
std::cerr << " --efConstruction " << "efConstruction parameter. Default: " << defaultEfConstruction << std::endl;
std::cerr << " --M " << "M parameter. Default: " << defaultM << std::endl;
std::cerr << std::endl;
std::cerr << "Query mode supports the following options:" << std::endl;
std::cerr << " --query " << "Query filename. Queries should be stored in binary format." << std::endl;
std::cerr << " " << "Vectors are written consecutive and numbers are" << std::endl;
std::cerr << " " << "represented in 4-bytes floating poing format (float in C/C++)" << std::endl;
std::cerr << " --querySize " << "Number of queries" << std::endl;
std::cerr << " --dimension " << "Dimension of vectors" << std::endl;
std::cerr << " --inputGraph " << "Filename for the input index graph" << std::endl;
std::cerr << " --efSearch " << "efSearch parameter. Default: " << defaultEfSearch << std::endl;
std::cerr << " --topK " << "Top size for retrieval. Default: " << defaultTopK << std::endl;
std::cerr << " --output " << "Filename to print the result. Default: " << "stdout" << std::endl;
}
void printError(std::string err)
{
std::cerr << err << std::endl;
std::cerr << std::endl;
printHelpMessage();
}
int main(int argc, char** argv) {
std::string mode;
std::ifstream input;
std::ifstream inputQ;
int efConstruction = defaultEfConstruction;
int efSearch = defaultEfSearch;
int M = defaultM;
int vecsize = -1;
int qsize = -1;
int vecdim = -1;
std::string graphname;
std::string outputname;
int topK = defaultTopK;
std::string dataname;
std::string queryname;
std::string benchmarkname;
hnswlib::HierarchicalNSW<float> *appr_alg;
for (int i = 1; i < argc; i++) {
if (std::string(argv[i]) == "--h" || std::string(argv[i]) == "--help") {
printHelpMessage();
return 0;
}
}
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--m" || std::string(argv[i]) == "--mode") {
if (std::string(argv[i + 1]) == "database") {
mode = "database";
} else if (std::string(argv[i + 1]) == "query") {
mode = "query";
} else {
printError("Unknown running mode \"" + std::string(argv[i + 1]) + "\". Please use \"database\" or \"query\"");
return 0;
}
break;
}
}
if (mode.empty()) {
printError("Running mode was not specified");
return 0;
}
std::cout << "Mode: " << mode << std::endl;
if (mode == "database") {
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--d" || std::string(argv[i]) == "--data" || std::string(argv[i]) == "--database") {
dataname = std::string(argv[i + 1]);
break;
}
}
std::cout << "Database file: " << dataname << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--dataSize" || std::string(argv[i]) == "--dSize" || std::string(argv[i]) == "--databaseSize") {
if (sscanf(argv[i + 1], "%d", &vecsize) != 1 || vecsize <= 0) {
printError("Inappropriate value for database size: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
if (vecsize == -1) {
printError("Database size was not specified");
return 0;
}
std::cout << "Database size: " << vecsize << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--dataDim" || std::string(argv[i]) == "--dimension" || std::string(argv[i]) == "--databaseDimension") {
if (sscanf(argv[i + 1], "%d", &vecdim) != 1 || vecdim <= 0) {
printError("Inappropriate value for database dimension: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
if (vecdim == -1) {
printError("Database dimension was not specified");
return 0;
}
std::cout << "Database dimension: " << vecdim << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--outGraph" || std::string(argv[i]) == "--outputGraph") {
std::ofstream outGraph(argv[i + 1]);
if (!outGraph.is_open()) {
printError("Cannot create file \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
outGraph.close();
graphname = std::string(argv[i + 1]);
break;
}
}
if (graphname.empty()) {
printError("Filename of the output graph was not specified");
return 0;
}
std::cout << "Output graph: " << graphname << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--efConstruction") {
if (sscanf(argv[i + 1], "%d", &efConstruction) != 1 || efConstruction <= 0) {
printError("Inappropriate value for efConstruction: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
std::cout << "efConstruction: " << efConstruction << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--M") {
if (sscanf(argv[i + 1], "%d", &M) != 1 || M <= 0) {
printError("Inappropriate value for M: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
std::cout << "M: " << M << std::endl;
hnswlib::L2Space l2space(vecdim);
float *mass = NULL;
size_t datadim = loadFvecs(mass, vecsize, dataname);
appr_alg = new hnswlib::HierarchicalNSW<float>(&l2space, vecsize, M, efConstruction);
std::cout << "Building index\n";
double t1 = omp_get_wtime();
for (int i = 0; i < 1; i++) {
appr_alg->addPoint((void *)(mass + vecdim*i), (size_t)i);
}
#pragma omp parallel for
for (int i = 1; i < vecsize; i++) {
appr_alg->addPoint((void *)(mass + vecdim*i), (size_t)i);
}
double t2 = omp_get_wtime();
std::cout << "Index built, time=" << t2 - t1 << " s" << "\n";
appr_alg->SaveIndex(graphname.data());
delete appr_alg;
delete mass;
} else {
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--q" || std::string(argv[i]) == "--query") {
queryname = std::string(argv[i + 1]);
break;
}
}
std::cout << "Query filename: " << queryname << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--benchmark") {
benchmarkname = std::string(argv[i + 1]);
break;
}
}
std::cout << "benchmark filename: " << benchmarkname << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--querySize" || std::string(argv[i]) == "--qSize") {
if (sscanf(argv[i + 1], "%d", &qsize) != 1 || qsize <= 0) {
printError("Inappropriate value for query size: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
if (qsize == -1) {
printError("Query size was not specified");
return 0;
}
std::cout << "Query size: " << qsize << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--queryDim" || std::string(argv[i]) == "--dimension" || std::string(argv[i]) == "--queryDimension") {
if (sscanf(argv[i + 1], "%d", &vecdim) != 1 || vecdim <= 0) {
printError("Inappropriate value for query dimension: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
if (vecdim == -1) {
printError("Query dimension was not specified");
return 0;
}
std::cout << "Query dimension: " << vecdim << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--inGraph" || std::string(argv[i]) == "--inputGraph") {
std::ifstream inGraph(argv[i + 1]);
if (!inGraph.is_open()) {
printError("Cannot open file \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
inGraph.close();
graphname = std::string(argv[i + 1]);
break;
}
}
if (graphname.empty()) {
printError("Filename of the input graph was not specified");
return 0;
}
std::cout << "Input graph: " << graphname << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--efSearch") {
if (sscanf(argv[i + 1], "%d", &efSearch) != 1 || efSearch <= 0) {
printError("Inappropriate value for efSearch: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
std::cout << "efSearch: " << efSearch << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--topK") {
if (sscanf(argv[i + 1], "%d", &topK) != 1 || topK <= 0) {
printError("Inappropriate value for top size: \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
break;
}
}
std::cout << "Top size: " << topK << std::endl;
for (int i = 1; i < argc - 1; i++) {
if (std::string(argv[i]) == "--output" || std::string(argv[i]) == "--out") {
std::ofstream output(argv[i + 1]);
if (!output.is_open()) {
printError("Cannot open file \"" + std::string(argv[i + 1]) + "\"");
return 0;
}
output.close();
outputname = std::string(argv[i + 1]);
break;
}
}
if (outputname.empty()) {
std::cout << "Output file: " << "stdout" << std::endl;
} else {
std::cout << "Output file: " << outputname << std::endl;
}
std::vector<std::priority_queue<std::pair<float, labeltype >>> answers = loadLSHBOX(benchmarkname);
hnswlib::L2Space l2space(vecdim);
float *massQ = NULL;
size_t datadim = loadFvecs(massQ, qsize, queryname);
std::priority_queue< std::pair< float, labeltype >> gt[qsize];
appr_alg = new hnswlib::HierarchicalNSW<float>(&l2space, graphname.data(), false);
std::cout << "max level : " << appr_alg->maxlevel_ << std::endl;
std::vector<int> efs;
for (int i = 10; i < 100; i+=10) {
efs.push_back(i);
}
for (int i = 100; i < 300; i += 20) {
efs.push_back(i);
}
for (int i = 300; i < 1000; i += 100) {
efs.push_back(i);
}
for (int i = 2000; i < 20000; i += 2000) {
efs.push_back(i);
}
//for (int i = 20000; i < 100000; i += 20000) {
// efs.push_back(i);
//}
for (int efSearch : efs) {
appr_alg->setEf(efSearch);
appr_alg->dist_calc = 0;
std::ofstream fres;
if (!outputname.empty()) {
fres.open(outputname);
}
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < qsize; i++) {
gt[i] = appr_alg->searchKnn(massQ + vecdim*i, topK);
}
auto end = std::chrono::high_resolution_clock::now();
// std::cout << "quality : " << appr_alg->quality_of_first_bot_bucket / qsize << std::endl;
int correct = 0, total = 0;
for (int i = 0; i < qsize; i++) {
std::vector <int> res;
while (!gt[i].empty()) {
res.push_back(gt[i].top().second);
gt[i].pop();
}
std::reverse(res.begin(), res.end());
std::unordered_set<labeltype> g;
std::priority_queue<std::pair<float, labeltype >> gt(answers[i]);
total += gt.size();
while (gt.size()) {
g.insert(gt.top().second);
gt.pop();
}
for (auto it : res) {
// to be done soon
if (g.find(it) != g.end())
correct++;
if (!outputname.empty()) {
fres << it << ' ';
} else {
std::cout << it << ' ';
}
}
if (!outputname.empty()) {
fres << std::endl;
} else {
std::cout << std::endl;
}
}
if (!outputname.empty()) {
fres.close();
}
std::cout << "ef : " << efSearch << ", ";
std::cout << 1.0f * correct / total << ", ";
std::cout << "Average query time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / (double)qsize << "ms, ";
std::cout << "dist_computations: " << appr_alg->dist_calc / (double)qsize << std::endl;
}
delete appr_alg;
delete massQ;
}
return 0;
}