文件 IndexIVFPQ.h
-
namespace faiss
实现具有多种变体的 k-means 聚类。
版权所有 (c) Facebook, Inc. 及其附属公司。
此源代码已根据 MIT 许可证获得许可,该许可证位于此源树的根目录中的 LICENSE 文件中。
IDSelector 旨在定义要处理的向量子集(用于删除或作为搜索子集)
PQ4 SIMD 压缩和累加函数
基本内核使用 bbs = nb * 2 * 16 个向量累加 nq 查询向量,并产生一个输出矩阵。 对于 nq * nb <= 4 来说,这很有趣,否则寄存器溢出将变得太大。
这些函数的实现分布在 3 个 cpp 文件中,以减少并行编译时间。 模板被显式实例化。
此文件包含用于计算距离的内核的回调。
在整个库中,向量以 float * 指针的形式提供。 当批量处理(添加/搜索)多个向量时,大多数算法都可以得到优化。 在这种情况下,它们作为矩阵传递。 当 n 个大小为 d 的向量作为 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
定义了一些将转换应用于向量集的对象。 通常这些是预处理步骤。
函数
-
void initialize_IVFPQ_precomputed_table(int &use_precomputed_table, const Index *quantizer, const ProductQuantizer &pq, AlignedTable<float> &precomputed_table, bool by_residual, bool verbose)
为具有 by-residual 和 METRIC_L2 的 IVFPQ 预计算距离表
- 参数:
use_precomputed_table – (I/O) =-1:强制禁用 =0:启发式决定(默认:仅当表 < precomputed_tables_max_bytes 时才使用表),在输出时设置 use_precomputed_table =1:适用于所有量化器的表(大小 256 * nlist * M) =2:MultiIndexQuantizer 的特定版本(更紧凑)
precomputed_table – 要初始化的预计算表
变量
- FAISS_API size_t precomputed_table_max_bytes
- FAISS_API int index_ivfpq_add_core_o_bs
- FAISS_API IndexIVFPQStats indexIVFPQ_stats
-
struct IVFPQSearchParameters : public faiss::SearchParametersIVF
-
struct IndexIVFPQ : public faiss::IndexIVF
- #include <IndexIVFPQ.h>
带有 Product Quantizer 编码的倒排文件。每个残差向量都被编码为乘积量化器代码。
由 faiss::IndexIVFPQR 继承
公共函数
-
IndexIVFPQ(Index *quantizer, size_t d, size_t nlist, size_t M, size_t nbits_per_idx, MetricType metric = METRIC_L2)
-
virtual void encode_vectors(idx_t n, const float *x, const idx_t *list_nos, uint8_t *codes, bool include_listnos = false) const override
将一组向量编码为它们在倒排列表中显示的形式
- 参数:
list_nos – 量化器返回的倒排列表 ID(大小为 n)。-1s 被忽略。
codes – 输出代码,大小为 n * code_size
include_listno – 将列表 ID 包含在代码中(在这种情况下,将 ceil(log8(nlist)) 添加到代码大小)
-
virtual void sa_decode(idx_t n, const uint8_t *bytes, float *x) const override
解码一组向量
- 参数:
n – 向量的数量
bytes – 输入编码向量,大小为 n * sa_code_size()
x – 输出向量,大小为 n * d
-
virtual void add_core(idx_t n, const float *x, const idx_t *xids, const idx_t *precomputed_idx, void *inverted_list_context = nullptr) override
向量加法的实现,其中向量分配是预定义的。 默认实现将代码提取交给 encode_vectors。
- 参数:
precomputed_idx – 输入向量的量化索引 (大小 n)
-
void add_core_o(idx_t n, const float *x, const idx_t *xids, float *residuals_2, const idx_t *precomputed_idx = nullptr, void *inverted_list_context = nullptr)
与 add_core 相同,也
如果 residuals_2 != NULL,则输出第二级残差
接受 precomputed_idx = nullptr
-
virtual void reconstruct_from_offset(int64_t list_no, int64_t offset, float *recons) const override
根据 (逆列表索引 + 逆列表偏移量) 而不是 id 来重建向量。
当不维护 direct_map 并且逆列表偏移量由 search_preassigned() 计算并且设置了
store_pairs
时,可用于重建。
-
size_t find_duplicates(idx_t *ids, size_t *lims) const
在数据集中查找完全重复项。
重复项在预先分配的数组中返回(参见最大大小)。
- 参数:
lims – 重复项组之间的限制 (最大大小 ntotal / 2 + 1)
ids – ids[lims[i]] : ids[lims[i+1]-1] 是一组重复项 (最大大小 ntotal)
- Returns:
n 找到的组数
-
void encode_multiple(size_t n, idx_t *keys, const float *x, uint8_t *codes, bool compute_keys = false) const
对多个向量进行编码
- 参数:
n – 要编码的向量的数量
keys – 这些向量的倒排列表 ID(大小为 n)
x – 向量(大小为 n * d)
codes – 输出代码(大小为 n * code_size)
compute_keys – 如果为 false,则假定已预先计算了键,否则计算它们
-
void decode_multiple(size_t n, const idx_t *keys, const uint8_t *xcodes, float *x) const
encode_multiple 的逆操作
-
virtual InvertedListScanner *get_InvertedListScanner(bool store_pairs, const IDSelector *sel) const override
获取此索引的扫描器 (store_pairs 表示忽略标签)
默认搜索实现使用它来计算距离
-
void precompute_table()
构建预计算表
-
IndexIVFPQ()
公共成员
-
ProductQuantizer pq
生成代码
-
bool do_polysemous_training
训练后重新排序 PQ 质心?
-
PolysemousTraining *polysemous_training
如果为 NULL,则使用默认值
-
size_t scan_table_threshold
使用表计算还是即时计算?
-
int polysemous_ht
多义过滤的汉明阈值。
-
int use_precomputed_table
预计算表,以加快查询预处理的速度,但会增加一些内存成本(仅用于使用 L2 度量的 by_residual)
-
AlignedTable<float> precomputed_table
如果 use_precompute_table 的大小为 nlist * pq.M * pq.ksub
-
IndexIVFPQ(Index *quantizer, size_t d, size_t nlist, size_t M, size_t nbits_per_idx, MetricType metric = METRIC_L2)
-
struct IndexIVFPQStats
- #include <IndexIVFPQ.h>
统计信息对于内部线程是稳健的,但如果 IndexIVFPQ::search_preassigned 由多个线程调用则不然
-
void initialize_IVFPQ_precomputed_table(int &use_precomputed_table, const Index *quantizer, const ProductQuantizer &pq, AlignedTable<float> &precomputed_table, bool by_residual, bool verbose)