Struct faiss::ScalarQuantizer

struct ScalarQuantizer : public faiss::Quantizer

The uniform quantizer has a range [vmin, vmax]. The range can be the same for all dimensions (uniform) or specific per dimension (default).

Public Types

enum QuantizerType


enumerator QT_8bit

8 bits per component

enumerator QT_4bit

4 bits per component

enumerator QT_8bit_uniform

same, shared range for all dimensions

enumerator QT_4bit_uniform
enumerator QT_fp16
enumerator QT_8bit_direct

fast indexing of uint8s

enumerator QT_6bit

6 bits per component

enumerator QT_bf16
enum RangeStat

The uniform encoder can estimate the range of representable values of the unform encoder using different statistics. Here rs = rangestat_arg


enumerator RS_minmax

[min - rs*(max-min), max + rs*(max-min)]

enumerator RS_meanstd

[mean - std * rs, mean + std * rs]

enumerator RS_quantiles

[Q(rs), Q(1-rs)]

enumerator RS_optim

alternate optimization of reconstruction error

Public Functions

ScalarQuantizer(size_t d, QuantizerType qtype)
void set_derived_sizes()

updates internal values based on qtype and d

virtual void train(size_t n, const float *x) override

Train the quantizer


x – training vectors, size n * d

virtual void compute_codes(const float *x, uint8_t *codes, size_t n) const override

Encode a set of vectors

  • x – vectors to encode, size n * d

  • codes – output codes, size n * code_size

virtual void decode(const uint8_t *code, float *x, size_t n) const override

Decode a set of vectors

  • codes – codes to decode, size n * code_size

  • x – output vectors, size n * d

SQuantizer *select_quantizer() const
SQDistanceComputer *get_distance_computer(MetricType metric = METRIC_L2) const
InvertedListScanner *select_InvertedListScanner(MetricType mt, const Index *quantizer, bool store_pairs, const IDSelector *sel, bool by_residual = false) const

Public Members

QuantizerType qtype = QT_8bit
RangeStat rangestat = RS_minmax
float rangestat_arg = 0
size_t bits = 0

bits per scalar code

std::vector<float> trained

trained values (including the range)

size_t d

size of the input vectors

size_t code_size

bytes per indexed vector

struct SQDistanceComputer : public faiss::FlatCodesDistanceComputer

Public Functions

inline SQDistanceComputer()
virtual float query_to_code(const uint8_t *code) const = 0
inline virtual float distance_to_code(const uint8_t *code) final

compute distance of current query to an encoded vector

inline virtual float operator()(idx_t i) override

compute distance of vector i to current query

virtual void set_query(const float *x) = 0

called before computing distances. Pointer x should remain valid while operator () is called

inline virtual void distances_batch_4(const idx_t idx0, const idx_t idx1, const idx_t idx2, const idx_t idx3, float &dis0, float &dis1, float &dis2, float &dis3)

compute distances of current query to 4 stored vectors. certain DistanceComputer implementations may benefit heavily from this.

virtual float symmetric_dis(idx_t i, idx_t j) = 0

compute distance between two stored vectors

Public Members

const float *q
const uint8_t *codes
size_t code_size
struct SQuantizer

Public Functions

virtual void encode_vector(const float *x, uint8_t *code) const = 0
virtual void decode_vector(const uint8_t *code, float *x) const = 0
inline virtual ~SQuantizer()