结构体 faiss::IndexBinaryIVF
-
struct IndexBinaryIVF : public faiss::IndexBinary
基于倒排文件 (IVF) 的 Index
在倒排文件中,量化器 (一个 IndexBinary 实例) 为每个要添加的向量提供一个量化索引。量化索引映射到一个列表(也称为倒排列表或发布列表),其中存储了向量的 id。
否则,该对象与 IndexIVF 类似
公共类型
-
using component_t = uint8_t
-
using distance_t = int32_t
公共函数
-
IndexBinaryIVF(IndexBinary *quantizer, size_t d, size_t nlist)
倒排文件采用一个量化器 (一个 IndexBinary) 作为输入,该量化器实现了将向量映射到列表标识符的函数。指针是被借用的:在使用 IndexBinaryIVF 时,不应删除量化器。
-
IndexBinaryIVF()
-
~IndexBinaryIVF() override
-
virtual void reset() override
从数据库中删除所有元素。
-
virtual void train(idx_t n, const uint8_t *x) override
训练量化器。
-
virtual void add(idx_t n, const uint8_t *x) override
将维度为 d 的 n 个向量添加到索引。
向量被隐式地分配标签 ntotal .. ntotal + n - 1
- 参数:
x – 输入矩阵,大小为 n * d / 8
-
virtual void add_with_ids(idx_t n, const uint8_t *x, const idx_t *xids) override
与 add 相同,但存储 xids 而不是顺序 ids。
默认实现会因断言而失败,因为它并非所有索引都支持。
- 参数:
xids – 如果非空,则为要为向量存储的 ids (大小为 n)
-
void add_core(idx_t n, const uint8_t *x, const idx_t *xids, const idx_t *precomputed_idx)
向量加法的实现,其中向量分配是预定义的。
- 参数:
precomputed_idx – 输入向量的量化索引 (大小为 n)
-
void search_preassigned(idx_t n, const uint8_t *x, idx_t k, const idx_t *assign, const int32_t *centroid_dis, int32_t *distances, idx_t *labels, bool store_pairs, const IVFSearchParameters *params = nullptr) const
搜索一组向量,这些向量由 IVF 量化器预先量化。使用查询结果填充相应的堆。 search() 调用此函数。
- 参数:
n – 要查询的向量数
x – 查询向量,大小 nx * d
assign – 粗量化索引,大小 nx * nprobe
centroid_dis – 到粗质心的距离,大小 nx * nprobe
distance – 输出距离,大小 n * k
labels – 输出标签,大小 n * k
store_pairs – 存储倒排列表索引 + 倒排列表偏移量,而不是结果的 upper/lower 32 位中的 id(用于重排序)。
params – 用于覆盖对象的搜索参数
-
virtual BinaryInvertedListScanner *get_InvertedListScanner(bool store_pairs = false) const
-
virtual void search(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels, const SearchParameters *params = nullptr) const override
分配向量,然后调用 search_preassign
-
virtual void range_search(idx_t n, const uint8_t *x, int radius, RangeSearchResult *result, const SearchParameters *params = nullptr) const override
查询 n 个维度为 d 的向量到索引。
返回所有距离 < radius 的向量。请注意,许多索引不实现 range_search(只有 k-NN 搜索是强制性的)。距离转换为 float 以重用 RangeSearchResult 结构,但它们是整数。按照惯例,只返回距离 < radius(严格比较)的距离,即 radius = 0 不返回任何结果,1 只返回完全相同的向量。
- 参数:
x – 要搜索的输入向量,大小为 n * d / 8
radius – 搜索半径
result – 结果表
-
void range_search_preassigned(idx_t n, const uint8_t *x, int radius, const idx_t *assign, const int32_t *centroid_dis, RangeSearchResult *result) const
-
virtual void reconstruct(idx_t key, uint8_t *recons) const override
重建存储的向量。
对于某些索引,可能未定义此函数。
- 参数:
key – 要重建的向量的 ID
recons – 重建的向量(大小为 d / 8)
-
virtual void reconstruct_n(idx_t i0, idx_t ni, uint8_t *recons) const override
重建索引向量的子集。
覆盖默认实现以绕过 reconstruct(),它要求维护 direct_map。
- 参数:
i0 – 要重建的第一个向量
ni – 要重建的向量数
recons – 重建向量的输出数组,大小为 ni * d / 8
-
virtual void search_and_reconstruct(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels, uint8_t *recons, const SearchParameters *params = nullptr) const override
与 search 类似,但也会重建存储的向量(对于有损编码的情况,则是近似值),以用于搜索结果。
覆盖默认实现以避免必须维护 direct_map,而是通过 search_preassigned() 中的
store_pairs
标志获取代码偏移量。- 参数:
recons – 重建的向量大小 (n, k, d / 8)
-
virtual void reconstruct_from_offset(idx_t list_no, idx_t offset, uint8_t *recons) const
根据位置(逆向列表索引 + 逆向列表偏移量)而不是ID,重建向量。
当direct_map未维护,且逆向列表偏移量通过设置了
store_pairs
的 search_preassigned() 计算得出时,此函数很有用。
-
virtual size_t remove_ids(const IDSelector &sel) override
数据集操作函数。
-
virtual void merge_from(IndexBinary &other, idx_t add_id) override
将条目从另一个数据集移动到自身。输出时,other 为空。 add_id 被添加到所有移动的ID(对于顺序ID,这将是 this->ntotal)
-
virtual void check_compatible_for_merge(const IndexBinary &otherIndex) const override
检查两个索引是否兼容(即,它们以相同的方式训练并具有相同的参数)。否则抛出异常。
-
inline size_t get_list_size(size_t list_no) const
-
void make_direct_map(bool new_maintain_direct_map = true)
初始化直接映射
- 参数:
new_maintain_direct_map – 如果为 true,则创建直接映射,否则清除它
-
void replace_invlists(InvertedLists *il, bool own = false)
-
void assign(idx_t n, const uint8_t *x, idx_t *labels, idx_t k = 1) const
返回与查询 x 最接近的 k 个向量的索引。
此函数与搜索相同,但仅返回邻居的标签。
- 参数:
x – 要搜索的输入向量,大小为 n * d / 8
labels – NNs 的输出标签,大小为 n*k
-
void display() const
显示实际的类名和更多信息。
-
virtual size_t sa_code_size() const
生成的代码的大小,以字节为单位
-
virtual void add_sa_codes(idx_t n, const uint8_t *codes, const idx_t *xids)
与 IndexBinary 的 add_with_ids 相同。
公共成员
-
InvertedLists *invlists = nullptr
访问实际数据。
-
bool own_invlists = true
-
size_t nprobe = 1
查询时的探针数量
-
size_t max_codes = 0
执行查询时要访问的最大代码数量
-
bool use_heap = true
在扫描倒排列表时,选择使用堆或计数来选择 k 个最小值。
-
bool per_invlist_search = false
按批次收集计算结果
-
DirectMap direct_map
用于直接访问元素的映射。启用 reconstruct()。
-
IndexBinary *quantizer = nullptr
将向量映射到倒排列表的量化器
-
size_t nlist = 0
可能的键值的数量
-
bool own_fields = false
对象是否拥有量化器
-
ClusteringParameters cp
用于覆盖默认的聚类参数
-
Index *clustering_index = nullptr
用于覆盖聚类期间使用的索引
-
int d = 0
向量维度
-
int code_size = 0
每个向量的字节数 ( = d / 8 )
-
idx_t ntotal = 0
索引向量的总数
-
bool verbose = false
详细程度
-
bool is_trained = true
如果 Index 不需要训练,或者已经完成训练,则设置
-
MetricType metric_type = METRIC_L2
此索引用于搜索的度量类型
-
using component_t = uint8_t