Skip to main content

BbqCodec

Struct BbqCodec 

Source
pub struct BbqCodec {
    pub dim: usize,
    pub oversample: u8,
    /* private fields */
}
Expand description

BBQ centroid-centered asymmetric 1-bit quantization codec.

Calibration computes the dataset centroid which is subtracted from each vector before sign quantization. The resulting 1-bit codes are Hamming- coarse-comparable; exact rerank uses the 14-byte corrective factors stored in the unified header.

Fields§

§dim: usize§oversample: u8

Oversample multiplier for the caller’s rerank pass. Caller fetches oversample × top_k coarse candidates from the Hamming coarse pass, then calls exact_asymmetric_distance on each.

Implementations§

Source§

impl BbqCodec

Source

pub const ENVELOPE_MAGIC: &'static [u8; 5] = b"NDBBQ"

On-disk magic for BBQ codec envelopes.

Source

pub const ENVELOPE_VERSION: u8 = 1

Current on-disk envelope version for BBQ codecs.

Source

pub fn calibrate(vectors: &[&[f32]], dim: usize, oversample: u8) -> Self

Calibrate a new BbqCodec from a set of training vectors.

Computes the centroid as the mean of all training vectors.

§Panics

Does not panic; returns a zero-centroid codec if vectors is empty.

Source

pub fn to_bytes(&self) -> Result<Vec<u8>, CodecError>

Serialize this codec to a self-describing byte buffer.

Source

pub fn from_bytes(buf: &[u8]) -> Result<Self, CodecError>

Deserialize a codec from a byte buffer produced by Self::to_bytes.

Trait Implementations§

Source§

impl<'de> Deserialize<'de> for BbqCodec

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'__msgpack_de> FromMessagePack<'__msgpack_de> for BbqCodec

Source§

fn read<R: Read<'__msgpack_de>>(reader: &mut R) -> Result<Self, Error>
where Self: Sized,

Reads the MessagePack representation of this value from the provided reader.
Source§

impl Serialize for BbqCodec

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl ToMessagePack for BbqCodec

Source§

fn write<W: Write>(&self, writer: &mut W) -> Result<(), Error>

Writes the MessagePack representation of this value into the provided writer.
Source§

impl VectorCodec for BbqCodec

Source§

fn fast_symmetric_distance(&self, q: &BbqQuantized, v: &BbqQuantized) -> f32

Fast Hamming-based symmetric distance estimate.

Uses the asymmetric corrective distance formula: approx = q_n² + v_n² − 2 · q_n · v_n · dot_estimate where dot_estimate = 1 − 2·hamming/dim maps the Hamming count to a normalised cosine-like similarity on {−1,+1} codes.

Source§

fn exact_asymmetric_distance(&self, q: &BbqQuery, v: &BbqQuantized) -> f32

Exact asymmetric L2 distance using the dequantized stored vector.

The query is exact centered FP32 (q.centered). The stored vector is reconstructed from its sign bits and residual_norm via BbqCodec::dequantize: each dimension ≈ ±residual_norm / √dim. This is the high-fidelity asymmetric path invoked during rerank on the oversample × top_k candidates returned by the coarse Hamming pass.

Source§

type Quantized = BbqQuantized

The packed quantized form. Must be convertible to a UnifiedQuantizedVector reference via AsRef.
Source§

type Query = BbqQuery

The prepared query form (codec-specific).
Source§

fn encode(&self, v: &[f32]) -> BbqQuantized

Encode a single FP32 vector into the codec’s packed form.
Source§

fn prepare_query(&self, q: &[f32]) -> BbqQuery

Prepare a query for distance computations against this codec. May rotate, normalize, build a LUT, etc.
Source§

fn adc_lut(&self, _q: &Self::Query) -> Option<AdcLut>

Optional: precompute ADC lookup table for codecs that use one (PQ, IVF-PQ, TurboQuant). Returns None for codecs that don’t (RaBitQ, BBQ, ternary, binary).
Source§

fn train(&mut self, samples: &[&[f32]]) -> Result<(), CodecError>

Optional: fit the codec’s learned parameters on a set of training vectors. Read more

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromMessagePackOwned for T
where T: for<'a> FromMessagePack<'a>,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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