文件 NNDescent.h

namespace faiss

实现了具有多种变体的 k-means 聚类。

版权所有 (c) Facebook, Inc. 及其附属公司。

此源代码已根据 MIT 许可证获得许可,该许可证位于此源树的根目录中的 LICENSE 文件中。

IDSelector 旨在定义要处理的向量子集(用于移除或作为搜索的子集)

PQ4 SIMD 打包和累加函数

基本内核使用 bbs = nb * 2 * 16 向量累加 nq 查询向量,并生成一个输出矩阵。 对于 nq * nb <= 4 来说是有用的,否则寄存器溢出太大。

这些函数的实现分布在 3 个 cpp 文件中,以减少并行编译时间。 模板被显式实例化。

此文件包含计算距离的内核的回调。

在整个库中,向量以 float * 指针的形式提供。 当批量处理(添加/搜索)多个向量时,大多数算法可以得到优化。 在这种情况下,它们作为矩阵传入。 当提供大小为 d 的 n 个向量作为 float * x 时,向量 i 的分量 j 是

x[ i * d + j ]

其中 0 <= i < n 且 0 <= j < d。 换句话说,矩阵始终是紧凑的。 指定矩阵大小时,我们称之为 n*d 矩阵,这意味着行优先存储。

I/O 函数可以读/写到文件名、文件句柄或抽象介质的对象。

读取函数返回的对象应使用 delete 释放。 这些对象中的所有引用都归该对象所有。

倒排列表的定义 + 一些实现该接口的常用类。

由于 IVF(倒排文件)索引对于大规模用例非常有用,因此我们将与它们相关的一些函数分组到这个小型库中。 大多数函数都适用于 IndexIVF 和嵌入在 IndexPreTransform 中的 IndexIVF。

在此文件中,实现了 L2 和内积之外的额外度量。

实现了一些神经网络层,主要用于支持 QINCo

定义了一些对象,这些对象将变换应用于一组向量。 这些通常是预处理步骤。

struct NNDescent

公共类型

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

公共函数

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)

随机初始化 KNN 图。

void nndescent(DistanceComputer &qdis, bool verbose)

执行 NNDescent 算法。

void join(DistanceComputer &qdis)

在每个节点上执行本地连接。

void update()

为每个节点采样新的邻居,以便稍后执行本地连接。

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

抽取少量点来评估 KNNG 构建的质量。

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

评估 KNNG 构建的质量。

公共成员

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

公共函数

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

公共成员

int id
float distance
bool flag
struct Nhood

公共函数

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 回调) const

公共成员

std::互斥锁 lock
std::vector<Neighbor>
int M
std::vector<int> nn_old
std::vector<int> nn_new
std::vector<int> rnn_old
std::vector<int> rnn_new