2017-07-18 17:52:23 +08:00
|
|
|
/**
|
2019-05-28 22:17:22 +08:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-07-18 17:52:23 +08:00
|
|
|
*
|
2019-05-28 22:17:22 +08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
2017-07-18 17:52:23 +08:00
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
2018-07-06 20:12:11 +08:00
|
|
|
// -*- c++ -*-
|
|
|
|
|
2017-07-18 17:52:23 +08:00
|
|
|
#ifndef FAISS_INDEX_SCALAR_QUANTIZER_H
|
|
|
|
#define FAISS_INDEX_SCALAR_QUANTIZER_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-09-21 00:59:10 +08:00
|
|
|
#include <faiss/IndexIVF.h>
|
|
|
|
#include <faiss/impl/ScalarQuantizer.h>
|
2017-07-18 17:52:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
namespace faiss {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The uniform quantizer has a range [vmin, vmax]. The range can be
|
|
|
|
* the same for all dimensions (uniform) or specific per dimension
|
|
|
|
* (default).
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct IndexScalarQuantizer: Index {
|
|
|
|
/// Used to encode the vectors
|
|
|
|
ScalarQuantizer sq;
|
|
|
|
|
|
|
|
/// Codes. Size ntotal * pq.code_size
|
|
|
|
std::vector<uint8_t> codes;
|
|
|
|
|
|
|
|
size_t code_size;
|
|
|
|
|
|
|
|
/** Constructor.
|
|
|
|
*
|
|
|
|
* @param d dimensionality of the input vectors
|
|
|
|
* @param M number of subquantizers
|
|
|
|
* @param nbits number of bit per subvector index
|
|
|
|
*/
|
|
|
|
IndexScalarQuantizer (int d,
|
|
|
|
ScalarQuantizer::QuantizerType qtype,
|
|
|
|
MetricType metric = METRIC_L2);
|
|
|
|
|
|
|
|
IndexScalarQuantizer ();
|
|
|
|
|
|
|
|
void train(idx_t n, const float* x) override;
|
|
|
|
|
|
|
|
void add(idx_t n, const float* x) override;
|
|
|
|
|
|
|
|
void search(
|
|
|
|
idx_t n,
|
|
|
|
const float* x,
|
|
|
|
idx_t k,
|
|
|
|
float* distances,
|
|
|
|
idx_t* labels) const override;
|
|
|
|
|
|
|
|
void reset() override;
|
|
|
|
|
|
|
|
void reconstruct_n(idx_t i0, idx_t ni, float* recons) const override;
|
|
|
|
|
|
|
|
void reconstruct(idx_t key, float* recons) const override;
|
|
|
|
|
2019-06-19 21:59:06 +08:00
|
|
|
DistanceComputer *get_distance_computer () const override;
|
2019-03-29 23:32:28 +08:00
|
|
|
|
2019-09-21 00:59:10 +08:00
|
|
|
/* standalone codec interface */
|
|
|
|
size_t sa_code_size () const override;
|
|
|
|
|
|
|
|
void sa_encode (idx_t n, const float *x,
|
|
|
|
uint8_t *bytes) const override;
|
|
|
|
|
|
|
|
void sa_decode (idx_t n, const uint8_t *bytes,
|
|
|
|
float *x) const override;
|
|
|
|
|
|
|
|
|
2017-07-18 17:52:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** An IVF implementation where the components of the residuals are
|
|
|
|
* encoded with a scalar uniform quantizer. All distance computations
|
|
|
|
* are asymmetric, so the encoded vectors are decoded and approximate
|
|
|
|
* distances are computed.
|
|
|
|
*/
|
|
|
|
|
2018-01-09 22:42:06 +08:00
|
|
|
struct IndexIVFScalarQuantizer: IndexIVF {
|
2017-07-18 17:52:23 +08:00
|
|
|
ScalarQuantizer sq;
|
2019-03-29 23:32:28 +08:00
|
|
|
bool by_residual;
|
2017-07-18 17:52:23 +08:00
|
|
|
|
|
|
|
IndexIVFScalarQuantizer(Index *quantizer, size_t d, size_t nlist,
|
|
|
|
ScalarQuantizer::QuantizerType qtype,
|
2019-09-21 00:59:10 +08:00
|
|
|
MetricType metric = METRIC_L2,
|
|
|
|
bool encode_residual = true);
|
2017-07-18 17:52:23 +08:00
|
|
|
|
|
|
|
IndexIVFScalarQuantizer();
|
|
|
|
|
|
|
|
void train_residual(idx_t n, const float* x) override;
|
|
|
|
|
2018-12-20 00:48:35 +08:00
|
|
|
void encode_vectors(idx_t n, const float* x,
|
|
|
|
const idx_t *list_nos,
|
2019-09-21 00:59:10 +08:00
|
|
|
uint8_t * codes,
|
|
|
|
bool include_listnos=false) const override;
|
2018-12-20 00:48:35 +08:00
|
|
|
|
2019-06-19 21:59:06 +08:00
|
|
|
void add_with_ids(idx_t n, const float* x, const idx_t* xids) override;
|
2017-07-18 17:52:23 +08:00
|
|
|
|
2018-12-20 00:48:35 +08:00
|
|
|
InvertedListScanner *get_InvertedListScanner (bool store_pairs)
|
|
|
|
const override;
|
|
|
|
|
2017-07-18 17:52:23 +08:00
|
|
|
|
2019-06-19 21:59:06 +08:00
|
|
|
void reconstruct_from_offset (int64_t list_no, int64_t offset,
|
2018-01-09 22:42:06 +08:00
|
|
|
float* recons) const override;
|
|
|
|
|
2019-09-21 00:59:10 +08:00
|
|
|
/* standalone codec interface */
|
|
|
|
void sa_decode (idx_t n, const uint8_t *bytes,
|
|
|
|
float *x) const override;
|
|
|
|
|
2017-07-18 17:52:23 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|