-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Expand file tree
/
Copy pathProductQuantizer.cpp
More file actions
899 lines (787 loc) · 26.6 KB
/
ProductQuantizer.cpp
File metadata and controls
899 lines (787 loc) · 26.6 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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/*
* 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/impl/ProductQuantizer.h>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <memory>
#include <algorithm>
#include <faiss/IndexFlat.h>
#include <faiss/VectorTransform.h>
#include <faiss/impl/FaissAssert.h>
#include <faiss/impl/simd_dispatch.h>
#include <faiss/utils/distances.h>
extern "C" {
/* declare BLAS functions, see http://www.netlib.org/clapack/cblas/ */
int sgemm_(
const char* transa,
const char* transb,
FINTEGER* m,
FINTEGER* n,
FINTEGER* k,
const float* alpha,
const float* a,
FINTEGER* lda,
const float* b,
FINTEGER* ldb,
float* beta,
float* c,
FINTEGER* ldc);
}
namespace faiss {
/*********************************************
* PQ implementation
*********************************************/
ProductQuantizer::ProductQuantizer(size_t d_in, size_t M_in, size_t nbits_in)
: Quantizer(d_in, 0), M(M_in), nbits(nbits_in), assign_index(nullptr) {
set_derived_values();
}
ProductQuantizer::ProductQuantizer() : ProductQuantizer(0, 1, 0) {}
void ProductQuantizer::set_derived_values() {
// quite a few derived values
FAISS_THROW_IF_NOT_MSG(M > 0, "M must be > 0");
FAISS_THROW_IF_NOT_MSG(
d % M == 0,
"The dimension of the vector (d) should be a multiple of the number of subquantizers (M)");
dsub = d / M;
FAISS_THROW_IF_MSG(nbits > 24, "nbits larger than 24 is not practical.");
code_size = (nbits * M + 7) / 8;
ksub = 1 << nbits;
centroids.resize(mul_no_overflow(d, (size_t)ksub, "PQ centroids"));
verbose = false;
train_type = Train_default;
}
void ProductQuantizer::set_params(const float* centroids_, int m) {
memcpy(get_centroids(m, 0),
centroids_,
ksub * dsub * sizeof(centroids_[0]));
}
static void init_hypercube(
int d,
int nbits,
int n,
const float* x,
float* centroids) {
std::vector<float> mean(d);
for (int i = 0; i < n; i++)
for (int j = 0; j < d; j++)
mean[j] += x[i * d + j];
float maxm = 0;
for (int j = 0; j < d; j++) {
mean[j] /= n;
if (fabs(mean[j]) > maxm)
maxm = fabs(mean[j]);
}
for (int i = 0; i < (1 << nbits); i++) {
float* cent = centroids + i * d;
for (int j = 0; j < nbits; j++)
cent[j] = mean[j] + (((i >> j) & 1) ? 1 : -1) * maxm;
for (int j = nbits; j < d; j++)
cent[j] = mean[j];
}
}
static void init_hypercube_pca(
int d,
int nbits,
int n,
const float* x,
float* centroids) {
PCAMatrix pca(d, nbits);
pca.train(n, x);
for (int i = 0; i < (1 << nbits); i++) {
float* cent = centroids + i * d;
for (int j = 0; j < d; j++) {
cent[j] = pca.mean[j];
float f = 1.0;
for (int k = 0; k < nbits; k++)
cent[j] += f * sqrt(pca.eigenvalues[k]) *
(((i >> k) & 1) ? 1 : -1) * pca.PCAMat[j + k * d];
}
}
}
void ProductQuantizer::train(size_t n, const float* x) {
if (train_type != Train_shared) {
train_type_t final_train_type;
final_train_type = train_type;
if (train_type == Train_hypercube ||
train_type == Train_hypercube_pca) {
if (dsub < nbits) {
final_train_type = Train_default;
printf("cannot train hypercube: nbits=%zd > log2(d=%zd)\n",
nbits,
dsub);
}
}
std::unique_ptr<float[]> xslice(new float[n * dsub]);
for (size_t m = 0; m < M; m++) {
for (size_t j = 0; j < n; j++)
memcpy(xslice.get() + j * dsub,
x + j * d + m * dsub,
dsub * sizeof(float));
Clustering clus(dsub, ksub, cp);
// we have some initialization for the centroids
if (final_train_type != Train_default) {
clus.centroids.resize(dsub * ksub);
}
switch (final_train_type) {
case Train_hypercube:
init_hypercube(
dsub,
nbits,
n,
xslice.get(),
clus.centroids.data());
break;
case Train_hypercube_pca:
init_hypercube_pca(
dsub,
nbits,
n,
xslice.get(),
clus.centroids.data());
break;
case Train_hot_start:
memcpy(clus.centroids.data(),
get_centroids(m, 0),
dsub * ksub * sizeof(float));
break;
default:;
}
if (verbose) {
clus.verbose = true;
printf("Training PQ slice %zd/%zd\n", m, M);
}
IndexFlatL2 index(dsub);
clus.train(n, xslice.get(), assign_index ? *assign_index : index);
set_params(clus.centroids.data(), m);
}
} else {
Clustering clus(dsub, ksub, cp);
if (verbose) {
clus.verbose = true;
printf("Training all PQ slices at once\n");
}
IndexFlatL2 index(dsub);
clus.train(n * M, x, assign_index ? *assign_index : index);
for (size_t m = 0; m < M; m++) {
set_params(clus.centroids.data(), m);
}
}
}
namespace {
template <class PQEncoder, SIMDLevel SL>
void compute_1_code(const ProductQuantizer& pq, const float* x, uint8_t* code) {
std::vector<float> distances(pq.ksub);
// It seems to be meaningless to allocate std::vector<float> distances.
// But it is done in order to cope the ineffectiveness of the way
// the compiler generates the code. Basically, doing something like
//
// size_t min_distance = HUGE_VALF;
// size_t idxm = 0;
// for (size_t i = 0; i < N; i++) {
// const float distance = compute_distance(x, y + i * d, d);
// if (distance < min_distance) {
// min_distance = distance;
// idxm = i;
// }
// }
//
// generates significantly more CPU instructions than the baseline
//
// std::vector<float> distances_cached(N);
// for (size_t i = 0; i < N; i++) {
// distances_cached[i] = compute_distance(x, y + i * d, d);
// }
// size_t min_distance = HUGE_VALF;
// size_t idxm = 0;
// for (size_t i = 0; i < N; i++) {
// const float distance = distances_cached[i];
// if (distance < min_distance) {
// min_distance = distance;
// idxm = i;
// }
// }
//
// So, the baseline is faster. This is because of the vectorization.
// I suppose that the branch predictor might affect the performance as well.
// So, the buffer is allocated, but it might be unused in
// manually optimized code. Let's hope that the compiler is smart enough to
// get rid of std::vector allocation in such a case.
PQEncoder encoder(code, pq.nbits);
for (size_t m = 0; m < pq.M; m++) {
const float* xsub = x + m * pq.dsub;
uint64_t idxm = 0;
if (pq.transposed_centroids.empty()) {
// the regular version
idxm = fvec_L2sqr_ny_nearest<SL>(
distances.data(),
xsub,
pq.get_centroids(m, 0),
pq.dsub,
pq.ksub);
} else {
// transposed centroids are available, use'em
idxm = fvec_L2sqr_ny_nearest_y_transposed<SL>(
distances.data(),
xsub,
pq.transposed_centroids.data() + m * pq.ksub,
pq.centroids_sq_lengths.data() + m * pq.ksub,
pq.dsub,
pq.M * pq.ksub,
pq.ksub);
}
encoder.encode(idxm);
}
}
} // namespace
void ProductQuantizer::compute_code(const float* x, uint8_t* code) const {
with_simd_level([&]<SIMDLevel SL>() {
switch (nbits) {
case 8:
compute_1_code<PQEncoder8, SL>(*this, x, code);
break;
case 16:
compute_1_code<PQEncoder16, SL>(*this, x, code);
break;
default:
compute_1_code<PQEncoderGeneric, SL>(*this, x, code);
break;
}
}); // with_simd_level
}
template <class PQDecoder>
void decode(const ProductQuantizer& pq, const uint8_t* code, float* x) {
PQDecoder decoder(code, pq.nbits);
for (size_t m = 0; m < pq.M; m++) {
uint64_t c = decoder.decode();
memcpy(x + m * pq.dsub,
pq.get_centroids(m, c),
sizeof(float) * pq.dsub);
}
}
void ProductQuantizer::decode(const uint8_t* code, float* x) const {
switch (nbits) {
case 8:
faiss::decode<PQDecoder8>(*this, code, x);
break;
case 16:
faiss::decode<PQDecoder16>(*this, code, x);
break;
default:
faiss::decode<PQDecoderGeneric>(*this, code, x);
break;
}
}
void ProductQuantizer::decode(const uint8_t* code, float* x, size_t n) const {
int64_t n_signed = n;
#pragma omp parallel for if (n > 100)
for (int64_t i = 0; i < n_signed; i++) {
this->decode(code + code_size * i, x + d * i);
}
}
void ProductQuantizer::compute_code_from_distance_table(
const float* tab,
uint8_t* code) const {
PQEncoderGeneric encoder(code, nbits);
for (size_t m = 0; m < M; m++) {
float mindis = 1e20;
uint64_t idxm = 0;
/* Find best centroid */
for (size_t j = 0; j < ksub; j++) {
float dis = *tab++;
if (dis < mindis) {
mindis = dis;
idxm = j;
}
}
encoder.encode(idxm);
}
}
void ProductQuantizer::compute_codes_with_assign_index(
const float* x,
uint8_t* codes,
size_t n) {
FAISS_THROW_IF_NOT(
assign_index && static_cast<size_t>(assign_index->d) == dsub);
for (size_t m = 0; m < M; m++) {
assign_index->reset();
assign_index->add(ksub, get_centroids(m, 0));
size_t bs = 65536;
std::unique_ptr<float[]> xslice(new float[bs * dsub]);
std::unique_ptr<idx_t[]> assign(new idx_t[bs]);
for (size_t i0 = 0; i0 < n; i0 += bs) {
size_t i1 = std::min(i0 + bs, n);
for (size_t i = i0; i < i1; i++) {
memcpy(xslice.get() + (i - i0) * dsub,
x + i * d + m * dsub,
dsub * sizeof(float));
}
assign_index->assign(i1 - i0, xslice.get(), assign.get());
if (nbits == 8) {
uint8_t* c = codes + code_size * i0 + m;
for (size_t i = i0; i < i1; i++) {
*c = assign[i - i0];
c += M;
}
} else if (nbits == 16) {
uint16_t* c = (uint16_t*)(codes + code_size * i0 + m * 2);
for (size_t i = i0; i < i1; i++) {
*c = assign[i - i0];
c += M;
}
} else {
for (size_t i = i0; i < i1; ++i) {
uint8_t* c = codes + code_size * i + ((m * nbits) / 8);
uint8_t offset = (m * nbits) % 8;
uint64_t ass = assign[i - i0];
PQEncoderGeneric encoder(c, nbits, offset);
encoder.encode(ass);
}
}
}
}
}
// block size used in ProductQuantizer::compute_codes
int product_quantizer_compute_codes_bs = 256 * 1024;
void ProductQuantizer::compute_codes(const float* x, uint8_t* codes, size_t n)
const {
// process by blocks to avoid using too much RAM
size_t bs = product_quantizer_compute_codes_bs;
if (n > bs) {
for (size_t i0 = 0; i0 < n; i0 += bs) {
size_t i1 = std::min(i0 + bs, n);
compute_codes(x + d * i0, codes + code_size * i0, i1 - i0);
}
return;
}
int64_t n_signed = n;
if (dsub < 16) { // simple direct computation
#pragma omp parallel for
for (int64_t i = 0; i < n_signed; i++)
compute_code(x + i * d, codes + i * code_size);
} else { // worthwhile to use BLAS
std::unique_ptr<float[]> dis_tables(new float[n * ksub * M]);
compute_distance_tables(n, x, dis_tables.get());
#pragma omp parallel for
for (int64_t i = 0; i < n_signed; i++) {
uint8_t* code = codes + i * code_size;
const float* tab = dis_tables.get() + i * ksub * M;
compute_code_from_distance_table(tab, code);
}
}
}
void ProductQuantizer::compute_distance_table(const float* x, float* dis_table)
const {
with_simd_level([&]<SIMDLevel SL>() {
if (transposed_centroids.empty()) {
// use regular version
for (size_t m = 0; m < M; m++) {
fvec_L2sqr_ny<SL>(
dis_table + m * ksub,
x + m * dsub,
get_centroids(m, 0),
dsub,
ksub);
}
} else {
// transposed centroids are available, use'em
for (size_t m = 0; m < M; m++) {
fvec_L2sqr_ny_transposed<SL>(
dis_table + m * ksub,
x + m * dsub,
transposed_centroids.data() + m * ksub,
centroids_sq_lengths.data() + m * ksub,
dsub,
M * ksub,
ksub);
}
}
});
}
void ProductQuantizer::compute_inner_prod_table(
const float* x,
float* dis_table) const {
with_simd_level([&]<SIMDLevel SL>() {
for (size_t m = 0; m < M; m++) {
fvec_inner_products_ny<SL>(
dis_table + m * ksub,
x + m * dsub,
get_centroids(m, 0),
dsub,
ksub);
}
});
}
void ProductQuantizer::compute_distance_tables(
size_t nx,
const float* x,
float* dis_tables) const {
int64_t nx_signed = nx;
#if defined(COMPILE_SIMD_AVX2) || defined(COMPILE_SIMD_ARM_NEON)
if (dsub == 2 && nbits < 8) { // interesting for a narrow range of settings
compute_PQ_dis_tables_dsub2(
d, ksub, centroids.data(), nx, x, false, dis_tables);
} else
#endif
if (dsub < 16) {
#pragma omp parallel for if (nx > 1)
for (int64_t i = 0; i < nx_signed; i++) {
compute_distance_table(x + i * d, dis_tables + i * ksub * M);
}
} else { // use BLAS
for (size_t m = 0; m < M; m++) {
pairwise_L2sqr(
dsub,
nx,
x + dsub * m,
ksub,
centroids.data() + m * dsub * ksub,
dis_tables + ksub * m,
d,
dsub,
ksub * M);
}
}
}
void ProductQuantizer::compute_inner_prod_tables(
size_t nx,
const float* x,
float* dis_tables) const {
int64_t nx_signed = nx;
#if defined(COMPILE_SIMD_AVX2) || defined(COMPILE_SIMD_ARM_NEON)
if (dsub == 2 && nbits < 8) {
compute_PQ_dis_tables_dsub2(
d, ksub, centroids.data(), nx, x, true, dis_tables);
} else
#endif
if (dsub < 16) {
#pragma omp parallel for if (nx > 1)
for (int64_t i = 0; i < nx_signed; i++) {
compute_inner_prod_table(x + i * d, dis_tables + i * ksub * M);
}
} else { // use BLAS
// compute distance tables
for (size_t m = 0; m < M; m++) {
FINTEGER ldc = ksub * M, nxi = nx, ksubi = ksub, dsubi = dsub,
di = d;
float one = 1.0, zero = 0;
sgemm_("Transposed",
"Not transposed",
&ksubi,
&nxi,
&dsubi,
&one,
¢roids[m * dsub * ksub],
&dsubi,
x + dsub * m,
&di,
&zero,
dis_tables + ksub * m,
&ldc);
}
}
}
/**********************************************
* Templatized search functions
* The template class C indicates whether to keep the highest or smallest values
**********************************************/
namespace {
/* compute an estimator using look-up tables for typical values of M */
template <typename CT, class C>
void pq_estimators_from_tables_Mmul4(
int M,
const CT* codes,
size_t ncodes,
const float* __restrict dis_table,
size_t ksub,
size_t k,
float* heap_dis,
int64_t* heap_ids) {
for (size_t j = 0; j < ncodes; j++) {
float dis = 0;
const float* dt = dis_table;
for (int m = 0; m < M; m += 4) {
float dism = 0;
dism = dt[*codes++];
dt += ksub;
dism += dt[*codes++];
dt += ksub;
dism += dt[*codes++];
dt += ksub;
dism += dt[*codes++];
dt += ksub;
dis += dism;
}
if (C::cmp(heap_dis[0], dis)) {
heap_replace_top<C>(k, heap_dis, heap_ids, dis, j);
}
}
}
template <typename CT, class C>
void pq_estimators_from_tables_M4(
const CT* codes,
size_t ncodes,
const float* __restrict dis_table,
size_t ksub,
size_t k,
float* heap_dis,
int64_t* heap_ids) {
for (size_t j = 0; j < ncodes; j++) {
float dis = 0;
const float* dt = dis_table;
dis = dt[*codes++];
dt += ksub;
dis += dt[*codes++];
dt += ksub;
dis += dt[*codes++];
dt += ksub;
dis += dt[*codes++];
if (C::cmp(heap_dis[0], dis)) {
heap_replace_top<C>(k, heap_dis, heap_ids, dis, j);
}
}
}
template <typename CT, class C>
void pq_estimators_from_tables(
const ProductQuantizer& pq,
const CT* codes,
size_t ncodes,
const float* dis_table,
size_t k,
float* heap_dis,
int64_t* heap_ids) {
if (pq.M == 4) {
pq_estimators_from_tables_M4<CT, C>(
codes, ncodes, dis_table, pq.ksub, k, heap_dis, heap_ids);
return;
}
if (pq.M % 4 == 0) {
pq_estimators_from_tables_Mmul4<CT, C>(
pq.M, codes, ncodes, dis_table, pq.ksub, k, heap_dis, heap_ids);
return;
}
/* Default is relatively slow */
const size_t M = pq.M;
const size_t ksub = pq.ksub;
for (size_t j = 0; j < ncodes; j++) {
float dis = 0;
const float* __restrict dt = dis_table;
for (size_t m = 0; m < M; m++) {
dis += dt[*codes++];
dt += ksub;
}
if (C::cmp(heap_dis[0], dis)) {
heap_replace_top<C>(k, heap_dis, heap_ids, dis, j);
}
}
}
template <class C>
void pq_estimators_from_tables_generic(
const ProductQuantizer& pq,
size_t nbits,
const uint8_t* codes,
size_t ncodes,
const float* dis_table,
size_t k,
float* heap_dis,
int64_t* heap_ids) {
const size_t M = pq.M;
const size_t ksub = pq.ksub;
for (size_t j = 0; j < ncodes; ++j) {
PQDecoderGeneric decoder(codes + j * pq.code_size, nbits);
float dis = 0;
const float* __restrict dt = dis_table;
for (size_t m = 0; m < M; m++) {
uint64_t c = decoder.decode();
dis += dt[c];
dt += ksub;
}
if (C::cmp(heap_dis[0], dis)) {
heap_replace_top<C>(k, heap_dis, heap_ids, dis, j);
}
}
}
template <class C>
void pq_knn_search_with_tables(
const ProductQuantizer& pq,
size_t nbits,
const float* dis_tables,
const uint8_t* codes,
const size_t ncodes,
HeapArray<C>* res,
bool init_finalize_heap) {
size_t k = res->k, nx = res->nh;
int64_t nx_signed = nx;
size_t ksub = pq.ksub, M = pq.M;
#pragma omp parallel for if (nx > 1)
for (int64_t i = 0; i < nx_signed; i++) {
/* query preparation for asymmetric search: compute look-up tables */
const float* dis_table = dis_tables + i * ksub * M;
/* Compute distances and keep smallest values */
int64_t* __restrict heap_ids = res->ids + i * k;
float* __restrict heap_dis = res->val + i * k;
if (init_finalize_heap) {
heap_heapify<C>(k, heap_dis, heap_ids);
}
switch (nbits) {
case 8:
pq_estimators_from_tables<uint8_t, C>(
pq, codes, ncodes, dis_table, k, heap_dis, heap_ids);
break;
case 16:
pq_estimators_from_tables<uint16_t, C>(
pq,
(uint16_t*)codes,
ncodes,
dis_table,
k,
heap_dis,
heap_ids);
break;
default:
pq_estimators_from_tables_generic<C>(
pq,
nbits,
codes,
ncodes,
dis_table,
k,
heap_dis,
heap_ids);
break;
}
if (init_finalize_heap) {
heap_reorder<C>(k, heap_dis, heap_ids);
}
}
}
} // anonymous namespace
void ProductQuantizer::search(
const float* __restrict x,
size_t nx,
const uint8_t* codes,
const size_t ncodes,
float_maxheap_array_t* res,
bool init_finalize_heap) const {
FAISS_THROW_IF_NOT(nx == res->nh);
std::unique_ptr<float[]> dis_tables(new float[nx * ksub * M]);
compute_distance_tables(nx, x, dis_tables.get());
pq_knn_search_with_tables<CMax<float, int64_t>>(
*this,
nbits,
dis_tables.get(),
codes,
ncodes,
res,
init_finalize_heap);
}
void ProductQuantizer::search_ip(
const float* __restrict x,
size_t nx,
const uint8_t* codes,
const size_t ncodes,
float_minheap_array_t* res,
bool init_finalize_heap) const {
FAISS_THROW_IF_NOT(nx == res->nh);
std::unique_ptr<float[]> dis_tables(new float[nx * ksub * M]);
compute_inner_prod_tables(nx, x, dis_tables.get());
pq_knn_search_with_tables<CMin<float, int64_t>>(
*this,
nbits,
dis_tables.get(),
codes,
ncodes,
res,
init_finalize_heap);
}
void ProductQuantizer::compute_sdc_table() {
sdc_table.resize(M * ksub * ksub);
if (dsub < 4) {
with_simd_level([&]<SIMDLevel SL>() {
#pragma omp parallel for
for (int64_t mk = 0; mk < static_cast<int64_t>(M * ksub); mk++) {
// allow omp to schedule in a more fine-grained way
// `collapse` is not supported in OpenMP 2.x
int m = mk / ksub;
int k = mk % ksub;
const float* cents = centroids.data() + m * ksub * dsub;
const float* centi = cents + k * dsub;
float* dis_tab = sdc_table.data() + m * ksub * ksub;
fvec_L2sqr_ny<SL>(dis_tab + k * ksub, centi, cents, dsub, ksub);
}
});
} else {
// NOTE: it would disable the omp loop in pairwise_L2sqr
// but still accelerate especially when M >= 4
#pragma omp parallel for
for (int64_t m = 0; m < static_cast<int64_t>(M); m++) {
const float* cents = centroids.data() + m * ksub * dsub;
float* dis_tab = sdc_table.data() + m * ksub * ksub;
pairwise_L2sqr(
dsub, ksub, cents, ksub, cents, dis_tab, dsub, dsub, ksub);
}
}
}
void ProductQuantizer::search_sdc(
const uint8_t* qcodes,
size_t nq,
const uint8_t* bcodes,
const size_t nb,
float_maxheap_array_t* res,
bool init_finalize_heap) const {
FAISS_THROW_IF_NOT(sdc_table.size() == M * ksub * ksub);
FAISS_THROW_IF_NOT(nbits == 8);
size_t k = res->k;
int64_t nq_signed = nq;
#pragma omp parallel for
for (int64_t i = 0; i < nq_signed; i++) {
/* Compute distances and keep smallest values */
idx_t* heap_ids = res->ids + i * k;
float* heap_dis = res->val + i * k;
const uint8_t* qcode = qcodes + i * code_size;
if (init_finalize_heap)
maxheap_heapify(k, heap_dis, heap_ids);
const uint8_t* bcode = bcodes;
for (size_t j = 0; j < nb; j++) {
float dis = 0;
const float* tab = sdc_table.data();
for (size_t m = 0; m < M; m++) {
dis += tab[bcode[m] + qcode[m] * ksub];
tab += ksub * ksub;
}
if (dis < heap_dis[0]) {
maxheap_replace_top(k, heap_dis, heap_ids, dis, j);
}
bcode += code_size;
}
if (init_finalize_heap)
maxheap_reorder(k, heap_dis, heap_ids);
}
}
void ProductQuantizer::sync_transposed_centroids() {
transposed_centroids.resize(d * ksub);
centroids_sq_lengths.resize(ksub * M);
for (size_t mi = 0; mi < M; mi++) {
for (size_t ki = 0; ki < ksub; ki++) {
float sqlen = 0;
for (size_t di = 0; di < dsub; di++) {
const float q = centroids[(mi * ksub + ki) * dsub + di];
transposed_centroids[(di * M + mi) * ksub + ki] = q;
sqlen += q * q;
}
centroids_sq_lengths[mi * ksub + ki] = sqlen;
}
}
}
void ProductQuantizer::clear_transposed_centroids() {
transposed_centroids.clear();
transposed_centroids.shrink_to_fit();
centroids_sq_lengths.clear();
centroids_sq_lengths.shrink_to_fit();
}
} // namespace faiss