结构体 faiss::ProductResidualQuantizer
-
struct ProductResidualQuantizer : public faiss::ProductAdditiveQuantizer
Product Residual Quantizer
公共类型
-
enum Search_type_t
编码如何执行搜索以及如何编码向量。
值
-
enumerator ST_decompress
解压缩数据库向量
-
enumerator ST_LUT_nonorm
使用 LUT,不包括范数 (适用于 IP 或标准化向量)
-
enumerator ST_norm_from_LUT
从查找表计算范数 (成本为 O(M^2))
-
enumerator ST_norm_float
使用 LUT,并将 float32 范数与向量一起存储
-
enumerator ST_norm_qint8
使用 LUT,并存储 8 位量化范数
-
enumerator ST_norm_qint4
-
enumerator ST_norm_cqint8
使用 LUT,并存储非均匀量化范数
-
enumerator ST_norm_cqint4
-
enumerator ST_norm_lsq2x4
使用 2x4 位 lsq 作为范数量化器 (用于快速扫描)
-
enumerator ST_norm_rq2x4
使用 2x4 位 rq 作为范数量化器 (用于快速扫描)
-
enumerator ST_decompress
公共函数
-
ProductResidualQuantizer(size_t d, size_t nsplits, size_t Msub, size_t nbits, Search_type_t search_type = ST_decompress)
构造一个 product 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
训练 product additive quantizer。
-
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 的前导维度
-
void compute_codebook_tables()
-
uint64_t encode_norm(float norm) const
将范数编码为 norm_bits 位
-
uint32_t encode_qcint(float x) const
通过非均匀标量量化编码范数
-
float decode_qcint(uint32_t c) const
通过非均匀标量量化解码范数
-
void set_derived_values()
训练范数量化器。
-
void train_norm(size_t n, const float *norms)
-
inline virtual void compute_codes(const float *x, uint8_t *codes, size_t n) const override
量化一组向量
- 参数:
x – 输入向量,大小为 n * d
codes – 输出代码,大小为 n * code_size
-
void pack_codes(size_t n, const int32_t *codes, uint8_t *packed_codes, int64_t ld_codes = -1, const float *norms = nullptr, const float *centroids = nullptr) const
将一系列码打包成位紧凑格式
- 参数:
codes – 要打包的代码,大小为 n * code_size
packed_codes – 输出位紧凑代码
ld_codes – 代码的前导维度
norms – 向量的范数(大小为 n)。 如果需要但未提供,将会被计算。
centroids – 要添加到 x 的质心,大小为 n * d
-
template<bool is_IP, Search_type_t effective_search_type>
float compute_1_distance_LUT(const uint8_t *codes, const float *LUT) const
-
void decode_64bit(idx_t n, float *x) const
用于解码64位字中的代码的函数
-
void knn_centroids_inner_product(idx_t n, const float *xq, idx_t k, float *distances, idx_t *labels) const
精确的IP搜索
-
void compute_centroid_norms(float *norms) const
对于L2搜索,我们需要质心的L2范数
- 参数:
norms – 输出范数表,大小为 total_codebook_size
公共成员
-
size_t nsplits
我们将一个向量分成多少个子向量
-
std::vector<AdditiveQuantizer*> quantizers
-
size_t M
码本的数量
-
std::vector<size_t> nbits
每个步骤的位数
-
std::vector<float> codebooks
码本
-
std::vector<uint64_t> codebook_offsets
码本 #1 存储在 codebooks 表中的行 codebook_offsets[i]:codebook_offsets[i+1] 中,该表的大小为 total_codebook_size x d
-
size_t tot_bits = 0
总位数(索引 + 范数)
-
size_t norm_bits = 0
为范数分配的位数
-
size_t total_codebook_size = 0
码本的大小(以向量为单位)
-
bool only_8bit = false
是否所有 nbits = 8(使用更快的解码器)
-
bool verbose = false
训练期间是否显示详细信息?
-
bool is_trained = false
是否已训练
-
std::vector<float> norm_tabs
用于 ST_norm_lsq2x4 和 ST_norm_rq2x4 的辅助数据,存储用于 4 位快速扫描的码本条目的范数
-
IndexFlat1D qnorm
存储和搜索范数
-
std::vector<float> centroid_norms
所有码本条目的范数(大小为 total_codebook_size)
-
std::vector<float> codebook_cross_products
所有码本条目与先前码本的点积,大小为 sum(codebook_offsets[m] * 2^nbits[m], m=0..M-1)
-
size_t max_mem_distances = 5 * (size_t(1) << 30)
范数和距离矩阵使用集束搜索可能会变得很大,因此使用此项来控制可以分配的内存量
-
Search_type_t search_type
也决定了代码中的内容。
-
float norm_min = NAN
范数量化的最小值/最大值
-
float norm_max = NAN
-
size_t d
输入向量的大小
-
size_t code_size
每个索引向量的字节数
-
enum Search_type_t