-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1ca352
commit fe7a615
Showing
11 changed files
with
704 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* 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 <cstdio> | ||
#include <cstdlib> | ||
#include <random> | ||
|
||
#include <faiss/IndexFlat.h> | ||
|
||
// 64-bit int | ||
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.; | ||
} | ||
|
||
faiss::IndexFlatL2 index(d); // call constructor | ||
printf("is_trained = %s\n", index.is_trained ? "true" : "false"); | ||
index.add(nb, xb); // add vectors to the index | ||
printf("ntotal = %zd\n", index.ntotal); | ||
|
||
int k = 4; | ||
|
||
{ // sanity check: search 5 first vectors of xb | ||
idx_t* I = new idx_t[k * 5]; | ||
float* D = new float[k * 5]; | ||
|
||
index.search(5, xb, k, D, I); | ||
|
||
// print results | ||
printf("I=\n"); | ||
for (int i = 0; i < 5; i++) { | ||
for (int j = 0; j < k; j++) | ||
printf("%5zd ", I[i * k + j]); | ||
printf("\n"); | ||
} | ||
|
||
printf("D=\n"); | ||
for (int i = 0; i < 5; i++) { | ||
for (int j = 0; j < k; j++) | ||
printf("%7g ", D[i * k + j]); | ||
printf("\n"); | ||
} | ||
|
||
delete[] I; | ||
delete[] D; | ||
} | ||
|
||
{ // search xq | ||
idx_t* I = new idx_t[k * nq]; | ||
float* D = new float[k * nq]; | ||
|
||
index.search(nq, xq, k, D, I); | ||
|
||
// print results | ||
printf("I (5 first results)=\n"); | ||
for (int i = 0; i < 5; i++) { | ||
for (int j = 0; j < k; j++) | ||
printf("%5zd ", I[i * k + j]); | ||
printf("\n"); | ||
} | ||
|
||
printf("I (5 last results)=\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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* 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/IndexFlat.h> | ||
#include <faiss/IndexIVFFlat.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 nlist = 100; | ||
int k = 4; | ||
|
||
faiss::IndexFlatL2 quantizer(d); // the other index | ||
faiss::IndexIVFFlat index(&quantizer, d, nlist); | ||
assert(!index.is_trained); | ||
index.train(nb, xb); | ||
assert(index.is_trained); | ||
index.add(nb, xb); | ||
|
||
{ // 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"); | ||
} | ||
|
||
index.nprobe = 10; | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* 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 <cstdio> | ||
#include <cstdlib> | ||
#include <random> | ||
|
||
#include <faiss/IndexFlat.h> | ||
#include <faiss/IndexIVFPQ.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 nlist = 100; | ||
int k = 4; | ||
int m = 8; // bytes per vector | ||
faiss::IndexFlatL2 quantizer(d); // the other index | ||
faiss::IndexIVFPQ index(&quantizer, d, nlist, m, 8); | ||
|
||
index.train(nb, xb); | ||
index.add(nb, xb); | ||
|
||
{ // sanity check | ||
idx_t* I = new idx_t[k * 5]; | ||
float* D = new float[k * 5]; | ||
|
||
index.search(5, xb, k, D, I); | ||
|
||
printf("I=\n"); | ||
for (int i = 0; i < 5; i++) { | ||
for (int j = 0; j < k; j++) | ||
printf("%5zd ", I[i * k + j]); | ||
printf("\n"); | ||
} | ||
|
||
printf("D=\n"); | ||
for (int i = 0; i < 5; i++) { | ||
for (int j = 0; j < k; j++) | ||
printf("%7g ", D[i * k + j]); | ||
printf("\n"); | ||
} | ||
|
||
delete[] I; | ||
delete[] D; | ||
} | ||
|
||
{ // search xq | ||
idx_t* I = new idx_t[k * nq]; | ||
float* D = new float[k * nq]; | ||
|
||
index.nprobe = 10; | ||
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; | ||
} |
Oops, something went wrong.