Skip to content

Commit 3ab73a6

Browse files
Xiao Fufacebook-github-bot
Xiao Fu
authored andcommitted
Add cpp tutorial for index factory refine index construction (facebookresearch#3494)
Summary: This tasks focus on the refine index construction tutorial with different index refinement on fp16/sq8 quantization. The python version was added a while ago. Differential Revision: D58161983
1 parent bf73e38 commit 3ab73a6

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

tutorial/cpp/9-RefineComparison.cpp

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
#include <faiss/index_factory.h>
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+
// Constructing the refine PQ index with SQfp16 with index factory
47+
faiss::Index* index_fp16;
48+
index_fp16 = faiss::index_factory(
49+
d, "PQ32x4fs,Refine(SQfp16)", faiss::METRIC_L2);
50+
index_fp16->train(nb, xb);
51+
index_fp16->add(nb, xb);
52+
53+
// Constructing the refine PQ index with SQ8
54+
faiss::Index* index_sq8;
55+
index_sq8 =
56+
faiss::index_factory(d, "PQ32x4fs,Refine(SQ8)", faiss::METRIC_L2);
57+
index_sq8->train(nb, xb);
58+
index_sq8->add(nb, xb);
59+
60+
int k = 10;
61+
{ // search xq
62+
idx_t* I_fp16 = new idx_t[(int)(k * nq)];
63+
float* D_fp16 = new float[(int)(k * nq)];
64+
idx_t* I_sq8 = new idx_t[(int)(k * nq)];
65+
float* D_sq8 = new float[(int)(k * nq)];
66+
67+
// Parameterization on k factor while doing search for index refinement
68+
float k_factor = 3;
69+
faiss::IndexRefineSearchParameters* params =
70+
new faiss::IndexRefineSearchParameters();
71+
params->k_factor = k_factor;
72+
73+
// Perform index search using different index refinement
74+
index_fp16->search(nq, xq, k, D_fp16, I_fp16, params);
75+
index_sq8->search(nq, xq, k, D_sq8, I_sq8, params);
76+
77+
printf("I_fp16=\n");
78+
for (int i = nq - 5; i < nq; i++) {
79+
for (int j = 0; j < k; j++) {
80+
printf("%5zd ", I_fp16[i * k + j]);
81+
}
82+
printf("\n");
83+
}
84+
85+
printf("I_sq8=\n");
86+
for (int i = nq - 5; i < nq; i++) {
87+
for (int j = 0; j < k; j++) {
88+
printf("%5zd ", I_sq8[i * k + j]);
89+
}
90+
printf("\n");
91+
}
92+
93+
delete[] I_fp16;
94+
delete[] D_fp16;
95+
delete[] I_sq8;
96+
delete[] D_sq8;
97+
delete params;
98+
99+
delete index_fp16;
100+
delete index_sq8;
101+
}
102+
103+
delete[] xb;
104+
delete[] xq;
105+
106+
return 0;
107+
}

tutorial/cpp/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ target_link_libraries(7-PQFastScan PRIVATE faiss)
2727

2828
add_executable(8-PQFastScanRefine EXCLUDE_FROM_ALL 8-PQFastScanRefine.cpp)
2929
target_link_libraries(8-PQFastScanRefine PRIVATE faiss)
30+
31+
add_executable(9-RefineComparison EXCLUDE_FROM_ALL 9-RefineComparison.cpp)
32+
target_link_libraries(9-RefineComparison PRIVATE faiss)

0 commit comments

Comments
 (0)