faiss/tests/test_binary_flat.cpp
Matthijs Douze a996a4a052 Put idx_t in the faiss namespace (#2582)
Summary:
Pull Request resolved: https://github.com/facebookresearch/faiss/pull/2582

A few more or less cosmetic improvements
* Index::idx_t was in the Index object, which does not make much sense, this diff moves it to faiss::idx_t
* replace multiprocessing.dummy with multiprocessing.pool
* add Alexandr as a core contributor of Faiss in the README ;-)

```
for i in $( find . -name \*.cu -o -name \*.cuh -o -name \*.h -o -name \*.cpp ) ; do
  sed -i s/Index::idx_t/idx_t/ $i
done
```

For the fbcode deps:
```
for i in $( fbgs Index::idx_t --exclude fbcode/faiss -l ) ; do
   sed -i s/Index::idx_t/idx_t/ $i
done
```

Reviewed By: algoriddle

Differential Revision: D41437507

fbshipit-source-id: 8300f2a3ae97cace6172f3f14a9be3a83999fb89
2022-11-30 08:25:30 -08:00

63 lines
1.6 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/utils/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);
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::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]);
}
}
}