文件 IndexIVFAdditiveQuantizer.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 IndexIVFAdditiveQuantizer : public faiss::IndexIVF
- #include <IndexIVFAdditiveQuantizer.h>
IVF 附加量化器的抽象类。 搜索功能是通用的。
由 faiss::IndexIVFLocalSearchQuantizer、faiss::IndexIVFProductLocalSearchQuantizer、faiss::IndexIVFProductResidualQuantizer、faiss::IndexIVFResidualQuantizer 子类化
公共类型
-
using Search_type_t = AdditiveQuantizer::Search_type_t
公共函数
-
IndexIVFAdditiveQuantizer(AdditiveQuantizer *aq, Index *quantizer, size_t d, size_t nlist, MetricType metric = METRIC_L2)
-
explicit IndexIVFAdditiveQuantizer(AdditiveQuantizer *aq)
-
virtual void train_encoder(idx_t n, const float *x, const idx_t *assign) override
训练向量的编码器。
如果 by_residual,则使用残差和相应的 assign 数组调用它,否则 x 是原始训练向量,assign=nullptr
-
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 InvertedListScanner *get_InvertedListScanner(bool store_pairs, const IDSelector *sel) const override
获取此索引的扫描器(store_pairs 意味着忽略标签)
默认搜索实现使用它来计算距离
-
virtual void sa_decode(idx_t n, const uint8_t *codes, float *x) const override
解码一组向量
- 参数:
n – 向量数
bytes – 输入编码向量,大小为 n * sa_code_size()
x – 输出向量,大小为 n * d
-
virtual void reconstruct_from_offset(int64_t list_no, int64_t offset, float *recons) const override
给定位置(以 inv 列表索引 + inv 列表偏移量表示)而不是 id,重建向量。
当不维护 direct_map 并且 inv 列表偏移量由 search_preassigned() 与
store_pairs
设置计算时很有用。
-
~IndexIVFAdditiveQuantizer() override
-
using Search_type_t = AdditiveQuantizer::Search_type_t
-
struct IndexIVFResidualQuantizer : public faiss::IndexIVFAdditiveQuantizer
- #include <IndexIVFAdditiveQuantizer.h>
基于残差量化器的 IndexIVF。存储的向量通过残差量化代码近似表示。
公共类型
-
using Search_type_t = AdditiveQuantizer::Search_type_t
公共函数
-
IndexIVFResidualQuantizer(Index *quantizer, size_t d, size_t nlist, const std::vector<size_t> &nbits, MetricType metric = METRIC_L2, Search_type_t search_type = AdditiveQuantizer::ST_decompress)
构造函数。
- 参数:
d – 输入向量的维度
M – 子量化器的数量
nbits – 每个子向量索引的位数
-
IndexIVFResidualQuantizer(Index *quantizer, size_t d, size_t nlist, size_t M, size_t nbits, MetricType metric = METRIC_L2, Search_type_t search_type = AdditiveQuantizer::ST_decompress)
-
IndexIVFResidualQuantizer()
-
virtual ~IndexIVFResidualQuantizer()
-
virtual void train_encoder(idx_t n, const float *x, const idx_t *assign) override
训练向量的编码器。
如果 by_residual,则使用残差和相应的 assign 数组调用它,否则 x 是原始训练向量,assign=nullptr
-
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 InvertedListScanner *get_InvertedListScanner(bool store_pairs, const IDSelector *sel) const override
获取此索引的扫描器(store_pairs 意味着忽略标签)
默认搜索实现使用它来计算距离
-
virtual void sa_decode(idx_t n, const uint8_t *codes, float *x) const override
解码一组向量
- 参数:
n – 向量数
bytes – 输入编码向量,大小为 n * sa_code_size()
x – 输出向量,大小为 n * d
-
virtual void reconstruct_from_offset(int64_t list_no, int64_t offset, float *recons) const override
给定位置(以 inv 列表索引 + inv 列表偏移量表示)而不是 id,重建向量。
当不维护 direct_map 并且 inv 列表偏移量由 search_preassigned() 与
store_pairs
设置计算时很有用。
-
using Search_type_t = AdditiveQuantizer::Search_type_t
-
struct IndexIVFLocalSearchQuantizer : public faiss::IndexIVFAdditiveQuantizer
- #include <IndexIVFAdditiveQuantizer.h>
基于残差量化器的 IndexIVF。存储的向量通过残差量化代码近似表示。
公共类型
-
using Search_type_t = AdditiveQuantizer::Search_type_t
公共函数
-
IndexIVFLocalSearchQuantizer(Index *quantizer, size_t d, size_t nlist, size_t M, size_t nbits, MetricType metric = METRIC_L2, Search_type_t search_type = AdditiveQuantizer::ST_decompress)
构造函数。
- 参数:
d – 输入向量的维度
M – 子量化器的数量
nbits – 每个子向量索引的位数
-
IndexIVFLocalSearchQuantizer()
-
virtual ~IndexIVFLocalSearchQuantizer()
-
virtual void train_encoder(idx_t n, const float *x, const idx_t *assign) override
训练向量的编码器。
如果 by_residual,则使用残差和相应的 assign 数组调用它,否则 x 是原始训练向量,assign=nullptr
-
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 InvertedListScanner *get_InvertedListScanner(bool store_pairs, const IDSelector *sel) const override
获取此索引的扫描器(store_pairs 意味着忽略标签)
默认搜索实现使用它来计算距离
-
virtual void sa_decode(idx_t n, const uint8_t *codes, float *x) const override
解码一组向量
- 参数:
n – 向量数
bytes – 输入编码向量,大小为 n * sa_code_size()
x – 输出向量,大小为 n * d
-
virtual void reconstruct_from_offset(int64_t list_no, int64_t offset, float *recons) const override
给定位置(以 inv 列表索引 + inv 列表偏移量表示)而不是 id,重建向量。
当不维护 direct_map 并且 inv 列表偏移量由 search_preassigned() 与
store_pairs
设置计算时很有用。
-
using Search_type_t = AdditiveQuantizer::Search_type_t
-
struct IndexIVFProductResidualQuantizer : public faiss::IndexIVFAdditiveQuantizer
- #include <IndexIVFAdditiveQuantizer.h>
基于乘积残差量化器的IndexIVF。 存储的向量通过乘积残差量化码近似。
公共类型
-
using Search_type_t = AdditiveQuantizer::Search_type_t
公共函数
-
IndexIVFProductResidualQuantizer(Index *quantizer, size_t d, size_t nlist, size_t nsplits, size_t Msub, size_t nbits, MetricType metric = METRIC_L2, Search_type_t search_type = AdditiveQuantizer::ST_decompress)
构造函数。
- 参数:
d – 输入向量的维度
nsplits – 残差量化器的数量
Msub – 每个RQ的子量化器的数量
nbits – 每个子向量索引的位数
-
IndexIVFProductResidualQuantizer()
-
virtual ~IndexIVFProductResidualQuantizer()
-
virtual void train_encoder(idx_t n, const float *x, const idx_t *assign) override
训练向量的编码器。
如果 by_residual,则使用残差和相应的 assign 数组调用它,否则 x 是原始训练向量,assign=nullptr
-
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 InvertedListScanner *get_InvertedListScanner(bool store_pairs, const IDSelector *sel) const override
获取此索引的扫描器(store_pairs 意味着忽略标签)
默认搜索实现使用它来计算距离
-
virtual void sa_decode(idx_t n, const uint8_t *codes, float *x) const override
解码一组向量
- 参数:
n – 向量数
bytes – 输入编码向量,大小为 n * sa_code_size()
x – 输出向量,大小为 n * d
-
virtual void reconstruct_from_offset(int64_t list_no, int64_t offset, float *recons) const override
给定位置(以 inv 列表索引 + inv 列表偏移量表示)而不是 id,重建向量。
当不维护 direct_map 并且 inv 列表偏移量由 search_preassigned() 与
store_pairs
设置计算时很有用。
-
using Search_type_t = AdditiveQuantizer::Search_type_t
-
struct IndexIVFProductLocalSearchQuantizer : public faiss::IndexIVFAdditiveQuantizer
- #include <IndexIVFAdditiveQuantizer.h>
基于乘积局部搜索量化器的IndexIVF。 存储的向量由乘积局部搜索量化码近似。
公共类型
-
using Search_type_t = AdditiveQuantizer::Search_type_t
公共函数
-
IndexIVFProductLocalSearchQuantizer(Index *quantizer, size_t d, size_t nlist, size_t nsplits, size_t Msub, size_t nbits, MetricType metric = METRIC_L2, Search_type_t search_type = AdditiveQuantizer::ST_decompress)
构造函数。
- 参数:
d – 输入向量的维度
nsplits – 本地搜索量化器的数量
Msub – 每个 LSQ 的子量化器数量
nbits – 每个子向量索引的位数
-
IndexIVFProductLocalSearchQuantizer()
-
virtual ~IndexIVFProductLocalSearchQuantizer()
-
virtual void train_encoder(idx_t n, const float *x, const idx_t *assign) override
训练向量的编码器。
如果 by_residual,则使用残差和相应的 assign 数组调用它,否则 x 是原始训练向量,assign=nullptr
-
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 InvertedListScanner *get_InvertedListScanner(bool store_pairs, const IDSelector *sel) const override
获取此索引的扫描器(store_pairs 意味着忽略标签)
默认搜索实现使用它来计算距离
-
virtual void sa_decode(idx_t n, const uint8_t *codes, float *x) const override
解码一组向量
- 参数:
n – 向量数
bytes – 输入编码向量,大小为 n * sa_code_size()
x – 输出向量,大小为 n * d
-
virtual void reconstruct_from_offset(int64_t list_no, int64_t offset, float *recons) const override
给定位置(以 inv 列表索引 + inv 列表偏移量表示)而不是 id,重建向量。
当不维护 direct_map 并且 inv 列表偏移量由 search_preassigned() 与
store_pairs
设置计算时很有用。
-
using Search_type_t = AdditiveQuantizer::Search_type_t
-
struct IndexIVFAdditiveQuantizer : public faiss::IndexIVF