pub struct MinMaxQuantizer { /* private fields */ }Expand description
Recall that from the module-level documentation, MinMaxQuantizer, quantizes X
into n bit vectors as follows -
X' = round((X - s) * (2^n - 1) / c).clamp(0, 2^n - 1))where s is a shift value and c is a scaling parameter computed from the range of values.
For most bit widths (>1), given a positive scaling parameter grid_scale : f32,
these are computed as:
- m = (max_i X[i] + min_i X[i]) / 2.0
- w = max_i X[i] - min_i X[i]
- s = m - w * grid_scale
- c = 2 * w * grid_scale
where `grid_scale` is an input to the quantizer.For 1-bit quantization, to avoid outliers, s and c are derived differently:
- Values are first split into two groups: those below and above the mean.
sis the average of values below the mean.cis the difference between the average of values above the mean ands.
See MinMaxCompensation for notation.
We have then that
X = X' * (c / (2^n - 1)) + s
-------------- -
| |
ax bxImplementations§
Source§impl MinMaxQuantizer
impl MinMaxQuantizer
Sourcepub fn new(
transform: Transform<GlobalAllocator>,
grid_scale: Positive<f32>,
) -> Self
pub fn new( transform: Transform<GlobalAllocator>, grid_scale: Positive<f32>, ) -> Self
Instantiates a new quantizer with specific transform.
Sourcepub fn output_dim(&self) -> usize
pub fn output_dim(&self) -> usize
Output dimension of vectors after applying transform.
Output storage vectors should use this dimension instead of self.dim() because
in general, the output dim may be different from the input dimension.
Trait Implementations§
Source§impl AsFunctor<MinMaxCosine> for MinMaxQuantizer
impl AsFunctor<MinMaxCosine> for MinMaxQuantizer
fn as_functor(&self) -> MinMaxCosine
Source§impl AsFunctor<MinMaxCosineNormalized> for MinMaxQuantizer
impl AsFunctor<MinMaxCosineNormalized> for MinMaxQuantizer
fn as_functor(&self) -> MinMaxCosineNormalized
Source§impl AsFunctor<MinMaxIP> for MinMaxQuantizer
impl AsFunctor<MinMaxIP> for MinMaxQuantizer
fn as_functor(&self) -> MinMaxIP
Source§impl AsFunctor<MinMaxL2Squared> for MinMaxQuantizer
impl AsFunctor<MinMaxL2Squared> for MinMaxQuantizer
fn as_functor(&self) -> MinMaxL2Squared
Source§impl<'a, T> CompressInto<&[T], Slice<&'a mut [f32], Mut<'a, FullQueryMeta>>> for MinMaxQuantizer
impl<'a, T> CompressInto<&[T], Slice<&'a mut [f32], Mut<'a, FullQueryMeta>>> for MinMaxQuantizer
Source§fn compress_into(
&self,
from: &[T],
to: FullQueryMut<'a>,
) -> Result<(), Self::Error>
fn compress_into( &self, from: &[T], to: FullQueryMut<'a>, ) -> Result<(), Self::Error>
Compress the input vector from into a FullQueryMut to.
This method simply applies the transformation to the input without any compression.
§Error
Returns an error if the input contains NaN.
§Panics
Panics if:
from.len() != self.dim(): Vector to be compressed must have the same dimensionality as the quantizer.to.len() != self.output_dim(): Compressed vector must have the same dimensionality as the quantizer.
Source§type Error = InputContainsNaN
type Error = InputContainsNaN
Source§impl<const NBITS: usize, T> CompressInto<&[T], VectorBase<NBITS, Unsigned, MutSlicePtr<'_, u8>, Mut<'_, MinMaxCompensation>>> for MinMaxQuantizer
impl<const NBITS: usize, T> CompressInto<&[T], VectorBase<NBITS, Unsigned, MutSlicePtr<'_, u8>, Mut<'_, MinMaxCompensation>>> for MinMaxQuantizer
Source§fn compress_into(
&self,
from: &[T],
to: DataMutRef<'_, NBITS>,
) -> Result<L2Loss, Self::Error>
fn compress_into( &self, from: &[T], to: DataMutRef<'_, NBITS>, ) -> Result<L2Loss, Self::Error>
Compress the input vector from into a mut ref of Data to.
This method computes and stores the compensation coefficients required for computing distances correctly.
§Error
Returns an error if the input contains NaN.
§Panics
Panics if:
from.len() != self.dim(): Vector to be compressed must have the same dimensionality as the quantizer.to.vector().len() != self.output_dim(): Compressed vector must have the same dimensionality as the quantizer.
Source§type Error = InputContainsNaN
type Error = InputContainsNaN
Source§impl<'a, 'b, const NBITS: usize, T> CompressInto<MatRef<'a, Standard<T>>, MatMut<'b, MinMaxMeta<NBITS>>> for MinMaxQuantizer
impl<'a, 'b, const NBITS: usize, T> CompressInto<MatRef<'a, Standard<T>>, MatMut<'b, MinMaxMeta<NBITS>>> for MinMaxQuantizer
Source§fn compress_into(
&self,
from: MatRef<'a, Standard<T>>,
to: MatMut<'b, MinMaxMeta<NBITS>>,
) -> Result<(), Self::Error>
fn compress_into( &self, from: MatRef<'a, Standard<T>>, to: MatMut<'b, MinMaxMeta<NBITS>>, ) -> Result<(), Self::Error>
Compress a multi-vector of full-precision vectors into a multi-vector of MinMax quantized vectors.
This method iterates row by row over the input matrix, quantizing each full-precision vector into the corresponding row of the output MinMax matrix.
§Error
Returns InputContainsNaN error if any input vector contains NaN.
§Panics
Panics if:
from.num_vectors() != to.num_vectors(): The input and output must have the same number of vectors.from.vector_dim() != self.dim(): Each input vector must have the same dimensionality as the quantizer.- The output intrinsic dimension doesn’t match
self.output_dim().
Source§type Error = InputContainsNaN
type Error = InputContainsNaN
Auto Trait Implementations§
impl Freeze for MinMaxQuantizer
impl RefUnwindSafe for MinMaxQuantizer
impl Send for MinMaxQuantizer
impl Sync for MinMaxQuantizer
impl Unpin for MinMaxQuantizer
impl UnsafeUnpin for MinMaxQuantizer
impl UnwindSafe for MinMaxQuantizer
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> 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