pub struct ProductQuantizer { /* private fields */ }Expand description
Product Quantizer for vector compression.
The quantizer stores codebooks (centroids) for each subspace and provides methods for encoding vectors and computing approximate distances.
Implementations§
Source§impl ProductQuantizer
impl ProductQuantizer
Sourcepub fn from_codebooks(
config: &PQConfig,
codebooks: Vec<Vec<Vec<f32>>>,
) -> Result<Self, VectorError>
pub fn from_codebooks( config: &PQConfig, codebooks: Vec<Vec<Vec<f32>>>, ) -> Result<Self, VectorError>
Sourcepub fn decode(&self, code: &PQCode) -> Vec<f32>
pub fn decode(&self, code: &PQCode) -> Vec<f32>
Decode a PQ code back to an approximate vector.
The reconstructed vector is the concatenation of the centroids indicated by the code.
Sourcepub fn compute_distance_table(&self, query: &[f32]) -> DistanceTable
pub fn compute_distance_table(&self, query: &[f32]) -> DistanceTable
Compute a distance lookup table for asymmetric distance computation (ADC).
The table contains precomputed distances from each subvector of the query to all centroids in the corresponding codebook.
Shape: table[segment][centroid_idx] = distance
§Arguments
query: Query vector with dimension == config.dimension
Sourcepub fn asymmetric_distance(&self, table: &DistanceTable, code: &PQCode) -> f32
pub fn asymmetric_distance(&self, table: &DistanceTable, code: &PQCode) -> f32
Compute asymmetric distance from a precomputed distance table to a PQ code.
This is the primary method for fast approximate nearest neighbor search. The query vector is exact, while the database vector is compressed.
§Arguments
table: Distance table fromcompute_distance_tablecode: PQ code of a database vector
Sourcepub fn asymmetric_distance_squared(
&self,
table: &DistanceTable,
code: &PQCode,
) -> f32
pub fn asymmetric_distance_squared( &self, table: &DistanceTable, code: &PQCode, ) -> f32
Compute asymmetric squared distance (faster, no sqrt).
For Euclidean distance, returns the squared distance.
For other metrics, returns the same as asymmetric_distance.
Sourcepub fn symmetric_distance(&self, code_a: &PQCode, code_b: &PQCode) -> f32
pub fn symmetric_distance(&self, code_a: &PQCode, code_b: &PQCode) -> f32
Compute symmetric distance between two PQ codes.
This is faster but less accurate than asymmetric distance. Both vectors are compressed.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, VectorError>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, VectorError>
Trait Implementations§
Source§impl Clone for ProductQuantizer
impl Clone for ProductQuantizer
Source§fn clone(&self) -> ProductQuantizer
fn clone(&self) -> ProductQuantizer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more