pub struct BinaryQuantizer;Expand description
A simple, training-free binary quantizer.
The canonical implementation of compression with a binary quantizer maps negative values to -1 (encoded as a bit 0) and positive values to 1. Distances can then be approximated using the Hamming distance between the compressed vectors.
As a convenience diskann_quantization::bits::SquaredL2 and
diskann_quantization::bits::InnerProduct may be used, which correctly dispatch to the
proper post-op for similarity scores versus mathematical values.
§Example
use diskann_quantization::{
AsFunctor, CompressInto,
distances::Hamming,
binary::BinaryQuantizer,
bits::{BoxedBitSlice, Binary},
};
use diskann_utils::{Reborrow, ReborrowMut};
use diskann_vector::{
PureDistanceFunction, DistanceFunction, MathematicalValue,
};
let x = vec![-1, 1, 1, -1, 1];
let y = vec![1, -1, 1, -1, -1];
// Create a quantizer
let quantizer = BinaryQuantizer;
// Create output vectors for compression.
let mut bx = BoxedBitSlice::<1, Binary>::new_boxed(x.len());
let mut by = BoxedBitSlice::<1, Binary>::new_boxed(y.len());
// Do the compression.
quantizer.compress_into(x.as_slice(), bx.reborrow_mut()).unwrap();
quantizer.compress_into(y.as_slice(), by.reborrow_mut()).unwrap();
// Because our inputs are limited to -1 and 1, the compression is perfect.
assert_eq!(bx.get(0).unwrap(), x[0]);
assert_eq!(bx.get(1).unwrap(), x[1]);
// But the compressed vectors only consume a single byte.
assert_eq!(bx.bytes(), 1);
// Lets compute some distances!
assert_eq!(
Hamming::evaluate(bx.reborrow(), by.reborrow()).unwrap(),
MathematicalValue::<u32>::new(3)
);
// We can also use the `AsFunctor` trait if we want more uniformity.
let f: Hamming = quantizer.as_functor();
assert_eq!(
f.evaluate_similarity(bx.reborrow(), by.reborrow()).unwrap(),
MathematicalValue::<u32>::new(3)
);Trait Implementations§
Source§impl AsFunctor<Hamming> for BinaryQuantizer
impl AsFunctor<Hamming> for BinaryQuantizer
Source§fn as_functor(&self) -> Hamming
fn as_functor(&self) -> Hamming
Return a crate::distances::Hamming functor for performing distance computations
on bit vectors.
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<T> CompressInto<&[T], BitSliceBase<1, Binary, MutSlicePtr<'_, u8>>> for BinaryQuantizerwhere
T: PartialOrd + Default,
impl<T> CompressInto<&[T], BitSliceBase<1, Binary, MutSlicePtr<'_, u8>>> for BinaryQuantizerwhere
T: PartialOrd + Default,
Source§fn compress_into(
&self,
from: &[T],
into: MutBitSlice<'_, 1, Binary>,
) -> Result<(), Self::Error>
fn compress_into( &self, from: &[T], into: MutBitSlice<'_, 1, Binary>, ) -> Result<(), Self::Error>
Compress the source vector into a binary representation.
This works by mapping positive numbers (as defined by v > T::default()) to 1 and
negative numbers (as defined by v <= T::default()) to -1.
§Panics
Panics if from.len() != into.len().
Source§type Error = Infallible
type Error = Infallible
Source§impl Debug for BinaryQuantizer
impl Debug for BinaryQuantizer
impl Copy for BinaryQuantizer
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> 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