文件 IndexIDMap.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

定义了一些对一组向量应用转换的对象。 通常这些是预处理步骤。

类型定义

using IndexIDMap = IndexIDMapTemplate<Index>
using IndexBinaryIDMap = IndexIDMapTemplate<IndexBinary>
using IndexIDMap2 = IndexIDMap2Template<Index>
using IndexBinaryIDMap2 = IndexIDMap2Template<IndexBinary>
template<typename IndexT>
struct IndexIDMapTemplate : public IndexT
#include <IndexIDMap.h>

Index,它将搜索结果转换为 IDs

faiss::IndexIDMap2Template< IndexT > 继承

公共类型

using component_t = typename IndexT::component_t
using distance_t = typename IndexT::distance_t

公共函数

explicit IndexIDMapTemplate(IndexT *index)
void add_with_ids(idx_t n, const component_t *x, const idx_t *xids) override
参数:

xids – 如果非空,则为要存储的向量的 IDs (大小为 n)

void add(idx_t n, const component_t *x) override

这将会失败。请使用 add_with_ids

void search(idx_t n, const component_t *x, idx_t k, distance_t *distances, idx_t *labels, const SearchParameters *params = nullptr) const override
void train(idx_t n, const component_t *x) override
void reset() override
size_t remove_ids(const IDSelector &sel) override

移除 ids 以适应 IndexFlat

void range_search(idx_t n, const component_t *x, distance_t radius, RangeSearchResult *result, const SearchParameters *params = nullptr) const override
void merge_from(IndexT &otherIndex, idx_t add_id = 0) override
void check_compatible_for_merge(const IndexT &otherIndex) const override
size_t sa_code_size() const override
void add_sa_codes(idx_t n, const uint8_t *x, const idx_t *xids) override
~IndexIDMapTemplate() override
inline IndexIDMapTemplate()

Public Members

IndexT *index = nullptr
bool own_fields = false

! the sub-index

std::vector<idx_t> id_map

! whether pointers are deleted in destructo

template<typename IndexT>
struct IndexIDMap2Template : public faiss::IndexIDMapTemplate<IndexT>
#include <IndexIDMap.h>

same as IndexIDMap but also provides an efficient reconstruction implementation via a 2-way index

公共类型

using component_t = typename IndexT::component_t
using distance_t = typename IndexT::distance_t

公共函数

explicit IndexIDMap2Template(IndexT *index)
void construct_rev_map()

make the rev_map from scratch

void add_with_ids(idx_t n, const component_t *x, const idx_t *xids) override
size_t remove_ids(const IDSelector &sel) override
void reconstruct(idx_t key, component_t *recons) const override
void check_consistency() const

检查 rev_map 和 id_map 是否同步

void merge_from(IndexT &otherIndex, idx_t add_id = 0) override
inline ~IndexIDMap2Template() override
inline IndexIDMap2Template()
void add(idx_t n, const component_t *x) override

这将会失败。请使用 add_with_ids

void search(idx_t n, const component_t *x, idx_t k, distance_t *distances, idx_t *labels, const SearchParameters *params = nullptr) const override
void train(idx_t n, const component_t *x) override
void reset() override
void range_search(idx_t n, const component_t *x, distance_t radius, RangeSearchResult *result, const SearchParameters *params = nullptr) const override
void check_compatible_for_merge(const IndexT &otherIndex) const override
size_t sa_code_size() const override
void add_sa_codes(idx_t n, const uint8_t *x, const idx_t *xids) override

Public Members

std::unordered_map<idx_t, idx_t> rev_map
IndexT *index = nullptr
bool own_fields = false

! the sub-index

std::vector<idx_t> id_map

! whether pointers are deleted in destructo

struct IDSelectorTranslated : public faiss::IDSelector

公共函数

inline IDSelectorTranslated(const std::vector<int64_t> &id_map, const IDSelector *sel)
inline IDSelectorTranslated(IndexBinaryIDMap &index_idmap, const IDSelector *sel)
inline IDSelectorTranslated(IndexIDMap &index_idmap, const IDSelector *sel)
inline virtual bool is_member(idx_t id) const override

Public Members

const std::vector<int64_t> &id_map
const IDSelector *sel