文件 IndexPQ.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
定义一些将变换应用于向量集的对象 通常这些是预处理步骤。
变量
- FAISS_API IndexPQStats indexPQ_stats
- FAISS_API int multi_index_quantizer_search_bs
-
struct IndexPQ : public faiss::IndexFlatCodes
- #include <IndexPQ.h>
基于乘积量化器的索引。存储的向量通过 PQ 代码近似。
公共类型
公共函数
-
IndexPQ(int d, size_t M, size_t nbits, MetricType metric = METRIC_L2)
构造函数。
- 参数:
d – 输入向量的维度
M – 子量化器的数量
nbits – 每个子向量索引的位数
-
IndexPQ()
-
virtual void train(idx_t n, const float *x) override
在一组代表性向量上执行训练
- 参数:
n – 训练向量的数量
x – 训练向量,大小为 n * d
-
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const SearchParameters *params = nullptr) const override
通过解码实现的搜索
-
virtual void sa_encode(idx_t n, const float *x, uint8_t *bytes) const override
编码一组向量
- 参数:
n – 向量的数量
x – 输入向量,大小为 n * d
bytes – 输出编码向量,大小为 n * sa_code_size()
-
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 FlatCodesDistanceComputer *get_FlatCodesDistanceComputer() const override
FlatCodesDistanceComputer 提供了一种 distance_to_code 方法
默认实现使用 sa_decode 显式解码向量。
-
void search_core_polysemous(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, int polysemous_ht, bool generalized_hamming) const
公共成员
-
ProductQuantizer pq
用于编码向量的乘积量化器。
-
bool do_polysemous_training
false = 标准 PQ
-
PolysemousTraining polysemous_training
用于多义训练的参数
-
Search_type_t search_type
-
bool encode_signs
-
int polysemous_ht
用于多义的汉明阈值。
-
IndexPQ(int d, size_t M, size_t nbits, MetricType metric = METRIC_L2)
-
struct SearchParametersPQ : public faiss::SearchParameters
- #include <IndexPQ.h>
覆盖类的搜索参数
-
struct IndexPQStats
- #include <IndexPQ.h>
统计信息对于内部线程是可靠的,但如果 IndexPQ::search 被多个线程调用则不然
-
struct MultiIndexQuantizer : public faiss::Index
- #include <IndexPQ.h>
Quantizer,其中质心是虚拟的:它们是子质心的笛卡尔积。
由 faiss::MultiIndexQuantizer2 继承
公共函数
-
MultiIndexQuantizer(int d, size_t M, size_t nbits)
每个子向量索引的位数
- 参数:
d – 输入向量的维度
M – 子量化器的数量
-
virtual void train(idx_t n, const float *x) override
在一组代表性向量上执行训练
- 参数:
n – 训练向量的数量
x – 训练向量,大小为 n * d
-
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const SearchParameters *params = nullptr) const override
查询索引中维度为 d 的 n 个向量。
最多返回 k 个向量。如果查询的结果不够,则结果数组会用 -1 填充。
- 参数:
n – 向量的数量
x – 要搜索的输入向量,大小为 n * d
k – 提取的向量数量
distances – 输出成对距离,大小为 n*k
labels – 输出最近邻的标签,大小为 n*k
-
virtual void reset() override
从数据库中删除所有元素。
-
inline MultiIndexQuantizer()
公共成员
-
MultiIndexQuantizer(int d, size_t M, size_t nbits)
-
struct MultiIndexQuantizer2 : public faiss::MultiIndexQuantizer
- #include <IndexPQ.h>
MultiIndexQuantizer,其中 PQ 分配由子索引执行
公共函数
-
virtual void train(idx_t n, const float *x) override
在一组代表性向量上执行训练
- 参数:
n – 训练向量的数量
x – 训练向量,大小为 n * d
-
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const SearchParameters *params = nullptr) const override
查询索引中维度为 d 的 n 个向量。
最多返回 k 个向量。如果查询的结果不够,则结果数组会用 -1 填充。
- 参数:
n – 向量的数量
x – 要搜索的输入向量,大小为 n * d
k – 提取的向量数量
distances – 输出成对距离,大小为 n*k
labels – 输出最近邻的标签,大小为 n*k
-
virtual void reset() override
从数据库中删除所有元素。
-
virtual void train(idx_t n, const float *x) override