-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathdata_util.h
More file actions
executable file
·453 lines (406 loc) · 12.9 KB
/
Copy pathdata_util.h
File metadata and controls
executable file
·453 lines (406 loc) · 12.9 KB
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
/** @file */
// Copyright 2012 Yandex Artem Babenko
#pragma once
#include <bitset>
#include <fstream>
#include <ios>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include "mkl_cblas.h"
#include "multitable.hpp"
using std::bitset;
using std::cout;
using std::ifstream;
using std::ios;
using std::endl;
using std::multimap;
using std::pair;
using std::set;
using std::string;
using std::vector;
/**
* \typedef
* Data type for coordinate (bool, char, int, float, etc.)
*/
typedef float Coord;
/**
* \typedef
* Data type for distance in multidimensional space
*/
typedef float Distance;
/**
* \typedef
* Dimensionality of space = number of point coordinates
*/
typedef int Dimensions;
/**
* \typedef
* Data type for point identifier
*/
typedef int PointId;
/**
* \typedef
* Data type for cluster identifier
*/
typedef int ClusterId;
/**
* \typedef
* Just vector of coordinates
*/
typedef vector<Coord> Point;
/**
* \typedef
* Class for a number of points
*/
typedef vector<Point> Points;
/**
* \typedef
* Class for a number of point identifiers
*/
typedef vector<PointId> PointIds;
/**
* \typedef
* Class for a set of points
*/
typedef set<PointId> SetPoints;
/**
* \typedef
* Class for a number of cluster identifiers
*/
typedef vector<ClusterId> ClusterIds;
/**
* \typedef
* Class for representation of point coarse quantization,
* ids of nearest centroids for each group of dimensions
*/
typedef vector<ClusterId> CoarseQuantization;
/**
* \typedef
* Data type for fine cluster identifier
*/
typedef unsigned char FineClusterId;
/**
* \typedef
* Class for representation of point fine quantization
*/
typedef vector<FineClusterId> FineQuantization;
/**
* \typedef
* Class for clusters representation
* ClusterId -> (PointId, PointId, PointId, .... )
*/
typedef vector<SetPoints> ClustersToPoints;
/**
* \typedef
* Class for belonging to clusters representation
* PointId -> ClusterId
*/
typedef std::vector<ClusterId> PointsToClusters;
/**
* \typedef
* Centroids of the clustered points set
*/
typedef std::vector<Point> Centroids;
/**
* \enum This enumeration presents different types of input point
* coordinate can be float or uint8
*/
enum PointType {
FVEC,
BVEC
};
/**
* \enum This enumeration presents different ways to get rerank info.
* Algorithm can quantize residuals after coarse quantization or init points without
* centroids subtraction
*/
enum RerankMode {
USE_RESIDUALS,
USE_INIT_POINTS
};
/**
* \struct MultiIndex incorporates all data structures we need to make search
*/
template<class Record>
struct MultiIndex {
vector<Record> multiindex;
Multitable<int> cell_edges; ///< Table with index cell edges in array
};
/**
* Function calculates squared euclidian distance between two points (points must have the same dimensionality)
* @param x first point
* @param y second point
*/
Distance Eucldistance(const Point& x, const Point& y);
/**
* Function calculates squared euclidian distance point with small dimensionality and
* subpoint of point with bigger dimensionality.
* @param x first point
* @param y second point
* @param start first dimension of subpoint
* @param finish dimension after the last dimension of subpoint
*/
Distance Eucldistance(const Point& x, const Point& y, Dimensions start, Dimensions finish);
/**
* This simple function casts number of type T to the nearest number of type U
*/
template<class T, class U>
inline U Round(T number) {
return (U)(number);
}
/**
* Function reads point written in .fvecs or .bvecs format.
* Input points have coordinates of type T.
* Result points have coordinates of type U
* @param filename .fvecs or .bvecs file name
* @param points_count how many points to read
* @param points result list of read points
*/
template<class T, class U>
void ReadPoints(const string& filename,
vector<vector<U> >* points,
int count) {
ifstream input;
input.open(filename.c_str(), ios::binary);
if(!input.good()) {
throw std::logic_error("Invalid filename");
}
points->resize(count);
int dimension;
for(PointId pid = 0; pid < count; ++pid) {
input.read((char*)&dimension, sizeof(dimension));
if(dimension <= 0) {
throw std::logic_error("Bad file content: non-positive dimension");
}
points->at(pid).resize(dimension);
for(Dimensions d = 0; d < dimension; ++d) {
T buffer;
input.read((char*)&(buffer), sizeof(T));
points->at(pid)[d] = Round<T, U>(buffer);
}
}
}
/**
* Function reads one vector of coordinates of type T.
* Function assumes that the first int32-number in input stream is
* vector dimensionality. Result vector will have coordinates of type U.
* @param input input stream
* @param v result vector
*/
template<class T, class U>
void ReadVector(ifstream& input, vector<U>* v) {
if(!input.good()) {
throw std::logic_error("Bad input stream");
}
int dimension;
input.read((char*)&dimension, sizeof(dimension));
if(dimension <= 0) {
throw std::logic_error("Bad file content: non-positive dimension");
}
v->resize(dimension);
for(Dimensions d = 0; d < dimension; ++d) {
T buffer;
input.read((char*)&buffer, sizeof(buffer));
v->at(d) = Round<T, U>(buffer);
}
}
/**
* Function reads vocabulary of centroids produced by matlab script.
* @param input input stream
* @param dimension one centroid dimensionality
* @param vocabulary_size centroids count
* @param Centroids* result centroids
*/
template<class T>
void ReadVocabulary(ifstream& input,
Dimensions dimension,
int vocabulary_size,
Centroids* centroids) {
if(!input.good()) {
throw std::logic_error("Bad input stream");
}
centroids->resize(vocabulary_size);
for(ClusterId centroid_index = 0; centroid_index < centroids->size(); ++centroid_index) {
centroids->at(centroid_index).resize(dimension);
for(Dimensions dimension_index = 0; dimension_index < dimension; ++dimension_index) {
T buffer;
input.read((char*)&buffer, sizeof(buffer));
centroids->at(centroid_index)[dimension_index] = Round<T, Coord>(buffer);
}
}
}
/**
* Function reads vocabularies of centroids produced by matlab script.
* Function assumes that the first int32 in input is dimensionality of centroids and
* the second is the number of centroids in each vocabulary
* @param input input stream
* @param dimension one centroid dimensionality
* @param vocabulary_size centroids count
* @param Centroids* result centroids
*/
template<class T>
void ReadVocabularies(const string& filename,
Dimensions space_dimension,
vector<Centroids>* centroids) {
ifstream vocabulary;
vocabulary.open(filename.c_str(), ios::binary);
if(!vocabulary.good()) {
throw std::logic_error("Bad vocabulary file");
}
int dimension;
vocabulary.read((char*)&dimension, sizeof(dimension));
if(dimension <= 0) {
throw std::logic_error("Bad file content: non-positive dimension");
}
int vocabs_count = space_dimension / dimension;
if(space_dimension < dimension) {
throw std::logic_error("Space dimension is less than vocabulary dimension");
}
centroids->resize(vocabs_count);
int vocabulary_size;
vocabulary.read((char*)&vocabulary_size, sizeof(vocabulary_size));
for(int vocab_item = 0; vocab_item < vocabs_count; ++vocab_item) {
ReadVocabulary<T>(vocabulary, dimension, vocabulary_size, &(centroids->at(vocab_item)));
}
}
/**
* This function reads fine vocabs of centroids
* @param fine_vocabs_filename file with vocabularies
* @param fine_vocabs fine centroids lists
*/
template<class T>
void ReadFineVocabs(const string& fine_vocabs_filename, vector<Centroids>* fine_vocabs) {
ifstream fine_vocabs_stream;
fine_vocabs_stream.open(fine_vocabs_filename.c_str(), ios::binary);
if(!fine_vocabs_stream.good()) {
throw std::logic_error("Bad fine vocabulary file");
}
int vocabs_count, centroids_count, vocabs_dim;
fine_vocabs_stream.read((char*)&vocabs_count, sizeof(vocabs_count));
if(vocabs_count < 1) {
throw std::logic_error("Bad fine vocabulary file content: number of vocabularies < 1");
}
fine_vocabs_stream.read((char*)¢roids_count, sizeof(centroids_count));
if(centroids_count < 1) {
throw std::logic_error("Bad fine vocabulary file content: vocabulary capacity < 1");
}
fine_vocabs_stream.read((char*)&vocabs_dim, sizeof(vocabs_dim));
if(vocabs_dim < 1) {
throw std::logic_error("Bad fine vocabulary file content: vocabulary dimension < 1");
}
fine_vocabs->resize(vocabs_count);
for(int voc_index = 0; voc_index < vocabs_count; ++voc_index) {
ReadVocabulary<T>(fine_vocabs_stream, vocabs_dim, centroids_count, &(fine_vocabs->at(voc_index)));
}
}
/**
* This function returns subpoints limited by start_dim and final_dim
* for every point in points
* @param points all points
* @param start_dim first dimension of subpoint
* @param final_dim dimension after the last dimension of subpoint
* @param subpoints result subpoints
*/
void GetSubpoints(const Points& points,
const Dimensions start_dim,
const Dimensions final_dim,
Points* subpoints);
/**
* This function returns identifier of clusters which centroid is the nearest to
* subpoint limited by start_dim and final_dim
* @param point full point
* @param Centroids all centroids (function finds the nearest one)
* @param start_dim first dimension of subpoint
* @param final_dim dimension after the last dimension of subpoint
*/
ClusterId GetNearestClusterId(const Point& point, const Centroids& centroids,
const Dimensions start_dim, const Dimensions final_dim);
/**
* This function calculates quantization residual.
* @param point initial point
* @param coarse_quantizations point coarse quantization
* @param centroids lists of centroids
* @param residual result residual
*/
void GetResidual(const Point& point, const CoarseQuantization& coarse_quantizations,
const vector<Centroids>& centroids, Point* residual);
/**
* This function calculates quantization residual.
* @param point initial point
* @param coarse_quantizations point coarse quantization
* @param centroids lists of centroids
* @param residual pointer to start of residual
*/
void GetResidual(const Point& point, const CoarseQuantization& coarse_quantizations,
const vector<Centroids>& centroids, Coord* residual);
/**
* This function finds nearest cluster identifiers for points from start_pid to final_pid.
* We need this function for multi-threading
* @param points all points
* @param centroids centroids of clusters
* @param start_pid first point function finds nearest cluster
* @param final_pid point after the last point function finds nearest cluster
*/
void GetNearestClusterIdsForPointSubset(const Points& points, const Centroids& centroids,
const PointId start_pid, const PointId final_pid,
vector<ClusterId>* nearest);
/**
* This function finds cluster identifiers nearest to subpoints for a number of points.
* Subpoints are limited by start_dim and finish_dim
* @param points all points
* @param centroids centroids of clusters
* @param start_dim first dimesion of subpoint
* @param final_dim dimesion after the last dimension of subpoint
* @param threads_count number of threads
* @param nearest result
*/
void GetNearestClusterIdsForSubpoints(const Points& points, const Centroids& centroids,
const Dimensions start_dim, const Dimensions final_dim,
int threads_count, vector<ClusterId>* nearest);
/**
* This function calculates points coarse product quantizations
* @param points all points
* @param centroids centroids of clusters
* @param threads_count number of threads
* @param coarse_quantizations result quantizations
*/
void GetPointsCoarseQuaintizations(const Points& points, const vector<Centroids>& centroids,
const int threads_count,
vector<CoarseQuantization>* coarse_quantizations);
/**
* \struct All indexation parameters
*/
struct IndexConfig {
RerankMode rerank_mode;
vector<Centroids> fine_vocabs;
};
/**
* \struct Type of record in multiindex, contains
* id of point and 8 bytes for ADC reranking
*/
struct RerankADC8 {
PointId pid;
FineClusterId quantizations[8];
template<class Archive>
void serialize(Archive& arc, unsigned int version) {
arc & pid;
arc & quantizations;
}
};
/**
* \struct Type of record in multiindex, contains
* id of point and 16 bytes for ADC reranking
*/
struct RerankADC16 {
PointId pid;
FineClusterId quantizations[16];
template<class Archive>
void serialize(Archive& arc, unsigned int version) {
arc & pid;
arc & quantizations;
}
};