forked from microsoft/DiskANN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition.cpp
657 lines (550 loc) · 27.6 KB
/
partition.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include <cmath>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <omp.h>
#include "tsl/robin_map.h"
#include "tsl/robin_set.h"
#if defined(RELEASE_UNUSED_TCMALLOC_MEMORY_AT_CHECKPOINTS) && defined(DISKANN_BUILD)
#include "gperftools/malloc_extension.h"
#endif
#include "utils.h"
#include "math_utils.h"
#include "index.h"
#include "parameters.h"
#include "memory_mapper.h"
#include "partition.h"
#ifdef _WINDOWS
#include <xmmintrin.h>
#endif
// block size for reading/ processing large files and matrices in blocks
#define BLOCK_SIZE 5000000
// #define SAVE_INFLATED_PQ true
template <typename T>
void gen_random_slice(const std::string base_file, const std::string output_prefix, double sampling_rate)
{
size_t read_blk_size = 64 * 1024 * 1024;
cached_ifstream base_reader(base_file.c_str(), read_blk_size);
std::ofstream sample_writer(std::string(output_prefix + "_data.bin").c_str(), std::ios::binary);
std::ofstream sample_id_writer(std::string(output_prefix + "_ids.bin").c_str(), std::ios::binary);
std::random_device rd; // Will be used to obtain a seed for the random number engine
auto x = rd();
std::mt19937 generator(x); // Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<float> distribution(0, 1);
size_t npts, nd;
uint32_t npts_u32, nd_u32;
uint32_t num_sampled_pts_u32 = 0;
uint32_t one_const = 1;
base_reader.read((char *)&npts_u32, sizeof(uint32_t));
base_reader.read((char *)&nd_u32, sizeof(uint32_t));
diskann::cout << "Loading base " << base_file << ". #points: " << npts_u32 << ". #dim: " << nd_u32 << "."
<< std::endl;
sample_writer.write((char *)&num_sampled_pts_u32, sizeof(uint32_t));
sample_writer.write((char *)&nd_u32, sizeof(uint32_t));
sample_id_writer.write((char *)&num_sampled_pts_u32, sizeof(uint32_t));
sample_id_writer.write((char *)&one_const, sizeof(uint32_t));
npts = npts_u32;
nd = nd_u32;
std::unique_ptr<T[]> cur_row = std::make_unique<T[]>(nd);
for (size_t i = 0; i < npts; i++)
{
base_reader.read((char *)cur_row.get(), sizeof(T) * nd);
float sample = distribution(generator);
if (sample < sampling_rate)
{
sample_writer.write((char *)cur_row.get(), sizeof(T) * nd);
uint32_t cur_i_u32 = (uint32_t)i;
sample_id_writer.write((char *)&cur_i_u32, sizeof(uint32_t));
num_sampled_pts_u32++;
}
}
sample_writer.seekp(0, std::ios::beg);
sample_writer.write((char *)&num_sampled_pts_u32, sizeof(uint32_t));
sample_id_writer.seekp(0, std::ios::beg);
sample_id_writer.write((char *)&num_sampled_pts_u32, sizeof(uint32_t));
sample_writer.close();
sample_id_writer.close();
diskann::cout << "Wrote " << num_sampled_pts_u32 << " points to sample file: " << output_prefix + "_data.bin"
<< std::endl;
}
// streams data from the file, and samples each vector with probability p_val
// and returns a matrix of size slice_size* ndims as floating point type.
// the slice_size and ndims are set inside the function.
/***********************************
* Reimplement using gen_random_slice(const T* inputdata,...)
************************************/
template <typename T>
void gen_random_slice(const std::string data_file, double p_val, float *&sampled_data, size_t &slice_size,
size_t &ndims)
{
size_t npts;
uint32_t npts32, ndims32;
std::vector<std::vector<float>> sampled_vectors;
// amount to read in one shot
size_t read_blk_size = 64 * 1024 * 1024;
// create cached reader + writer
cached_ifstream base_reader(data_file.c_str(), read_blk_size);
// metadata: npts, ndims
base_reader.read((char *)&npts32, sizeof(uint32_t));
base_reader.read((char *)&ndims32, sizeof(uint32_t));
npts = npts32;
ndims = ndims32;
std::unique_ptr<T[]> cur_vector_T = std::make_unique<T[]>(ndims);
p_val = p_val < 1 ? p_val : 1;
std::random_device rd; // Will be used to obtain a seed for the random number
size_t x = rd();
std::mt19937 generator((uint32_t)x);
std::uniform_real_distribution<float> distribution(0, 1);
for (size_t i = 0; i < npts; i++)
{
base_reader.read((char *)cur_vector_T.get(), ndims * sizeof(T));
float rnd_val = distribution(generator);
if (rnd_val < p_val)
{
std::vector<float> cur_vector_float;
for (size_t d = 0; d < ndims; d++)
cur_vector_float.push_back(cur_vector_T[d]);
sampled_vectors.push_back(cur_vector_float);
}
}
slice_size = sampled_vectors.size();
sampled_data = new float[slice_size * ndims];
for (size_t i = 0; i < slice_size; i++)
{
for (size_t j = 0; j < ndims; j++)
{
sampled_data[i * ndims + j] = sampled_vectors[i][j];
}
}
}
// same as above, but samples from the matrix inputdata instead of a file of
// npts*ndims to return sampled_data of size slice_size*ndims.
template <typename T>
void gen_random_slice(const T *inputdata, size_t npts, size_t ndims, double p_val, float *&sampled_data,
size_t &slice_size)
{
std::vector<std::vector<float>> sampled_vectors;
const T *cur_vector_T;
p_val = p_val < 1 ? p_val : 1;
std::random_device rd; // Will be used to obtain a seed for the random number engine
size_t x = rd();
std::mt19937 generator((uint32_t)x); // Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<float> distribution(0, 1);
for (size_t i = 0; i < npts; i++)
{
cur_vector_T = inputdata + ndims * i;
float rnd_val = distribution(generator);
if (rnd_val < p_val)
{
std::vector<float> cur_vector_float;
for (size_t d = 0; d < ndims; d++)
cur_vector_float.push_back(cur_vector_T[d]);
sampled_vectors.push_back(cur_vector_float);
}
}
slice_size = sampled_vectors.size();
sampled_data = new float[slice_size * ndims];
for (size_t i = 0; i < slice_size; i++)
{
for (size_t j = 0; j < ndims; j++)
{
sampled_data[i * ndims + j] = sampled_vectors[i][j];
}
}
}
int estimate_cluster_sizes(float *test_data_float, size_t num_test, float *pivots, const size_t num_centers,
const size_t test_dim, const size_t k_base, std::vector<size_t> &cluster_sizes)
{
cluster_sizes.clear();
size_t *shard_counts = new size_t[num_centers];
for (size_t i = 0; i < num_centers; i++)
{
shard_counts[i] = 0;
}
size_t block_size = num_test <= BLOCK_SIZE ? num_test : BLOCK_SIZE;
uint32_t *block_closest_centers = new uint32_t[block_size * k_base];
float *block_data_float;
size_t num_blocks = DIV_ROUND_UP(num_test, block_size);
for (size_t block = 0; block < num_blocks; block++)
{
size_t start_id = block * block_size;
size_t end_id = (std::min)((block + 1) * block_size, num_test);
size_t cur_blk_size = end_id - start_id;
block_data_float = test_data_float + start_id * test_dim;
math_utils::compute_closest_centers(block_data_float, cur_blk_size, test_dim, pivots, num_centers, k_base,
block_closest_centers);
for (size_t p = 0; p < cur_blk_size; p++)
{
for (size_t p1 = 0; p1 < k_base; p1++)
{
size_t shard_id = block_closest_centers[p * k_base + p1];
shard_counts[shard_id]++;
}
}
}
diskann::cout << "Estimated cluster sizes: ";
for (size_t i = 0; i < num_centers; i++)
{
uint32_t cur_shard_count = (uint32_t)shard_counts[i];
cluster_sizes.push_back((size_t)cur_shard_count);
diskann::cout << cur_shard_count << " ";
}
diskann::cout << std::endl;
delete[] shard_counts;
delete[] block_closest_centers;
return 0;
}
template <typename T>
int shard_data_into_clusters(const std::string data_file, float *pivots, const size_t num_centers, const size_t dim,
const size_t k_base, std::string prefix_path)
{
size_t read_blk_size = 64 * 1024 * 1024;
// uint64_t write_blk_size = 64 * 1024 * 1024;
// create cached reader + writer
cached_ifstream base_reader(data_file, read_blk_size);
uint32_t npts32;
uint32_t basedim32;
base_reader.read((char *)&npts32, sizeof(uint32_t));
base_reader.read((char *)&basedim32, sizeof(uint32_t));
size_t num_points = npts32;
if (basedim32 != dim)
{
diskann::cout << "Error. dimensions dont match for train set and base set" << std::endl;
return -1;
}
std::unique_ptr<size_t[]> shard_counts = std::make_unique<size_t[]>(num_centers);
std::vector<std::ofstream> shard_data_writer(num_centers);
std::vector<std::ofstream> shard_idmap_writer(num_centers);
uint32_t dummy_size = 0;
uint32_t const_one = 1;
for (size_t i = 0; i < num_centers; i++)
{
std::string data_filename = prefix_path + "_subshard-" + std::to_string(i) + ".bin";
std::string idmap_filename = prefix_path + "_subshard-" + std::to_string(i) + "_ids_uint32.bin";
shard_data_writer[i] = std::ofstream(data_filename.c_str(), std::ios::binary);
shard_idmap_writer[i] = std::ofstream(idmap_filename.c_str(), std::ios::binary);
shard_data_writer[i].write((char *)&dummy_size, sizeof(uint32_t));
shard_data_writer[i].write((char *)&basedim32, sizeof(uint32_t));
shard_idmap_writer[i].write((char *)&dummy_size, sizeof(uint32_t));
shard_idmap_writer[i].write((char *)&const_one, sizeof(uint32_t));
shard_counts[i] = 0;
}
size_t block_size = num_points <= BLOCK_SIZE ? num_points : BLOCK_SIZE;
std::unique_ptr<uint32_t[]> block_closest_centers = std::make_unique<uint32_t[]>(block_size * k_base);
std::unique_ptr<T[]> block_data_T = std::make_unique<T[]>(block_size * dim);
std::unique_ptr<float[]> block_data_float = std::make_unique<float[]>(block_size * dim);
size_t num_blocks = DIV_ROUND_UP(num_points, block_size);
for (size_t block = 0; block < num_blocks; block++)
{
size_t start_id = block * block_size;
size_t end_id = (std::min)((block + 1) * block_size, num_points);
size_t cur_blk_size = end_id - start_id;
base_reader.read((char *)block_data_T.get(), sizeof(T) * (cur_blk_size * dim));
diskann::convert_types<T, float>(block_data_T.get(), block_data_float.get(), cur_blk_size, dim);
math_utils::compute_closest_centers(block_data_float.get(), cur_blk_size, dim, pivots, num_centers, k_base,
block_closest_centers.get());
for (size_t p = 0; p < cur_blk_size; p++)
{
for (size_t p1 = 0; p1 < k_base; p1++)
{
size_t shard_id = block_closest_centers[p * k_base + p1];
uint32_t original_point_map_id = (uint32_t)(start_id + p);
shard_data_writer[shard_id].write((char *)(block_data_T.get() + p * dim), sizeof(T) * dim);
shard_idmap_writer[shard_id].write((char *)&original_point_map_id, sizeof(uint32_t));
shard_counts[shard_id]++;
}
}
}
size_t total_count = 0;
diskann::cout << "Actual shard sizes: " << std::flush;
for (size_t i = 0; i < num_centers; i++)
{
uint32_t cur_shard_count = (uint32_t)shard_counts[i];
total_count += cur_shard_count;
diskann::cout << cur_shard_count << " ";
shard_data_writer[i].seekp(0);
shard_data_writer[i].write((char *)&cur_shard_count, sizeof(uint32_t));
shard_data_writer[i].close();
shard_idmap_writer[i].seekp(0);
shard_idmap_writer[i].write((char *)&cur_shard_count, sizeof(uint32_t));
shard_idmap_writer[i].close();
}
diskann::cout << "\n Partitioned " << num_points << " with replication factor " << k_base << " to get "
<< total_count << " points across " << num_centers << " shards " << std::endl;
return 0;
}
// useful for partitioning large dataset. we first generate only the IDS for
// each shard, and retrieve the actual vectors on demand.
template <typename T>
int shard_data_into_clusters_only_ids(const std::string data_file, float *pivots, const size_t num_centers,
const size_t dim, const size_t k_base, std::string prefix_path)
{
size_t read_blk_size = 64 * 1024 * 1024;
// uint64_t write_blk_size = 64 * 1024 * 1024;
// create cached reader + writer
cached_ifstream base_reader(data_file, read_blk_size);
uint32_t npts32;
uint32_t basedim32;
base_reader.read((char *)&npts32, sizeof(uint32_t));
base_reader.read((char *)&basedim32, sizeof(uint32_t));
size_t num_points = npts32;
if (basedim32 != dim)
{
diskann::cout << "Error. dimensions dont match for train set and base set" << std::endl;
return -1;
}
std::unique_ptr<size_t[]> shard_counts = std::make_unique<size_t[]>(num_centers);
std::vector<std::ofstream> shard_idmap_writer(num_centers);
uint32_t dummy_size = 0;
uint32_t const_one = 1;
for (size_t i = 0; i < num_centers; i++)
{
std::string idmap_filename = prefix_path + "_subshard-" + std::to_string(i) + "_ids_uint32.bin";
shard_idmap_writer[i] = std::ofstream(idmap_filename.c_str(), std::ios::binary);
shard_idmap_writer[i].write((char *)&dummy_size, sizeof(uint32_t));
shard_idmap_writer[i].write((char *)&const_one, sizeof(uint32_t));
shard_counts[i] = 0;
}
size_t block_size = num_points <= BLOCK_SIZE ? num_points : BLOCK_SIZE;
std::unique_ptr<uint32_t[]> block_closest_centers = std::make_unique<uint32_t[]>(block_size * k_base);
std::unique_ptr<T[]> block_data_T = std::make_unique<T[]>(block_size * dim);
std::unique_ptr<float[]> block_data_float = std::make_unique<float[]>(block_size * dim);
size_t num_blocks = DIV_ROUND_UP(num_points, block_size);
for (size_t block = 0; block < num_blocks; block++)
{
size_t start_id = block * block_size;
size_t end_id = (std::min)((block + 1) * block_size, num_points);
size_t cur_blk_size = end_id - start_id;
base_reader.read((char *)block_data_T.get(), sizeof(T) * (cur_blk_size * dim));
diskann::convert_types<T, float>(block_data_T.get(), block_data_float.get(), cur_blk_size, dim);
math_utils::compute_closest_centers(block_data_float.get(), cur_blk_size, dim, pivots, num_centers, k_base,
block_closest_centers.get());
for (size_t p = 0; p < cur_blk_size; p++)
{
for (size_t p1 = 0; p1 < k_base; p1++)
{
size_t shard_id = block_closest_centers[p * k_base + p1];
uint32_t original_point_map_id = (uint32_t)(start_id + p);
shard_idmap_writer[shard_id].write((char *)&original_point_map_id, sizeof(uint32_t));
shard_counts[shard_id]++;
}
}
}
size_t total_count = 0;
diskann::cout << "Actual shard sizes: " << std::flush;
for (size_t i = 0; i < num_centers; i++)
{
uint32_t cur_shard_count = (uint32_t)shard_counts[i];
total_count += cur_shard_count;
diskann::cout << cur_shard_count << " ";
shard_idmap_writer[i].seekp(0);
shard_idmap_writer[i].write((char *)&cur_shard_count, sizeof(uint32_t));
shard_idmap_writer[i].close();
}
diskann::cout << "\n Partitioned " << num_points << " with replication factor " << k_base << " to get "
<< total_count << " points across " << num_centers << " shards " << std::endl;
return 0;
}
template <typename T>
int retrieve_shard_data_from_ids(const std::string data_file, std::string idmap_filename, std::string data_filename)
{
size_t read_blk_size = 64 * 1024 * 1024;
// uint64_t write_blk_size = 64 * 1024 * 1024;
// create cached reader + writer
cached_ifstream base_reader(data_file, read_blk_size);
uint32_t npts32;
uint32_t basedim32;
base_reader.read((char *)&npts32, sizeof(uint32_t));
base_reader.read((char *)&basedim32, sizeof(uint32_t));
size_t num_points = npts32;
size_t dim = basedim32;
uint32_t dummy_size = 0;
std::ofstream shard_data_writer(data_filename.c_str(), std::ios::binary);
shard_data_writer.write((char *)&dummy_size, sizeof(uint32_t));
shard_data_writer.write((char *)&basedim32, sizeof(uint32_t));
uint32_t *shard_ids;
uint64_t shard_size, tmp;
diskann::load_bin<uint32_t>(idmap_filename, shard_ids, shard_size, tmp);
uint32_t cur_pos = 0;
uint32_t num_written = 0;
std::cout << "Shard has " << shard_size << " points" << std::endl;
size_t block_size = num_points <= BLOCK_SIZE ? num_points : BLOCK_SIZE;
std::unique_ptr<T[]> block_data_T = std::make_unique<T[]>(block_size * dim);
size_t num_blocks = DIV_ROUND_UP(num_points, block_size);
for (size_t block = 0; block < num_blocks; block++)
{
size_t start_id = block * block_size;
size_t end_id = (std::min)((block + 1) * block_size, num_points);
size_t cur_blk_size = end_id - start_id;
base_reader.read((char *)block_data_T.get(), sizeof(T) * (cur_blk_size * dim));
for (size_t p = 0; p < cur_blk_size; p++)
{
uint32_t original_point_map_id = (uint32_t)(start_id + p);
if (cur_pos == shard_size)
break;
if (original_point_map_id == shard_ids[cur_pos])
{
cur_pos++;
shard_data_writer.write((char *)(block_data_T.get() + p * dim), sizeof(T) * dim);
num_written++;
}
}
if (cur_pos == shard_size)
break;
}
diskann::cout << "Written file with " << num_written << " points" << std::endl;
shard_data_writer.seekp(0);
shard_data_writer.write((char *)&num_written, sizeof(uint32_t));
shard_data_writer.close();
delete[] shard_ids;
return 0;
}
// partitions a large base file into many shards using k-means hueristic
// on a random sample generated using sampling_rate probability. After this, it
// assignes each base point to the closest k_base nearest centers and creates
// the shards.
// The total number of points across all shards will be k_base * num_points.
template <typename T>
int partition(const std::string data_file, const float sampling_rate, size_t num_parts, size_t max_k_means_reps,
const std::string prefix_path, size_t k_base)
{
size_t train_dim;
size_t num_train;
float *train_data_float;
gen_random_slice<T>(data_file, sampling_rate, train_data_float, num_train, train_dim);
float *pivot_data;
std::string cur_file = std::string(prefix_path);
std::string output_file;
// kmeans_partitioning on training data
// cur_file = cur_file + "_kmeans_partitioning-" +
// std::to_string(num_parts);
output_file = cur_file + "_centroids.bin";
pivot_data = new float[num_parts * train_dim];
// Process Global k-means for kmeans_partitioning Step
diskann::cout << "Processing global k-means (kmeans_partitioning Step)" << std::endl;
kmeans::kmeanspp_selecting_pivots(train_data_float, num_train, train_dim, pivot_data, num_parts);
kmeans::run_lloyds(train_data_float, num_train, train_dim, pivot_data, num_parts, max_k_means_reps, NULL, NULL);
diskann::cout << "Saving global k-center pivots" << std::endl;
diskann::save_bin<float>(output_file.c_str(), pivot_data, (size_t)num_parts, train_dim);
// now pivots are ready. need to stream base points and assign them to
// closest clusters.
shard_data_into_clusters<T>(data_file, pivot_data, num_parts, train_dim, k_base, prefix_path);
delete[] pivot_data;
delete[] train_data_float;
return 0;
}
template <typename T>
int partition_with_ram_budget(const std::string data_file, const double sampling_rate, double ram_budget,
size_t graph_degree, const std::string prefix_path, size_t k_base)
{
size_t train_dim;
size_t num_train;
float *train_data_float;
size_t max_k_means_reps = 10;
int num_parts = 3;
bool fit_in_ram = false;
gen_random_slice<T>(data_file, sampling_rate, train_data_float, num_train, train_dim);
size_t test_dim;
size_t num_test;
float *test_data_float;
gen_random_slice<T>(data_file, sampling_rate, test_data_float, num_test, test_dim);
float *pivot_data = nullptr;
std::string cur_file = std::string(prefix_path);
std::string output_file;
// kmeans_partitioning on training data
// cur_file = cur_file + "_kmeans_partitioning-" +
// std::to_string(num_parts);
output_file = cur_file + "_centroids.bin";
while (!fit_in_ram)
{
fit_in_ram = true;
double max_ram_usage = 0;
if (pivot_data != nullptr)
delete[] pivot_data;
pivot_data = new float[num_parts * train_dim];
// Process Global k-means for kmeans_partitioning Step
diskann::cout << "Processing global k-means (kmeans_partitioning Step)" << std::endl;
kmeans::kmeanspp_selecting_pivots(train_data_float, num_train, train_dim, pivot_data, num_parts);
kmeans::run_lloyds(train_data_float, num_train, train_dim, pivot_data, num_parts, max_k_means_reps, NULL, NULL);
// now pivots are ready. need to stream base points and assign them to
// closest clusters.
std::vector<size_t> cluster_sizes;
estimate_cluster_sizes(test_data_float, num_test, pivot_data, num_parts, train_dim, k_base, cluster_sizes);
for (auto &p : cluster_sizes)
{
// to account for the fact that p is the size of the shard over the
// testing sample.
p = (uint64_t)(p / sampling_rate);
double cur_shard_ram_estimate =
diskann::estimate_ram_usage(p, (uint32_t)train_dim, sizeof(T), (uint32_t)graph_degree);
if (cur_shard_ram_estimate > max_ram_usage)
max_ram_usage = cur_shard_ram_estimate;
}
diskann::cout << "With " << num_parts
<< " parts, max estimated RAM usage: " << max_ram_usage / (1024 * 1024 * 1024)
<< "GB, budget given is " << ram_budget << std::endl;
if (max_ram_usage > 1024 * 1024 * 1024 * ram_budget)
{
fit_in_ram = false;
num_parts += 2;
}
}
diskann::cout << "Saving global k-center pivots" << std::endl;
diskann::save_bin<float>(output_file.c_str(), pivot_data, (size_t)num_parts, train_dim);
shard_data_into_clusters_only_ids<T>(data_file, pivot_data, num_parts, train_dim, k_base, prefix_path);
delete[] pivot_data;
delete[] train_data_float;
delete[] test_data_float;
return num_parts;
}
// Instantations of supported templates
template void DISKANN_DLLEXPORT gen_random_slice<int8_t>(const std::string base_file, const std::string output_prefix,
double sampling_rate);
template void DISKANN_DLLEXPORT gen_random_slice<uint8_t>(const std::string base_file, const std::string output_prefix,
double sampling_rate);
template void DISKANN_DLLEXPORT gen_random_slice<float>(const std::string base_file, const std::string output_prefix,
double sampling_rate);
template void DISKANN_DLLEXPORT gen_random_slice<float>(const float *inputdata, size_t npts, size_t ndims, double p_val,
float *&sampled_data, size_t &slice_size);
template void DISKANN_DLLEXPORT gen_random_slice<uint8_t>(const uint8_t *inputdata, size_t npts, size_t ndims,
double p_val, float *&sampled_data, size_t &slice_size);
template void DISKANN_DLLEXPORT gen_random_slice<int8_t>(const int8_t *inputdata, size_t npts, size_t ndims,
double p_val, float *&sampled_data, size_t &slice_size);
template void DISKANN_DLLEXPORT gen_random_slice<float>(const std::string data_file, double p_val, float *&sampled_data,
size_t &slice_size, size_t &ndims);
template void DISKANN_DLLEXPORT gen_random_slice<uint8_t>(const std::string data_file, double p_val,
float *&sampled_data, size_t &slice_size, size_t &ndims);
template void DISKANN_DLLEXPORT gen_random_slice<int8_t>(const std::string data_file, double p_val,
float *&sampled_data, size_t &slice_size, size_t &ndims);
template DISKANN_DLLEXPORT int partition<int8_t>(const std::string data_file, const float sampling_rate,
size_t num_centers, size_t max_k_means_reps,
const std::string prefix_path, size_t k_base);
template DISKANN_DLLEXPORT int partition<uint8_t>(const std::string data_file, const float sampling_rate,
size_t num_centers, size_t max_k_means_reps,
const std::string prefix_path, size_t k_base);
template DISKANN_DLLEXPORT int partition<float>(const std::string data_file, const float sampling_rate,
size_t num_centers, size_t max_k_means_reps,
const std::string prefix_path, size_t k_base);
template DISKANN_DLLEXPORT int partition_with_ram_budget<int8_t>(const std::string data_file,
const double sampling_rate, double ram_budget,
size_t graph_degree, const std::string prefix_path,
size_t k_base);
template DISKANN_DLLEXPORT int partition_with_ram_budget<uint8_t>(const std::string data_file,
const double sampling_rate, double ram_budget,
size_t graph_degree, const std::string prefix_path,
size_t k_base);
template DISKANN_DLLEXPORT int partition_with_ram_budget<float>(const std::string data_file, const double sampling_rate,
double ram_budget, size_t graph_degree,
const std::string prefix_path, size_t k_base);
template DISKANN_DLLEXPORT int retrieve_shard_data_from_ids<float>(const std::string data_file,
std::string idmap_filename,
std::string data_filename);
template DISKANN_DLLEXPORT int retrieve_shard_data_from_ids<uint8_t>(const std::string data_file,
std::string idmap_filename,
std::string data_filename);
template DISKANN_DLLEXPORT int retrieve_shard_data_from_ids<int8_t>(const std::string data_file,
std::string idmap_filename,
std::string data_filename);