ProductQuantizer

Struct ProductQuantizer 

Source
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

Source

pub fn train( config: &PQConfig, training_data: &[&[f32]], ) -> Result<Self, VectorError>

Train a Product Quantizer on training data.

§Arguments
  • config: PQ configuration
  • training_data: Training vectors (must all have dimension == config.dimension)
§Errors

Returns an error if:

  • Configuration is invalid
  • Training data is empty
  • Training vectors have wrong dimension
Source

pub fn from_codebooks( config: &PQConfig, codebooks: Vec<Vec<Vec<f32>>>, ) -> Result<Self, VectorError>

Create a Product Quantizer from pre-trained codebooks.

§Arguments
  • config: PQ configuration
  • codebooks: Pre-trained codebooks, shape [num_segments][num_centroids][subspace_dim]
§Errors

Returns an error if codebooks don’t match the configuration.

Source

pub fn config(&self) -> &PQConfig

Get the configuration.

Source

pub fn codebooks(&self) -> &[Vec<Vec<f32>>]

Get the codebooks.

Source

pub fn encode(&self, vector: &[f32]) -> PQCode

Encode a vector into a PQ code.

§Arguments
  • vector: Input vector with dimension == config.dimension
§Panics

Panics if the vector has wrong dimension.

Source

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.

Source

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
Source

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 from compute_distance_table
  • code: PQ code of a database vector
Source

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.

Source

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.

Source

pub fn to_bytes(&self) -> Vec<u8>

Serialize the quantizer to bytes.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self, VectorError>

Deserialize a quantizer from bytes.

§Errors

Returns an error if the bytes are malformed.

Trait Implementations§

Source§

impl Clone for ProductQuantizer

Source§

fn clone(&self) -> ProductQuantizer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ProductQuantizer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more