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

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

函数

inline uint64_t lo_build(uint64_t list_id, uint64_t offset)
inline uint64_t lo_listno(uint64_t lo)
inline uint64_t lo_offset(uint64_t lo)
struct DirectMap
#include <DirectMap.h>

直接映射:一种从 ID 反向映射到倒排列表的方法

公共类型

enum Type

enumerator NoMap
enumerator Array
enumerator Hashtable

公共函数

DirectMap()
void set_type(Type new_type, const InvertedLists *invlists, size_t ntotal)

设置类型并初始化

idx_t get(idx_t id) const

获取一个条目

inline bool no() const

用于快速检查

void check_can_add(const idx_t *ids)

如果为 Array 且 ids 不为 NULL,则抛出异常

更新 direct_map

void add_single_id(idx_t id, idx_t list_no, size_t offset)

非线程安全版本

void clear()

删除所有条目

size_t remove_ids(const IDSelector &sel, InvertedLists *invlists)

InvertedLists 中删除 ID,可能会使用直接映射

倒排列表上的操作需要通过 DirectMap 进行转换

void update_codes(InvertedLists *invlists, int n, const idx_t *ids, const idx_t *list_nos, const uint8_t *codes)

使用直接映射更新条目

公共成员

Type type
std::vector<idx_t> array

用于直接访问元素的映射。将 ID 映射到 LO 编码的条目。

std::unordered_map<idx_t, idx_t> hashtable
struct DirectMapAdd
#include <DirectMap.h>

更新 direct_map 的线程安全方式。

公共类型

using Type = DirectMap::Type

公共函数

DirectMapAdd(DirectMap &direct_map, size_t n, const idx_t *xids)
void add(size_t i, idx_t list_no, size_t offset)

在 list_no 和 offset 处添加向量 i (id 为 xids[i])

~DirectMapAdd()

公共成员

DirectMap &direct_map
DirectMap::Type type
size_t ntotal
size_t n
const idx_t *xids
std::vector<idx_t> all_ofs