File ProductAdditiveQuantizer.h

namespace faiss

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)

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.

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.

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.

Definition of inverted lists + a few common classes that implement the interface.

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.

In this file are the implementations of extra metrics beyond L2 and inner product

Implements a few neural net layers, mainly to support QINCo

Defines a few objects that apply transformations to a set of vectors Often these are pre-processing steps.

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

Product Additive Quantizers

The product additive quantizer is a variant of AQ and PQ. It first splits the vector space into multiple orthogonal sub-spaces just like PQ does. And then it quantizes each sub-space by an independent additive quantizer.

Subclassed by faiss::ProductLocalSearchQuantizer, faiss::ProductResidualQuantizer

Public Functions

ProductAdditiveQuantizer(size_t d, const std::vector<AdditiveQuantizer*> &aqs, Search_type_t search_type = ST_decompress)

Construct a product additive quantizer.

The additive quantizers passed in will be cloned into the ProductAdditiveQuantizer object.

Parameters:
  • d – dimensionality of the input vectors

  • aqs – sub-additive quantizers

  • search_type – AQ search type

ProductAdditiveQuantizer()
virtual ~ProductAdditiveQuantizer()
void init(size_t d, const std::vector<AdditiveQuantizer*> &aqs, Search_type_t search_type)
AdditiveQuantizer *subquantizer(size_t m) const

Train the product additive quantizer.

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

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

void compute_unpacked_codes(const float *x, int32_t *codes, size_t n, const float *centroids = nullptr) const
virtual void decode_unpacked(const int32_t *codes, float *x, size_t n, int64_t ld_codes = -1) const override

Decode a set of vectors in non-packed format

Parameters:
  • codes – codes to decode, size n * ld_codes

  • x – output vectors, size n * d

virtual void decode(const uint8_t *codes, float *x, size_t n) const override

Decode a set of vectors

Parameters:
  • codes – codes to decode, size n * code_size

  • x – output vectors, size n * d

virtual void compute_LUT(size_t n, const float *xq, float *LUT, float alpha = 1.0f, long ld_lut = -1) const override

Compute inner-product look-up tables. Used in the search functions.

Parameters:
  • xq – query vector, size (n, d)

  • LUT – look-up table, size (n, total_codebook_size)

  • alpha – compute alpha * inner-product

  • ld_lut – leading dimension of LUT

Public Members

size_t nsplits

number of sub-vectors we split a vector into

std::vector<AdditiveQuantizer*> quantizers
struct ProductLocalSearchQuantizer : public faiss::ProductAdditiveQuantizer
#include <ProductAdditiveQuantizer.h>

Product Local Search Quantizer

Public Functions

ProductLocalSearchQuantizer(size_t d, size_t nsplits, size_t Msub, size_t nbits, Search_type_t search_type = ST_decompress)

Construct a product LSQ object.

Parameters:
  • d – dimensionality of the input vectors

  • nsplits – number of sub-vectors we split a vector into

  • Msub – number of codebooks of each LSQ

  • nbits – bits for each step

  • search_type – AQ search type

ProductLocalSearchQuantizer()
void init(size_t d, const std::vector<AdditiveQuantizer*> &aqs, Search_type_t search_type)
AdditiveQuantizer *subquantizer(size_t m) const

Train the product additive quantizer.

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

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

void compute_unpacked_codes(const float *x, int32_t *codes, size_t n, const float *centroids = nullptr) const
virtual void decode_unpacked(const int32_t *codes, float *x, size_t n, int64_t ld_codes = -1) const override

Decode a set of vectors in non-packed format

Parameters:
  • codes – codes to decode, size n * ld_codes

  • x – output vectors, size n * d

virtual void decode(const uint8_t *codes, float *x, size_t n) const override

Decode a set of vectors

Parameters:
  • codes – codes to decode, size n * code_size

  • x – output vectors, size n * d

virtual void compute_LUT(size_t n, const float *xq, float *LUT, float alpha = 1.0f, long ld_lut = -1) const override

Compute inner-product look-up tables. Used in the search functions.

Parameters:
  • xq – query vector, size (n, d)

  • LUT – look-up table, size (n, total_codebook_size)

  • alpha – compute alpha * inner-product

  • ld_lut – leading dimension of LUT

Public Members

size_t nsplits

number of sub-vectors we split a vector into

std::vector<AdditiveQuantizer*> quantizers
struct ProductResidualQuantizer : public faiss::ProductAdditiveQuantizer
#include <ProductAdditiveQuantizer.h>

Product Residual Quantizer

Public Functions

ProductResidualQuantizer(size_t d, size_t nsplits, size_t Msub, size_t nbits, Search_type_t search_type = ST_decompress)

Construct a product RQ object.

Parameters:
  • d – dimensionality of the input vectors

  • nsplits – number of sub-vectors we split a vector into

  • Msub – number of codebooks of each RQ

  • nbits – bits for each step

  • search_type – AQ search type

ProductResidualQuantizer()
void init(size_t d, const std::vector<AdditiveQuantizer*> &aqs, Search_type_t search_type)
AdditiveQuantizer *subquantizer(size_t m) const

Train the product additive quantizer.

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

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

void compute_unpacked_codes(const float *x, int32_t *codes, size_t n, const float *centroids = nullptr) const
virtual void decode_unpacked(const int32_t *codes, float *x, size_t n, int64_t ld_codes = -1) const override

Decode a set of vectors in non-packed format

Parameters:
  • codes – codes to decode, size n * ld_codes

  • x – output vectors, size n * d

virtual void decode(const uint8_t *codes, float *x, size_t n) const override

Decode a set of vectors

Parameters:
  • codes – codes to decode, size n * code_size

  • x – output vectors, size n * d

virtual void compute_LUT(size_t n, const float *xq, float *LUT, float alpha = 1.0f, long ld_lut = -1) const override

Compute inner-product look-up tables. Used in the search functions.

Parameters:
  • xq – query vector, size (n, d)

  • LUT – look-up table, size (n, total_codebook_size)

  • alpha – compute alpha * inner-product

  • ld_lut – leading dimension of LUT

Public Members

size_t nsplits

number of sub-vectors we split a vector into

std::vector<AdditiveQuantizer*> quantizers