pub struct ProductQuantizer {
pub subspace_count: usize,
pub codebook_size: usize,
pub codebooks: Vec<Vec<Vec<f32>>>,
pub subvec_dim: usize,
}Expand description
Product quantizer: divides the embedding into subspaces and learns a codebook per subspace for efficient approximate nearest-neighbor search.
Fields§
§subspace_count: usizeNumber of subspaces.
codebook_size: usizeNumber of codes per subspace (e.g. 256 for 8-bit codes).
codebooks: Vec<Vec<Vec<f32>>>Codebooks: [subspace][code][subvec_dim].
subvec_dim: usizeDimension of each sub-vector (embedding_dim / subspace_count).
Implementations§
Source§impl ProductQuantizer
impl ProductQuantizer
Sourcepub fn new(subspace_count: usize, codebook_size: usize) -> Self
pub fn new(subspace_count: usize, codebook_size: usize) -> Self
Create an untrained product quantizer.
Sourcepub fn train(&mut self, embeddings: &[Vec<f32>])
pub fn train(&mut self, embeddings: &[Vec<f32>])
Train the product quantizer on a set of embeddings.
Uses a simplified k-means: randomly selects codebook_size distinct
embeddings as initial centroids, then runs a few iterations.
Sourcepub fn encode(&self, embedding: &[f32]) -> Vec<u8> ⓘ
pub fn encode(&self, embedding: &[f32]) -> Vec<u8> ⓘ
Encode an embedding as a vector of codebook indices (one per subspace).
Sourcepub fn decode(&self, codes: &[u8]) -> Vec<f32>
pub fn decode(&self, codes: &[u8]) -> Vec<f32>
Decode a code vector back to an approximate embedding.
Sourcepub fn approx_distance(&self, codes1: &[u8], codes2: &[u8]) -> f32
pub fn approx_distance(&self, codes1: &[u8], codes2: &[u8]) -> f32
Compute approximate L2 distance between two encoded vectors using codebook lookups.
Sourcepub fn is_trained(&self) -> bool
pub fn is_trained(&self) -> bool
Check whether the quantizer has been trained.
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 moreAuto Trait Implementations§
impl Freeze for ProductQuantizer
impl RefUnwindSafe for ProductQuantizer
impl Send for ProductQuantizer
impl Sync for ProductQuantizer
impl Unpin for ProductQuantizer
impl UnsafeUnpin for ProductQuantizer
impl UnwindSafe for ProductQuantizer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.