hermes_core/structures/vector/quantization/mod.rs
1//! Vector quantization methods for IVF indexes
2//!
3//! This module provides different quantization strategies:
4//! - `rabitq` - RaBitQ binary quantization (32x compression)
5//! - `pq` - Product Quantization with OPQ and anisotropic loss (ScaNN-style)
6//!
7//! All quantizers implement the `Quantizer` trait for use with IVF indexes.
8
9mod pq;
10mod rabitq;
11
12pub use pq::{DistanceTable, PQCodebook, PQConfig, PQVector};
13pub use rabitq::{QuantizedQuery, QuantizedVector, RaBitQCodebook, RaBitQConfig};
14
15use super::ivf::cluster::QuantizedCode;
16
17/// Trait for vector quantization methods
18///
19/// Quantizers encode vectors into compact codes and provide
20/// fast approximate distance computation.
21pub trait Quantizer: Clone + Send + Sync {
22 /// The quantized code type
23 type Code: QuantizedCode;
24
25 /// Configuration type
26 type Config: Clone;
27
28 /// Query-specific precomputed data for fast distance computation
29 type QueryData;
30
31 /// Encode a vector (optionally relative to a centroid)
32 fn encode(&self, vector: &[f32], centroid: Option<&[f32]>) -> Self::Code;
33
34 /// Prepare query-specific data for fast distance computation
35 fn prepare_query(&self, query: &[f32], centroid: Option<&[f32]>) -> Self::QueryData;
36
37 /// Compute approximate distance using precomputed query data
38 fn compute_distance(&self, query_data: &Self::QueryData, code: &Self::Code) -> f32;
39
40 /// Decode a code back to approximate vector (if supported)
41 fn decode(&self, _code: &Self::Code) -> Option<Vec<f32>> {
42 None
43 }
44
45 /// Memory usage of the quantizer itself (codebooks, etc.)
46 fn size_bytes(&self) -> usize;
47}