2024-10-23 00:46:48 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2017-02-23 06:26:44 +08:00
|
|
|
*
|
2019-05-28 22:17:22 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
2017-02-23 06:26:44 +08:00
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2020-08-24 15:41:13 +08:00
|
|
|
#include <random>
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
#include <faiss/IndexFlat.h>
|
|
|
|
#include <faiss/IndexIVFPQ.h>
|
|
|
|
|
2022-12-01 00:25:30 +08:00
|
|
|
using idx_t = faiss::idx_t;
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
int main() {
|
2021-02-25 20:44:50 +08:00
|
|
|
int d = 64; // dimension
|
|
|
|
int nb = 100000; // database size
|
|
|
|
int nq = 10000; // nb of queries
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2020-08-24 15:41:13 +08:00
|
|
|
std::mt19937 rng;
|
|
|
|
std::uniform_real_distribution<> distrib;
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
float* xb = new float[d * nb];
|
|
|
|
float* xq = new float[d * nq];
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
for (int i = 0; i < nb; i++) {
|
|
|
|
for (int j = 0; j < d; j++)
|
2020-08-24 15:41:13 +08:00
|
|
|
xb[d * i + j] = distrib(rng);
|
2017-02-23 06:26:44 +08:00
|
|
|
xb[d * i] += i / 1000.;
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
for (int i = 0; i < nq; i++) {
|
|
|
|
for (int j = 0; j < d; j++)
|
2020-08-24 15:41:13 +08:00
|
|
|
xq[d * i + j] = distrib(rng);
|
2017-02-23 06:26:44 +08:00
|
|
|
xq[d * i] += i / 1000.;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nlist = 100;
|
|
|
|
int k = 4;
|
2021-02-25 20:44:50 +08:00
|
|
|
int m = 8; // bytes per vector
|
|
|
|
faiss::IndexFlatL2 quantizer(d); // the other index
|
2017-02-23 06:26:44 +08:00
|
|
|
faiss::IndexIVFPQ index(&quantizer, d, nlist, m, 8);
|
2020-08-17 10:51:04 +08:00
|
|
|
|
2017-02-23 06:26:44 +08:00
|
|
|
index.train(nb, xb);
|
|
|
|
index.add(nb, xb);
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
{ // sanity check
|
|
|
|
idx_t* I = new idx_t[k * 5];
|
|
|
|
float* D = new float[k * 5];
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
index.search(5, xb, k, D, I);
|
|
|
|
|
|
|
|
printf("I=\n");
|
2021-02-25 20:44:50 +08:00
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
for (int j = 0; j < k; j++)
|
2020-06-28 19:00:44 +08:00
|
|
|
printf("%5zd ", I[i * k + j]);
|
2017-02-23 06:26:44 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("D=\n");
|
2021-02-25 20:44:50 +08:00
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
for (int j = 0; j < k; j++)
|
2017-02-23 06:26:44 +08:00
|
|
|
printf("%7g ", D[i * k + j]);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
delete[] I;
|
|
|
|
delete[] D;
|
2017-02-23 06:26:44 +08:00
|
|
|
}
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
{ // search xq
|
|
|
|
idx_t* I = new idx_t[k * nq];
|
|
|
|
float* D = new float[k * nq];
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
index.nprobe = 10;
|
|
|
|
index.search(nq, xq, k, D, I);
|
|
|
|
|
|
|
|
printf("I=\n");
|
2021-02-25 20:44:50 +08:00
|
|
|
for (int i = nq - 5; i < nq; i++) {
|
|
|
|
for (int j = 0; j < k; j++)
|
2020-06-28 19:00:44 +08:00
|
|
|
printf("%5zd ", I[i * k + j]);
|
2017-02-23 06:26:44 +08:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
delete[] I;
|
|
|
|
delete[] D;
|
2017-02-23 06:26:44 +08:00
|
|
|
}
|
|
|
|
|
2021-02-25 20:44:50 +08:00
|
|
|
delete[] xb;
|
|
|
|
delete[] xq;
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|