Skip to content

Commit 83c980b

Browse files
Xiao Fufacebook-github-bot
Xiao Fu
authored andcommitted
Add tutorial for FastScan with refinement for cpp
Summary: This commit focus on the cpp version of PQfastscan tutorial with index refinement by defining the k factor. Differential Revision: D57680905
1 parent f352168 commit 83c980b

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

tutorial/cpp/8-PQFastScanRefine.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <cassert>
9+
#include <cstdio>
10+
#include <cstdlib>
11+
#include <random>
12+
13+
#include <faiss/IndexPQFastScan.h>
14+
#include <faiss/IndexRefine.h>
15+
16+
using idx_t = faiss::idx_t;
17+
18+
int main() {
19+
int d = 64; // dimension
20+
int nb = 100000; // database size
21+
int nq = 10000; // nb of queries
22+
23+
std::mt19937 rng;
24+
std::uniform_real_distribution<> distrib;
25+
26+
float* xb = new float[(int)(d * nb)];
27+
float* xq = new float[(int)(d * nq)];
28+
29+
for (int i = 0; i < nb; i++) {
30+
for (int j = 0; j < d; j++) {
31+
xb[d * i + j] = distrib(rng);
32+
}
33+
xb[d * i] += i / 1000.;
34+
}
35+
36+
for (int i = 0; i < nq; i++) {
37+
for (int j = 0; j < d; j++) {
38+
xq[d * i + j] = distrib(rng);
39+
}
40+
xq[d * i] += i / 1000.;
41+
}
42+
43+
int m = 8;
44+
int n_bit = 4;
45+
46+
faiss::IndexPQFastScan index(d, m, n_bit);
47+
faiss::IndexRefineFlat index_refine(&index);
48+
// refine index after PQFastScan
49+
50+
printf("Index is trained? %s\n",
51+
index_refine.is_trained ? "true" : "false");
52+
index_refine.train(nb, xb);
53+
printf("Index is trained? %s\n",
54+
index_refine.is_trained ? "true" : "false");
55+
index_refine.add(nb, xb);
56+
57+
int k = 4;
58+
{ // search xq
59+
idx_t* I = new idx_t[(int)(k * nq)];
60+
float* D = new float[(int)(k * nq)];
61+
float k_factor = 3;
62+
faiss::IndexRefineSearchParameters* params =
63+
new faiss::IndexRefineSearchParameters();
64+
params->k_factor = k_factor;
65+
index_refine.search(nq, xq, k, D, I, params);
66+
67+
printf("I=\n");
68+
for (int i = nq - 5; i < nq; i++) {
69+
for (int j = 0; j < k; j++) {
70+
printf("%5zd ", I[i * k + j]);
71+
}
72+
printf("\n");
73+
}
74+
75+
delete[] I;
76+
delete[] D;
77+
delete params;
78+
}
79+
80+
delete[] xb;
81+
delete[] xq;
82+
83+
return 0;
84+
}

tutorial/cpp/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ target_link_libraries(6-HNSW PRIVATE faiss)
2424

2525
add_executable(7-PQFastScan EXCLUDE_FROM_ALL 7-PQFastScan.cpp)
2626
target_link_libraries(7-PQFastScan PRIVATE faiss)
27+
28+
add_executable(8-PQFastScanRefine EXCLUDE_FROM_ALL 8-PQFastScanRefine.cpp)
29+
target_link_libraries(8-PQFastScanRefine PRIVATE faiss)

0 commit comments

Comments
 (0)