-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathClustering.cpp
More file actions
759 lines (652 loc) · 23.4 KB
/
Clustering.cpp
File metadata and controls
759 lines (652 loc) · 23.4 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
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// -*- c++ -*-
#include <faiss/Clustering.h>
#include <faiss/VectorTransform.h>
#include <faiss/impl/AuxIndexStructures.h>
#include <chrono>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <omp.h>
#include <faiss/IndexFlat.h>
#include <faiss/impl/FaissAssert.h>
#include <faiss/impl/kmeans1d.h>
#include <faiss/utils/distances.h>
#include <faiss/utils/random.h>
#include <faiss/utils/utils.h>
namespace faiss {
Clustering::Clustering(int d_, int k_) : d(d_), k(k_) {}
Clustering::Clustering(int d_, int k_, const ClusteringParameters& cp)
: ClusteringParameters(cp), d(d_), k(k_) {}
void Clustering::post_process_centroids() {
if (spherical) {
fvec_renorm_L2(d, k, centroids.data());
}
if (int_centroids) {
for (size_t i = 0; i < centroids.size(); i++) {
centroids[i] = roundf(centroids[i]);
}
}
}
void Clustering::train(
idx_t nx,
const float* x_in,
Index& index,
const float* weights) {
train_encoded(
nx,
reinterpret_cast<const uint8_t*>(x_in),
nullptr,
index,
weights);
}
namespace {
uint64_t get_actual_rng_seed(const int seed) {
return (seed >= 0)
? seed
: static_cast<uint64_t>(std::chrono::high_resolution_clock::now()
.time_since_epoch()
.count());
}
idx_t subsample_training_set(
const Clustering& clus,
idx_t nx,
const uint8_t* x,
size_t line_size,
const float* weights,
uint8_t** x_out,
float** weights_out) {
if (clus.verbose) {
printf("Sampling a subset of %zd / %" PRId64 " for training\n",
clus.k * clus.max_points_per_centroid,
nx);
}
const uint64_t actual_seed = get_actual_rng_seed(clus.seed);
std::vector<int> perm;
if (clus.use_faster_subsampling) {
// use subsampling with splitmix64 rng
SplitMix64RandomGenerator rng(actual_seed);
const idx_t new_nx = clus.k * clus.max_points_per_centroid;
perm.resize(new_nx);
for (idx_t i = 0; i < new_nx; i++) {
perm[i] = rng.rand_int(static_cast<int>(nx));
}
} else {
// use subsampling with a default std rng
perm.resize(nx);
rand_perm(perm.data(), nx, actual_seed);
}
nx = clus.k * clus.max_points_per_centroid;
uint8_t* x_new = new uint8_t[nx * line_size];
*x_out = x_new;
// might be worth omp-ing as well
for (idx_t i = 0; i < nx; i++) {
memcpy(x_new + i * line_size, x + perm[i] * line_size, line_size);
}
if (weights) {
float* weights_new = new float[nx];
for (idx_t i = 0; i < nx; i++) {
weights_new[i] = weights[perm[i]];
}
*weights_out = weights_new;
} else {
*weights_out = nullptr;
}
return nx;
}
/** compute centroids as (weighted) sum of training points
*
* @param x training vectors, size n * code_size (from codec)
* @param codec how to decode the vectors (if NULL then cast to float*)
* @param weights per-training vector weight, size n (or NULL)
* @param assign nearest centroid for each training vector, size n
* @param k_frozen do not update the k_frozen first centroids
* @param centroids centroid vectors (output only), size k * d
* @param hassign histogram of assignments per centroid (size k),
* should be 0 on input
*
*/
void compute_centroids(
size_t d,
size_t k,
size_t n,
size_t k_frozen,
const uint8_t* x,
const Index* codec,
const int64_t* assign,
const float* weights,
float* hassign,
float* centroids) {
k -= k_frozen;
centroids += k_frozen * d;
memset(centroids, 0, sizeof(*centroids) * d * k);
size_t line_size = codec ? codec->sa_code_size() : d * sizeof(float);
#pragma omp parallel
{
int nt = omp_get_num_threads();
int rank = omp_get_thread_num();
// this thread is taking care of centroids c0:c1
size_t c0 = (k * rank) / nt;
size_t c1 = (k * (rank + 1)) / nt;
std::vector<float> decode_buffer(d);
for (size_t i = 0; i < n; i++) {
int64_t ci = assign[i];
FAISS_THROW_IF_NOT_MSG(
ci >= 0 && ci < k + k_frozen, "invalid cluster assignment");
ci -= k_frozen;
if (ci >= static_cast<int64_t>(c0) &&
ci < static_cast<int64_t>(c1)) {
float* c = centroids + ci * d;
const float* xi;
if (!codec) {
xi = reinterpret_cast<const float*>(x + i * line_size);
} else {
float* xif = decode_buffer.data();
codec->sa_decode(1, x + i * line_size, xif);
xi = xif;
}
if (weights) {
float w = weights[i];
hassign[ci] += w;
for (size_t j = 0; j < d; j++) {
c[j] += xi[j] * w;
}
} else {
hassign[ci] += 1.0;
for (size_t j = 0; j < d; j++) {
c[j] += xi[j];
}
}
}
}
}
#pragma omp parallel for
for (idx_t ci = 0; ci < static_cast<idx_t>(k); ci++) {
if (hassign[ci] == 0) {
continue;
}
float norm = 1 / hassign[ci];
float* c = centroids + ci * d;
for (size_t j = 0; j < d; j++) {
c[j] *= norm;
}
}
}
// a bit above machine epsilon for float16
#define EPS (1 / 1024.)
/** Handle empty clusters by splitting larger ones.
*
* It works by slightly changing the centroids to make 2 clusters from
* a single one. Takes the same arguments as compute_centroids.
*
* @return nb of splitting operations (larger is worse)
*/
int split_clusters(
size_t d,
size_t k,
size_t n,
size_t k_frozen,
float* hassign,
float* centroids) {
k -= k_frozen;
centroids += k_frozen * d;
/* Take care of void clusters */
size_t nsplit = 0;
RandomGenerator rng(1234);
for (size_t ci = 0; ci < k; ci++) {
if (hassign[ci] == 0) { /* need to redefine a centroid */
size_t cj;
for (cj = 0; true; cj = (cj + 1) % k) {
/* probability to pick this cluster for split */
float p = (hassign[cj] - 1.0) / (float)(n - k);
float r = rng.rand_float();
if (r < p) {
break; /* found our cluster to be split */
}
}
memcpy(centroids + ci * d,
centroids + cj * d,
sizeof(*centroids) * d);
/* small symmetric perturbation */
for (size_t j = 0; j < d; j++) {
if (j % 2 == 0) {
centroids[ci * d + j] *= 1 + EPS;
centroids[cj * d + j] *= 1 - EPS;
} else {
centroids[ci * d + j] *= 1 - EPS;
centroids[cj * d + j] *= 1 + EPS;
}
}
/* assume even split of the cluster */
hassign[ci] = hassign[cj] / 2;
hassign[cj] -= hassign[ci];
nsplit++;
}
}
return static_cast<int>(nsplit);
}
} // namespace
void Clustering::train_encoded(
idx_t nx,
const uint8_t* x_in,
const Index* codec,
Index& index,
const float* weights) {
FAISS_THROW_IF_NOT_FMT(
nx >= static_cast<idx_t>(k),
"Number of training points (%" PRId64
") should be at least "
"as large as number of clusters (%zd)",
nx,
k);
FAISS_THROW_IF_NOT_FMT(
(!codec || static_cast<size_t>(codec->d) == d),
"Codec dimension %d not the same as data dimension %d",
int(codec->d),
int(d));
FAISS_THROW_IF_NOT_FMT(
static_cast<size_t>(index.d) == d,
"Index dimension %d not the same as data dimension %d",
int(index.d),
int(d));
double t0 = getmillisecs();
if (!codec && check_input_data_for_NaNs) {
// Check for NaNs in input data. Normally it is the user's
// responsibility, but it may spare us some hard-to-debug
// reports.
const float* x = reinterpret_cast<const float*>(x_in);
for (size_t i = 0; i < nx * d; i++) {
FAISS_THROW_IF_NOT_MSG(
std::isfinite(x[i]), "input contains NaN's or Inf's");
}
}
const uint8_t* x = x_in;
std::unique_ptr<uint8_t[]> del1;
std::unique_ptr<float[]> del3;
size_t line_size = codec ? codec->sa_code_size() : sizeof(float) * d;
if (static_cast<size_t>(nx) > k * max_points_per_centroid) {
uint8_t* x_new;
float* weights_new;
nx = subsample_training_set(
*this, nx, x, line_size, weights, &x_new, &weights_new);
del1.reset(x_new);
x = x_new;
del3.reset(weights_new);
weights = weights_new;
} else if (static_cast<size_t>(nx) < k * min_points_per_centroid) {
fprintf(stderr,
"WARNING clustering %" PRId64
" points to %zd centroids: "
"please provide at least %" PRId64 " training points\n",
nx,
k,
idx_t(k) * min_points_per_centroid);
}
if (static_cast<size_t>(nx) == k) {
// this is a corner case, just copy training set to clusters
if (verbose) {
printf("Number of training points (%" PRId64
") same as number of "
"clusters, just copying\n",
nx);
}
centroids.resize(d * k);
if (!codec) {
memcpy(centroids.data(), x_in, sizeof(float) * d * k);
} else {
codec->sa_decode(nx, x_in, centroids.data());
}
// one fake iteration...
ClusteringIterationStats stats = {0.0, 0.0, 0.0, 1.0, 0};
iteration_stats.push_back(stats);
index.reset();
index.add(k, centroids.data());
return;
}
if (verbose) {
printf("Clustering %" PRId64
" points in %zdD to %zd clusters, "
"redo %d times, %d iterations\n",
nx,
d,
k,
nredo,
niter);
if (codec) {
printf("Input data encoded in %zd bytes per vector\n",
codec->sa_code_size());
}
}
std::unique_ptr<idx_t[]> assign(new idx_t[nx]);
std::unique_ptr<float[]> dis(new float[nx]);
// remember best iteration for redo
bool lower_is_better = !is_similarity_metric(index.metric_type);
float best_obj = lower_is_better ? HUGE_VALF : -HUGE_VALF;
std::vector<ClusteringIterationStats> best_iteration_stats;
std::vector<float> best_centroids;
// support input centroids
FAISS_THROW_IF_NOT_MSG(
centroids.size() % d == 0,
"size of provided input centroids not a multiple of dimension");
size_t n_input_centroids = centroids.size() / d;
if (verbose && n_input_centroids > 0) {
printf(" Using %zd centroids provided as input (%sfrozen)\n",
n_input_centroids,
frozen_centroids ? "" : "not ");
}
double t_search_tot = 0;
if (verbose) {
printf(" Preprocessing in %.2f s\n", (getmillisecs() - t0) / 1000.);
}
t0 = getmillisecs();
// initialize seed
const uint64_t actual_seed = get_actual_rng_seed(seed);
// temporary buffer to decode vectors during the optimization
std::vector<float> decode_buffer(codec ? d * decode_block_size : 0);
for (int redo = 0; redo < nredo; redo++) {
if (verbose && nredo > 1) {
printf("Outer iteration %d / %d\n", redo, nredo);
}
// initialize centroids using the selected method
centroids.resize(d * k);
size_t k_to_init = k - n_input_centroids;
if (k_to_init > 0) {
// Fast path for RANDOM initialization - preserves exact original
// behavior
if (init_method == ClusteringInitMethod::RANDOM) {
std::vector<int> perm(nx);
rand_perm(perm.data(), nx, actual_seed + 1 + redo * 15486557L);
for (size_t i = 0; i < k_to_init; i++) {
if (!codec) {
memcpy(centroids.data() + (n_input_centroids + i) * d,
x + perm[n_input_centroids + i] * line_size,
line_size);
} else {
codec->sa_decode(
1,
x + perm[n_input_centroids + i] * line_size,
centroids.data() + (n_input_centroids + i) * d);
}
}
} else {
// For k-means++ and AFK-MC², we need all vectors decoded
const float* x_float = nullptr;
std::vector<float> x_decoded;
if (!codec) {
x_float = reinterpret_cast<const float*>(x);
} else {
// Decode all vectors for initialization
x_decoded.resize(nx * d);
codec->sa_decode(nx, x, x_decoded.data());
x_float = x_decoded.data();
}
ClusteringInitialization initializer(d, k_to_init);
initializer.method = init_method;
initializer.seed = actual_seed + 1 + redo * 15486557L;
initializer.afkmc2_chain_length = afkmc2_chain_length;
initializer.init_centroids(
nx,
x_float,
centroids.data() + n_input_centroids * d,
n_input_centroids,
n_input_centroids > 0 ? centroids.data() : nullptr);
}
}
post_process_centroids();
// prepare the index
if (index.ntotal != 0) {
index.reset();
}
if (!index.is_trained) {
index.train(k, centroids.data());
}
index.add(k, centroids.data());
// k-means iterations
float obj = 0;
for (int i = 0; i < niter; i++) {
double t0s = getmillisecs();
if (!codec) {
index.search(
nx,
reinterpret_cast<const float*>(x),
1,
dis.get(),
assign.get());
} else {
// search by blocks of decode_block_size vectors
size_t code_size = codec->sa_code_size();
for (size_t i0 = 0; i0 < static_cast<size_t>(nx);
i0 += decode_block_size) {
size_t i1 = i0 + decode_block_size;
if (i1 > static_cast<size_t>(nx)) {
i1 = nx;
}
codec->sa_decode(
i1 - i0, x + code_size * i0, decode_buffer.data());
index.search(
i1 - i0,
decode_buffer.data(),
1,
dis.get() + i0,
assign.get() + i0);
}
}
InterruptCallback::check();
t_search_tot += getmillisecs() - t0s;
// accumulate objective
obj = 0;
for (int j = 0; j < nx; j++) {
obj += dis[j];
}
// update the centroids
std::vector<float> hassign(k);
size_t k_frozen = frozen_centroids ? n_input_centroids : 0;
compute_centroids(
d,
k,
nx,
k_frozen,
x,
codec,
assign.get(),
weights,
hassign.data(),
centroids.data());
int nsplit = split_clusters(
d, k, nx, k_frozen, hassign.data(), centroids.data());
// collect statistics
ClusteringIterationStats stats = {
obj,
(getmillisecs() - t0) / 1000.0,
t_search_tot / 1000,
imbalance_factor(nx, static_cast<int>(k), assign.get()),
nsplit};
iteration_stats.push_back(stats);
if (verbose) {
printf(" Iteration %d (%.2f s, search %.2f s): "
"objective=%g imbalance=%.3f nsplit=%d \r",
i,
stats.time,
stats.time_search,
stats.obj,
stats.imbalance_factor,
nsplit);
fflush(stdout);
}
post_process_centroids();
// add centroids to index for the next iteration (or for output)
index.reset();
if (update_index) {
index.train(k, centroids.data());
}
index.add(k, centroids.data());
InterruptCallback::check();
// Early stopping: if objective didn't change, we've converged.
// Safe to access iteration_stats[size - 2] because we push_back
// above, so size >= i + 1, and when i > 0 we have size >= 2.
if (i > 0) {
float prev_obj =
iteration_stats[iteration_stats.size() - 2].obj;
double change = (prev_obj == 0)
? std::numeric_limits<double>::max()
: std::abs(prev_obj - obj) / std::abs(prev_obj);
if (change >= 0 && change <= early_stop_threshold) {
if (verbose) {
printf("\n Converged at iteration %d: "
"objective did not change\n",
i);
}
break;
}
}
}
if (verbose) {
printf("\n");
}
if (nredo > 1) {
if ((lower_is_better && obj < best_obj) ||
(!lower_is_better && obj > best_obj)) {
if (verbose) {
printf("Objective improved: keep new clusters\n");
}
best_centroids = centroids;
best_iteration_stats = iteration_stats;
best_obj = obj;
}
index.reset();
}
}
if (nredo > 1) {
centroids = best_centroids;
iteration_stats = best_iteration_stats;
index.reset();
index.add(k, best_centroids.data());
}
}
Clustering1D::Clustering1D(int k_) : Clustering(1, k_) {}
Clustering1D::Clustering1D(int k_, const ClusteringParameters& cp)
: Clustering(1, k_, cp) {}
void Clustering1D::train_exact(idx_t n, const float* x) {
const float* xt = x;
std::unique_ptr<uint8_t[]> del;
if (static_cast<size_t>(n) > k * max_points_per_centroid) {
uint8_t* x_new;
float* weights_new;
n = subsample_training_set(
*this,
n,
(uint8_t*)x,
sizeof(float) * d,
nullptr,
&x_new,
&weights_new);
del.reset(x_new);
xt = (float*)x_new;
}
centroids.resize(k);
double uf = kmeans1d(xt, n, k, centroids.data());
ClusteringIterationStats stats = {0.0, 0.0, 0.0, uf, 0};
iteration_stats.push_back(stats);
}
float kmeans_clustering(
size_t d,
size_t n,
size_t k,
const float* x,
float* centroids) {
Clustering clus(static_cast<int>(d), static_cast<int>(k));
clus.verbose = d * n * k > (size_t(1) << 30);
// display logs if > 1Gflop per iteration
IndexFlatL2 index(d);
clus.train(n, x, index);
memcpy(centroids, clus.centroids.data(), sizeof(*centroids) * d * k);
return clus.iteration_stats.back().obj;
}
/******************************************************************************
* ProgressiveDimClustering implementation
******************************************************************************/
ProgressiveDimClusteringParameters::ProgressiveDimClusteringParameters() {
progressive_dim_steps = 10;
apply_pca = true; // seems a good idea to do this by default
niter = 10; // reduce nb of iterations per step
}
Index* ProgressiveDimIndexFactory::operator()(int dim) {
return new IndexFlatL2(dim);
}
ProgressiveDimClustering::ProgressiveDimClustering(int d_, int k_)
: d(d_), k(k_) {}
ProgressiveDimClustering::ProgressiveDimClustering(
int d_,
int k_,
const ProgressiveDimClusteringParameters& cp)
: ProgressiveDimClusteringParameters(cp), d(d_), k(k_) {}
namespace {
void copy_columns(idx_t n, idx_t d1, const float* src, idx_t d2, float* dest) {
idx_t d = std::min(d1, d2);
for (idx_t i = 0; i < n; i++) {
memcpy(dest, src, sizeof(float) * d);
src += d1;
dest += d2;
}
}
} // namespace
void ProgressiveDimClustering::train(
idx_t n,
const float* x,
ProgressiveDimIndexFactory& factory) {
int d_prev = 0;
PCAMatrix pca(static_cast<int>(d), static_cast<int>(d));
std::vector<float> xbuf;
if (apply_pca) {
if (verbose) {
printf("Training PCA transform\n");
}
pca.train(n, x);
if (verbose) {
printf("Apply PCA\n");
}
xbuf.resize(n * d);
pca.apply_noalloc(n, x, xbuf.data());
x = xbuf.data();
}
for (int iter = 0; iter < progressive_dim_steps; iter++) {
int di = int(pow(d, (1. + iter) / progressive_dim_steps));
if (verbose) {
printf("Progressive dim step %d: cluster in dimension %d\n",
iter,
di);
}
std::unique_ptr<Index> clustering_index(factory(di));
Clustering clus(di, static_cast<int>(k), *this);
if (d_prev > 0) {
// copy warm-start centroids (padded with 0s)
clus.centroids.resize(k * di);
copy_columns(
k, d_prev, centroids.data(), di, clus.centroids.data());
}
std::vector<float> xsub(n * di);
copy_columns(n, d, x, di, xsub.data());
clus.train(n, xsub.data(), *clustering_index.get());
centroids = clus.centroids;
iteration_stats.insert(
iteration_stats.end(),
clus.iteration_stats.begin(),
clus.iteration_stats.end());
d_prev = di;
}
if (apply_pca) {
if (verbose) {
printf("Revert PCA transform on centroids\n");
}
std::vector<float> cent_transformed(d * k);
pca.reverse_transform(k, centroids.data(), cent_transformed.data());
cent_transformed.swap(centroids);
}
}
} // namespace faiss