-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertified_cosine_benchmark_main.cc
378 lines (318 loc) · 12.3 KB
/
certified_cosine_benchmark_main.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
371
372
373
374
375
376
377
378
#include <iostream>
#include <random>
#include <omp.h>
#include <Eigen/Dense>
#define CERTIFIEDCOSINE_DEBUG_ACCESS
#define CERTIFIEDCOSINE_DEBUG
#include "lookup.hpp"
#include "policy.hpp"
#include "pre_processing.hpp"
#include "vector_stats.hpp"
using namespace std;
using namespace Eigen;
// #define RANDOM_TEST
// #define RANDOM_TEST_NDIM 30
// int global_number_edges = 50;
//#define NUMBER_EDGES (int)(global_number_edges)
//#define NUMBER_EDGES_RAND 9
//#define NUMBER_BOOTSTRAP 400
///#define NUMBER_RAND_LOOKS 3
// the batch size that we should split the vectors up per thread
//#define PARALLEL_BATCH_SIZE 100
// the number of items from the origional matrix that need to get multiplied by at a time
//#define PARALLEL_MATMUL_SIZE 10
/**
* Read a file containing embeddings of word vectors in the binary format.
*/
class WordVecs {
public:
Matrix<float, Dynamic, Dynamic, RowMajor> words;
int words_cnt;
int length;
WordVecs(string fname) {
ifstream file(fname);
string word;
file >> words_cnt;
file >> length;
float *buffer = new float[length];
int i = 0;
words = MatrixXf(words_cnt, length); // rows, cols
while (file >> word) {
file.get(); // ignore space
file.read((char *)buffer, sizeof(float) * length);
for (int j = 0; j < length; j++) {
words(i, j) = buffer[j];
}
i++;
if (i >= words_cnt) break;
}
delete[] buffer;
}
};
template <typename matrix_t, typename storage_t>
void random_test(const matrix_t &matrix, const storage_t &storage, ostream &file) {
file << "test,"
"success,"
"proof,"
"around_area,"
"best_distance,"
"selected_distance,"
"proof_distance,"
"number_inner_products,"
"number_till_located,"
"x_steps,"
"num_prove_items,"
"optimize_calls,"
"optimize_loops,"
"time(ms),"
"direct_time(ms)\n";
int lcnt = 0;
using namespace certified_cosine;
typedef Eigen::Matrix<typename matrix_t::Scalar, matrix_t::ColsAtCompileTime, 1> VecD;
typedef matrix_t MatD;
auto randomVector = [&]() { return VecD::Random(matrix.cols()).eval().normalized().eval(); };
#ifdef CERTIFIEDCOSINE_USE_PARALLEL
#pragma omp parallel
#endif
{
// uint num_innerproducts = 0;
// auto ignore = [&](int a) {
// num_innerproducts++;
// // there is nothing to ignore
// return true;
// };
// auto stop = [](int a) { return false; }; // don't stop looking for vertices
// // use the proof to identify what it is locating
// lookupManager_2<decltype(ignore), decltype(stop), true> lookupMan(ignore, stop, *this);
LookupCertifiedCosine<storage_t, Eigen::Ref<const MatD>> lookupMan(matrix, &storage);
struct timespec start_time, end_time;
#ifdef CERTIFIEDCOSINE_USE_PARALLEL
#pragma omp for schedule(dynamic, 3)
#endif
for (int i = 0; i < 120000; i++) {
// num_innerproducts = 0;
// lookupMan.reset();
int t = rand() % matrix.rows();
VecD vec;
if (i < 60000) {
vec = (matrix.row(t).transpose() + ((i / 3) / 5000.) * randomVector()).normalized().eval();
} else {
vec = randomVector().normalized().eval();
}
// do the brute force method first, so in the debugger can know what the maxIndex is for comparison
clock_gettime(CLOCK_MONOTONIC_RAW, &start_time);
typename MatD::Index maxIndex;
float_t best_score = (matrix * vec).maxCoeff(&maxIndex);
clock_gettime(CLOCK_MONOTONIC_RAW, &end_time);
float delta_ms2 =
((end_time.tv_sec - start_time.tv_sec) * 1000000 + (end_time.tv_nsec - start_time.tv_nsec) / 1000) / 1000.0;
// CountExpandPolicy<OneBestPolicy<typename matrix_t::Scalar>> policy;
CountingTillBest<typename matrix_t::Scalar> policy;
// CountingNBestPolicy<typename matrix_t::Scalar> policy(2);
clock_gettime(CLOCK_MONOTONIC_RAW, &start_time);
// lookupMan.lookupOperation(vec, 25, 0);
lookupMan.lookup(vec, policy);
clock_gettime(CLOCK_MONOTONIC_RAW, &end_time);
int selected = policy.id; // policy.items.max().id;
float_t selected_score = policy.distance; // policy.items.max().score;
float delta_ms1 =
((end_time.tv_sec - start_time.tv_sec) * 1000000 + (end_time.tv_nsec - start_time.tv_nsec) / 1000) / 1000.0;
// the number of vertices that could move the current proof item if they were located
int around_area = 0;
if (lookupMan.lp_project.located_cosim > policy.proof_distance()) {
// then we are going to search for more items that match the work with the inner product
auto scores = (matrix * lookupMan.target.row(0).transpose()).array().eval();
for (int i = 0; i < matrix.rows(); i++) {
auto vx = storage.get_vertex(i);
if (compareScore(scores(i), vx.proof_distance(&storage))) {
around_area++;
}
}
}
assert(selected == maxIndex);
#ifdef CERTIFIEDCOSINE_USE_PARALLEL
#pragma omp critical
#endif
{
file << (lcnt++) << "," << (selected == maxIndex) << "," << (int)policy.got_proof() << "," << around_area << ","
<< best_score << "," << selected_score << "," << lookupMan.located_cosim << "," << policy.count << ","
<< policy.count_located << "," << lookupMan.score_step << "," << lookupMan.lp_project.num_rows << ","
<< lookupMan.lp_project.optimize_calls << "," << lookupMan.lp_project.optimize_loops << "," << delta_ms1
<< "," << delta_ms2 << endl;
// assert that if we are finding a proof that we were actually successful
// if(lookupMan.found_proof && selected != maxIndex) {
// cout << "======================================= FAILED " << (lcnt - 1) << "
// ================================\n";
// //assert(selected == maxIndex);
// }
}
}
}
}
// method in svd_wrapper.cc as it takes a while to compile
MatrixXf getSVD_U(const MatrixXf &inp);
// {
// return inp.bdcSvd(Eigen::ComputeFullU).matrixU();
// }
int main(int argc, char **argv) {
Eigen::initParallel();
int num_neighbors = 50; // number of outgoing edges to run with
string input_filename;
string output_edges_filename;
string lookup_log_filename;
string load_edges;
int num_random_vectors = 50000;
int embedding_dim = 30;
int random_seed = 0;
int use_cuda = 0;
int use_compact = 0;
int svd_reduce = 0; // if this should try and reduce the dim using the svd
for (int c = 1; c < argc; c++) {
string s = argv[c];
if (s == "--outgoing") {
num_neighbors = atoi(argv[c + 1]);
c++;
} else if (s == "--input") {
input_filename = argv[c + 1];
c++;
num_random_vectors = 0;
} else if (s == "--save_edges") {
output_edges_filename = argv[c + 1];
c++;
} else if (s == "--test_vecs") {
num_random_vectors = atoi(argv[c + 1]);
c++;
} else if (s == "--dim") {
embedding_dim = atoi(argv[c + 1]);
c++;
} else if (s == "--svd") {
svd_reduce = 1;
// no c++; as there are no additional arguments
} else if (s == "--load_edges") {
load_edges = argv[c + 1];
c++;
} else if (s == "--seed") {
random_seed = atoi(argv[c + 1]);
c++;
} else if (s == "--log") {
lookup_log_filename = argv[c + 1];
c++;
} else if (s == "--compact") {
use_compact = 1;
// } else if(s == "--cuda") {
// #ifdef USE_CUDA
// use_cuda = 1;
// #else
// cerr << "not compiled with cuda\n";
// return 1;
// #endif
} else if (s == "--help") {
cout << argv[0]
<< ": A testing program for certified cosine\n"
"Dataset can either be word vectors in the binary format or randomly generated\n"
"\n"
"NOTE !!!: This compares against brute force linear scan, and thus is much slower than just running "
"certified cosine\n"
"\n"
" --outgoing Number of outgoing edges from every vertex in the neighbor graph [default: 50]\n"
" --input Input file name of binary word embedding vectors to test [default: Not set, randomly "
"generate dataset]\n"
" --save_edges Save the KNNG neighborhood graph [default: Not set]\n"
" --load_edges Load the KNNG neighborhood graph instead of constructing it [default: Not set]\n"
" --dim Dimension of randomly generated vectors or reduce rank of loaded vectors [default: "
"30]\n"
" --svd If set, reduce the dimension of loaded word embeddings otherwise use datasets "
"dimension [default: Not set]\n"
" --seed Random seed for generating random dataset [default: 0]\n"
" --log CSV file to log the results of running [default: stdout]\n"
" --compact Use the compacted storage when benchmarking queries\n";
return 1;
} else {
cerr << "failed to parse argument " << s << endl;
return 1;
}
}
// #ifdef USE_CUDA
// // claim the gpu early
// if(use_cuda) {
// cuda_dummy_init();
// }
// #endif
// global_number_edges = num_neighbors;
typedef Matrix<float, Dynamic, Dynamic, RowMajor> Mat;
Mat embeddedMatrix;
if (input_filename.empty()) {
// then create a random representation for this
normal_distribution<float> nd;
mt19937 rng(random_seed);
auto testMat =
MatrixXf::Zero(num_random_vectors, embedding_dim).unaryExpr([&](auto f) -> float { return nd(rng); }).eval();
auto norms = testMat.rowwise().norm().eval();
embeddedMatrix = testMat.array().colwise() / norms.array();
} else {
// load word embedding file, this will be about a 3 million if this is google word vectors
// this also will need the embedding dimention to match
WordVecs wv(input_filename);
if (svd_reduce) {
// take the svd of the word vectors, and then use the top embedding_dim rows of the matrix
// this way, we will be able to use the real data but have a slightly faster operation when it comes to
// experimenting with stuff
// compiling the svd is really slow, so maybe just leave that out for now?
// embeddedMatrix = wv.words;
// auto a = getVSVD(embeddedMatrix);
// auto a = wv.words.bdcSvd(Eigen::ComputeFullV).matrixV();
// JacobiSVD<MatrixXf> svd(wv.words, ComputeThinU | ComputeThinV);
// embeddedMatrix = svd.matrixU().leftCols(embedding_dim);
auto U = getSVD_U(wv.words.transpose()).leftCols(embedding_dim);
embeddedMatrix = wv.words * U;
auto norms = embeddedMatrix.rowwise().norm().eval();
embeddedMatrix.array().colwise() /= norms.array();
} else {
embeddedMatrix = wv.words;
// normalize the vectors
auto norms = embeddedMatrix.rowwise().norm().eval();
embeddedMatrix.array().colwise() /= norms.array();
}
}
// #ifndef NDEBUG
// if(0) {
// // this should automatically check that the dimentions align correctly
// // since we have hard coded in what the shapes of things are
// auto tm = Matrix<float, 11, 30>::Random();
// FastLookup14<decltype(tm)> gg(tm);
// }
// #endif
using namespace certified_cosine;
dynamic_storage<float> storage;
// exact_neighbors(embeddedMatrix, storage, num_neighbors);
if (load_edges.empty()) {
preprocess<float>(embeddedMatrix, storage, num_neighbors);
} else {
storage.Load(load_edges);
}
print_summarization(storage);
if (!output_edges_filename.empty()) {
ofstream save_file(output_edges_filename);
storage.Save(save_file);
}
if (use_compact) {
// compact storage
compact_storage<float> cstorage;
storage.BuildCompactStorage(cstorage);
if (!lookup_log_filename.empty()) {
ofstream log_file(lookup_log_filename);
random_test(embeddedMatrix, cstorage, log_file);
} else {
random_test(embeddedMatrix, cstorage, cout);
}
} else {
// dynamic storage
if (!lookup_log_filename.empty()) {
ofstream log_file(lookup_log_filename);
random_test(embeddedMatrix, storage, log_file);
} else {
random_test(embeddedMatrix, storage, cout);
}
}
return 0;
}