2017-02-23 06:26:44 +08:00
|
|
|
/**
|
2019-05-28 22:17:22 +08:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-02-23 06:26:44 +08:00
|
|
|
*
|
2019-05-28 22:17:22 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
2017-02-23 06:26:44 +08:00
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
2018-07-06 20:12:11 +08:00
|
|
|
// -*- c++ -*-
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2019-09-21 00:59:10 +08:00
|
|
|
#include <faiss/Index.h>
|
|
|
|
|
|
|
|
#include <faiss/impl/AuxIndexStructures.h>
|
|
|
|
#include <faiss/impl/FaissAssert.h>
|
|
|
|
#include <faiss/utils/distances.h>
|
2017-02-23 06:26:44 +08:00
|
|
|
|
2018-01-09 22:42:06 +08:00
|
|
|
#include <cstring>
|
|
|
|
|
2018-07-06 20:12:11 +08:00
|
|
|
|
2017-02-23 06:26:44 +08:00
|
|
|
namespace faiss {
|
|
|
|
|
2018-01-09 22:42:06 +08:00
|
|
|
Index::~Index ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Index::train(idx_t /*n*/, const float* /*x*/) {
|
|
|
|
// does nothing by default
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:26:44 +08:00
|
|
|
|
|
|
|
void Index::range_search (idx_t , const float *, float,
|
|
|
|
RangeSearchResult *) const
|
|
|
|
{
|
2017-06-21 21:54:28 +08:00
|
|
|
FAISS_THROW_MSG ("range search not implemented");
|
2017-02-23 06:26:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Index::assign (idx_t n, const float * x, idx_t * labels, idx_t k)
|
|
|
|
{
|
2017-06-21 21:54:28 +08:00
|
|
|
float * distances = new float[n * k];
|
|
|
|
ScopeDeleter<float> del(distances);
|
|
|
|
search (n, x, k, distances, labels);
|
2017-02-23 06:26:44 +08:00
|
|
|
}
|
|
|
|
|
2017-07-26 18:11:01 +08:00
|
|
|
void Index::add_with_ids(
|
|
|
|
idx_t /*n*/,
|
|
|
|
const float* /*x*/,
|
2019-06-19 21:59:06 +08:00
|
|
|
const idx_t* /*xids*/) {
|
2017-06-21 21:54:28 +08:00
|
|
|
FAISS_THROW_MSG ("add_with_ids not implemented for this type of index");
|
2017-02-23 06:26:44 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:59:06 +08:00
|
|
|
size_t Index::remove_ids(const IDSelector& /*sel*/) {
|
2017-06-21 21:54:28 +08:00
|
|
|
FAISS_THROW_MSG ("remove_ids not implemented for this type of index");
|
|
|
|
return -1;
|
2017-02-23 06:26:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Index::reconstruct (idx_t, float * ) const {
|
2017-11-22 21:11:28 +08:00
|
|
|
FAISS_THROW_MSG ("reconstruct not implemented for this type of index");
|
2017-02-23 06:26:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Index::reconstruct_n (idx_t i0, idx_t ni, float *recons) const {
|
2017-06-21 21:54:28 +08:00
|
|
|
for (idx_t i = 0; i < ni; i++) {
|
|
|
|
reconstruct (i0 + i, recons + i * d);
|
|
|
|
}
|
2017-02-23 06:26:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-09 22:42:06 +08:00
|
|
|
void Index::search_and_reconstruct (idx_t n, const float *x, idx_t k,
|
|
|
|
float *distances, idx_t *labels,
|
|
|
|
float *recons) const {
|
|
|
|
search (n, x, k, distances, labels);
|
|
|
|
for (idx_t i = 0; i < n; ++i) {
|
|
|
|
for (idx_t j = 0; j < k; ++j) {
|
|
|
|
idx_t ij = i * k + j;
|
|
|
|
idx_t key = labels[ij];
|
|
|
|
float* reconstructed = recons + ij * d;
|
|
|
|
if (key < 0) {
|
|
|
|
// Fill with NaNs
|
|
|
|
memset(reconstructed, -1, sizeof(*reconstructed) * d);
|
|
|
|
} else {
|
|
|
|
reconstruct (key, reconstructed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-23 06:26:44 +08:00
|
|
|
void Index::compute_residual (const float * x,
|
|
|
|
float * residual, idx_t key) const {
|
2017-06-21 21:54:28 +08:00
|
|
|
reconstruct (key, residual);
|
2019-09-21 00:59:10 +08:00
|
|
|
for (size_t i = 0; i < d; i++) {
|
2017-06-21 21:54:28 +08:00
|
|
|
residual[i] = x[i] - residual[i];
|
2019-09-21 00:59:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Index::compute_residual_n (idx_t n, const float* xs,
|
|
|
|
float* residuals,
|
|
|
|
const idx_t* keys) const {
|
|
|
|
#pragma omp parallel for
|
|
|
|
for (idx_t i = 0; i < n; ++i) {
|
|
|
|
compute_residual(&xs[i * d], &residuals[i * d], keys[i]);
|
|
|
|
}
|
2017-02-23 06:26:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-21 00:59:10 +08:00
|
|
|
|
|
|
|
size_t Index::sa_code_size () const
|
|
|
|
{
|
|
|
|
FAISS_THROW_MSG ("standalone codec not implemented for this type of index");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Index::sa_encode (idx_t, const float *,
|
|
|
|
uint8_t *) const
|
|
|
|
{
|
|
|
|
FAISS_THROW_MSG ("standalone codec not implemented for this type of index");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Index::sa_decode (idx_t, const uint8_t *,
|
|
|
|
float *) const
|
|
|
|
{
|
|
|
|
FAISS_THROW_MSG ("standalone codec not implemented for this type of index");
|
2017-02-23 06:26:44 +08:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:59:06 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
|
// storage that explicitly reconstructs vectors before computing distances
|
|
|
|
struct GenericDistanceComputer : DistanceComputer {
|
|
|
|
size_t d;
|
|
|
|
const Index& storage;
|
|
|
|
std::vector<float> buf;
|
|
|
|
const float *q;
|
|
|
|
|
|
|
|
explicit GenericDistanceComputer(const Index& storage)
|
|
|
|
: storage(storage) {
|
|
|
|
d = storage.d;
|
|
|
|
buf.resize(d * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
float operator () (idx_t i) override {
|
|
|
|
storage.reconstruct(i, buf.data());
|
|
|
|
return fvec_L2sqr(q, buf.data(), d);
|
|
|
|
}
|
|
|
|
|
|
|
|
float symmetric_dis(idx_t i, idx_t j) override {
|
|
|
|
storage.reconstruct(i, buf.data());
|
|
|
|
storage.reconstruct(j, buf.data() + d);
|
|
|
|
return fvec_L2sqr(buf.data() + d, buf.data(), d);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_query(const float *x) override {
|
|
|
|
q = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
DistanceComputer * Index::get_distance_computer() const {
|
|
|
|
if (metric_type == METRIC_L2) {
|
|
|
|
return new GenericDistanceComputer(*this);
|
|
|
|
} else {
|
|
|
|
FAISS_THROW_MSG ("get_distance_computer() not implemented");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-23 06:26:44 +08:00
|
|
|
}
|