文件 ProductAdditiveQuantizer.h

namespace faiss

实现了多种变体的 k-means 聚类。

版权所有 (c) Facebook, Inc. 及其附属公司。

此源代码根据 MIT 许可证获得许可,该许可证位于此源树根目录的 LICENSE 文件中。

IDSelector 旨在定义要处理的向量子集(用于删除或作为搜索的子集)

PQ4 SIMD 打包和累加函数

基本内核用 bbs = nb * 2 * 16 个向量累加 nq 个查询向量,并为此生成一个输出矩阵。 当 nq * nb <= 4 时,这很有意思,否则寄存器溢出变得太大。

这些函数的实现分布在 3 个 cpp 文件中,以减少并行编译时间。 模板被显式实例化。

此文件包含用于计算距离的内核的回调。

在整个库中,向量以 float * 指针的形式提供。 当在一批中一起处理(添加/搜索)多个向量时,大多数算法都可以优化。 在这种情况下,它们作为矩阵传递。 当 n 个大小为 d 的向量作为 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 ProductAdditiveQuantizer : public faiss::AdditiveQuantizer
#include <ProductAdditiveQuantizer.h>

乘积加法量化器

乘积加法量化器是 AQ 和 PQ 的一个变体。 它首先像 PQ 一样将向量空间分成多个正交子空间。 然后,它通过独立的加法量化器量化每个子空间。

faiss::ProductLocalSearchQuantizerfaiss::ProductResidualQuantizer 子类化

公共函数

ProductAdditiveQuantizer(size_t d, const std::vector<AdditiveQuantizer*> &aqs, Search_type_t search_type = ST_decompress)

构造一个乘积加法量化器。

传入的加法量化器将被克隆到 ProductAdditiveQuantizer 对象中。

参数:
  • d – 输入向量的维度

  • aqs – 子加法量化器

  • search_type – AQ 搜索类型

ProductAdditiveQuantizer()
virtual ~ProductAdditiveQuantizer()
void init(size_t d, const std::vector<AdditiveQuantizer*> &aqs, Search_type_t search_type)
AdditiveQuantizer *subquantizer(size_t m) const

训练乘积加法量化器。

virtual void train(size_t n, const float *x) override

训练量化器

参数:

x – 训练向量,大小为 n * d

virtual void compute_codes_add_centroids(const float *x, uint8_t *codes, size_t n, const float *centroids = nullptr) const override

编码一组向量

参数:
  • x – 要编码的向量,大小为 n * d

  • codes – 输出编码,大小为 n * code_size

  • centroids – 要添加到 x 的质心,大小为 n * d

void compute_unpacked_codes(const float *x, int32_t *codes, size_t n, const float *centroids = nullptr) const
virtual void decode_unpacked(const int32_t *codes, float *x, size_t n, int64_t ld_codes = -1) const override

解码一组非压缩格式的向量

参数:
  • codes – 要解码的编码,大小为 n * ld_codes

  • x – 输出向量,大小为 n * d

virtual void decode(const uint8_t *codes, float *x, size_t n) const override

解码一组向量

参数:
  • codes – 要解码的编码,大小为 n * code_size

  • x – 输出向量,大小为 n * d

virtual void compute_LUT(size_t n, const float *xq, float *LUT, float alpha = 1.0f, long ld_lut = -1) const override

计算内积查找表。 用于搜索函数。

参数:
  • xq – 查询向量,大小为 (n, d)

  • LUT – 查找表,大小为 (n, total_codebook_size)

  • alpha – 计算 alpha * 内积

  • ld_lut – LUT 的前导维度

公共成员

size_t nsplits

我们将向量分割成的子向量数量

std::vector<AdditiveQuantizer*> quantizers
struct ProductLocalSearchQuantizer : public faiss::ProductAdditiveQuantizer
#include <ProductAdditiveQuantizer.h>

乘积局部搜索 量化器

公共函数

ProductLocalSearchQuantizer(size_t d, size_t nsplits, size_t Msub, size_t nbits, Search_type_t search_type = ST_decompress)

构造一个乘积 LSQ 对象。

参数:
  • d – 输入向量的维度

  • nsplits – 我们将向量分割成的子向量数量

  • Msub – 每个 LSQ 的码本数量

  • nbits – 每个步骤的比特数

  • search_type – AQ 搜索类型

