结构体 faiss::ProductLocalSearchQuantizer
-
struct ProductLocalSearchQuantizer : public faiss::ProductAdditiveQuantizer
产品局部搜索 量化器
公共类型
-
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
公共函数
-
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 的引导维度
-
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 存储在大小为 total_codebook_size by d 的码本表中 codebook_offsets[i]:codebook_offsets[i+1] 行中
-
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