PQConfig

Struct PQConfig 

Source
pub struct PQConfig {
    pub dimension: usize,
    pub num_segments: usize,
    pub num_centroids: usize,
    pub distance_metric: DistanceMetric,
    pub training_iterations: usize,
    pub seed: Option<u64>,
}
Expand description

Configuration for Product Quantization.

§Parameters

  • dimension: The dimension of vectors to quantize
  • num_segments: Number of subspaces (M). Must divide dimension evenly.
  • num_centroids: Number of centroids per subspace (K). Typically 256 (8 bits per code).
  • distance_metric: Distance metric for codebook training and distance computation

§Memory Usage

  • Codebooks: M × K × (D/M) × 4 bytes
  • Per-vector codes: M × ceil(log2(K)/8) bytes

For typical settings (D=128, M=8, K=256):

  • Codebooks: 8 × 256 × 16 × 4 = 128KB
  • Per-vector: 8 bytes (compression ratio: 64x)

Fields§

§dimension: usize

Dimension of input vectors.

§num_segments: usize

Number of subspaces (segments).

§num_centroids: usize

Number of centroids per subspace.

§distance_metric: DistanceMetric

Distance metric for training and search.

§training_iterations: usize

Number of training iterations for k-means.

§seed: Option<u64>

Random seed for reproducible training.

Implementations§

Source§

impl PQConfig

Source

pub fn new(dimension: usize, num_segments: usize) -> Self

Create a new PQ configuration.

§Arguments
  • dimension: The dimension of vectors to quantize
  • num_segments: Number of subspaces. Must divide dimension evenly.
§Defaults
  • num_centroids: 256 (8-bit codes)
  • distance_metric: Euclidean
  • training_iterations: 25
  • seed: None (non-deterministic)
§Panics

Panics if num_segments is 0 or doesn’t divide dimension evenly.

Source

pub const fn with_num_centroids(self, k: usize) -> Self

Set the number of centroids per subspace.

Common values:

  • 256 (8-bit codes, default)
  • 65536 (16-bit codes, more accurate but larger codebooks)
Source

pub const fn with_distance_metric(self, metric: DistanceMetric) -> Self

Set the distance metric.

Source

pub const fn with_training_iterations(self, iterations: usize) -> Self

Set the number of training iterations.

Source

pub const fn with_seed(self, seed: u64) -> Self

Set the random seed for reproducible training.

Source

pub const fn subspace_dimension(&self) -> usize

Get the dimension of each subspace.

Source

pub fn bits_per_code(&self) -> usize

Calculate the number of bits per code.

Returns the number of bits needed to represent a centroid index.

Source

pub fn bytes_per_code(&self) -> usize

Calculate bytes per encoded vector.

Source

pub fn validate(&self) -> Result<(), VectorError>

Validate the configuration.

§Errors

Returns an error if:

  • dimension is 0
  • num_segments is 0 or doesn’t divide dimension
  • num_centroids is 0
Source

pub fn compression_ratio(&self) -> f32

Calculate compression ratio compared to full f32 vectors.

Trait Implementations§

Source§

impl Clone for PQConfig

Source§

fn clone(&self) -> PQConfig

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 PQConfig

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