结构体 faiss::IndexIVFPQFastScan
-
struct IndexIVFPQFastScan : public faiss::IndexIVFFastScan
IVFPQ 的快速扫描版本。目前适用于 4 位 PQ。
倒排列表中的代码不是顺序存储的,而是以大小为 bbs 的块进行分组。这使得使用 SIMD 指令非常快速地计算距离成为可能。
实现 (implem): 0: 自动选择实现(默认) 1: orig 的搜索,重新实现 2: orig 的搜索,按 invlist 重新排序 10: 优化器 int16 搜索,在堆中收集结果,没有 qbs 11: idem,在 reservoir 中收集结果 12: 优化器 int16 搜索,在堆中收集结果,使用 qbs 13: idem,在 reservoir 中收集结果
公共类型
-
using component_t = float
-
using distance_t = float
公共函数
-
IndexIVFPQFastScan(Index *quantizer, size_t d, size_t nlist, size_t M, size_t nbits, MetricType metric = METRIC_L2, int bbs = 32)
-
IndexIVFPQFastScan()
-
explicit IndexIVFPQFastScan(const IndexIVFPQ &orig, int bbs = 32)
-
virtual void train_encoder(idx_t n, const float *x, const idx_t *assign) override
训练向量的编码器。
如果 by_residual 则使用残差和相应的 assign 数组调用它,否则 x 是原始训练向量并且 assign=nullptr
-
virtual idx_t train_encoder_num_vectors() const override
可以被子类重新定义以指示他们需要多少训练向量
-
void precompute_table()
构建预计算表,可能更新 use_precomputed_table
-
virtual void encode_vectors(idx_t n, const float *x, const idx_t *list_nos, uint8_t *codes, bool include_listno = false) const override
与常规 IVFPQ 编码器相同。 代码此时未被 a 块重新组织
-
virtual bool lookup_table_is_3d() const override
-
virtual void compute_LUT(size_t n, const float *x, const CoarseQuantized &cq, AlignedTable<float> &dis_tables, AlignedTable<float> &biases) const override
-
virtual void sa_decode(idx_t n, const uint8_t *bytes, float *x) const override
解码一组向量
- 参数:
n – 向量的数量
bytes – 输入编码的向量,大小为 n * sa_code_size()
x – 输出向量,大小为 n * d
-
void init_fastscan(size_t M, size_t nbits, size_t nlist, MetricType metric, int bbs)
-
void init_code_packer()
-
virtual void add_with_ids(idx_t n, const float *x, const idx_t *xids) override
调用 encode_vectors 的默认实现
-
void compute_LUT_uint8(size_t n, const float *x, const CoarseQuantized &cq, AlignedTable<uint8_t> &dis_tables, AlignedTable<uint16_t> &biases, float *normalizers) const
-
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const SearchParameters *params = nullptr) const override
分配向量,然后调用 search_preassign
-
virtual void search_preassigned(idx_t n, const float *x, idx_t k, const idx_t *assign, const float *centroid_dis, float *distances, idx_t *labels, bool store_pairs, const IVFSearchParameters *params = nullptr, IndexIVFStats *stats = nullptr) const override
搜索一组向量,这些向量已经通过 IVF 量化器进行了预量化。使用查询结果填充相应的堆。默认实现使用 InvertedListScanners 来进行搜索。
- 参数:
n – 要查询的向量数量
x – 查询向量,大小为 nx * d
assign – 粗量化索引,大小为 nx * nprobe
centroid_dis – 到粗糙质心的距离,大小为 nx * nprobe
distance – 输出距离,大小为 n * k
labels – 输出标签,大小为 n * k
store_pairs – 将倒排列表索引 + 倒排列表偏移量存储在结果的上/下 32 位中,而不是 ID(用于重排序)。
params – 用于覆盖对象的搜索参数
stats – 要更新的搜索统计信息(可以为 null)
-
virtual void range_search(idx_t n, const float *x, float radius, RangeSearchResult *result, const SearchParameters *params = nullptr) const override
查询索引中维度为 d 的 n 个向量。
返回所有距离 < radius 的向量。请注意,许多索引不实现 range_search(只有 k-NN 搜索是强制性的)。
- 参数:
n – 向量的数量
x – 要搜索的输入向量,大小为 n * d
radius – 搜索半径
result – 结果表
-
void search_dispatch_implem(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const CoarseQuantized &cq, const NormTableScaler *scaler, const IVFSearchParameters *params = nullptr) const
-
void range_search_dispatch_implem(idx_t n, const float *x, float radius, RangeSearchResult &rres, const CoarseQuantized &cq_in, const NormTableScaler *scaler, const IVFSearchParameters *params = nullptr) const
-
template<class C>
void search_implem_1(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const CoarseQuantized &cq, const NormTableScaler *scaler, const IVFSearchParameters *params = nullptr) const
-
template<class C>
void search_implem_2(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const CoarseQuantized &cq, const NormTableScaler *scaler, const IVFSearchParameters *params = nullptr) const
-
void search_implem_10(idx_t n, const float *x, SIMDResultHandlerToFloat &handler, const CoarseQuantized &cq, size_t *ndis_out, size_t *nlist_out, const NormTableScaler *scaler, const IVFSearchParameters *params = nullptr) const
-
void search_implem_12(idx_t n, const float *x, SIMDResultHandlerToFloat &handler, const CoarseQuantized &cq, size_t *ndis_out, size_t *nlist_out, const NormTableScaler *scaler, const IVFSearchParameters *params = nullptr) const
-
void search_implem_14(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const CoarseQuantized &cq, int impl, const NormTableScaler *scaler, const IVFSearchParameters *params = nullptr) const
-
virtual void reconstruct_from_offset(int64_t list_no, int64_t offset, float *recons) const override
根据(倒排列表索引 + 倒排列表偏移)而不是 ID 来重建向量。
当不维护 direct_map 并且倒排列表偏移由 search_preassigned() 计算且
store_pairs
设置时很有用。
-
virtual CodePacker *get_CodePacker() const override
-
void reconstruct_orig_invlists()
-
virtual void reset() override
从数据库中移除所有元素。
-
virtual void train(idx_t n, const float *x) override
训练量化器并调用 train_encoder 来训练子量化器。
-
virtual void add(idx_t n, const float *x) override
使用 NULL id 调用 add_with_ids。
-
virtual void add_core(idx_t n, const float *x, const idx_t *xids, const idx_t *precomputed_idx, void *inverted_list_context = nullptr)
向量加法的实现,其中向量分配是预定义的。 默认实现将代码提取交给 encode_vectors。
- 参数:
precomputed_idx – 输入向量的量化索引(大小为 n)
-
virtual void add_sa_codes(idx_t n, const uint8_t *codes, const idx_t *xids) override
添加使用独立编解码器计算的向量
- 参数:
codes – 要添加的代码,大小为 n * sa_code_size()
xids – 相应的 id,大小为 n
-
virtual void range_search_preassigned(idx_t nx, const float *x, float radius, const idx_t *keys, const float *coarse_dis, RangeSearchResult *result, bool store_pairs = false, const IVFSearchParameters *params = nullptr, IndexIVFStats *stats = nullptr) const override
范围搜索一组由 IVF 量化器预量化的向量。 填写 RangeSearchResults 结果。 默认实现使用 InvertedListScanners 进行搜索。
- 参数:
n – 要查询的向量数量
x – 查询向量,大小为 nx * d
assign – 粗量化索引,大小为 nx * nprobe
centroid_dis – 到粗糙质心的距离,大小为 nx * nprobe
result – 输出结果
store_pairs – 将倒排列表索引 + 倒排列表偏移量存储在结果的上/下 32 位中,而不是 ID(用于重排序)。
params – 用于覆盖对象的搜索参数
stats – 要更新的搜索统计信息(可以为 null)
-
virtual InvertedListScanner *get_InvertedListScanner(bool store_pairs = false, const IDSelector *sel = nullptr) const
获取此索引的扫描器(store_pairs 表示忽略标签)
默认的搜索实现使用它来计算距离
-
virtual void reconstruct(idx_t key, float *recons) const override
重建向量。仅当 maintain_direct_map 设置为 1 或 2 时有效
-
virtual void update_vectors(int nv, const idx_t *idx, const float *v)
更新向量的子集。
索引必须具有 direct_map
- 参数:
nv – 要更新的向量数
idx – 要更新的向量索引,大小为 nv
v – 新值的向量,大小为 nv*d
-
virtual void reconstruct_n(idx_t i0, idx_t ni, float *recons) const override
重建索引向量的子集。
覆盖默认实现以绕过 reconstruct(),这需要维护 direct_map。
- 参数:
i0 – 要重建的第一个向量
ni – 要重建的向量数
recons – 重建向量的输出数组,大小为 ni * d
-
virtual void search_and_reconstruct(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, float *recons, const SearchParameters *params = nullptr) const override
类似于搜索,但也重建了存储的向量(或者在有损编码的情况下,重建近似值)用于搜索结果。
覆盖默认实现以避免维护 direct_map,而是通过 search_preassigned() 中的
store_pairs
标志获取代码偏移量。- 参数:
recons – 重建向量大小 (n, k, d)
-
void search_and_return_codes(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, uint8_t *recons, bool include_listno = false, const SearchParameters *params = nullptr) const
类似于搜索,但也返回与搜索结果的存储向量相对应的代码。
- 参数:
codes – 代码 (n, k, code_size)
include_listno – 在代码中包含列表 ID(在这种情况下,将 ceil(log8(nlist)) 添加到代码大小)
-
virtual size_t remove_ids(const IDSelector &sel) override
数据集操作函数。
-
virtual void check_compatible_for_merge(const Index &otherIndex) const override
检查两个索引是否兼容(即,它们以相同的方式训练并具有相同的参数)。否则抛出异常。
-
virtual void merge_from(Index &otherIndex, idx_t add_id) override
将条目从另一个数据集移动到 self。输出时,other 为空。 add_id 被添加到所有移动的 ID(对于连续 ID,这将是 this->ntotal)
-
virtual void copy_subset_to(IndexIVF &other, InvertedLists::subset_type_t subset_type, idx_t a1, idx_t a2) const
将索引的条目的子集复制到另一个索引,有关 subset_type 的含义,请参阅 Invlists::copy_subset_to。
-
inline size_t get_list_size(size_t list_no) const
-
bool check_ids_sorted() const
ID 是否已排序?
-
void make_direct_map(bool new_maintain_direct_map = true)
初始化直接映射
- 参数:
new_maintain_direct_map – 如果为 true,则创建直接映射,否则清除它
-
void replace_invlists(InvertedLists *il, bool own = false)
替换倒排列表,如果 own_invlists 为 true,则取消分配旧的倒排列表
-
virtual size_t sa_code_size() const override
生成的代码的大小(以字节为单位)
-
virtual void sa_encode(idx_t n, const float *x, uint8_t *bytes) const override
编码一组向量,sa_encode 将调用 encode_vector,其中 include_listno=true
- 参数:
n – 要编码的向量数
x – 要编码的向量
bytes – 代码的输出数组
- 返回值:
写入代码的字节数
-
virtual void assign(idx_t n, const float *x, idx_t *labels, idx_t k = 1) const
返回与查询 x 最接近的 k 个向量的索引。
此函数与搜索相同,但仅返回邻居的标签。
- 参数:
n – 向量的数量
x – 要搜索的输入向量,大小为 n * d
labels – NN 的输出标签,大小为 n*k
k – 最近邻的数量
-
virtual void reconstruct_batch(idx_t n, const idx_t *keys, float *recons) const
重建几个存储的向量(如果是有损编码,则是近似值)
对于某些索引,可能未定义此函数
- 参数:
n – 要重建的向量数
keys – 要重建的向量的 ID(大小为 n)
recons – 重构的向量(大小为 n * d)
-
virtual void compute_residual(const float *x, float *residual, idx_t key) const
在索引编码后计算残差向量。
残差向量是向量与可以从其在索引中的表示形式解码的重构之间的差异。 残差可用于多阶段索引方法,例如 IndexIVF 的方法。
- 参数:
x – 输入向量,大小为 d
residual – 输出残差向量,大小为 d
key – 编码的索引,由搜索和分配返回
-
virtual void compute_residual_n(idx_t n, const float *xs, float *residuals, const idx_t *keys) const
在索引编码后计算残差向量(批处理形式)。 等效于为每个向量调用 compute_residual。
残差向量是向量与可以从其在索引中的表示形式解码的重构之间的差异。 残差可用于多阶段索引方法,例如 IndexIVF 的方法。
- 参数:
n – 向量的数量
xs – 输入向量,大小为 (n x d)
residuals – 输出残差向量,大小为 (n x d)
keys – 编码的索引,由搜索和分配返回
-
virtual DistanceComputer *get_distance_computer() const
获取此索引类型的 DistanceComputer (在 AuxIndexStructures 中定义) 对象。
DistanceComputer 针对支持随机访问其向量的索引实现。
-
void train_q1(size_t n, const float *x, bool verbose, MetricType metric_type)
训练量化器并调用 train_residual 来训练子量化器。
-
size_t coarse_code_size() const
计算存储列表 ID 所需的字节数
-
void encode_listno(idx_t list_no, uint8_t *code) const
-
idx_t decode_listno(const uint8_t *code) const
公共成员
-
ProductQuantizer pq
生成代码
-
int use_precomputed_table = 0
预计算表管理
-
AlignedTable<float> precomputed_table
如果 use_precompute_table 大小 (nlist, pq.M, pq.ksub)
-
int bbs
-
size_t M
-
size_t nbits
-
size_t ksub
-
size_t M2
-
int implem = 0
-
int skip = 0
-
int qbs = 0
-
size_t qbs2 = 0
-
InvertedLists *orig_invlists = nullptr
orig 的倒排列表(用于调试)
-
InvertedLists *invlists = nullptr
访问实际数据。
-
bool own_invlists = false
-
size_t code_size = 0
每个向量的码字大小(以字节为单位)
-
int parallel_mode = 0
并行模式决定了如何使用 OpenMP 对查询进行并行化
0(默认):在查询中分割 1:在倒排列表上并行化 2:在两者上并行化 3:以更精细的粒度在查询中分割
PARALLEL_MODE_NO_HEAP_INIT:二进制或与前一个组合以防止堆被初始化和最终化
-
const int PARALLEL_MODE_NO_HEAP_INIT = 1024
-
DirectMap direct_map
可选的映射,将 ID 映射回倒排列表条目。这使得 reconstruct() 成为可能。
-
bool by_residual = true
倒排列表中的代码是否编码了相对于质心的向量?
-
int d
向量维度
-
idx_t ntotal
索引向量的总数
-
bool verbose
详细程度
-
bool is_trained
如果 Index 不需要训练,或者已经完成训练,则设置此项
-
MetricType metric_type
此索引用于搜索的度量类型
-
float metric_arg
度量类型的参数
-
size_t nprobe = 1
查询时使用的探测数量
-
size_t max_codes = 0
执行查询时要访问的最大代码数量
-
Index *quantizer = nullptr
将向量映射到倒排列表的量化器
-
size_t nlist = 0
倒排列表的数量
-
char quantizer_trains_alone = 0
= 0:在 k-means 训练中使用量化器作为索引 = 1:仅将训练集传递给量化器的 train() = 2:在扁平索引上进行 k-means 训练 + 将质心添加到量化器
-
bool own_fields = false
对象是否拥有量化器
-
ClusteringParameters cp
用于覆盖默认聚类参数
-
Index *clustering_index = nullptr
用于覆盖聚类期间使用的索引
-
using component_t = float