pub struct BinaryQuantizer { /* private fields */ }Expand description
Binary quantizer for extreme memory compression (32x reduction)
Converts float32 vectors to 1-bit by thresholding each dimension. Uses efficient bit-packing and Hamming distance for similarity.
Implementations§
Source§impl BinaryQuantizer
impl BinaryQuantizer
Sourcepub fn new(config: BinaryQuantizationConfig) -> Self
pub fn new(config: BinaryQuantizationConfig) -> Self
Create a new binary quantizer
Sourcepub fn fit(&mut self, vectors: &[Vec<f32>]) -> Result<()>
pub fn fit(&mut self, vectors: &[Vec<f32>]) -> Result<()>
Fit quantizer to training data
Computes per-dimension thresholds (mean or zero).
Sourcepub fn quantize(&self, vector: &[f32]) -> Vec<u8> ⓘ
pub fn quantize(&self, vector: &[f32]) -> Vec<u8> ⓘ
Quantize a float32 vector to binary (packed as u8 array)
Each bit represents whether the value is above the threshold. Bits are packed into u8 bytes (8 bits per byte).
Sourcepub fn dequantize(&self, binary: &[u8]) -> Vec<f32>
pub fn dequantize(&self, binary: &[u8]) -> Vec<f32>
Dequantize a binary vector back to float32 (approximate)
Reconstructs as threshold ± 1.0 based on bit values.
Sourcepub fn dequantize_batch(&self, binary: &[Vec<u8>]) -> Vec<Vec<f32>>
pub fn dequantize_batch(&self, binary: &[Vec<u8>]) -> Vec<Vec<f32>>
Dequantize multiple vectors
Sourcepub fn hamming_distance(&self, a: &[u8], b: &[u8]) -> u32
pub fn hamming_distance(&self, a: &[u8], b: &[u8]) -> u32
Compute Hamming distance between binary vectors
Hamming distance = number of differing bits (very fast with XOR + popcount).
Sourcepub fn hamming_similarity(&self, a: &[u8], b: &[u8]) -> f32
pub fn hamming_similarity(&self, a: &[u8], b: &[u8]) -> f32
Compute normalized Hamming similarity (0.0 to 1.0)
Similarity = 1 - (hamming_distance / num_bits)
Sourcepub fn compression_ratio(&self) -> f32
pub fn compression_ratio(&self) -> f32
Get compression ratio
Sourcepub fn memory_savings(&self) -> f32
pub fn memory_savings(&self) -> f32
Get memory savings percentage
Sourcepub fn dimensions(&self) -> usize
pub fn dimensions(&self) -> usize
Get number of dimensions
Trait Implementations§
Source§impl Clone for BinaryQuantizer
impl Clone for BinaryQuantizer
Source§fn clone(&self) -> BinaryQuantizer
fn clone(&self) -> BinaryQuantizer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for BinaryQuantizer
impl Debug for BinaryQuantizer
Source§impl<'de> Deserialize<'de> for BinaryQuantizer
impl<'de> Deserialize<'de> for BinaryQuantizer
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for BinaryQuantizer
impl RefUnwindSafe for BinaryQuantizer
impl Send for BinaryQuantizer
impl Sync for BinaryQuantizer
impl Unpin for BinaryQuantizer
impl UnwindSafe for BinaryQuantizer
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 more