Skip to main content

Codebook

Struct Codebook 

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

Immutable lookup table mapping quantized u8 indices to f32 values.

Construction always validates three invariants:

  1. entries.len() == 1 << bit_width (matches CodecConfig::num_codebook_entries).
  2. Entries are strictly ascending under f32::total_cmp.
  3. All entries are distinct (no adjacent equals).

The inner buffer is an Arc<[f32]> so Clone is O(1). Equality compares the numerical contents, not the allocation identity.

Implementations§

Source§

impl Codebook

Source

pub fn new(entries: Box<[f32]>, bit_width: u8) -> Result<Self, CodecError>

Build a codebook from a caller-owned Box<[f32]>.

§Errors
Source

pub fn train(vectors: &[f32], config: &CodecConfig) -> Result<Self, CodecError>

Train a codebook by uniform-quantile estimation over a flattened f32 sample buffer.

Mirrors Python’s np.quantile(flat.astype(np.float64), np.linspace(0, 1, num_entries)).astype(np.float32) exactly:

  1. Promote every sample to f64.
  2. Sort with f64::total_cmp.
  3. For each k in 0..num_entries, compute the linearly- interpolated quantile value in f64.
  4. Cast to f32 (round-to-nearest-even) and enforce distinctness.

config.bit_width determines the number of entries; config.seed and config.dimension are not consulted by this function.

§Errors
Source

pub fn num_entries(&self) -> u32

Number of entries (2^bit_width).

Source

pub const fn bit_width(&self) -> u8

The bit width this codebook was built for.

Source

pub fn entries(&self) -> &[f32]

Borrow the underlying sorted entries.

Source

pub fn quantize_into( &self, values: &[f32], indices: &mut [u8], ) -> Result<(), CodecError>

Quantize values into indices by finding the nearest entry for each value. Ties favor the right (higher-valued) neighbor, matching Python’s strict < tie-break.

Under feature = "simd" this delegates to [crate::codec::simd_api::quantize_into], which is the single source of truth for dispatch selection. Without the feature, it calls the scalar reference kernel directly.

§Errors
Source

pub fn dequantize_into( &self, indices: &[u8], values: &mut [f32], ) -> Result<(), CodecError>

Dequantize indices into values by gathering the corresponding codebook entries.

Under feature = "simd" this delegates to [crate::codec::simd_api::dequantize_into].

§Errors
Source

pub fn quantize(&self, values: &[f32]) -> Result<Vec<u8>, CodecError>

Convenience: allocate and return the quantized indices.

§Errors

See Codebook::quantize_into.

Source

pub fn dequantize(&self, indices: &[u8]) -> Result<Vec<f32>, CodecError>

Convenience: allocate and return the dequantized values.

§Errors

See Codebook::dequantize_into.

Trait Implementations§

Source§

impl Clone for Codebook

Source§

fn clone(&self) -> Codebook

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 Codebook

Source§

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

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

impl PartialEq for Codebook

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

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

Source§

type Output = T

Should always be Self
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