File lattice_Zn.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 ZnSphereSearch
#include <lattice_Zn.h>

returns the nearest vertex in the sphere to a query. Returns only the coordinates, not an id.

Algorithm: all points are derived from a one atom vector up to a permutation and sign changes. The search function finds the most appropriate atom and transformation.

Subclassed by faiss::ZnSphereCodec

Public Functions

ZnSphereSearch(int dim, int r2)
float search(const float *x, float *c) const

find nearest centroid. x does not need to be normalized

float search(const float *x, float *c, float *tmp, int *tmp_int, int *ibest_out = nullptr) const

full call. Requires externally-allocated temp space

void search_multi(int n, const float *x, float *c_out, float *dp_out)

Public Members

int dimS
int r2
int natom
std::vector<float> voc

size dim * ntatom

struct EnumeratedVectors

Subclassed by faiss::ZnSphereCodec, faiss::ZnSphereCodecRec

Public Functions

inline explicit EnumeratedVectors(int dim)
virtual uint64_t encode(const float *x) const = 0

encode a vector from a collection

virtual void decode(uint64_t code, float *c) const = 0

decode it

void encode_multi(size_t nc, const float *c, uint64_t *codes) const
void decode_multi(size_t nc, const uint64_t *codes, float *c) const
void find_nn(size_t n, const uint64_t *codes, size_t nq, const float *xq, int64_t *idx, float *dis)
inline virtual ~EnumeratedVectors()

Public Members

uint64_t nv

size of the collection

int dim
struct Repeat

Public Members

float val
int n
struct Repeats
#include <lattice_Zn.h>

Repeats: used to encode a vector that has n occurrences of val. Encodes the signs and permutation of the vector. Useful for atoms.

Subclassed by faiss::ZnSphereCodec::CodeSegment

Public Functions

Repeats(int dim = 0, const float *c = nullptr)
uint64_t count() const
uint64_t encode(const float *c) const
void decode(uint64_t code, float *c) const

Public Members

int dim
std::vector<Repeat> repeats
struct ZnSphereCodec : public faiss::ZnSphereSearch, public faiss::EnumeratedVectors
#include <lattice_Zn.h>

codec that can return ids for the encoded vectors

uses the ZnSphereSearch to encode the vector by encoding the permutation and signs. Depends on ZnSphereSearch because it uses the atom numbers

Subclassed by faiss::ZnSphereCodecAlt

Public Functions

ZnSphereCodec(int dim, int r2)
uint64_t search_and_encode(const float *x) const
virtual void decode(uint64_t code, float *c) const override

decode it

virtual uint64_t encode(const float *x) const override

takes vectors that do not need to be centroids

float search(const float *x, float *c) const

find nearest centroid. x does not need to be normalized

float search(const float *x, float *c, float *tmp, int *tmp_int, int *ibest_out = nullptr) const

full call. Requires externally-allocated temp space

void search_multi(int n, const float *x, float *c_out, float *dp_out)
void encode_multi(size_t nc, const float *c, uint64_t *codes) const
void decode_multi(size_t nc, const uint64_t *codes, float *c) const
void find_nn(size_t n, const uint64_t *codes, size_t nq, const float *xq, int64_t *idx, float *dis)

Public Members

std::vector<CodeSegment> code_segments
uint64_t nv
size_t code_size
int dimS
int r2
int natom
std::vector<float> voc

size dim * ntatom

int dim
struct CodeSegment : public faiss::Repeats

Public Functions

inline explicit CodeSegment(const Repeats &r)
uint64_t count() const
uint64_t encode(const float *c) const
void decode(uint64_t code, float *c) const

Public Members

uint64_t c0
int signbits
int dim
std::vector<Repeat> repeats
struct ZnSphereCodecRec : public faiss::EnumeratedVectors
#include <lattice_Zn.h>

recursive sphere codec

Uses a recursive decomposition on the dimensions to encode centroids found by the ZnSphereSearch. The codes are not compatible with the ones of ZnSpehreCodec

Public Functions

ZnSphereCodecRec(int dim, int r2)
uint64_t encode_centroid(const float *c) const
virtual void decode(uint64_t code, float *c) const override

decode it

virtual uint64_t encode(const float *x) const override

vectors need to be centroids (does not work on arbitrary vectors)

uint64_t get_nv(int ld, int r2a) const
uint64_t get_nv_cum(int ld, int r2t, int r2a) const
void set_nv_cum(int ld, int r2t, int r2a, uint64_t v)
void encode_multi(size_t nc, const float *c, uint64_t *codes) const
void decode_multi(size_t nc, const uint64_t *codes, float *c) const
void find_nn(size_t n, const uint64_t *codes, size_t nq, const float *xq, int64_t *idx, float *dis)

Public Members

int r2
int log2_dim
int code_size
std::vector<uint64_t> all_nv
std::vector<uint64_t> all_nv_cum
int decode_cache_ld
std::vector<std::vector<float>> decode_cache
uint64_t nv

size of the collection

int dim
struct ZnSphereCodecAlt : public faiss::ZnSphereCodec
#include <lattice_Zn.h>

Codec that uses the recursive codec if dim is a power of 2 and the regular one otherwise

Public Functions

ZnSphereCodecAlt(int dim, int r2)
virtual uint64_t encode(const float *x) const override

takes vectors that do not need to be centroids

virtual void decode(uint64_t code, float *c) const override

decode it

uint64_t search_and_encode(const float *x) const
float search(const float *x, float *c) const

find nearest centroid. x does not need to be normalized

float search(const float *x, float *c, float *tmp, int *tmp_int, int *ibest_out = nullptr) const

full call. Requires externally-allocated temp space

void search_multi(int n, const float *x, float *c_out, float *dp_out)
void encode_multi(size_t nc, const float *c, uint64_t *codes) const
void decode_multi(size_t nc, const uint64_t *codes, float *c) const
void find_nn(size_t n, const uint64_t *codes, size_t nq, const float *xq, int64_t *idx, float *dis)

Public Members

bool use_rec
ZnSphereCodecRec znc_rec
std::vector<CodeSegment> code_segments
uint64_t nv
size_t code_size
int dimS
int r2
int natom
std::vector<float> voc

size dim * ntatom

int dim