pub struct EmbeddingCompressor { /* private fields */ }Expand description
Random projection matrix compressor using Achlioptas (2003) sparse projection.
Each entry of the projection matrix is +sqrt(3), 0, or -sqrt(3) with probabilities 1/6, 2/3, 1/6 respectively.
Implementations§
Source§impl EmbeddingCompressor
impl EmbeddingCompressor
Sourcepub fn new(config: CompressionConfig) -> Self
pub fn new(config: CompressionConfig) -> Self
Build compressor with a sparse random projection matrix (Achlioptas, 2003).
Each matrix entry is:
- +sqrt(3) with probability 1/6
- 0 with probability 2/3
- -sqrt(3) with probability 1/6
Sourcepub fn compress(&self, embedding: &[f32]) -> Result<Vec<f32>, String>
pub fn compress(&self, embedding: &[f32]) -> Result<Vec<f32>, String>
Compress a single embedding vector.
Returns an error if the input length does not match input_dim.
Sourcepub fn compress_batch(
&self,
embeddings: &[Vec<f32>],
) -> Result<Vec<Vec<f32>>, String>
pub fn compress_batch( &self, embeddings: &[Vec<f32>], ) -> Result<Vec<Vec<f32>>, String>
Compress a batch of embeddings.
Returns an error if any embedding has incorrect length.
Sourcepub fn similarity_preservation_ratio(
&self,
a: &[f32],
b: &[f32],
) -> Result<f32, String>
pub fn similarity_preservation_ratio( &self, a: &[f32], b: &[f32], ) -> Result<f32, String>
Approximate the similarity preservation ratio between two vectors.
Computes cosine similarity in both original and compressed spaces and returns the ratio (compressed / original). Should be close to 1.0 for high-dimensional inputs (Johnson-Lindenstrauss lemma).
Sourcepub fn config(&self) -> &CompressionConfig
pub fn config(&self) -> &CompressionConfig
Return a reference to the compression configuration.
Sourcepub fn compression_ratio(&self) -> f32
pub fn compression_ratio(&self) -> f32
Return the compression ratio (input_dim / output_dim).
Auto Trait Implementations§
impl Freeze for EmbeddingCompressor
impl RefUnwindSafe for EmbeddingCompressor
impl Send for EmbeddingCompressor
impl Sync for EmbeddingCompressor
impl Unpin for EmbeddingCompressor
impl UnsafeUnpin for EmbeddingCompressor
impl UnwindSafe for EmbeddingCompressor
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> 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.