pub struct ScalarQuantizer { /* private fields */ }Expand description
Scalar quantizer for vector compression
Quantizes floating-point vectors to int8 or uint8, achieving 4x compression with typically < 5% accuracy loss.
Implementations§
Source§impl ScalarQuantizer
impl ScalarQuantizer
Sourcepub fn new(dimension: usize, signed: bool) -> Self
pub fn new(dimension: usize, signed: bool) -> Self
Create a new scalar quantizer for the given dimension
§Arguments
dimension- Vector dimensionsigned- Use int8 (true) or uint8 (false)
Sourcepub fn train(&mut self, vectors: &[Vec<f32>]) -> Result<()>
pub fn train(&mut self, vectors: &[Vec<f32>]) -> Result<()>
Train the quantizer on a set of vectors
Learns min/max values for each dimension to establish the quantization range.
§Arguments
vectors- Training vectors
Sourcepub fn train_incremental(&mut self, vector: &[f32]) -> Result<()>
pub fn train_incremental(&mut self, vector: &[f32]) -> Result<()>
Train on a single vector (incremental training)
Sourcepub fn is_trained(&self) -> bool
pub fn is_trained(&self) -> bool
Check if the quantizer has been trained
Sourcepub fn quantize(&self, vector: &[f32]) -> Result<QuantizedVector>
pub fn quantize(&self, vector: &[f32]) -> Result<QuantizedVector>
Quantize a vector to uint8
Sourcepub fn dequantize(&self, quantized: &QuantizedVector) -> Result<Vec<f32>>
pub fn dequantize(&self, quantized: &QuantizedVector) -> Result<Vec<f32>>
Dequantize a vector back to f32
Sourcepub fn distance_l2_quantized(
&self,
a: &QuantizedVector,
b: &QuantizedVector,
) -> Result<f32>
pub fn distance_l2_quantized( &self, a: &QuantizedVector, b: &QuantizedVector, ) -> Result<f32>
Compute L2 distance between two quantized vectors (approximate)
Uses integer arithmetic for fast computation
Sourcepub fn dot_product_quantized(
&self,
a: &QuantizedVector,
b: &QuantizedVector,
) -> Result<f32>
pub fn dot_product_quantized( &self, a: &QuantizedVector, b: &QuantizedVector, ) -> Result<f32>
Compute dot product between two quantized vectors (approximate)
Sourcepub fn compression_ratio(&self) -> f32
pub fn compression_ratio(&self) -> f32
Get compression ratio (always 4x for 8-bit quantization)
Sourcepub fn memory_estimate(&self, num_vectors: usize) -> usize
pub fn memory_estimate(&self, num_vectors: usize) -> usize
Get memory usage estimate for a given number of vectors
Trait Implementations§
Source§impl Clone for ScalarQuantizer
impl Clone for ScalarQuantizer
Source§fn clone(&self) -> ScalarQuantizer
fn clone(&self) -> ScalarQuantizer
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ScalarQuantizer
impl Debug for ScalarQuantizer
Source§impl<'de> Deserialize<'de> for ScalarQuantizer
impl<'de> Deserialize<'de> for ScalarQuantizer
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>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for ScalarQuantizer
impl RefUnwindSafe for ScalarQuantizer
impl Send for ScalarQuantizer
impl Sync for ScalarQuantizer
impl Unpin for ScalarQuantizer
impl UnwindSafe for ScalarQuantizer
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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<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>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
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
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.