Faiss
 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 
33 
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.
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 
103  /// Used to encode the vectors
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,
119  MetricType metric = METRIC_L2);
120 
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 
150  ScalarQuantizer sq;
151 
152  IndexIVFScalarQuantizer(Index *quantizer, size_t d, size_t nlist,
154  MetricType metric = METRIC_L2);
155 
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
void encode_vectors(idx_t n, const float *x, const idx_t *list_nos, uint8_t *codes) const override
size_t code_size
bytes per vector
void search(idx_t n, const float *x, idx_t k, float *distances, idx_t *labels) const override
void train_residual(idx_t n, const float *x) override
alternate optimization of reconstruction error
same, shared range for all dimensions
void reconstruct_from_offset(long list_no, long offset, float *recons) const override
void add(idx_t n, const float *x) override
virtual float compute_distance(const float *x, const uint8_t *code) const =0
vector-to-code distance computation
void reset() override
removes all elements from the database.
void add_with_ids(idx_t n, const float *x, const long *xids) override
int d
vector dimension
Definition: Index.h:66
std::vector< uint8_t > codes
Codes. Size ntotal * pq.code_size.
ScalarQuantizer sq
Used to encode the vectors.
long idx_t
all indices are this type
Definition: Index.h:64
[mean - std * rs, mean + std * rs]
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)
InvertedListScanner * get_InvertedListScanner(bool store_pairs) const override
get a scanner for this index (store_pairs means ignore labels)
void compute_codes(const float *x, uint8_t *codes, size_t n) const
same as compute_code for several vectors
virtual float compute_code_distance(const uint8_t *code1, const uint8_t *code2) const =0
code-to-code distance computation
void reconstruct_n(idx_t i0, idx_t ni, float *recons) const override
void reconstruct(idx_t key, float *recons) const override
[min - rs*(max-min), max + rs*(max-min)]
std::vector< float > trained
trained values (including the range)
Index * quantizer
quantizer that maps vectors to inverted lists
Definition: IndexIVF.h:33
void train(idx_t n, const float *x) override
size_t d
dimension of input vectors
size_t nlist
number of possible key values
Definition: IndexIVF.h:34
MetricType
Some algorithms support both an inner product version and a L2 search version.
Definition: Index.h:45