Skip to main content

SphericalQuantizer

Struct SphericalQuantizer 

Source
pub struct SphericalQuantizer<A = GlobalAllocator>
where A: Allocator,
{ /* private fields */ }

Implementations§

Source§

impl<A> SphericalQuantizer<A>
where A: Allocator,

Source

pub fn input_dim(&self) -> usize

Return the number dimensions this quantizer has been trained for.

Source

pub fn output_dim(&self) -> usize

Return the dimension of the post-transformed vector.

Output storage vectors should use this dimension instead of self.dim() because in general, the output dim may be different from the input dimension.

Source

pub fn shift(&self) -> &[f32]

Return the per-dimension shift vector.

This vector is meant to accomplish two goals:

  1. Centers the data around the training dataset mean.
  2. Offsets each dimension into a range that can be encoded in unsigned values.
Source

pub fn mean_norm(&self) -> Positive<f32>

Return the approximate mean norm of the training data.

Source

pub fn pre_scale(&self) -> Positive<f32>

Return the pre-scaling parameter for data. This value is multiplied to every compressed vector to adjust its dynamic range.

A value of 1.0 means that no scaling is occurring.

Source

pub fn allocator(&self) -> &A

Return a reference to the allocator used by this data structure.

Source

pub fn generate( centroid: Poly<[f32], A>, mean_norm: f32, transform: TransformKind, metric: SupportedMetric, pre_scale: Option<f32>, rng: &mut dyn RngCore, allocator: A, ) -> Result<Self, TrainError>

A lower-level constructor that accepts a centroid, mean norm, and pre-scale directly.

Source

pub fn metric(&self) -> SupportedMetric

Return the metric used by this quantizer.

Source

pub fn train<T, R>( data: MatrixView<'_, T>, transform: TransformKind, metric: SupportedMetric, pre_scale: PreScale, rng: &mut R, allocator: A, ) -> Result<Self, TrainError>
where T: Copy + Into<f64> + Into<f32>, R: Rng,

Construct a quantizer for vectors in the distribution of data.

The type of distance-preserving transform to use is selected by the TransformKind.

Vectors compressed with this quantizer will be metric specific and optimized for distance computations rather than reconstruction. This means that vectors compressed targeting the inner-product distance will not return meaningful results if used for L2 distance computations.

Additionally, vectors compressed when using the SupportedMetric::Cosine distance will be implicitly normalized before being compressed to enable better compression.

If argument pre_scale is given, then all vectors compressed by this quantizer will first be scaled by this value. Note that if given, pre_scale must be positive.

Source

pub fn rescale(&self, v: &mut [f32])

Rescale the argument v to be in the rough dynamic range of the training dataset.

Trait Implementations§

Source§

impl<A> AsFunctor<CompensatedCosine> for SphericalQuantizer<A>
where A: Allocator,

Source§

impl<A> AsFunctor<CompensatedIP> for SphericalQuantizer<A>
where A: Allocator,

Source§

impl<A> AsFunctor<CompensatedSquaredL2> for SphericalQuantizer<A>
where A: Allocator,

Source§

impl<A> CompressIntoWith<&[f32], Slice<&mut [f32], Mut<'_, FullQueryMeta>>, ScopedAllocator<'_>> for SphericalQuantizer<A>
where A: Allocator,

Source§

fn compress_into_with( &self, from: &[f32], into: FullQueryMut<'_>, allocator: ScopedAllocator<'_>, ) -> Result<(), Self::Error>

Compress the input vector from into the bitslice into.

§Error

Returns an error if

  • The input contains NaN.
  • from.len() != self.dim(): Vector to be compressed must have the same dimensionality as the quantizer.
  • into.len() != self.output_dim(): Compressed vector must have the same dimensionality as the output of the distance-preserving transform. Importantely, this may be different than self.dim() and should be retrieved from self.output_dim().
Source§

type Error = CompressionError

Errors that may occur during compression.
Source§

impl<const NBITS: usize, A> CompressIntoWith<&[f32], VectorBase<NBITS, Unsigned, MutSlicePtr<'_, u8>, Mut<'_, DataMeta>>, ScopedAllocator<'_>> for SphericalQuantizer<A>
where A: Allocator, Unsigned: Representation<NBITS>, for<'a> DataMut<'a, NBITS>: FinishCompressing,

Source§

fn compress_into_with( &self, from: &[f32], into: DataMut<'_, NBITS>, allocator: ScopedAllocator<'_>, ) -> Result<(), Self::Error>

Compress the input vector from into the bitslice into.

§Error

Returns an error if

  • The input contains NaN.
  • from.len() != self.dim(): Vector to be compressed must have the same dimensionality as the quantizer.
  • into.len() != self.output_dim(): Compressed vector must have the same dimensionality as the output of the distance-preserving transform. Importantely, this may be different than self.dim() and should be retrieved from self.output_dim().
Source§

type Error = CompressionError

Errors that may occur during compression.
Source§

impl<const NBITS: usize, Perm, A> CompressIntoWith<&[f32], VectorBase<NBITS, Unsigned, MutSlicePtr<'_, u8>, Mut<'_, QueryMeta>, Perm>, ScopedAllocator<'_>> for SphericalQuantizer<A>
where Unsigned: Representation<NBITS>, Perm: PermutationStrategy<NBITS>, A: Allocator,

Source§

fn compress_into_with( &self, from: &[f32], into: QueryMut<'_, NBITS, Perm>, allocator: ScopedAllocator<'_>, ) -> Result<(), Self::Error>

Compress the input vector from into the bitslice into.

§Error

Returns an error if

  • The input contains NaN.
  • from.len() != self.dim(): Vector to be compressed must have the same dimensionality as the quantizer.
  • into.len() != self.output_dim(): Compressed vector must have the same dimensionality as the output of the distance-preserving transform. Importantely, this may be different than self.dim() and should be retrieved from self.output_dim().
Source§

type Error = CompressionError

Errors that may occur during compression.
Source§

impl<A> Debug for SphericalQuantizer<A>
where A: Allocator + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<A> Freeze for SphericalQuantizer<A>
where A: Freeze,

§

impl<A> RefUnwindSafe for SphericalQuantizer<A>
where A: RefUnwindSafe,

§

impl<A> Send for SphericalQuantizer<A>
where A: Send,

§

impl<A> Sync for SphericalQuantizer<A>
where A: Sync,

§

impl<A> Unpin for SphericalQuantizer<A>
where A: Unpin,

§

impl<A> UnsafeUnpin for SphericalQuantizer<A>
where A: UnsafeUnpin,

§

impl<A> UnwindSafe for SphericalQuantizer<A>
where A: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> AsyncFriendly for T
where T: Send + Sync + 'static,