2017-06-21 21:54:28 +08:00
|
|
|
/**
|
2019-05-28 22:17:22 +08:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-06-21 21:54:28 +08:00
|
|
|
*
|
2019-05-28 22:17:22 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
2017-06-21 21:54:28 +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-06-21 21:54:28 +08:00
|
|
|
|
2021-01-18 05:40:10 +08:00
|
|
|
#include <omp.h>
|
|
|
|
|
2017-06-21 21:54:28 +08:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <faiss/IndexIVFPQ.h>
|
|
|
|
#include <faiss/IndexFlat.h>
|
2019-09-21 00:59:10 +08:00
|
|
|
#include <faiss/utils/utils.h>
|
|
|
|
#include <faiss/utils/distances.h>
|
2017-06-21 21:54:28 +08:00
|
|
|
|
|
|
|
|
2018-06-02 14:35:30 +08:00
|
|
|
namespace {
|
|
|
|
|
2017-06-21 21:54:28 +08:00
|
|
|
// dimension of the vectors to index
|
|
|
|
int d = 64;
|
|
|
|
|
|
|
|
// size of the database we plan to index
|
|
|
|
size_t nb = 8000;
|
|
|
|
|
|
|
|
|
|
|
|
double eval_codec_error (long ncentroids, long m, const std::vector<float> &v)
|
|
|
|
{
|
|
|
|
faiss::IndexFlatL2 coarse_quantizer (d);
|
|
|
|
faiss::IndexIVFPQ index (&coarse_quantizer, d,
|
|
|
|
ncentroids, m, 8);
|
|
|
|
index.pq.cp.niter = 10; // speed up train
|
|
|
|
index.train (nb, v.data());
|
|
|
|
|
|
|
|
// encode and decode to compute reconstruction error
|
|
|
|
|
2019-06-19 21:59:06 +08:00
|
|
|
std::vector<faiss::Index::idx_t> keys (nb);
|
2017-06-21 21:54:28 +08:00
|
|
|
std::vector<uint8_t> codes (nb * m);
|
|
|
|
index.encode_multiple (nb, keys.data(), v.data(), codes.data(), true);
|
|
|
|
|
|
|
|
std::vector<float> v2 (nb * d);
|
|
|
|
index.decode_multiple (nb, keys.data(), codes.data(), v2.data());
|
|
|
|
|
|
|
|
return faiss::fvec_L2sqr (v.data(), v2.data(), nb * d);
|
|
|
|
}
|
|
|
|
|
2018-06-02 14:35:30 +08:00
|
|
|
} // namespace
|
2017-06-21 21:54:28 +08:00
|
|
|
|
|
|
|
|
2021-01-18 05:40:10 +08:00
|
|
|
|
|
|
|
bool runs_on_sandcastle() {
|
|
|
|
// see discussion here https://fburl.com/qc5kpdo2
|
|
|
|
const char * sandcastle = getenv("SANDCASTLE");
|
|
|
|
if (sandcastle && !strcmp(sandcastle, "1")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
const char * tw_job_user = getenv("TW_JOB_USER");
|
|
|
|
if (tw_job_user && !strcmp(tw_job_user, "sandcastle")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-21 21:54:28 +08:00
|
|
|
TEST(IVFPQ, codec) {
|
|
|
|
|
|
|
|
std::vector <float> database (nb * d);
|
2020-08-24 15:41:13 +08:00
|
|
|
std::mt19937 rng;
|
|
|
|
std::uniform_real_distribution<> distrib;
|
2017-06-21 21:54:28 +08:00
|
|
|
for (size_t i = 0; i < nb * d; i++) {
|
2020-08-24 15:41:13 +08:00
|
|
|
database[i] = distrib(rng);
|
2017-06-21 21:54:28 +08:00
|
|
|
}
|
|
|
|
|
2021-01-18 05:40:10 +08:00
|
|
|
// limit number of threads when running on heavily parallelized test environment
|
|
|
|
if (runs_on_sandcastle()) {
|
|
|
|
omp_set_num_threads(2);
|
|
|
|
}
|
|
|
|
|
2017-06-21 21:54:28 +08:00
|
|
|
double err0 = eval_codec_error(16, 8, database);
|
|
|
|
|
|
|
|
// should be more accurate as there are more coarse centroids
|
|
|
|
double err1 = eval_codec_error(128, 8, database);
|
|
|
|
EXPECT_GT(err0, err1);
|
|
|
|
|
|
|
|
// should be more accurate as there are more PQ codes
|
|
|
|
double err2 = eval_codec_error(16, 16, database);
|
|
|
|
EXPECT_GT(err0, err2);
|
|
|
|
}
|