#[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:
CompensatedSquaredL2: For computing squared euclidean distances.CompensatedIP: For computing inner products.
§Examples
The CompensatedVector has several named variants that are commonly used:
CompensatedVector: An owning, indepndently allocatedCompensatedVector.MutCompensatedVectorRef: A mutable, reference-like type to aCompensatedVector.CompensatedVectorRef: A const, reference-like type to aCompensatedVector.
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: f32Trait Implementations§
Source§impl Clone for Compensation
impl Clone for Compensation
Source§fn clone(&self) -> Compensation
fn clone(&self) -> Compensation
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Compensation
impl Debug for Compensation
Source§impl Default for Compensation
impl Default for Compensation
Source§fn default() -> Compensation
fn default() -> Compensation
impl Copy for Compensation
impl Pod for Compensation
Auto Trait Implementations§
impl Freeze for Compensation
impl RefUnwindSafe for Compensation
impl Send for Compensation
impl Sync for Compensation
impl Unpin for Compensation
impl UnsafeUnpin for Compensation
impl UnwindSafe for Compensation
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> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
Source§type Bits = T
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
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self.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