File LocalSearchQuantizer.h

namespace faiss

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.

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. Implementation of k-means clustering with many variants.

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. IDSelector is intended to define a subset of vectors to handle (for removal or as subset to search)

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. PQ4 SIMD packing and accumulation functions

The basic kernel accumulates nq query vectors with bbs = nb * 2 * 16 vectors and produces an output matrix for that. It is interesting for nq * nb <= 4, otherwise register spilling becomes too large.

The implementation of these functions is spread over 3 cpp files to reduce parallel compile times. Templates are instantiated explicitly.

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. This file contains callbacks for kernels that compute distances.

Throughout the library, vectors are provided as float * pointers. Most algorithms can be optimized when several vectors are processed (added/searched) together in a batch. In this case, they are passed in as a matrix. When n vectors of size d are provided as float * x, component j of vector i is

x[ i * d + j ]

where 0 <= i < n and 0 <= j < d. In other words, matrices are always compact. When specifying the size of the matrix, we call it an n*d matrix, which implies a row-major storage.

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. I/O functions can read/write to a filename, a file handle or to an object that abstracts the medium.

The read functions return objects that should be deallocated with delete. All references within these objectes are owned by the object.

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. Definition of inverted lists + a few common classes that implement the interface.

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. Since IVF (inverted file) indexes are of so much use for large-scale use cases, we group a few functions related to them in this small library. Most functions work both on IndexIVFs and IndexIVFs embedded within an IndexPreTransform.

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. In this file are the implementations of extra metrics beyond L2 and inner product

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. Defines a few objects that apply transformations to a set of vectors Often these are pre-processing steps.

struct LocalSearchQuantizer : public faiss::AdditiveQuantizer
#include <LocalSearchQuantizer.h>

Implementation of LSQ/LSQ++ described in the following two papers:

Revisiting additive quantization Julieta Martinez, et al. ECCV 2016

LSQ++: Lower running time and higher recall in multi-codebook quantization Julieta Martinez, et al. ECCV 2018

This implementation is mostly translated from the Julia implementations by Julieta Martinez: (https://github.com/una-dinosauria/local-search-quantization, https://github.com/una-dinosauria/Rayuela.jl)

The trained codes are stored in codebooks which is called centroids in PQ and RQ.

Public Functions

LocalSearchQuantizer(size_t d, size_t M, size_t nbits, Search_type_t search_type = ST_decompress)
LocalSearchQuantizer()
~LocalSearchQuantizer() override
virtual void train(size_t n, const float *x) override

Train the quantizer

Parameters:

x – training vectors, size n * d

virtual void compute_codes_add_centroids(const float *x, uint8_t *codes, size_t n, const float *centroids = nullptr) const override

Encode a set of vectors

Parameters:
  • x – vectors to encode, size n * d

  • codes – output codes, size n * code_size

  • n – number of vectors

  • centroids – centroids to be added to x, size n * d

void update_codebooks(const float *x, const int32_t *codes, size_t n)

Update codebooks given encodings

Parameters:
  • x – training vectors, size n * d

  • codes – encoded training vectors, size n * M

  • n – number of vectors

void icm_encode(int32_t *codes, const float *x, size_t n, size_t ils_iters, std::mt19937 &gen) const

Encode vectors given codebooks using iterative conditional mode (icm).

Parameters:
  • codes – output codes, size n * M

  • x – vectors to encode, size n * d

  • n – number of vectors

  • ils_iters – number of iterations of iterative local search

void icm_encode_impl(int32_t *codes, const float *x, const float *unaries, std::mt19937 &gen, size_t n, size_t ils_iters, bool verbose) const
void icm_encode_step(int32_t *codes, const float *unaries, const float *binaries, size_t n, size_t n_iters) const
void perturb_codes(int32_t *codes, size_t n, std::mt19937 &gen) const

Add some perturbation to codes

Parameters:
  • codes – codes to be perturbed, size n * M

  • n – number of vectors

void perturb_codebooks(float T, const std::vector<float> &stddev, std::mt19937 &gen)

Add some perturbation to codebooks

Parameters:
  • T – temperature of simulated annealing

  • stddev – standard derivations of each dimension in training data

void compute_binary_terms(float *binaries) const

Compute binary terms

Parameters:

binaries – binary terms, size M * M * K * K

void compute_unary_terms(const float *x, float *unaries, size_t n) const

Compute unary terms

Parameters:
  • n – number of vectors

  • x – vectors to encode, size n * d

  • unaries – unary terms, size n * M * K

float evaluate(const int32_t *codes, const float *x, size_t n, float *objs = nullptr) const

Helper function to compute reconstruction error

Parameters:
  • codes – encoded codes, size n * M

  • x – vectors to encode, size n * d

  • n – number of vectors

  • objs – if it is not null, store reconstruction error of each vector into it, size n

Public Members

size_t K

number of codes per codebook

size_t train_iters = 25

number of iterations in training

size_t encode_ils_iters = 16

iterations of local search in encoding

size_t train_ils_iters = 8

iterations of local search in training

size_t icm_iters = 4

number of iterations in icm

float p = 0.5f

temperature factor

float lambd = 1e-2f

regularization factor

size_t chunk_size = 10000

nb of vectors to encode at a time

int random_seed = 0x12345

seed for random generator

size_t nperts = 4

number of perturbation in each code

if non-NULL, use this encoder to encode (owned by the object)

lsq::IcmEncoderFactory *icm_encoder_factory = nullptr
bool update_codebooks_with_double = true
namespace lsq
struct IcmEncoder

Subclassed by faiss::gpu::GpuIcmEncoder

Public Functions

explicit IcmEncoder(const LocalSearchQuantizer *lsq)
inline virtual ~IcmEncoder()

compute binary terms

virtual void set_binary_term()
virtual void encode(int32_t *codes, const float *x, std::mt19937 &gen, size_t n, size_t ils_iters) const

Encode vectors given codebooks

Parameters:
  • codes – output codes, size n * M

  • x – vectors to encode, size n * d

  • gen – random generator

  • n – number of vectors

  • ils_iters – number of iterations of iterative local search

Public Members

std::vector<float> binaries
bool verbose
const LocalSearchQuantizer *lsq
struct IcmEncoderFactory

Subclassed by faiss::gpu::GpuIcmEncoderFactory

Public Functions

inline virtual IcmEncoder *get(const LocalSearchQuantizer *lsq)
inline virtual ~IcmEncoderFactory()
struct LSQTimer
#include <LocalSearchQuantizer.h>

A helper struct to count consuming time during training. It is NOT thread-safe.

Public Functions

inline LSQTimer()
double get(const std::string &name)
void add(const std::string &name, double delta)
void reset()

Public Members

std::unordered_map<std::string, double> t
struct LSQTimerScope

Public Functions

LSQTimerScope(LSQTimer *timer, std::string name)
void finish()
~LSQTimerScope()

Public Members

double t0
LSQTimer *timer
std::string name
bool finished