mirror of
https://github.com/facebookresearch/faiss.git
synced 2025-06-03 21:54:02 +08:00
Changelog: - changed license: BSD+Patents -> MIT - propagates exceptions raised in sub-indexes of IndexShards and IndexReplicas - support for searching several inverted lists in parallel (parallel_mode != 0) - better support for PQ codes where nbit != 8 or 16 - IVFSpectralHash implementation: spectral hash codes inside an IVF - 6-bit per component scalar quantizer (4 and 8 bit were already supported) - combinations of inverted lists: HStackInvertedLists and VStackInvertedLists - configurable number of threads for OnDiskInvertedLists prefetching (including 0=no prefetch) - more test and demo code compatible with Python 3 (print with parentheses) - refactored benchmark code: data loading is now in a single file
65 lines
1.5 KiB
C++
65 lines
1.5 KiB
C++
/**
|
|
* 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 <gtest/gtest.h>
|
|
|
|
#include <faiss/IndexBinaryFlat.h>
|
|
#include <faiss/hamming.h>
|
|
|
|
TEST(BinaryFlat, accuracy) {
|
|
// dimension of the vectors to index
|
|
int d = 64;
|
|
|
|
// size of the database we plan to index
|
|
size_t nb = 1000;
|
|
|
|
// make the index object and train it
|
|
faiss::IndexBinaryFlat index(d);
|
|
|
|
srand(35);
|
|
|
|
std::vector<uint8_t> database(nb * (d / 8));
|
|
for (size_t i = 0; i < nb * (d / 8); i++) {
|
|
database[i] = rand() % 0x100;
|
|
}
|
|
|
|
{ // populating the database
|
|
index.add(nb, database.data());
|
|
}
|
|
|
|
size_t nq = 200;
|
|
|
|
{ // searching the database
|
|
|
|
std::vector<uint8_t> queries(nq * (d / 8));
|
|
for (size_t i = 0; i < nq * (d / 8); i++) {
|
|
queries[i] = rand() % 0x100;
|
|
}
|
|
|
|
int k = 5;
|
|
std::vector<faiss::IndexBinary::idx_t> nns(k * nq);
|
|
std::vector<int> dis(k * nq);
|
|
|
|
index.search(nq, queries.data(), k, dis.data(), nns.data());
|
|
|
|
for (size_t i = 0; i < nq; ++i) {
|
|
faiss::HammingComputer8 hc(queries.data() + i * (d / 8), d / 8);
|
|
hamdis_t dist_min = hc.hamming(database.data());
|
|
for (size_t j = 1; j < nb; ++j) {
|
|
hamdis_t dist = hc.hamming(database.data() + j * (d / 8));
|
|
if (dist < dist_min) {
|
|
dist_min = dist;
|
|
}
|
|
}
|
|
EXPECT_EQ(dist_min, dis[k * i]);
|
|
}
|
|
}
|
|
}
|