文件 NSG.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 NSG

公共类型

using storage_idx_t = int32_t

向量的内部存储(32 位:这很昂贵)

using Node = nsg::Node
using Neighbor = nsg::Neighbor

公共函数

explicit NSG(int R = 32)
void build(Index *storage, idx_t n, const nsg::Graph<idx_t> &knn_graph, bool verbose)
void reset()
void search(DistanceComputer &dis, int k, idx_t *I, float *D, VisitedTable &vt) const
void init_graph(Index *storage, const nsg::Graph<idx_t> &knn_graph)
template<bool collect_fullset, class index_t>
void search_on_graph(const nsg::Graph<index_t> &graph, DistanceComputer &dis, VisitedTable &vt, int ep, int pool_size, std::vector<Neighbor> &retset, std::vector<Node> &fullset) const
void add_reverse_links(int q, std::vector<std::mutex> &locks, DistanceComputer &dis, nsg::Graph<Node> &graph)
void sync_prune(int q, std::vector<Node> &pool, DistanceComputer &dis, VisitedTable &vt, const nsg::Graph<idx_t> &knn_graph, nsg::Graph<Node> &graph)
将节点链接到图。参数:存储索引,knn图(idx_t类型),图(Node类型),是否显示详细信息。
扩展树结构。参数:存储索引,节点度数向量。
执行深度优先搜索。参数:已访问表,根节点,计数器。返回一个整数。
连接未连接的节点。参数:存储索引,两个已访问表,节点度数向量。返回一个整数。
检查图的完整性。

公共成员

节点数量

节点数量

每个节点的邻居数

每个节点的邻居数

构造时搜索路径的长度

构造时搜索路径的长度

构造时候选池的大小

构造时候选池的大小

搜索路径的长度

搜索路径的长度

int enterpoint

进入点

std::shared_ptr<nsg::Graph<int32_t>> final_graph

NSG 图结构。

bool is_built = false

NSG 是否已构建。

RandomGenerator rng

随机数生成器

namespace nsg

函数

DistanceComputer *storage_distance_computer(const Index *storage)
template<class node_t>
struct Graph

公共函数

inline Graph(node_t *data, int N, int K)
inline Graph(int N, int K)
inline Graph(const Graph &g)
inline virtual ~Graph()
inline node_t at(int i, int j) const
inline node_t &at(int i, int j)
inline virtual size_t get_neighbors(int i, node_t *neighbors) const

公共成员

node_t *data

展平的邻接矩阵,大小为 N-by-K

int K

每个节点的邻居数

int N

节点总数

bool own_fields

底层数据是否由自身拥有