Skip to content

Commit

Permalink
Add tutorial on PQFastScan for cpp (#3468)
Browse files Browse the repository at this point in the history
Summary:

This commit includes the tutorial for PQFastScan in the cpp environment.

Reviewed By: junjieqi

Differential Revision: D57631441
  • Loading branch information
Xiao Fu authored and facebook-github-bot committed May 21, 2024
1 parent 59e3ee1 commit 906de4f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
75 changes: 75 additions & 0 deletions tutorial/cpp/7-PQFastScan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <random>

#include <faiss/IndexPQFastScan.h>

using idx_t = faiss::idx_t;

int main() {
int d = 64; // dimension
int nb = 100000; // database size
int nq = 10000; // nb of queries

std::mt19937 rng;
std::uniform_real_distribution<> distrib;

float* xb = new float[d * nb];
float* xq = new float[d * nq];

for (int i = 0; i < nb; i++) {
for (int j = 0; j < d; j++) {
xb[d * i + j] = distrib(rng);
}
xb[d * i] += i / 1000.;
}

for (int i = 0; i < nq; i++) {
for (int j = 0; j < d; j++) {
xq[d * i + j] = distrib(rng);
}
xq[d * i] += i / 1000.;
}

int m = 8;
int n_bit = 4;

faiss::IndexPQFastScan index(d, m, n_bit);
printf("Index is trained? %s\n", index.is_trained ? "true" : "false");
index.train(nb, xb);
printf("Index is trained? %s\n", index.is_trained ? "true" : "false");
index.add(nb, xb);

int k = 4;

{ // search xq
idx_t* I = new idx_t[k * nq];
float* D = new float[k * nq];

index.search(nq, xq, k, D, I);

printf("I=\n");
for (int i = nq - 5; i < nq; i++) {
for (int j = 0; j < k; j++) {
printf("%5zd ", I[i * k + j]);
}
printf("\n");
}

delete[] I;
delete[] D;
}

delete[] xb;
delete[] xq;

return 0;
} // namespace facebook::detail
3 changes: 3 additions & 0 deletions tutorial/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ target_link_libraries(5-Multiple-GPUs PRIVATE faiss)

add_executable(6-HNSW EXCLUDE_FROM_ALL 6-HNSW.cpp)
target_link_libraries(6-HNSW PRIVATE faiss)

add_executable(7-PQFastScan EXCLUDE_FROM_ALL 7-PQFastScan.cpp)
target_link_libraries(7-PQFastScan PRIVATE faiss)

0 comments on commit 906de4f

Please sign in to comment.