Skip to main content

Compensation

Struct Compensation 

Source
#[repr(transparent)]
pub struct Compensation(pub f32);
Expand description

A per-vector precomputed coefficient to help compute inner products.

To understand the use of the compensation coefficient, assume that we wish to compute the inner product between two scalar compressed vectors where the quantization has scale parameter a and centroid B (note: capital letters represent vectors, lower case letters represent scalars).

The inner product between a X = a * (X' + B) and Y = a * (Y' + B) where X' and Y' are the scalar encodings for X and Y respectively is:

P = <a * X' + B, a * Y' + B>
  = a^2 * <X', Y'> + a * <X', B> + a * <Y', B> + <B, B>
           ------    -----------   -----------   ------
              |           |             |           |
         Integer Dot      |        Compensation     |
           Product        |           for Y         |
                          |                    Constant for
                     Compensation               all vectors
                        for X

In other words, the inner product can be decomposed into an integer dot-product plus a bunch of other terms that compensate for the compression.

These compensation terms can be computed as the vectors are compressed. At run time, we can the return vectors consisting of the quantized encodings (e.g. X') and the compensation <X', B>.

Computation of squared Euclidean distance is more straight forward:

P = sum( ((a * X' + B) - (a * Y' + B))^2 )
  = sum( a^2 * (X' - Y')^2 )
  = a^2 * sum( (X' - Y')^2 )

This means the squared Euclidean distance is computed by scaling the squared Euclidean distance computed directly on the integer codes.

§Distance Implementations

The following distance function types are implemented:

§Examples

The CompensatedVector has several named variants that are commonly used:

use diskann_quantization::{
    scalar::{
        self,
        CompensatedVector,
        MutCompensatedVectorRef,
        CompensatedVectorRef
    },
};

use diskann_utils::{Reborrow, ReborrowMut};

// Create a new heap-allocated CompensatedVector for 4-bit compressions capable of
// holding 3 elements.
let mut v = CompensatedVector::<4>::new_boxed(3);

// We can inspect the underlying bitslice.
let bitslice = v.vector();
assert_eq!(bitslice.get(0).unwrap(), 0);
assert_eq!(bitslice.get(1).unwrap(), 0);
assert_eq!(v.meta().0, 0.0, "expected default compensation value");

// If we want, we can mutably borrow the bitslice and mutate its components.
let mut bitslice = v.vector_mut();
bitslice.set(0, 1).unwrap();
bitslice.set(1, 2).unwrap();
bitslice.set(2, 3).unwrap();

assert!(bitslice.set(3, 4).is_err(), "out-of-bounds access");

// Get the underlying pointer for comparision.
let ptr = bitslice.as_ptr();

// Vectors can be converted to a generalized reference.
let mut v_ref = v.reborrow_mut();

// The generalized reference preserves the underlying pointer.
assert_eq!(v_ref.vector().as_ptr(), ptr);
let mut bitslice = v_ref.vector_mut();
bitslice.set(0, 10).unwrap();

// Setting the underlying compensation will be visible in the original allocation.
v_ref.set_meta(scalar::Compensation(1.0));

// Check that the changes are visible.
assert_eq!(v.meta().0, 1.0);
assert_eq!(v.vector().get(0).unwrap(), 10);

// Finally, the immutable ref also maintains pointer compatibility.
let v_ref = v.reborrow();
assert_eq!(v_ref.vector().as_ptr(), ptr);

§Constructing a MutCompensatedVectorRef From Components

The following example shows how to assemble a MutCompensatedVectorRef from raw memory.

use diskann_quantization::{
    bits::{Unsigned, MutBitSlice},
    scalar::{self, MutCompensatedVectorRef}
};

// Start with 2 bytes of memory. We will impose a 4-bit scalar quantization on top of
// these 4 bytes.
let mut data = vec![0u8; 2];
let mut compensation = scalar::Compensation(0.0);
{
    // First, we need to construct a bit-slice over the data.
    // This will check that it is sized properly for 4, 4-bit values.
    let mut slice = MutBitSlice::<4, Unsigned>::new(data.as_mut_slice(), 4).unwrap();

    // Next, we construct the `MutCompensatedVectorRef`.
    let mut v = MutCompensatedVectorRef::new(slice, &mut compensation);

    // Through `v`, we can set all the components in `slice` and the compensation.
    v.set_meta(scalar::Compensation(1.0));
    let mut from_v = v.vector_mut();
    from_v.set(0, 1).unwrap();
    from_v.set(1, 2).unwrap();
    from_v.set(2, 3).unwrap();
    from_v.set(3, 4).unwrap();
}

// Now we can check that the changes made internally are visible.
assert_eq!(&data, &[0x21, 0x43]);
assert_eq!(compensation.0, 1.0);

Tuple Fields§

§0: f32

Trait Implementations§

Source§

impl Clone for Compensation

Source§

fn clone(&self) -> Compensation

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Compensation

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Compensation

Source§

fn default() -> Compensation

Returns the “default value” for a type. Read more
Source§

impl Zeroable for Compensation

Source§

fn zeroed() -> Self

Source§

impl Copy for Compensation

Source§

impl Pod for Compensation

Auto Trait Implementations§

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> CheckedBitPattern for T
where T: AnyBitPattern,

Source§

type Bits = T

Self must have the same layout as the specified Bits except for the possible invalid bit patterns being checked during is_valid_bit_pattern.
Source§

fn is_valid_bit_pattern(_bits: &T) -> bool

If this function returns true, then it must be valid to reinterpret bits as &Self.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Generator<T> for T
where T: Clone,

Source§

fn generate(&mut self) -> T

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> SampleLatinHyperCube for T
where T: Copy + Default,

Source§

fn sample_latin_hypercube( data: MatrixBase<&[T]>, num_samples: usize, seed: Option<u64>, ) -> MatrixBase<Box<[T]>>

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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> AnyBitPattern for T
where T: Pod,

Source§

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

Source§

impl<T> Interleave for T
where T: Pod,

Source§

impl<T> NoUninit for T
where T: Pod,