文件 IndexAdditiveQuantizerFastScan.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 IndexAdditiveQuantizerFastScan : public faiss::IndexFastScan
- #include <IndexAdditiveQuantizerFastScan.h>
IndexAQ 的快速扫描版本。目前适用于 4 位 AQ。
代码不是顺序存储的,而是以大小为 bbs 的块分组。这使得可以使用 SIMD 指令快速计算距离。
实现:12:具有 Q 上内部循环的阻塞循环,qbs 13:相同,使用存储结果的储层累加器 14:没有带有堆累加器的 qbs 15:没有带有储层累加器的 qbs
被以下类继承:faiss::IndexLocalSearchQuantizerFastScan, faiss::IndexProductLocalSearchQuantizerFastScan, faiss::IndexProductResidualQuantizerFastScan, faiss::IndexResidualQuantizerFastScan
公共类型
-
using Search_type_t = AdditiveQuantizer::Search_type_t
公共函数
-
explicit IndexAdditiveQuantizerFastScan(AdditiveQuantizer *aq, MetricType metric = METRIC_L2, int bbs = 32)
-
void init(AdditiveQuantizer *aq, MetricType metric = METRIC_L2, int bbs = 32)
-
IndexAdditiveQuantizerFastScan()
-
~IndexAdditiveQuantizerFastScan() override
-
explicit IndexAdditiveQuantizerFastScan(const IndexAdditiveQuantizer &orig, int bbs = 32)
从现有的 IndexAQ 构建
-
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 – 输出 NNs 的标签,大小为 n*k
-
virtual void sa_decode(idx_t n, const uint8_t *bytes, float *x) const override
解码一组向量。
注意:IndexAdditiveQuantizerFastScan 对象中的代码是不连续的。 但是此方法需要连续表示。
- 参数:
n – 向量的数量
bytes – 输入编码向量,大小为 n * code_size
x – 输出向量,大小为 n * d
-
using Search_type_t = AdditiveQuantizer::Search_type_t
-
struct IndexResidualQuantizerFastScan : public faiss::IndexAdditiveQuantizerFastScan
- #include <IndexAdditiveQuantizerFastScan.h>
基于残差量化器的Index。 存储的向量通过残差量化代码进行近似。 也可以用作编解码器
公共类型
-
using Search_type_t = AdditiveQuantizer::Search_type_t
公共函数
-
IndexResidualQuantizerFastScan(int d, size_t M, size_t nbits, MetricType metric = METRIC_L2, Search_type_t search_type = AdditiveQuantizer::ST_norm_rq2x4, int bbs = 32)
构造函数。
- 参数:
d – 输入向量的维度
M – 子量化器的数量
nbits – 每个子向量索引的位数
d – 输入向量的维度
M – 子量化器的数量
nbits – 每个子向量索引的位数
metric – 度量类型
search_type – AQ 搜索类型
-
IndexResidualQuantizerFastScan()
-
void init(AdditiveQuantizer *aq, MetricType metric = METRIC_L2, int bbs = 32)
-
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 – 输出 NNs 的标签,大小为 n*k
-
virtual void sa_decode(idx_t n, const uint8_t *bytes, float *x) const override
解码一组向量。
注意:IndexAdditiveQuantizerFastScan 对象中的代码是不连续的。 但是此方法需要连续表示。
- 参数:
n – 向量的数量
bytes – 输入编码向量,大小为 n * code_size
x – 输出向量,大小为 n * d
公共成员
-
ResidualQuantizer rq
The residual quantizer used to encode the vectors.
-
bool rescale_norm = true
-
int norm_scale = 1
-
size_t max_train_points = 0
-
using Search_type_t = AdditiveQuantizer::Search_type_t
-
struct IndexLocalSearchQuantizerFastScan : public faiss::IndexAdditiveQuantizerFastScan
- #include <IndexAdditiveQuantizerFastScan.h>
基于本地搜索量化器的Index。存储的向量通过本地搜索量化代码近似。也可以用作编解码器
公共类型
-
using Search_type_t = AdditiveQuantizer::Search_type_t
公共函数
-
IndexLocalSearchQuantizerFastScan(int d, size_t M, size_t nbits, MetricType metric = METRIC_L2, Search_type_t search_type = AdditiveQuantizer::ST_norm_lsq2x4, int bbs = 32)
构造函数。
- 参数:
d – 输入向量的维度
M – 子量化器的数量
nbits – 每个子向量索引的位数
d – 输入向量的维度
M – 子量化器的数量
nbits – 每个子向量索引的位数
metric – 度量类型
search_type – AQ 搜索类型
-
IndexLocalSearchQuantizerFastScan()
-
init(AdditiveQuantizer *aq, MetricType metric = METRIC_L2, int bbs = 32)
-
virtual void train(idx_t n, const float *x) override
对具有代表性的向量集执行训练
- 参数:
n – 训练向量的数量
x – 训练向量,大小为 n * d
-
estimate_norm_scale(idx_t n, const float *x)
-
virtual void compute_codes(uint8_t *codes, idx_t n, const float *x) const override
-
virtual void compute_float_LUT(float *lut, idx_t n, const float *x) const override
-
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 – 输出 NNs 的标签,大小为 n*k
-
virtual void sa_decode(idx_t n, const uint8_t *bytes, float *x) const override
解码一组向量。
注意:IndexAdditiveQuantizerFastScan 对象中的代码是不连续的。 但是此方法需要连续表示。
- 参数:
n – 向量的数量
bytes – 输入编码向量,大小为 n * code_size
x – 输出向量,大小为 n * d
-
using Search_type_t = AdditiveQuantizer::Search_type_t
-
struct IndexProductResidualQuantizerFastScan : public faiss::IndexAdditiveQuantizerFastScan
- #include <IndexAdditiveQuantizerFastScan.h>
Index based on a product residual quantizer. Stored vectors are approximated by product residual quantization codes. Can also be used as a codec
公共类型
-
using Search_type_t = AdditiveQuantizer::Search_type_t
公共函数
-
IndexProductResidualQuantizerFastScan(int d, size_t nsplits, size_t Msub, size_t nbits, MetricType metric = METRIC_L2, Search_type_t search_type = AdditiveQuantizer::ST_norm_rq2x4, int bbs = 32)
构造函数。
- 参数:
d – 输入向量的维度
nsplits – number of residual quantizers
Msub – number of subquantizers per RQ
nbits – 每个子向量索引的位数
d – 输入向量的维度
nsplits – number of residual quantizers
Msub – number of subquantizers per RQ
nbits – 每个子向量索引的位数
metric – 度量类型
search_type – AQ 搜索类型
-
IndexProductResidualQuantizerFastScan()
-
void init(AdditiveQuantizer *aq, MetricType metric = METRIC_L2, int bbs = 32)
-
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 – 输出 NNs 的标签,大小为 n*k
-
virtual void sa_decode(idx_t n, const uint8_t *bytes, float *x) const override
解码一组向量。
注意:IndexAdditiveQuantizerFastScan 对象中的代码是不连续的。 但是此方法需要连续表示。
- 参数:
n – 向量的数量
bytes – 输入编码向量,大小为 n * code_size
x – 输出向量,大小为 n * d
公共成员
-
ProductResidualQuantizer prq
用于编码向量的乘积残差量化器。
-
bool rescale_norm = true
-
int norm_scale = 1
-
size_t max_train_points = 0
-
using Search_type_t = AdditiveQuantizer::Search_type_t
-
struct IndexProductLocalSearchQuantizerFastScan : public faiss::IndexAdditiveQuantizerFastScan
- #include <IndexAdditiveQuantizerFastScan.h>
基于乘积局部搜索量化器的 Index。 存储的向量通过乘积局部搜索量化代码进行近似。 也可以用作编解码器
公共类型
-
using Search_type_t = AdditiveQuantizer::Search_type_t
公共函数
-
IndexProductLocalSearchQuantizerFastScan(int d, size_t nsplits, size_t Msub, size_t nbits, MetricType metric = METRIC_L2, Search_type_t search_type = AdditiveQuantizer::ST_norm_rq2x4, int bbs = 32)
构造函数。
- 参数:
d – 输入向量的维度
nsplits – 局部搜索量化器的数量
Msub – 每个 LSQ 的子量化器数量
nbits – 每个子向量索引的位数
d – 输入向量的维度
nsplits – 局部搜索量化器的数量
Msub – 每个 LSQ 的子量化器数量
nbits – 每个子向量索引的位数
metric – 度量类型
search_type – AQ 搜索类型
-
IndexProductLocalSearchQuantizerFastScan()
-
void init(AdditiveQuantizer *aq, MetricType metric = METRIC_L2, int bbs = 32)
-
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 – 输出 NNs 的标签,大小为 n*k
-
virtual void sa_decode(idx_t n, const uint8_t *bytes, float *x) const override
解码一组向量。
注意:IndexAdditiveQuantizerFastScan 对象中的代码是不连续的。 但是此方法需要连续表示。
- 参数:
n – 向量的数量
bytes – 输入编码向量,大小为 n * code_size
x – 输出向量,大小为 n * d
公共成员
-
ProductLocalSearchQuantizer plsq
用于编码向量的乘积局部搜索量化器。
-
bool rescale_norm = true
-
int norm_scale = 1
-
size_t max_train_points = 0
-
using Search_type_t = AdditiveQuantizer::Search_type_t
-
struct IndexAdditiveQuantizerFastScan : public faiss::IndexFastScan