ProductLocalSearchQuantizer()
void init(size_t d, const std::vector<AdditiveQuantizer*> &aqs, Search_type_t search_type)
AdditiveQuantizer *subquantizer(size_t m) const

训练乘积加法量化器。

virtual void train(size_t n, const float *x) override

训练量化器

参数:

x – 训练向量,大小为 n * d

virtual void compute_codes_add_centroids(const float *x, uint8_t *codes, size_t n, const float *centroids = nullptr) const override

编码一组向量

参数:
  • x – 要编码的向量,大小为 n * d

  • codes – 输出编码,大小为 n * code_size

  • centroids – 要添加到 x 的质心,大小为 n * d

void compute_unpacked_codes(const float *x, int32_t *codes, size_t n, const float *centroids = nullptr) const
virtual void decode_unpacked(const int32_t *codes, float *x, size_t n, int64_t ld_codes = -1) const override

解码一组非压缩格式的向量

参数:
  • codes – 要解码的编码,大小为 n * ld_codes

  • x – 输出向量,大小为 n * d

virtual void decode(const uint8_t *codes, float *x, size_t n) const override

解码一组向量

参数:
  • codes – 要解码的编码,大小为 n * code_size

  • x – 输出向量,大小为 n * d

virtual void compute_LUT(size_t n, const float *xq, float *LUT, float alpha = 1.0f, long ld_lut = -1) const override

计算内积查找表。 用于搜索函数。

参数:
  • xq – 查询向量,大小为 (n, d)

  • LUT – 查找表,大小为 (n, total_codebook_size)

  • alpha – 计算 alpha * 内积

  • ld_lut – LUT 的前导维度

公共成员

size_t nsplits

我们将向量分割成的子向量数量

std::vector<AdditiveQuantizer*> quantizers
struct ProductResidualQuantizer : public faiss::ProductAdditiveQuantizer
#include <ProductAdditiveQuantizer.h>

Product Residual Quantizer

公共函数

ProductResidualQuantizer(size_t d, size_t nsplits, size_t Msub, size_t nbits, Search_type_t search_type = ST_decompress)

构造一个乘积 RQ 对象。

参数:
  • d – 输入向量的维度

  • nsplits – 我们将向量分割成的子向量数量

  • Msub – 每个 RQ 的码本数量

  • nbits – 每个步骤的比特数

  • search_type – AQ 搜索类型

ProductResidualQuantizer()
void init(size_t d, const std::vector<AdditiveQuantizer*> &aqs, Search_type_t search_type)
AdditiveQuantizer *subquantizer(size_t m) const

训练乘积加法量化器。

virtual void train(size_t n, const float *x) override

训练量化器

参数:

x – 训练向量,大小为 n * d

virtual void compute_codes_add_centroids(const float *x, uint8_t *codes, size_t n, const float *centroids = nullptr) const override

编码一组向量

参数:
  • x – 要编码的向量,大小为 n * d

  • codes – 输出编码,大小为 n * code_size

  • centroids – 要添加到 x 的质心,大小为 n * d

void compute_unpacked_codes(const float *x, int32_t *codes, size_t n, const float *centroids = nullptr) const
virtual void decode_unpacked(const int32_t *codes, float *x, size_t n, int64_t ld_codes = -1) const override

解码一组非压缩格式的向量

参数:
  • codes – 要解码的编码,大小为 n * ld_codes

  • x – 输出向量,大小为 n * d

virtual void decode(const uint8_t *codes, float *x, size_t n) const override

解码一组向量

参数:
  • codes – 要解码的编码,大小为 n * code_size

  • x – 输出向量,大小为 n * d

virtual void compute_LUT(size_t n, const float *xq, float *LUT, float alpha = 1.0f, long ld_lut = -1) const override

计算内积查找表。 用于搜索函数。

参数:
  • xq – 查询向量,大小为 (n, d)

  • LUT – 查找表,大小为 (n, total_codebook_size)

  • alpha – 计算 alpha * 内积

  • ld_lut – LUT 的前导维度

公共成员

size_t nsplits

我们将向量分割成的子向量数量

std::vector<AdditiveQuantizer*> quantizers