Skip to main content

BinaryQuantizer

Struct BinaryQuantizer 

Source
pub struct BinaryQuantizer;
Expand description

Binary quantizer: f32 -> 1 bit (sign only).

Provides extreme compression (32x) at the cost of accuracy (~80% recall). Uses hamming distance for fast comparison. Best used with rescoring.

§Example

use grafeo_core::index::vector::quantization::BinaryQuantizer;

let v1 = vec![0.5f32, -0.3, 0.0, 0.8, -0.1, 0.2, -0.4, 0.9];
let v2 = vec![0.4f32, -0.2, 0.1, 0.7, -0.2, 0.3, -0.3, 0.8];

let bits1 = BinaryQuantizer::quantize(&v1);
let bits2 = BinaryQuantizer::quantize(&v2);

let dist = BinaryQuantizer::hamming_distance(&bits1, &bits2);
// Vectors are similar, so hamming distance should be low
assert!(dist < 4);

Implementations§

Source§

impl BinaryQuantizer

Source

pub fn quantize(vector: &[f32]) -> Vec<u64>

Quantizes f32 vector to binary (sign bits packed in u64).

Each f32 becomes 1 bit: 1 if >= 0, 0 if < 0. Bits are packed into u64 words (64 dimensions per word).

Source

pub fn quantize_batch(vectors: &[&[f32]]) -> Vec<Vec<u64>>

Quantizes multiple vectors in batch.

Source

pub fn hamming_distance(a: &[u64], b: &[u64]) -> u32

Computes hamming distance between binary vectors.

Counts the number of differing bits. Lower = more similar.

Source

pub fn hamming_distance_normalized( a: &[u64], b: &[u64], dimensions: usize, ) -> f32

Computes normalized hamming distance (0.0 to 1.0).

Returns the fraction of bits that differ.

Source

pub fn approximate_euclidean(a: &[u64], b: &[u64], dimensions: usize) -> f32

Estimates Euclidean distance from hamming distance.

Uses an empirical approximation: d_euclidean ≈ sqrt(2 * hamming / dim). This is a rough estimate suitable for initial filtering.

Source

pub const fn words_needed(dimensions: usize) -> usize

Returns the number of u64 words needed for the given dimensions.

Source

pub const fn bytes_needed(dimensions: usize) -> usize

Returns the memory footprint in bytes for quantized storage.

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> 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.