结构体 faiss::IndexHNSW
-
struct IndexHNSW : public faiss::Index
HNSW 索引是一个普通的随机访问索引,在其上构建了一个 HNSW 链接结构
被 faiss::IndexHNSW2Level, faiss::IndexHNSWCagra, faiss::IndexHNSWFlat, faiss::IndexHNSWPQ, faiss::IndexHNSWSQ 继承
公共函数
-
explicit IndexHNSW(int d = 0, int M = 32, MetricType metric = METRIC_L2)
-
explicit IndexHNSW(Index *storage, int M = 32)
-
~IndexHNSW() override
-
virtual void add(idx_t n, const float *x) override
将维度为 d 的 n 个向量添加到索引。
向量被隐式分配标签 ntotal .. ntotal + n - 1。此函数将输入向量切片成小于 blocksize_add 的块并调用 add_core。
- 参数:
n – 向量的数量
x – 输入矩阵,大小为 n * d
-
virtual void train(idx_t n, const float *x) override
如果需要,训练存储。
-
virtual void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels, const SearchParameters *params = nullptr) const override
搜索的入口点
-
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 – 结果表
-
virtual void reconstruct(idx_t key, float *recons) const override
重构存储的向量 (如果是有损编码,则是近似值)
对于某些索引,此函数可能未定义
- 参数:
key – 要重构的向量的 ID
recons – 重构的向量 (大小为 d)
-
virtual void reset() override
从数据库中删除所有元素。
-
void shrink_level_0_neighbors(int size)
-
void search_level_0(idx_t n, const float *x, idx_t k, const storage_idx_t *nearest, const float *nearest_d, float *distances, idx_t *labels, int nprobe = 1, int search_type = 1, const SearchParameters *params = nullptr) const
仅在级别 0 上执行搜索,给定每个顶点的起始点。
- 参数:
search_type – 1:每次 nprobe 执行一次搜索,2:将所有入口点排队
-
void init_level_0_from_knngraph(int k, const float *D, const idx_t *I)
替代图构建
-
void init_level_0_from_entry_points(int npt, const storage_idx_t *points, const storage_idx_t *nearests)
替代图构建
-
void reorder_links()
-
void link_singletons()
-
void permute_entries(const idx_t *perm)
-
virtual DistanceComputer *get_distance_computer() const override
获取此索引类型的 DistanceComputer (在 AuxIndexStructures 中定义) 对象。
DistanceComputer 为支持对其向量进行随机访问的索引实现。
-
virtual void add_with_ids(idx_t n, const float *x, const idx_t *xids)
与 add 相同,但存储 xids 而不是顺序 ID。
默认实现失败并出现断言,因为它并非所有索引都支持。
- 参数:
n – 向量的数量
x – 输入向量,大小 n * d
xids – 如果非空,则为要存储的向量的 ID(大小为 n)
-
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 size_t remove_ids(const IDSelector &sel)
从索引中删除 ID。并非所有索引都支持。 返回删除的元素数量。
-
virtual void reconstruct_batch(idx_t n, const idx_t *keys, float *recons) const
重建多个存储的向量(如果是有损编码,则进行近似)。
对于某些索引,此函数可能未定义
- 参数:
n – 要重建的向量数量
keys – 要重建的向量的 ID(大小为 n)
recons – 重建的向量(大小为 n * d)
-
virtual void reconstruct_n(idx_t i0, idx_t ni, float *recons) const
重建向量 i0 到 i0 + ni - 1
对于某些索引,此函数可能未定义
- 参数:
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
类似于搜索,但也重建搜索结果的存储向量(或者在有损编码的情况下进行近似)。
如果查询的结果不足,则生成的数组将用 -1 填充。
- 参数:
n – 向量的数量
x – 要搜索的输入向量,大小为 n * d
k – 提取的向量数
distances – 输出成对距离,大小为 n*k
labels – NN 的输出标签,大小为 n*k
recons – 重建的向量大小 (n, k, 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 size_t sa_code_size() const
生成的代码的大小(以字节为单位)
-
virtual void sa_encode(idx_t n, const float *x, uint8_t *bytes) const
编码一组向量
- 参数:
n – 向量的数量
x – 输入向量,大小 n * d
bytes – 输出编码后的向量,大小为 n * sa_code_size()
-
virtual void sa_decode(idx_t n, const uint8_t *bytes, float *x) const
解码一组向量
- 参数:
n – 向量的数量
bytes – 输入编码后的向量,大小为 n * sa_code_size()
x – 输出向量,大小为 n * d
-
virtual void merge_from(Index &otherIndex, idx_t add_id = 0)
将另一个数据集的条目移动到自身。 输出时,other 为空。 add_id 添加到所有移动的 ID(对于连续的 ID,这将是 this->ntotal)
-
virtual void check_compatible_for_merge(const Index &otherIndex) const
检查两个索引是否兼容(即,它们以相同的方式训练并且具有相同的参数)。 否则抛出异常。
-
virtual void add_sa_codes(idx_t n, const uint8_t *codes, const idx_t *xids)
添加使用独立编解码器计算的向量
- 参数:
codes – 要添加的代码,大小为 n * sa_code_size()
xids – 相应的 IDs,大小为 n
公共成员
-
HNSW hnsw
-
bool own_fields = false
-
Index *storage = nullptr
-
bool init_level0 = true
-
bool keep_max_size_level0 = false
-
int d
向量维度
-
idx_t ntotal
已索引向量的总数
-
bool verbose
详细级别
-
bool is_trained
如果 Index 不需要训练,或者已经完成训练,则设置
-
MetricType metric_type
此索引用于搜索的度量类型
-
float metric_arg
度量类型的参数
-
explicit IndexHNSW(int d = 0, int M = 32, MetricType metric = METRIC_L2)