Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
/tmp/faiss/IndexScalarQuantizer.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_INDEX_SCALAR_QUANTIZER_H
12 #define FAISS_INDEX_SCALAR_QUANTIZER_H
13
14 #include <stdint.h>
15
16
17 #include <vector>
18
19
20 #include "IndexIVF.h"
21
22
23 namespace faiss {
24
25 /**
26 * The uniform quantizer has a range [vmin, vmax]. The range can be
27 * the same for all dimensions (uniform) or specific per dimension
28 * (default).
29 */
30
31
32 struct ScalarQuantizer {
33
34 enum QuantizerType {
35 QT_8bit, ///< 8 bits per component
36 QT_4bit, ///< 4 bits per component
37 QT_8bit_uniform, ///< same, shared range for all dimensions
38 QT_4bit_uniform,
39 QT_fp16,
40 };
41
42 QuantizerType qtype;
43
44 /** The uniform encoder can estimate the range of representable
45 * values of the unform encoder using different statistics. Here
46 * rs = rangestat_arg */
47
48 // rangestat_arg.
49 enum RangeStat {
50 RS_minmax, ///< [min - rs*(max-min), max + rs*(max-min)]
51 RS_meanstd, ///< [mean - std * rs, mean + std * rs]
52 RS_quantiles, ///< [Q(rs), Q(1-rs)]
53 RS_optim, ///< alternate optimization of reconstruction error
54 };
55
56 RangeStat rangestat;
57 float rangestat_arg;
58
59 /// dimension of input vectors
60 size_t d;
61
62 /// bytes per vector
63 size_t code_size;
64
65 /// trained values (including the range)
66 std::vector<float> trained;
67
68 ScalarQuantizer (size_t d, QuantizerType qtype);
69 ScalarQuantizer ();
70
71 void train (size_t n, const float *x);
72
73
74 /// same as compute_code for several vectors
75 void compute_codes (const float * x,
76 uint8_t * codes,
77 size_t n) const ;
78
79 /// decode a vector from a given code (or n vectors if third argument)
80 void decode (const uint8_t *code, float *x, size_t n) const;
81
82 // fast, non thread-safe way of computing vector-to-code and
83 // code-to-code distances.
84 struct DistanceComputer {
85
86 /// vector-to-code distance computation
87 virtual float compute_distance (const float *x,
88 const uint8_t *code) const = 0;
89
90 /// code-to-code distance computation
91 virtual float compute_code_distance (const uint8_t *code1,
92 const uint8_t *code2) const = 0;
93 virtual ~DistanceComputer () {}
94 };
95
96 DistanceComputer *get_distance_computer (MetricType metric = METRIC_L2)
97 const;
98
99 };
100
101
102 struct IndexScalarQuantizer: Index {
103 /// Used to encode the vectors
104 ScalarQuantizer sq;
105
106 /// Codes. Size ntotal * pq.code_size
107 std::vector<uint8_t> codes;
108
109 size_t code_size;
110
111 /** Constructor.
112 *
113 * @param d dimensionality of the input vectors
114 * @param M number of subquantizers
115 * @param nbits number of bit per subvector index
116 */
117 IndexScalarQuantizer (int d,
118 ScalarQuantizer::QuantizerType qtype,
119 MetricType metric = METRIC_L2);
120
121 IndexScalarQuantizer ();
122
123 void train(idx_t n, const float* x) override;
124
125 void add(idx_t n, const float* x) override;
126
127 void search(
128 idx_t n,
129 const float* x,
130 idx_t k,
131 float* distances,
132 idx_t* labels) const override;
133
134 void reset() override;
135
136 void reconstruct_n(idx_t i0, idx_t ni, float* recons) const override;
137
138 void reconstruct(idx_t key, float* recons) const override;
139
140 };
141
142
143 /** An IVF implementation where the components of the residuals are
144 * encoded with a scalar uniform quantizer. All distance computations
145 * are asymmetric, so the encoded vectors are decoded and approximate
146 * distances are computed.
147 */
148
149 struct IndexIVFScalarQuantizer: IndexIVF {
150 ScalarQuantizer sq;
151
152 IndexIVFScalarQuantizer(Index *quantizer, size_t d, size_t nlist,
153 ScalarQuantizer::QuantizerType qtype,
154 MetricType metric = METRIC_L2);
155
156 IndexIVFScalarQuantizer();
157
158 void train_residual(idx_t n, const float* x) override;
159
160 void encode_vectors(idx_t n, const float* x,
161 const idx_t *list_nos,
162 uint8_t * codes) const override;
163
164 void add_with_ids(idx_t n, const float* x, const long* xids) override;
165
166 InvertedListScanner *get_InvertedListScanner (bool store_pairs)
167 const override;
168
169
170 void reconstruct_from_offset (long list_no, long offset,
171 float* recons) const override;
172
173 };
174
175
176 }
177
178
179 #endif
faiss::IndexIVF
Definition: IndexIVF.h:91
faiss::IndexIVFScalarQuantizer::encode_vectors
void encode_vectors(idx_t n, const float *x, const idx_t *list_nos, uint8_t *codes) const override
Definition: IndexScalarQuantizer.cpp:1155
faiss::ScalarQuantizer::code_size
size_t code_size
bytes per vector
Definition: IndexScalarQuantizer.h:63
faiss::ScalarQuantizer::QuantizerType
QuantizerType
Definition: IndexScalarQuantizer.h:34
faiss::IndexScalarQuantizer::search
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
Definition: IndexScalarQuantizer.cpp:1072
faiss::IndexIVFScalarQuantizer::train_residual
void train_residual(idx_t n, const float *x) override
Definition: IndexScalarQuantizer.cpp:1129
faiss::ScalarQuantizer::RS_optim
alternate optimization of reconstruction error
Definition: IndexScalarQuantizer.h:53
faiss::IndexScalarQuantizer
Definition: IndexScalarQuantizer.h:102
faiss::ScalarQuantizer::QT_8bit_uniform
same, shared range for all dimensions
Definition: IndexScalarQuantizer.h:37
faiss::IndexIVFScalarQuantizer::reconstruct_from_offset
void reconstruct_from_offset(long list_no, long offset, float *recons) const override
Definition: IndexScalarQuantizer.cpp:1418
faiss::IndexScalarQuantizer::add
void add(idx_t n, const float *x) override
Definition: IndexScalarQuantizer.cpp:1017
faiss::ScalarQuantizer::QT_4bit
4 bits per component
Definition: IndexScalarQuantizer.h:36
faiss::ScalarQuantizer::DistanceComputer::compute_distance
virtual float compute_distance(const float *x, const uint8_t *code) const =0
vector-to-code distance computation
faiss::IndexScalarQuantizer::reset
void reset() override
removes all elements from the database.
Definition: IndexScalarQuantizer.cpp:1087
faiss::ScalarQuantizer
Definition: IndexScalarQuantizer.h:32
faiss::IndexIVFScalarQuantizer::add_with_ids
void add_with_ids(idx_t n, const float *x, const long *xids) override
Definition: IndexScalarQuantizer.cpp:1188
faiss::Index::d
int d
vector dimension
Definition: Index.h:66
faiss::IndexScalarQuantizer::codes
std::vector< uint8_t > codes
Codes. Size ntotal * pq.code_size.
Definition: IndexScalarQuantizer.h:107
faiss::ScalarQuantizer::DistanceComputer
Definition: IndexScalarQuantizer.h:84
faiss::IndexScalarQuantizer::sq
ScalarQuantizer sq
Used to encode the vectors.
Definition: IndexScalarQuantizer.h:104
faiss::Index::idx_t
long idx_t
all indices are this type
Definition: Index.h:64
faiss::ScalarQuantizer::RangeStat
RangeStat
Definition: IndexScalarQuantizer.h:49
faiss::IndexIVFScalarQuantizer
Definition: IndexScalarQuantizer.h:149
faiss::ScalarQuantizer::RS_quantiles
[Q(rs), Q(1-rs)]
Definition: IndexScalarQuantizer.h:52
faiss::ScalarQuantizer::RS_meanstd
[mean - std * rs, mean + std * rs]
Definition: IndexScalarQuantizer.h:51
faiss::InvertedListScanner
Definition: IndexIVF.h:261
faiss::ScalarQuantizer::decode
void decode(const uint8_t *code, float *x, size_t n) const
decode a vector from a given code (or n vectors if third argument)
Definition: IndexScalarQuantizer.cpp:955
faiss::IndexIVFScalarQuantizer::get_InvertedListScanner
InvertedListScanner * get_InvertedListScanner(bool store_pairs) const override
get a scanner for this index (store_pairs means ignore labels)
Definition: IndexScalarQuantizer.cpp:1404
faiss::ScalarQuantizer::compute_codes
void compute_codes(const float *x, uint8_t *codes, size_t n) const
same as compute_code for several vectors
Definition: IndexScalarQuantizer.cpp:943
faiss::ScalarQuantizer::DistanceComputer::compute_code_distance
virtual float compute_code_distance(const uint8_t *code1, const uint8_t *code2) const =0
code-to-code distance computation
faiss::ScalarQuantizer::QT_8bit
8 bits per component
Definition: IndexScalarQuantizer.h:35
faiss::Index
Definition: Index.h:62
faiss::IndexScalarQuantizer::reconstruct_n
void reconstruct_n(idx_t i0, idx_t ni, float *recons) const override
Definition: IndexScalarQuantizer.cpp:1093
faiss::IndexScalarQuantizer::reconstruct
void reconstruct(idx_t key, float *recons) const override
Definition: IndexScalarQuantizer.cpp:1103
faiss::ScalarQuantizer::RS_minmax
[min - rs*(max-min), max + rs*(max-min)]
Definition: IndexScalarQuantizer.h:50
faiss::ScalarQuantizer::trained
std::vector< float > trained
trained values (including the range)
Definition: IndexScalarQuantizer.h:66
faiss::Level1Quantizer::quantizer
Index * quantizer
quantizer that maps vectors to inverted lists
Definition: IndexIVF.h:33
faiss::IndexScalarQuantizer::train
void train(idx_t n, const float *x) override
Definition: IndexScalarQuantizer.cpp:1011
faiss::ScalarQuantizer::d
size_t d
dimension of input vectors
Definition: IndexScalarQuantizer.h:60
faiss::Level1Quantizer::nlist
size_t nlist
number of possible key values
Definition: IndexIVF.h:34
faiss::MetricType
MetricType
Some algorithms support both an inner product version and a L2 search version.
Definition: Index.h:45