Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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