Skip to main content

FibScorer

Struct FibScorer 

Source
pub struct FibScorer { /* private fields */ }
Expand description

Approximate scorer for FibQuant codes.

Wraps a FibQuantizer with a precomputed Gram table. The scorer can estimate inner products between a raw query vector and a stored FibCodeV1 without decoding the stored code.

The scorer is cheap to construct (~1ms for N=32, k=4) and the Gram table is cached for the lifetime of the scorer.

Implementations§

Source§

impl FibScorer

Source

pub fn new(quantizer: FibQuantizer) -> Result<Self>

Build a scorer from a quantizer. Computes the Gram table once.

Source

pub fn quantizer(&self) -> &FibQuantizer

Get a reference to the underlying quantizer.

Source

pub fn gram_table(&self) -> &GramTable

Get a reference to the Gram table.

Source

pub fn inner_product_estimate( &self, query: &[f32], code: &FibCodeV1, ) -> Result<f32>

Estimate the inner product <query, stored_vector> where stored_vector is represented by code.

This does NOT decode the stored vector. It:

  1. Normalizes the query, applies the rotation.
  2. For each block, finds the nearest codeword index for the query.
  3. Looks up G[query_idx, stored_idx] from the Gram table.
  4. Sums block-level estimates and scales by the stored norm.

Returns the approximate inner product.

Source

pub fn score_batch( &self, query: &[f32], codes: &[FibCodeV1], ) -> Result<Vec<ScoredItem>>

Score a batch of stored codes against a single query.

Returns Vec<(idx, score)> sorted by descending score.

Source

pub fn search( &self, query: &[f32], codes: &[FibCodeV1], top_k: usize, oversample: usize, ) -> Result<Vec<ScoredItem>>

Search the top-K closest codes to a query, with optional oversampling.

Returns the top-K ScoredItems by approximate inner product. oversample > 1 returns more candidates than top_k for downstream exact reranking.

Source

pub fn prepare_query(&self, query: &[f32]) -> Result<FibPreparedQuery>

Prepare a query for batch scoring.

Normalizes the query, applies the rotation, converts to f32, and precomputes the nearest-codeword index for each block. The resulting FibPreparedQuery can be passed to score_prepared, score_batch_prepared, or search_prepared to avoid recomputing the rotation and argmin for every code in a batch.

Source

pub fn score_prepared( &self, prepared: &FibPreparedQuery, code: &FibCodeV1, ) -> Result<f32>

Score a single code against a prepared query.

This skips the rotation and argmin steps (already done in prepare_query) and only performs:

  1. Unpack the stored code’s indices.
  2. For each block: Gram table lookup G[query_idx, stored_idx].
  3. Sum and scale by query_norm * stored_norm.
Source

pub fn score_batch_prepared( &self, prepared: &FibPreparedQuery, codes: &[FibCodeV1], ) -> Result<Vec<ScoredItem>>

Score a batch of codes against a prepared query.

Like score_batch but uses the pre-rotated query, avoiding redundant rotation + argmin per code. Returns Vec<ScoredItem> sorted by descending score.

Source

pub fn search_prepared( &self, prepared: &FibPreparedQuery, codes: &[FibCodeV1], top_k: usize, oversample: usize, ) -> Result<Vec<ScoredItem>>

Search top-K closest codes to a prepared query, with optional oversampling.

Like search but uses the pre-rotated query.

Source

pub fn l2_distance_sq_estimate( &self, query: &[f32], code: &FibCodeV1, ) -> Result<f32>

Estimate L2 distance ||query - stored|| without decoding the stored vector.

Uses the identity: ||q - v||^2 = ||q||^2 + ||v||^2 - 2<q, v> where <q, v> is the approximate inner product from the Gram table. Returns the estimated squared L2 distance (avoiding a sqrt for comparison purposes — callers can sqrt if needed).

Source

pub fn cosine_estimate(&self, query: &[f32], code: &FibCodeV1) -> Result<f32>

Estimate cosine similarity <query, stored> / (||query|| * ||stored||) without decoding the stored vector.

Source

pub fn l2_distance_sq_prepared( &self, prepared: &FibPreparedQuery, code: &FibCodeV1, ) -> Result<f32>

L2 distance using a prepared query — avoids recomputing query rotation.

Source

pub fn cosine_prepared( &self, prepared: &FibPreparedQuery, code: &FibCodeV1, ) -> Result<f32>

Cosine similarity using a prepared query.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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