Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/data/users/matthijs/github_faiss/faiss/VectorTransform.cpp
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD+Patents license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 // Copyright 2004-present Facebook. All Rights Reserved
10 // -*- c++ -*-
11
12 #include "VectorTransform.h"
13
14 #include <cstdio>
15 #include <cmath>
16 #include <cstring>
17
18 #include "utils.h"
19 #include "FaissAssert.h"
20 #include "IndexPQ.h"
21
22 using namespace faiss;
23
24
25 extern "C" {
26
27 // this is to keep the clang syntax checker happy
28 #ifndef FINTEGER
29 #define FINTEGER int
30 #endif
31
32
33 /* declare BLAS functions, see http://www.netlib.org/clapack/cblas/ */
34
35 int sgemm_ (
36 const char *transa, const char *transb, FINTEGER *m, FINTEGER *
37 n, FINTEGER *k, const float *alpha, const float *a,
38 FINTEGER *lda, const float *b,
39 FINTEGER *ldb, float *beta,
40 float *c, FINTEGER *ldc);
41
42 int ssyrk_ (
43 const char *uplo, const char *trans, FINTEGER *n, FINTEGER *k,
44 float *alpha, float *a, FINTEGER *lda,
45 float *beta, float *c, FINTEGER *ldc);
46
47 /* Lapack functions from http://www.netlib.org/clapack/old/single/ */
48
49 int ssyev_ (
50 const char *jobz, const char *uplo, FINTEGER *n, float *a,
51 FINTEGER *lda, float *w, float *work, FINTEGER *lwork,
52 FINTEGER *info);
53
54 int dsyev_ (
55 const char *jobz, const char *uplo, FINTEGER *n, double *a,
56 FINTEGER *lda, double *w, double *work, FINTEGER *lwork,
57 FINTEGER *info);
58
59 int sgesvd_(
60 const char *jobu, const char *jobvt, FINTEGER *m, FINTEGER *n,
61 float *a, FINTEGER *lda, float *s, float *u, FINTEGER *ldu, float *vt,
62 FINTEGER *ldvt, float *work, FINTEGER *lwork, FINTEGER *info);
63
64 }
65
66 /*********************************************
67 * VectorTransform
68 *********************************************/
69
70
71
72 float * VectorTransform::apply (Index::idx_t n, const float * x) const
73 {
74 float * xt = new float[n * d_out];
75 apply_noalloc (n, x, xt);
76 return xt;
77 }
78
79
80 void VectorTransform::train (idx_t, const float *) {
81 // does nothing by default
82 }
83
84
85 void VectorTransform::reverse_transform (
86 idx_t , const float *,
87 float *) const
88 {
89 FAISS_THROW_MSG ("reverse transform not implemented");
90 }
91
92
93
94
95 /*********************************************
96 * LinearTransform
97 *********************************************/
98
99 /// both d_in > d_out and d_out < d_in are supported
100 LinearTransform::LinearTransform (int d_in, int d_out,
101 bool have_bias):
102 VectorTransform (d_in, d_out), have_bias (have_bias),
103 is_orthonormal (false), verbose (false)
104 {}
105
106 void LinearTransform::apply_noalloc (Index::idx_t n, const float * x,
107 float * xt) const
108 {
109 FAISS_THROW_IF_NOT_MSG(is_trained, "Transformation not trained yet");
110
111 float c_factor;
112 if (have_bias) {
113 FAISS_THROW_IF_NOT_MSG (b.size() == d_out, "Bias not initialized");
114 float * xi = xt;
115 for (int i = 0; i < n; i++)
116 for(int j = 0; j < d_out; j++)
117 *xi++ = b[j];
118 c_factor = 1.0;
119 } else {
120 c_factor = 0.0;
121 }
122
123 FAISS_THROW_IF_NOT_MSG (A.size() == d_out * d_in,
124 "Transformation matrix not initialized");
125
126 float one = 1;
127 FINTEGER nbiti = d_out, ni = n, di = d_in;
128 sgemm_ ("Transposed", "Not transposed",
129 &nbiti, &ni, &di,
130 &one, A.data(), &di, x, &di, &c_factor, xt, &nbiti);
131
132 }
133
134
135 void LinearTransform::transform_transpose (idx_t n, const float * y,
136 float *x) const
137 {
138 if (have_bias) { // allocate buffer to store bias-corrected data
139 float *y_new = new float [n * d_out];
140 const float *yr = y;
141 float *yw = y_new;
142 for (idx_t i = 0; i < n; i++) {
143 for (int j = 0; j < d_out; j++) {
144 *yw++ = *yr++ - b [j];
145 }
146 }
147 y = y_new;
148 }
149
150 {
151 FINTEGER dii = d_in, doi = d_out, ni = n;
152 float one = 1.0, zero = 0.0;
153 sgemm_ ("Not", "Not", &dii, &ni, &doi,
154 &one, A.data (), &dii, y, &doi, &zero, x, &dii);
155 }
156
157 if (have_bias) delete [] y;
158 }
159
160 void LinearTransform::set_is_orthonormal ()
161 {
162 if (d_out > d_in) {
163 // not clear what we should do in this case
164 is_orthonormal = false;
165 return;
166 }
167 if (d_out == 0) { // borderline case, unnormalized matrix
168 is_orthonormal = true;
169 return;
170 }
171
172 double eps = 4e-5;
173 FAISS_ASSERT(A.size() >= d_out * d_in);
174 {
175 std::vector<float> ATA(d_out * d_out);
176 FINTEGER dii = d_in, doi = d_out;
177 float one = 1.0, zero = 0.0;
178
179 sgemm_ ("Transposed", "Not", &doi, &doi, &dii,
180 &one, A.data (), &dii,
181 A.data(), &dii,
182 &zero, ATA.data(), &doi);
183
184 is_orthonormal = true;
185 for (long i = 0; i < d_out; i++) {
186 for (long j = 0; j < d_out; j++) {
187 float v = ATA[i + j * d_out];
188 if (i == j) v-= 1;
189 if (fabs(v) > eps) {
190 is_orthonormal = false;
191 }
192 }
193 }
194 }
195
196 }
197
198
199 void LinearTransform::reverse_transform (idx_t n, const float * xt,
200 float *x) const
201 {
202 if (is_orthonormal) {
203 transform_transpose (n, xt, x);
204 } else {
205 FAISS_THROW_MSG ("reverse transform not implemented for non-orthonormal matrices");
206 }
207 }
208
209
210
211 /*********************************************
212 * RandomRotationMatrix
213 *********************************************/
214
215 void RandomRotationMatrix::init (int seed)
216 {
217
218 if(d_out <= d_in) {
219 A.resize (d_out * d_in);
220 float *q = A.data();
221 float_randn(q, d_out * d_in, seed);
222 matrix_qr(d_in, d_out, q);
223 } else {
224 A.resize (d_out * d_out);
225 float *q = A.data();
226 float_randn(q, d_out * d_out, seed);
227 matrix_qr(d_out, d_out, q);
228 // remove columns
229 int i, j;
230 for (i = 0; i < d_out; i++) {
231 for(j = 0; j < d_in; j++) {
232 q[i * d_in + j] = q[i * d_out + j];
233 }
234 }
235 A.resize(d_in * d_out);
236 }
237 is_orthonormal = true;
238 }
239
240 /*********************************************
241 * PCAMatrix
242 *********************************************/
243
244 PCAMatrix::PCAMatrix (int d_in, int d_out,
245 float eigen_power, bool random_rotation):
246 LinearTransform(d_in, d_out, true),
247 eigen_power(eigen_power), random_rotation(random_rotation)
248 {
249 is_trained = false;
250 max_points_per_d = 1000;
251 balanced_bins = 0;
252 }
253
254
255 namespace {
256
257 /// Compute the eigenvalue decomposition of symmetric matrix cov,
258 /// dimensions d_in-by-d_in. Output eigenvectors in cov.
259
260 void eig(size_t d_in, double *cov, double *eigenvalues, int verbose)
261 {
262 { // compute eigenvalues and vectors
263 FINTEGER info = 0, lwork = -1, di = d_in;
264 double workq;
265
266 dsyev_ ("Vectors as well", "Upper",
267 &di, cov, &di, eigenvalues, &workq, &lwork, &info);
268 lwork = FINTEGER(workq);
269 double *work = new double[lwork];
270
271 dsyev_ ("Vectors as well", "Upper",
272 &di, cov, &di, eigenvalues, work, &lwork, &info);
273
274 delete [] work;
275
276 if (info != 0) {
277 fprintf (stderr, "WARN ssyev info returns %d, "
278 "a very bad PCA matrix is learnt\n",
279 int(info));
280 // do not throw exception, as the matrix could still be useful
281 }
282
283
284 if(verbose && d_in <= 10) {
285 printf("info=%ld new eigvals=[", long(info));
286 for(int j = 0; j < d_in; j++) printf("%g ", eigenvalues[j]);
287 printf("]\n");
288
289 double *ci = cov;
290 printf("eigenvecs=\n");
291 for(int i = 0; i < d_in; i++) {
292 for(int j = 0; j < d_in; j++)
293 printf("%10.4g ", *ci++);
294 printf("\n");
295 }
296 }
297
298 }
299
300 // revert order of eigenvectors & values
301
302 for(int i = 0; i < d_in / 2; i++) {
303
304 std::swap(eigenvalues[i], eigenvalues[d_in - 1 - i]);
305 double *v1 = cov + i * d_in;
306 double *v2 = cov + (d_in - 1 - i) * d_in;
307 for(int j = 0; j < d_in; j++)
308 std::swap(v1[j], v2[j]);
309 }
310
311 }
312
313
314 }
315
316 void PCAMatrix::train (Index::idx_t n, const float *x)
317 {
318 const float * x_in = x;
319
320 x = fvecs_maybe_subsample (d_in, (size_t*)&n,
321 max_points_per_d * d_in, x, verbose);
322
323 ScopeDeleter<float> del_x (x != x_in ? x : nullptr);
324
325 // compute mean
326 mean.clear(); mean.resize(d_in, 0.0);
327 if (have_bias) { // we may want to skip the bias
328 const float *xi = x;
329 for (int i = 0; i < n; i++) {
330 for(int j = 0; j < d_in; j++)
331 mean[j] += *xi++;
332 }
333 for(int j = 0; j < d_in; j++)
334 mean[j] /= n;
335 }
336 if(verbose) {
337 printf("mean=[");
338 for(int j = 0; j < d_in; j++) printf("%g ", mean[j]);
339 printf("]\n");
340 }
341
342 if(n >= d_in) {
343 // compute covariance matrix, store it in PCA matrix
344 PCAMat.resize(d_in * d_in);
345 float * cov = PCAMat.data();
346 { // initialize with mean * mean^T term
347 float *ci = cov;
348 for(int i = 0; i < d_in; i++) {
349 for(int j = 0; j < d_in; j++)
350 *ci++ = - n * mean[i] * mean[j];
351 }
352 }
353 {
354 FINTEGER di = d_in, ni = n;
355 float one = 1.0;
356 ssyrk_ ("Up", "Non transposed",
357 &di, &ni, &one, (float*)x, &di, &one, cov, &di);
358
359 }
360 if(verbose && d_in <= 10) {
361 float *ci = cov;
362 printf("cov=\n");
363 for(int i = 0; i < d_in; i++) {
364 for(int j = 0; j < d_in; j++)
365 printf("%10g ", *ci++);
366 printf("\n");
367 }
368 }
369
370 std::vector<double> covd (d_in * d_in);
371 for (size_t i = 0; i < d_in * d_in; i++) covd [i] = cov [i];
372
373 std::vector<double> eigenvaluesd (d_in);
374
375 eig (d_in, covd.data (), eigenvaluesd.data (), verbose);
376
377 for (size_t i = 0; i < d_in * d_in; i++) PCAMat [i] = covd [i];
378 eigenvalues.resize (d_in);
379
380 for (size_t i = 0; i < d_in; i++)
381 eigenvalues [i] = eigenvaluesd [i];
382
383
384 } else {
385
386 std::vector<float> xc (n * d_in);
387
388 for (size_t i = 0; i < n; i++)
389 for(size_t j = 0; j < d_in; j++)
390 xc [i * d_in + j] = x [i * d_in + j] - mean[j];
391
392 // compute Gram matrix
393 std::vector<float> gram (n * n);
394 {
395 FINTEGER di = d_in, ni = n;
396 float one = 1.0, zero = 0.0;
397 ssyrk_ ("Up", "Transposed",
398 &ni, &di, &one, xc.data(), &di, &zero, gram.data(), &ni);
399 }
400
401 if(verbose && d_in <= 10) {
402 float *ci = gram.data();
403 printf("gram=\n");
404 for(int i = 0; i < n; i++) {
405 for(int j = 0; j < n; j++)
406 printf("%10g ", *ci++);
407 printf("\n");
408 }
409 }
410
411 std::vector<double> gramd (n * n);
412 for (size_t i = 0; i < n * n; i++)
413 gramd [i] = gram [i];
414
415 std::vector<double> eigenvaluesd (n);
416
417 // eig will fill in only the n first eigenvals
418
419 eig (n, gramd.data (), eigenvaluesd.data (), verbose);
420
421 PCAMat.resize(d_in * n);
422
423 for (size_t i = 0; i < n * n; i++)
424 gram [i] = gramd [i];
425
426 eigenvalues.resize (d_in);
427 // fill in only the n first ones
428 for (size_t i = 0; i < n; i++)
429 eigenvalues [i] = eigenvaluesd [i];
430
431 { // compute PCAMat = x' * v
432 FINTEGER di = d_in, ni = n;
433 float one = 1.0;
434
435 sgemm_ ("Non", "Non Trans",
436 &di, &ni, &ni,
437 &one, xc.data(), &di, gram.data(), &ni,
438 &one, PCAMat.data(), &di);
439 }
440
441 if(verbose && d_in <= 10) {
442 float *ci = PCAMat.data();
443 printf("PCAMat=\n");
444 for(int i = 0; i < n; i++) {
445 for(int j = 0; j < d_in; j++)
446 printf("%10g ", *ci++);
447 printf("\n");
448 }
449 }
450 fvec_renorm_L2 (d_in, n, PCAMat.data());
451
452 }
453
454 prepare_Ab();
455 is_trained = true;
456 }
457
458 void PCAMatrix::copy_from (const PCAMatrix & other)
459 {
460 FAISS_THROW_IF_NOT (other.is_trained);
461 mean = other.mean;
462 eigenvalues = other.eigenvalues;
463 PCAMat = other.PCAMat;
464 prepare_Ab ();
465 is_trained = true;
466 }
467
468 void PCAMatrix::prepare_Ab ()
469 {
470 FAISS_THROW_IF_NOT_FMT (
471 d_out * d_in <= PCAMat.size(),
472 "PCA matrix cannot output %d dimensions from %d ",
473 d_out, d_in);
474
475 if (!random_rotation) {
476 A = PCAMat;
477 A.resize(d_out * d_in); // strip off useless dimensions
478
479 // first scale the components
480 if (eigen_power != 0) {
481 float *ai = A.data();
482 for (int i = 0; i < d_out; i++) {
483 float factor = pow(eigenvalues[i], eigen_power);
484 for(int j = 0; j < d_in; j++)
485 *ai++ *= factor;
486 }
487 }
488
489 if (balanced_bins != 0) {
490 FAISS_THROW_IF_NOT (d_out % balanced_bins == 0);
491 int dsub = d_out / balanced_bins;
492 std::vector <float> Ain;
493 std::swap(A, Ain);
494 A.resize(d_out * d_in);
495
496 std::vector <float> accu(balanced_bins);
497 std::vector <int> counter(balanced_bins);
498
499 // greedy assignment
500 for (int i = 0; i < d_out; i++) {
501 // find best bin
502 int best_j = -1;
503 float min_w = 1e30;
504 for (int j = 0; j < balanced_bins; j++) {
505 if (counter[j] < dsub && accu[j] < min_w) {
506 min_w = accu[j];
507 best_j = j;
508 }
509 }
510 int row_dst = best_j * dsub + counter[best_j];
511 accu[best_j] += eigenvalues[i];
512 counter[best_j] ++;
513 memcpy (&A[row_dst * d_in], &Ain[i * d_in],
514 d_in * sizeof (A[0]));
515 }
516
517 if (verbose) {
518 printf(" bin accu=[");
519 for (int i = 0; i < balanced_bins; i++)
520 printf("%g ", accu[i]);
521 printf("]\n");
522 }
523 }
524
525
526 } else {
527 FAISS_THROW_IF_NOT_MSG (balanced_bins == 0,
528 "both balancing bins and applying a random rotation "
529 "does not make sense");
530 RandomRotationMatrix rr(d_out, d_out);
531
532 rr.init(5);
533
534 // apply scaling on the rotation matrix (right multiplication)
535 if (eigen_power != 0) {
536 for (int i = 0; i < d_out; i++) {
537 float factor = pow(eigenvalues[i], eigen_power);
538 for(int j = 0; j < d_out; j++)
539 rr.A[j * d_out + i] *= factor;
540 }
541 }
542
543 A.resize(d_in * d_out);
544 {
545 FINTEGER dii = d_in, doo = d_out;
546 float one = 1.0, zero = 0.0;
547
548 sgemm_ ("Not", "Not", &dii, &doo, &doo,
549 &one, PCAMat.data(), &dii, rr.A.data(), &doo, &zero,
550 A.data(), &dii);
551
552 }
553
554 }
555
556 b.clear(); b.resize(d_out);
557
558 for (int i = 0; i < d_out; i++) {
559 float accu = 0;
560 for (int j = 0; j < d_in; j++)
561 accu -= mean[j] * A[j + i * d_in];
562 b[i] = accu;
563 }
564
565 is_orthonormal = eigen_power == 0;
566
567 }
568
569 /*********************************************
570 * OPQMatrix
571 *********************************************/
572
573
574 OPQMatrix::OPQMatrix (int d, int M, int d2):
575 LinearTransform (d, d2 == -1 ? d : d2, false), M(M),
576 niter (50),
577 niter_pq (4), niter_pq_0 (40),
578 verbose(false)
579 {
580 is_trained = false;
581 // OPQ is quite expensive to train, so set this right.
582 max_train_points = 256 * 256;
583 }
584
585
586
587 void OPQMatrix::train (Index::idx_t n, const float *x)
588 {
589
590 const float * x_in = x;
591
592 x = fvecs_maybe_subsample (d_in, (size_t*)&n,
593 max_train_points, x, verbose);
594
595 ScopeDeleter<float> del_x (x != x_in ? x : nullptr);
596
597 // To support d_out > d_in, we pad input vectors with 0s to d_out
598 size_t d = d_out <= d_in ? d_in : d_out;
599 size_t d2 = d_out;
600
601 #if 0
602 // what this test shows: the only way of getting bit-exact
603 // reproducible results with sgeqrf and sgesvd seems to be forcing
604 // single-threading.
605 { // test repro
606 std::vector<float> r (d * d);
607 float * rotation = r.data();
608 float_randn (rotation, d * d, 1234);
609 printf("CS0: %016lx\n",
610 ivec_checksum (128*128, (int*)rotation));
611 matrix_qr (d, d, rotation);
612 printf("CS1: %016lx\n",
613 ivec_checksum (128*128, (int*)rotation));
614 return;
615 }
616 #endif
617
618 if (verbose) {
619 printf ("OPQMatrix::train: training an OPQ rotation matrix "
620 "for M=%d from %ld vectors in %dD -> %dD\n",
621 M, n, d_in, d_out);
622 }
623
624 std::vector<float> xtrain (n * d);
625 // center x
626 {
627 std::vector<float> sum (d);
628 const float *xi = x;
629 for (size_t i = 0; i < n; i++) {
630 for (int j = 0; j < d_in; j++)
631 sum [j] += *xi++;
632 }
633 for (int i = 0; i < d; i++) sum[i] /= n;
634 float *yi = xtrain.data();
635 xi = x;
636 for (size_t i = 0; i < n; i++) {
637 for (int j = 0; j < d_in; j++)
638 *yi++ = *xi++ - sum[j];
639 yi += d - d_in;
640 }
641 }
642 float *rotation;
643
644 if (A.size () == 0) {
645 A.resize (d * d);
646 rotation = A.data();
647 if (verbose)
648 printf(" OPQMatrix::train: making random %ld*%ld rotation\n",
649 d, d);
650 float_randn (rotation, d * d, 1234);
651 matrix_qr (d, d, rotation);
652 // we use only the d * d2 upper part of the matrix
653 A.resize (d * d2);
654 } else {
655 FAISS_THROW_IF_NOT (A.size() == d * d2);
656 rotation = A.data();
657 }
658
659
660 std::vector<float>
661 xproj (d2 * n), pq_recons (d2 * n), xxr (d * n),
662 tmp(d * d * 4);
663
664 std::vector<uint8_t> codes (M * n);
665 ProductQuantizer pq_regular (d2, M, 8);
666 double t0 = getmillisecs();
667 for (int iter = 0; iter < niter; iter++) {
668
669 { // torch.mm(xtrain, rotation:t())
670 FINTEGER di = d, d2i = d2, ni = n;
671 float zero = 0, one = 1;
672 sgemm_ ("Transposed", "Not transposed",
673 &d2i, &ni, &di,
674 &one, rotation, &di,
675 xtrain.data(), &di,
676 &zero, xproj.data(), &d2i);
677 }
678
679 pq_regular.cp.max_points_per_centroid = 1000;
680 pq_regular.cp.niter = iter == 0 ? niter_pq_0 : niter_pq;
681 pq_regular.cp.verbose = verbose;
682 pq_regular.train (n, xproj.data());
683
684 pq_regular.compute_codes (xproj.data(), codes.data(), n);
685 pq_regular.decode (codes.data(), pq_recons.data(), n);
686
687 float pq_err = fvec_L2sqr (pq_recons.data(), xproj.data(), n * d2) / n;
688
689 if (verbose)
690 printf (" Iteration %d (%d PQ iterations):"
691 "%.3f s, obj=%g\n", iter, pq_regular.cp.niter,
692 (getmillisecs () - t0) / 1000.0, pq_err);
693
694 {
695 float *u = tmp.data(), *vt = &tmp [d * d];
696 float *sing_val = &tmp [2 * d * d];
697 FINTEGER di = d, d2i = d2, ni = n;
698 float one = 1, zero = 0;
699
700 // torch.mm(xtrain:t(), pq_recons)
701 sgemm_ ("Not", "Transposed",
702 &d2i, &di, &ni,
703 &one, pq_recons.data(), &d2i,
704 xtrain.data(), &di,
705 &zero, xxr.data(), &d2i);
706
707
708 FINTEGER lwork = -1, info = -1;
709 float worksz;
710 // workspace query
711 sgesvd_ ("All", "All",
712 &d2i, &di, xxr.data(), &d2i,
713 sing_val,
714 vt, &d2i, u, &di,
715 &worksz, &lwork, &info);
716
717 lwork = int(worksz);
718 std::vector<float> work (lwork);
719 // u and vt swapped
720 sgesvd_ ("All", "All",
721 &d2i, &di, xxr.data(), &d2i,
722 sing_val,
723 vt, &d2i, u, &di,
724 work.data(), &lwork, &info);
725
726 sgemm_ ("Transposed", "Transposed",
727 &di, &d2i, &d2i,
728 &one, u, &di, vt, &d2i,
729 &zero, rotation, &di);
730
731 }
732 pq_regular.train_type = ProductQuantizer::Train_hot_start;
733 }
734
735 // revert A matrix
736 if (d > d_in) {
737 for (long i = 0; i < d_out; i++)
738 memmove (&A[i * d_in], &A[i * d], sizeof(A[0]) * d_in);
739 A.resize (d_in * d_out);
740 }
741
742 is_trained = true;
743 is_orthonormal = true;
744 }
745
746
747 /*********************************************
748 * NormalizationTransform
749 *********************************************/
750
751 NormalizationTransform::NormalizationTransform (int d, float norm):
752 VectorTransform (d, d), norm (norm)
753 {
754 }
755
756 NormalizationTransform::NormalizationTransform ():
757 VectorTransform (-1, -1), norm (-1)
758 {
759 }
760
761 void NormalizationTransform::apply_noalloc
762 (idx_t n, const float* x, float* xt) const
763 {
764 if (norm == 2.0) {
765 memcpy (xt, x, sizeof (x[0]) * n * d_in);
766 fvec_renorm_L2 (d_in, n, xt);
767 } else {
768 FAISS_THROW_MSG ("not implemented");
769 }
770 }
771
772 void NormalizationTransform::reverse_transform (idx_t n, const float* xt,
773 float* x) const
774 {
775 memcpy (x, xt, sizeof (xt[0]) * n * d_in);
776 }
777
778 /*********************************************
779 * IndexPreTransform
780 *********************************************/
781
782 IndexPreTransform::IndexPreTransform ():
783 index(nullptr), own_fields (false)
784 {
785 }
786
787
788 IndexPreTransform::IndexPreTransform (
789 Index * index):
790 Index (index->d, index->metric_type),
791 index (index), own_fields (false)
792 {
793 is_trained = index->is_trained;
794 }
795
796
797 IndexPreTransform::IndexPreTransform (
798 VectorTransform * ltrans,
799 Index * index):
800 Index (index->d, index->metric_type),
801 index (index), own_fields (false)
802 {
803 is_trained = index->is_trained;
804 prepend_transform (ltrans);
805 }
806
807 void IndexPreTransform::prepend_transform (VectorTransform *ltrans)
808 {
809 FAISS_THROW_IF_NOT (ltrans->d_out == d);
810 is_trained = is_trained && ltrans->is_trained;
811 chain.insert (chain.begin(), ltrans);
812 d = ltrans->d_in;
813 }
814
815
816 IndexPreTransform::~IndexPreTransform ()
817 {
818 if (own_fields) {
819 for (int i = 0; i < chain.size(); i++)
820 delete chain[i];
821 delete index;
822 }
823 }
824
825
826
827
828 void IndexPreTransform::train (idx_t n, const float *x)
829 {
830 int last_untrained = 0;
831 if (!index->is_trained) {
832 last_untrained = chain.size();
833 } else {
834 for (int i = chain.size() - 1; i >= 0; i--) {
835 if (!chain[i]->is_trained) {
836 last_untrained = i;
837 break;
838 }
839 }
840 }
841 const float *prev_x = x;
842 ScopeDeleter<float> del;
843
844 if (verbose) {
845 printf("IndexPreTransform::train: training chain 0 to %d\n",
846 last_untrained);
847 }
848
849 for (int i = 0; i <= last_untrained; i++) {
850
851 if (i < chain.size()) {
852 VectorTransform *ltrans = chain [i];
853 if (!ltrans->is_trained) {
854 if (verbose) {
855 printf(" Training chain component %d/%zd\n",
856 i, chain.size());
857 if (OPQMatrix *opqm = dynamic_cast<OPQMatrix*>(ltrans)) {
858 opqm->verbose = true;
859 }
860 }
861 ltrans->train (n, prev_x);
862 }
863 } else {
864 if (verbose) {
865 printf(" Training sub-index\n");
866 }
867 index->train (n, prev_x);
868 }
869 if (i == last_untrained) break;
870 if (verbose) {
871 printf(" Applying transform %d/%zd\n",
872 i, chain.size());
873 }
874
875 float * xt = chain[i]->apply (n, prev_x);
876
877 if (prev_x != x) delete [] prev_x;
878 prev_x = xt;
879 del.set(xt);
880 }
881
882 is_trained = true;
883 }
884
885
886 const float *IndexPreTransform::apply_chain (idx_t n, const float *x) const
887 {
888 const float *prev_x = x;
889 ScopeDeleter<float> del;
890
891 for (int i = 0; i < chain.size(); i++) {
892 float * xt = chain[i]->apply (n, prev_x);
893 ScopeDeleter<float> del2 (xt);
894 del2.swap (del);
895 prev_x = xt;
896 }
897 del.release ();
898 return prev_x;
899 }
900
901 void IndexPreTransform::reverse_chain (idx_t n, const float* xt, float* x) const
902 {
903 const float* next_x = xt;
904 ScopeDeleter<float> del;
905
906 for (int i = chain.size() - 1; i >= 0; i--) {
907 float* prev_x = (i == 0) ? x : new float [n * chain[i]->d_in];
908 ScopeDeleter<float> del2 ((prev_x == x) ? nullptr : prev_x);
909 chain [i]->reverse_transform (n, next_x, prev_x);
910 del2.swap (del);
911 next_x = prev_x;
912 }
913 }
914
915 void IndexPreTransform::add (idx_t n, const float *x)
916 {
917 FAISS_THROW_IF_NOT (is_trained);
918 const float *xt = apply_chain (n, x);
919 ScopeDeleter<float> del(xt == x ? nullptr : xt);
920 index->add (n, xt);
921 ntotal = index->ntotal;
922 }
923
924 void IndexPreTransform::add_with_ids (idx_t n, const float * x,
925 const long *xids)
926 {
927 FAISS_THROW_IF_NOT (is_trained);
928 const float *xt = apply_chain (n, x);
929 ScopeDeleter<float> del(xt == x ? nullptr : xt);
930 index->add_with_ids (n, xt, xids);
931 ntotal = index->ntotal;
932 }
933
934
935
936
937 void IndexPreTransform::search (idx_t n, const float *x, idx_t k,
938 float *distances, idx_t *labels) const
939 {
940 FAISS_THROW_IF_NOT (is_trained);
941 const float *xt = apply_chain (n, x);
942 ScopeDeleter<float> del(xt == x ? nullptr : xt);
943 index->search (n, xt, k, distances, labels);
944 }
945
946
947 void IndexPreTransform::reset () {
948 index->reset();
949 ntotal = 0;
950 }
951
952 long IndexPreTransform::remove_ids (const IDSelector & sel) {
953 long nremove = index->remove_ids (sel);
954 ntotal = index->ntotal;
955 return nremove;
956 }
957
958
959 void IndexPreTransform::reconstruct (idx_t key, float * recons) const
960 {
961 float *x = chain.empty() ? recons : new float [index->d];
962 ScopeDeleter<float> del (recons == x ? nullptr : x);
963 // Initial reconstruction
964 index->reconstruct (key, x);
965
966 // Revert transformations from last to first
967 reverse_chain (1, x, recons);
968 }
969
970
971 void IndexPreTransform::reconstruct_n (idx_t i0, idx_t ni, float *recons) const
972 {
973 float *x = chain.empty() ? recons : new float [ni * index->d];
974 ScopeDeleter<float> del (recons == x ? nullptr : x);
975 // Initial reconstruction
976 index->reconstruct_n (i0, ni, x);
977
978 // Revert transformations from last to first
979 reverse_chain (ni, x, recons);
980 }
981
982
983 void IndexPreTransform::search_and_reconstruct (
984 idx_t n, const float *x, idx_t k,
985 float *distances, idx_t *labels, float* recons) const
986 {
987 FAISS_THROW_IF_NOT (is_trained);
988
989 const float* xt = apply_chain (n, x);
990 ScopeDeleter<float> del ((xt == x) ? nullptr : xt);
991
992 float* recons_temp = chain.empty() ? recons : new float [n * k * index->d];
993 ScopeDeleter<float> del2 ((recons_temp == recons) ? nullptr : recons_temp);
994 index->search_and_reconstruct (n, xt, k, distances, labels, recons_temp);
995
996 // Revert transformations from last to first
997 reverse_chain (n * k, recons_temp, recons);
998 }
999
1000
1001 /*********************************************
1002 * RemapDimensionsTransform
1003 *********************************************/
1004
1005
1006 RemapDimensionsTransform::RemapDimensionsTransform (
1007 int d_in, int d_out, const int *map_in):
1008 VectorTransform (d_in, d_out)
1009 {
1010 map.resize (d_out);
1011 for (int i = 0; i < d_out; i++) {
1012 map[i] = map_in[i];
1013 FAISS_THROW_IF_NOT (map[i] == -1 || (map[i] >= 0 && map[i] < d_in));
1014 }
1015 }
1016
1017 RemapDimensionsTransform::RemapDimensionsTransform (
1018 int d_in, int d_out, bool uniform): VectorTransform (d_in, d_out)
1019 {
1020 map.resize (d_out, -1);
1021
1022 if (uniform) {
1023 if (d_in < d_out) {
1024 for (int i = 0; i < d_in; i++) {
1025 map [i * d_out / d_in] = i;
1026 }
1027 } else {
1028 for (int i = 0; i < d_out; i++) {
1029 map [i] = i * d_in / d_out;
1030 }
1031 }
1032 } else {
1033 for (int i = 0; i < d_in && i < d_out; i++)
1034 map [i] = i;
1035 }
1036 }
1037
1038
1039 void RemapDimensionsTransform::apply_noalloc (idx_t n, const float * x,
1040 float *xt) const
1041 {
1042 for (idx_t i = 0; i < n; i++) {
1043 for (int j = 0; j < d_out; j++) {
1044 xt[j] = map[j] < 0 ? 0 : x[map[j]];
1045 }
1046 x += d_in;
1047 xt += d_out;
1048 }
1049 }
1050
1051 void RemapDimensionsTransform::reverse_transform (idx_t n, const float * xt,
1052 float *x) const
1053 {
1054 memset (x, 0, sizeof (*x) * n * d_in);
1055 for (idx_t i = 0; i < n; i++) {
1056 for (int j = 0; j < d_out; j++) {
1057 if (map[j] >= 0) x[map[j]] = xt[j];
1058 }
1059 x += d_in;
1060 xt += d_out;
1061 }
1062 }
faiss::LinearTransform::transform_transpose
void transform_transpose(idx_t n, const float *y, float *x) const
Definition: VectorTransform.cpp:135
faiss::IndexPreTransform::index
Index * index
! chain of tranforms
Definition: VectorTransform.h:249
faiss::RandomRotationMatrix
Randomly rotate a set of vectors.
Definition: VectorTransform.h:117
faiss::ClusteringParameters::niter
int niter
clustering iterations
Definition: Clustering.h:25
faiss::OPQMatrix::niter
int niter
Number of outer training iterations.
Definition: VectorTransform.h:189
faiss::ProductQuantizer::decode
void decode(const uint8_t *code, float *x) const
decode a vector from a given code (or n vectors if third argument)
Definition: ProductQuantizer.cpp:338
faiss::fvec_L2sqr
float fvec_L2sqr(const float *x, const float *y, size_t d)
Squared L2 distance between two vectors.
Definition: utils.cpp:574
faiss::RandomRotationMatrix::init
void init(int seed)
must be called before the transform is used
Definition: VectorTransform.cpp:215
faiss::IndexPreTransform::reset
void reset() override
removes all elements from the database.
Definition: VectorTransform.cpp:947
faiss::Index::reset
virtual void reset()=0
removes all elements from the database.
faiss::OPQMatrix::niter_pq
int niter_pq
Number of training iterations for the PQ.
Definition: VectorTransform.h:190
faiss::LinearTransform::A
std::vector< float > A
Transformation matrix, size d_out * d_in.
Definition: VectorTransform.h:85
faiss::Index::search_and_reconstruct
virtual void search_and_reconstruct(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, float *recons) const
Definition: Index.cpp:66
faiss::fvecs_maybe_subsample
const float * fvecs_maybe_subsample(size_t d, size_t *n, size_t nmax, const float *x, bool verbose, long seed)
Definition: utils.cpp:1975
faiss::PCAMatrix::eigen_power
float eigen_power
Definition: VectorTransform.h:140
faiss::IDSelector
Definition: AuxIndexStructures.h:52
faiss::LinearTransform::LinearTransform
LinearTransform(int d_in=0, int d_out=0, bool have_bias=false)
both d_in &gt; d_out and d_out &lt; d_in are supported
Definition: VectorTransform.cpp:100
faiss::PCAMatrix
Definition: VectorTransform.h:132
faiss::LinearTransform::set_is_orthonormal
void set_is_orthonormal()
compute A^T * A to set the is_orthonormal flag
Definition: VectorTransform.cpp:160
faiss::Index::train
virtual void train(idx_t n, const float *x)
Definition: Index.cpp:23
faiss::Index::add_with_ids
virtual void add_with_ids(idx_t n, const float *x, const long *xids)
Definition: Index.cpp:41
faiss::OPQMatrix
Definition: VectorTransform.h:186
faiss::ScopeDeleter
Definition: FaissException.h:39
faiss::PCAMatrix::train
void train(Index::idx_t n, const float *x) override
Definition: VectorTransform.cpp:316
faiss::PCAMatrix::mean
std::vector< float > mean
Mean, size d_in.
Definition: VectorTransform.h:152
faiss::RemapDimensionsTransform::map
std::vector< int > map
Definition: VectorTransform.h:211
faiss::IndexPreTransform::apply_chain
const float * apply_chain(idx_t n, const float *x) const
Definition: VectorTransform.cpp:886
faiss::PCAMatrix::PCAMat
std::vector< float > PCAMat
PCA matrix, size d_in * d_in.
Definition: VectorTransform.h:158
faiss::IndexPreTransform::train
void train(idx_t n, const float *x) override
Definition: VectorTransform.cpp:828
faiss::ProductQuantizer::compute_codes
void compute_codes(const float *x, uint8_t *codes, size_t n) const
same as compute_code for several vectors
Definition: ProductQuantizer.cpp:385
faiss::Index::d
int d
vector dimension
Definition: Index.h:64
faiss::LinearTransform::b
std::vector< float > b
bias vector, size d_out
Definition: VectorTransform.h:88
faiss::LinearTransform::reverse_transform
void reverse_transform(idx_t n, const float *xt, float *x) const override
works only if is_orthonormal
Definition: VectorTransform.cpp:199
faiss::Index::reconstruct_n
virtual void reconstruct_n(idx_t i0, idx_t ni, float *recons) const
Definition: Index.cpp:59
faiss::Index::add
virtual void add(idx_t n, const float *x)=0
faiss::NormalizationTransform::reverse_transform
void reverse_transform(idx_t n, const float *xt, float *x) const override
Identity transform since norm is not revertible.
Definition: VectorTransform.cpp:772
faiss::LinearTransform
Definition: VectorTransform.h:77
faiss::OPQMatrix::train
void train(Index::idx_t n, const float *x) override
Definition: VectorTransform.cpp:587
faiss::PCAMatrix::balanced_bins
int balanced_bins
try to distribute output eigenvectors in this many bins
Definition: VectorTransform.h:149
faiss::VectorTransform::is_trained
bool is_trained
Definition: VectorTransform.h:42
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:62
faiss::IndexPreTransform::reconstruct_n
void reconstruct_n(idx_t i0, idx_t ni, float *recons) const override
Definition: VectorTransform.cpp:971
faiss::Index::ntotal
idx_t ntotal
total nb of indexed vectors
Definition: Index.h:65
faiss::NormalizationTransform::apply_noalloc
void apply_noalloc(idx_t n, const float *x, float *xt) const override
same as apply, but result is pre-allocated
Definition: VectorTransform.cpp:762
faiss::ProductQuantizer::Train_hot_start
the centroids are already initialized
Definition: ProductQuantizer.h:42
faiss::getmillisecs
double getmillisecs()
ms elapsed since some arbitrary epoch
Definition: utils.cpp:74
faiss::Index::remove_ids
virtual long remove_ids(const IDSelector &sel)
Definition: Index.cpp:48
faiss::Index::search
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const =0
faiss::VectorTransform
Definition: VectorTransform.h:28
faiss::matrix_qr
void matrix_qr(int m, int n, float *a)
Definition: utils.cpp:1322
faiss::IndexPreTransform::own_fields
bool own_fields
! the sub-index
Definition: VectorTransform.h:251
faiss::OPQMatrix::niter_pq_0
int niter_pq_0
same, for the first outer iteration
Definition: VectorTransform.h:191
faiss::ProductQuantizer::cp
ClusteringParameters cp
parameters used during clustering
Definition: ProductQuantizer.h:49
faiss::ivec_checksum
size_t ivec_checksum(size_t n, const int *a)
compute a checksum on a table.
Definition: utils.cpp:1672
faiss::VectorTransform::train
virtual void train(idx_t n, const float *x)
Definition: VectorTransform.cpp:80
faiss::VectorTransform::reverse_transform
virtual void reverse_transform(idx_t n, const float *xt, float *x) const
Definition: VectorTransform.cpp:85
faiss::IndexPreTransform::search_and_reconstruct
void search_and_reconstruct(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, float *recons) const override
Definition: VectorTransform.cpp:983
faiss::RemapDimensionsTransform::reverse_transform
void reverse_transform(idx_t n, const float *xt, float *x) const override
reverse transform correct only when the mapping is a permuation
Definition: VectorTransform.cpp:1051
faiss::OPQMatrix::max_train_points
size_t max_train_points
if there are too many training points, resample
Definition: VectorTransform.h:194
faiss::PCAMatrix::copy_from
void copy_from(const PCAMatrix &other)
copy pre-trained PCA matrix
Definition: VectorTransform.cpp:458
faiss::VectorTransform::d_out
int d_out
! input dimension
Definition: VectorTransform.h:33
faiss::OPQMatrix::OPQMatrix
OPQMatrix(int d=0, int M=1, int d2=-1)
if d2 != -1, output vectors of this dimension
Definition: VectorTransform.cpp:574
faiss::Index
Definition: Index.h:60
faiss::PCAMatrix::prepare_Ab
void prepare_Ab()
called after mean, PCAMat and eigenvalues are computed
Definition: VectorTransform.cpp:468
faiss::IndexPreTransform::add
void add(idx_t n, const float *x) override
Definition: VectorTransform.cpp:915
faiss::RemapDimensionsTransform::apply_noalloc
void apply_noalloc(idx_t n, const float *x, float *xt) const override
same as apply, but result is pre-allocated
Definition: VectorTransform.cpp:1039
faiss::ProductQuantizer
Definition: ProductQuantizer.h:25
faiss::Index::is_trained
bool is_trained
set if the Index does not require training, or if training is done already
Definition: Index.h:69
faiss::IndexPreTransform::reverse_chain
void reverse_chain(idx_t n, const float *xt, float *x) const
Definition: VectorTransform.cpp:901
faiss::LinearTransform::is_orthonormal
bool is_orthonormal
! whether to use the bias term
Definition: VectorTransform.h:82
faiss::PCAMatrix::eigenvalues
std::vector< float > eigenvalues
eigenvalues of covariance matrix (= squared singular values)
Definition: VectorTransform.h:155
faiss::IndexPreTransform::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: VectorTransform.cpp:937
faiss::Index::reconstruct
virtual void reconstruct(idx_t key, float *recons) const
Definition: Index.cpp:54
faiss::IndexPreTransform::add_with_ids
void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: VectorTransform.cpp:924
faiss::PCAMatrix::random_rotation
bool random_rotation
random rotation after PCA
Definition: VectorTransform.h:143
faiss::PCAMatrix::max_points_per_d
size_t max_points_per_d
ratio between # training vectors and dimension
Definition: VectorTransform.h:146
faiss::ClusteringParameters::max_points_per_centroid
int max_points_per_centroid
to limit size of dataset
Definition: Clustering.h:34
faiss::VectorTransform::apply
float * apply(idx_t n, const float *x) const
Definition: VectorTransform.cpp:72
faiss::IndexPreTransform::remove_ids
long remove_ids(const IDSelector &sel) override
Definition: VectorTransform.cpp:952
faiss::VectorTransform::apply_noalloc
virtual void apply_noalloc(idx_t n, const float *x, float *xt) const =0
same as apply, but result is pre-allocated
faiss::IndexPreTransform::reconstruct
void reconstruct(idx_t key, float *recons) const override
Definition: VectorTransform.cpp:959
faiss::OPQMatrix::M
int M
nb of subquantizers
Definition: VectorTransform.h:188
faiss::LinearTransform::apply_noalloc
void apply_noalloc(idx_t n, const float *x, float *xt) const override
same as apply, but result is pre-allocated
Definition: VectorTransform.cpp:106