Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/tmp/faiss/ProductQuantizer.h
1 /**
2 * Copyright (c) 2015-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD+Patents license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9 // -*- c++ -*-
10
11 #ifndef FAISS_PRODUCT_QUANTIZER_H
12 #define FAISS_PRODUCT_QUANTIZER_H
13
14 #include <stdint.h>
15
16 #include <vector>
17
18 #include "Clustering.h"
19 #include "Heap.h"
20
21 namespace faiss {
22
23 /** Product Quantizer. Implemented only for METRIC_L2 */
24 struct ProductQuantizer {
25
26 size_t d; ///< size of the input vectors
27 size_t M; ///< number of subquantizers
28 size_t nbits; ///< number of bits per quantization index
29
30 // values derived from the above
31 size_t dsub; ///< dimensionality of each subvector
32 size_t byte_per_idx; ///< nb bytes per code component (1 or 2)
33 size_t code_size; ///< byte per indexed vector
34 size_t ksub; ///< number of centroids for each subquantizer
35 bool verbose; ///< verbose during training?
36
37
38 /// initialization
39 enum train_type_t {
40 Train_default,
41 Train_hot_start, ///< the centroids are already initialized
42 Train_shared, ///< share dictionary accross PQ segments
43 Train_hypercube, ///< intialize centroids with nbits-D hypercube
44 Train_hypercube_pca, ///< intialize centroids with nbits-D hypercube
45 };
46 train_type_t train_type;
47
48 ClusteringParameters cp; ///< parameters used during clustering
49
50 /// if non-NULL, use this index for assignment (should be of size
51 /// d / M)
52 Index *assign_index;
53
54 /// Centroid table, size M * ksub * dsub
55 std::vector<float> centroids;
56
57 /// return the centroids associated with subvector m
58 float * get_centroids (size_t m, size_t i) {
59 return &centroids [(m * ksub + i) * dsub];
60 }
61 const float * get_centroids (size_t m, size_t i) const {
62 return &centroids [(m * ksub + i) * dsub];
63 }
64
65 // Train the product quantizer on a set of points. A clustering
66 // can be set on input to define non-default clustering parameters
67 void train (int n, const float *x);
68
69 ProductQuantizer(size_t d, /* dimensionality of the input vectors */
70 size_t M, /* number of subquantizers */
71 size_t nbits); /* number of bit per subvector index */
72
73 ProductQuantizer ();
74
75 /// compute derived values when d, M and nbits have been set
76 void set_derived_values ();
77
78 /// Define the centroids for subquantizer m
79 void set_params (const float * centroids, int m);
80
81 /// Quantize one vector with the product quantizer
82 void compute_code (const float * x, uint8_t * code) const ;
83
84 /// same as compute_code for several vectors
85 void compute_codes (const float * x,
86 uint8_t * codes,
87 size_t n) const ;
88
89 /// decode a vector from a given code (or n vectors if third argument)
90 void decode (const uint8_t *code, float *x) const;
91 void decode (const uint8_t *code, float *x, size_t n) const;
92
93 /// If we happen to have the distance tables precomputed, this is
94 /// more efficient to compute the codes.
95 void compute_code_from_distance_table (const float *tab,
96 uint8_t *code) const;
97
98
99 /** Compute distance table for one vector.
100 *
101 * The distance table for x = [x_0 x_1 .. x_(M-1)] is a M * ksub
102 * matrix that contains
103 *
104 * dis_table (m, j) = || x_m - c_(m, j)||^2
105 * for m = 0..M-1 and j = 0 .. ksub - 1
106 *
107 * where c_(m, j) is the centroid no j of sub-quantizer m.
108 *
109 * @param x input vector size d
110 * @param dis_table output table, size M * ksub
111 */
112 void compute_distance_table (const float * x,
113 float * dis_table) const;
114
115 void compute_inner_prod_table (const float * x,
116 float * dis_table) const;
117
118
119 /** compute distance table for several vectors
120 * @param nx nb of input vectors
121 * @param x input vector size nx * d
122 * @param dis_table output table, size nx * M * ksub
123 */
124 void compute_distance_tables (size_t nx,
125 const float * x,
126 float * dis_tables) const;
127
128 void compute_inner_prod_tables (size_t nx,
129 const float * x,
130 float * dis_tables) const;
131
132
133 /** perform a search (L2 distance)
134 * @param x query vectors, size nx * d
135 * @param nx nb of queries
136 * @param codes database codes, size ncodes * byte_per_idx
137 * @param ncodes nb of nb vectors
138 * @param res heap array to store results (nh == nx)
139 * @param init_finalize_heap initialize heap (input) and sort (output)?
140 */
141 void search (const float * x,
142 size_t nx,
143 const uint8_t * codes,
144 const size_t ncodes,
145 float_maxheap_array_t *res,
146 bool init_finalize_heap = true) const;
147
148 /** same as search, but with inner product similarity */
149 void search_ip (const float * x,
150 size_t nx,
151 const uint8_t * codes,
152 const size_t ncodes,
153 float_minheap_array_t *res,
154 bool init_finalize_heap = true) const;
155
156
157 /// Symmetric Distance Table
158 std::vector<float> sdc_table;
159
160 // intitialize the SDC table from the centroids
161 void compute_sdc_table ();
162
163 void search_sdc (const uint8_t * qcodes,
164 size_t nq,
165 const uint8_t * bcodes,
166 const size_t ncodes,
167 float_maxheap_array_t * res,
168 bool init_finalize_heap = true) const;
169
170 };
171
172
173 } // namespace faiss
174
175
176 #endif
faiss::ProductQuantizer::set_params
void set_params(const float *centroids, int m)
Define the centroids for subquantizer m.
Definition: ProductQuantizer.cpp:175
faiss::ProductQuantizer::Train_hypercube_pca
intialize centroids with nbits-D hypercube
Definition: ProductQuantizer.h:44
faiss::ProductQuantizer::nbits
size_t nbits
number of bits per quantization index
Definition: ProductQuantizer.h:28
faiss::ProductQuantizer::decode
void decode(const uint8_t *code, float *x) const
decode a vector from a given code (or n vectors if third argument)
Definition: ProductQuantizer.cpp:335
faiss::ProductQuantizer::byte_per_idx
size_t byte_per_idx
nb bytes per code component (1 or 2)
Definition: ProductQuantizer.h:32
faiss::ProductQuantizer::Train_hypercube
intialize centroids with nbits-D hypercube
Definition: ProductQuantizer.h:43
faiss::ProductQuantizer::set_derived_values
void set_derived_values()
compute derived values when d, M and nbits have been set
Definition: ProductQuantizer.cpp:162
faiss::ProductQuantizer::sdc_table
std::vector< float > sdc_table
Symmetric Distance Table.
Definition: ProductQuantizer.h:158
faiss::ProductQuantizer::Train_shared
share dictionary accross PQ segments
Definition: ProductQuantizer.h:42
faiss::ProductQuantizer::dsub
size_t dsub
dimensionality of each subvector
Definition: ProductQuantizer.h:31
faiss::ProductQuantizer::compute_distance_tables
void compute_distance_tables(size_t nx, const float *x, float *dis_tables) const
Definition: ProductQuantizer.cpp:436
faiss::ProductQuantizer::compute_code_from_distance_table
void compute_code_from_distance_table(const float *tab, uint8_t *code) const
Definition: ProductQuantizer.cpp:360
faiss::ProductQuantizer::compute_codes
void compute_codes(const float *x, uint8_t *codes, size_t n) const
same as compute_code for several vectors
Definition: ProductQuantizer.cpp:382
faiss::ProductQuantizer::compute_distance_table
void compute_distance_table(const float *x, float *dis_table) const
Definition: ProductQuantizer.cpp:407
faiss::ProductQuantizer::search
void search(const float *x, size_t nx, const uint8_t *codes, const size_t ncodes, float_maxheap_array_t *res, bool init_finalize_heap=true) const
Definition: ProductQuantizer.cpp:537
faiss::ProductQuantizer::code_size
size_t code_size
byte per indexed vector
Definition: ProductQuantizer.h:33
faiss::ProductQuantizer::assign_index
Index * assign_index
Definition: ProductQuantizer.h:52
faiss::ProductQuantizer::ksub
size_t ksub
number of centroids for each subquantizer
Definition: ProductQuantizer.h:34
faiss::HeapArray
Definition: Heap.h:350
faiss::ProductQuantizer::search_ip
void search_ip(const float *x, size_t nx, const uint8_t *codes, const size_t ncodes, float_minheap_array_t *res, bool init_finalize_heap=true) const
Definition: ProductQuantizer.cpp:562
faiss::ProductQuantizer::compute_code
void compute_code(const float *x, uint8_t *code) const
Quantize one vector with the product quantizer.
Definition: ProductQuantizer.cpp:308
faiss::ProductQuantizer::Train_hot_start
the centroids are already initialized
Definition: ProductQuantizer.h:41
faiss::ProductQuantizer::cp
ClusteringParameters cp
parameters used during clustering
Definition: ProductQuantizer.h:48
faiss::ClusteringParameters
Definition: Clustering.h:23
faiss::ProductQuantizer::M
size_t M
number of subquantizers
Definition: ProductQuantizer.h:27
faiss::Index
Definition: Index.h:62
faiss::ProductQuantizer
Definition: ProductQuantizer.h:24
faiss::ProductQuantizer::get_centroids
float * get_centroids(size_t m, size_t i)
return the centroids associated with subvector m
Definition: ProductQuantizer.h:58
faiss::ProductQuantizer::d
size_t d
size of the input vectors
Definition: ProductQuantizer.h:26
faiss::ProductQuantizer::verbose
bool verbose
verbose during training?
Definition: ProductQuantizer.h:35
faiss::ProductQuantizer::centroids
std::vector< float > centroids
Centroid table, size M * ksub * dsub.
Definition: ProductQuantizer.h:55
faiss::ProductQuantizer::train_type_t
train_type_t
initialization
Definition: ProductQuantizer.h:39