File NNDescent.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 NNDescent

Public Types

using storage_idx_t = int
using KNNGraph = std::vector<nndescent::Nhood>

Public Functions

explicit NNDescent(const int d, const int K)
~NNDescent()
void build(DistanceComputer &qdis, const int n, bool verbose)
void search(DistanceComputer &qdis, const int topk, idx_t *indices, float *dists, VisitedTable &vt) const
void reset()
void init_graph(DistanceComputer &qdis)

Initialize the KNN graph randomly.

void nndescent(DistanceComputer &qdis, bool verbose)

Perform NNDescent algorithm.

void join(DistanceComputer &qdis)

Perform local join on each node.

void update()

Sample new neighbors for each node to peform local join later.

void generate_eval_set(DistanceComputer &qdis, std::vector<int> &c, std::vector<std::vector<int>> &v, int N)

Sample a small number of points to evaluate the quality of KNNG built.

float eval_recall(std::vector<int> &ctrl_points, std::vector<std::vector<int>> &acc_eval_set)

Evaluate the quality of KNNG built.

Public Members

bool has_built = false
int S = 10
int R = 100
int iter = 10
int search_L = 0
int random_seed = 2021
int K
int d
int L
int ntotal = 0
KNNGraph graph
std::vector<int> final_graph
namespace nndescent
struct Neighbor

Public Functions

Neighbor() = default
inline Neighbor(int id, float distance, bool f)
inline bool operator<(const Neighbor &other) const

Public Members

int id
float distance
bool flag
struct Nhood

Public Functions

Nhood() = default
Nhood(int l, int s, std::mt19937 &rng, int N)
Nhood &operator=(const Nhood &other)
Nhood(const Nhood &other)
void insert(int id, float dist)
template<typename C>
void join(C callback) const

Public Members

std::mutex lock
std::vector<Neighbor> pool
int M
std::vector<int> nn_old
std::vector<int> nn_new
std::vector<int> rnn_old
std::vector<int> rnn_new