Trait Quantizer

Source
pub trait Quantizer: Send + Sync {
    type Code: Clone + Send + Sync;

    // Required methods
    fn encode(&self, vector: &[f32]) -> Self::Code;
    fn distance_approx(&self, a: &Self::Code, b: &Self::Code) -> f32;
    fn distance_exact(&self, a: &[f32], b: &[f32]) -> f32;
    fn is_accelerated(&self) -> bool;
    fn name(&self) -> &str;

    // Provided methods
    fn needs_training(&self) -> bool { ... }
    fn train(&mut self, _sample: &[&[f32]]) { ... }
}
Expand description

Acceleration strategy for distance computations.

Implementations encode vectors into compact codes and provide fast approximate distance between codes.

The graph uses distance_approx for candidate exploration (hot path) and distance_exact for final neighbor selection (quality-critical).

Required Associated Types§

Source

type Code: Clone + Send + Sync

Compact representation of a vector.

  • NoQuantizer: () (zero overhead)
  • Scalar: Vec<u8> (D bytes)
  • Product: Vec<u8> (M bytes, where M = D/subvector_dim)
  • Binary: Vec<u64> (D/64 words)

Required Methods§

Source

fn encode(&self, vector: &[f32]) -> Self::Code

Encode a full-precision vector into a compact code.

Called once per insert. The code is stored alongside the vector.

Source

fn distance_approx(&self, a: &Self::Code, b: &Self::Code) -> f32

Fast approximate distance between two codes.

This is the hot path: called O(ef_construction × log N) times per insert. Must be significantly faster than distance_exact for the acceleration to be worthwhile.

Source

fn distance_exact(&self, a: &[f32], b: &[f32]) -> f32

Exact distance between full-precision vectors.

Used for final neighbor selection (heuristic pruning) where distance quality matters more than speed.

Source

fn is_accelerated(&self) -> bool

Whether this quantizer provides actual acceleration.

When false, distance_approx is unused and the graph calls distance_exact directly. This avoids storing codes.

Source

fn name(&self) -> &str

Human-readable name of this strategy.

Provided Methods§

Source

fn needs_training(&self) -> bool

Whether this quantizer needs training on a data sample before use.

Product Quantization requires training a codebook; Scalar and Binary don’t.

Source

fn train(&mut self, _sample: &[&[f32]])

Train the quantizer on a sample of vectors.

Only called when needs_training() returns true. For PQ: trains the codebook via k-means on subvectors.

Implementors§