Skip to main content

ProductQuantizer

Struct ProductQuantizer 

Source
pub struct ProductQuantizer {
    pub num_subvectors: usize,
    pub num_centroids: usize,
    pub dim: usize,
    pub sub_dim: usize,
    pub codebook: Vec<f32>,
}
Expand description

Product Quantizer with learned codebooks.

Fields§

§num_subvectors: usize

Number of sub-vector segments.

§num_centroids: usize

Number of centroids per sub-vector (max 256 for u8 codes).

§dim: usize

Original vector dimension.

§sub_dim: usize

Dimension of each sub-vector.

§codebook: Vec<f32>

Codebook: [num_subvectors][num_centroids][sub_dim] stored flat. Layout: codebook[sv * num_centroids * sub_dim + c * sub_dim + d]

Implementations§

Source§

impl ProductQuantizer

Source

pub fn train( vectors: &[Vec<f32>], dim: usize, num_subvectors: usize, num_centroids: usize, ) -> Self

Train a product quantizer from a set of vectors using k-means.

num_subvectors must evenly divide the vector dimension. num_centroids must be <= 256 (for u8 encoding).

Source

pub fn encode(&self, vector: &[f32]) -> Vec<u8>

Encode a vector into PQ codes (one u8 per subvector).

Source

pub fn decode(&self, codes: &[u8]) -> Vec<f32>

Decode PQ codes back to an approximate vector.

Source

pub fn precompute_distance_table(&self, query: &[f32]) -> Vec<f32>

Precompute distance table for asymmetric distance computation.

Returns a table of shape [num_subvectors][num_centroids] (stored flat) containing the squared L2 distance from each query sub-vector to each centroid.

Source

pub fn asymmetric_distance_with_table(&self, table: &[f32], codes: &[u8]) -> f32

Compute asymmetric distance between query and encoded vector.

Uses a precomputed distance table for speed — this is just num_subvectors table lookups + additions.

Source

pub fn asymmetric_distance(&self, query: &[f32], codes: &[u8]) -> f32

Compute asymmetric distance between a query and an encoded vector.

Source

pub fn search( &self, query: &[f32], all_codes: &[u8], tombstones: &[u8], k: usize, ) -> Vec<(usize, f32)>

Search a collection of PQ-encoded vectors and return the top-k nearest by asymmetric distance (smallest distance = most similar).

all_codes is a flat buffer: [n_vectors * num_subvectors]. tombstones marks deleted vectors.

Source

pub fn search_rerank( &self, query: &[f32], all_codes: &[u8], vectors: &[Vec<f32>], tombstones: &[u8], candidates: usize, k: usize, ) -> Vec<(usize, f32)>

Search with PQ then re-rank top candidates with exact cosine similarity.

Returns (index, cosine_score) pairs sorted by score descending.

Source

pub fn encode_all(&self, vectors: &[Vec<f32>]) -> Vec<u8>

Encode all vectors and return flat code buffer.

Source

pub fn to_hdf5_data(&self) -> (&[f32], [i64; 3])

Serialize the quantizer state to flat data for HDF5 storage. Returns (codebook_flat, metadata: [num_subvectors, num_centroids, dim]).

Source

pub fn from_hdf5_data(codebook: Vec<f32>, metadata: [i64; 3]) -> Self

Reconstruct from HDF5 data.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.