pub struct ProductQuantizer {
pub codebook: PQCodebook,
pub rotation: Option<Vec<f32>>,
}Expand description
Product quantizer model and helpers for train/encode/decode.
Fields§
§codebook: PQCodebookTrained codebook.
rotation: Option<Vec<f32>>OPQ rotation matrix (flattened row-major D x D). None if OPQ disabled.
Implementations§
Source§impl ProductQuantizer
impl ProductQuantizer
Sourcepub fn train(
vectors: &[Vec<f32>],
num_subspaces: usize,
num_centroids: usize,
) -> Result<Self, Error>
pub fn train( vectors: &[Vec<f32>], num_subspaces: usize, num_centroids: usize, ) -> Result<Self, Error>
Train a PQ codebook using simplified k-means for each subspace.
§Errors
Returns Error::InvalidQuantizerConfig if:
vectorsis emptynum_subspacesis 0num_centroidsis 0 or exceedsu16::MAX- vector dimension is not divisible by
num_subspaces num_centroidsexceedsvectors.len()
Sourcepub fn quantize(&self, vector: &[f32]) -> Result<PQVector, Error>
pub fn quantize(&self, vector: &[f32]) -> Result<PQVector, Error>
Quantize a full-precision vector into PQ codes.
Applies OPQ rotation (if present) before encoding, so that codebook centroids — which were trained on rotated vectors — remain consistent with the encoded representation.
§Errors
Returns Error::InvalidQuantizerConfig if vector.len() does not match
the codebook dimension.
Source§impl ProductQuantizer
impl ProductQuantizer
Sourcepub fn validate_loaded(&self) -> Result<(), Error>
pub fn validate_loaded(&self) -> Result<(), Error>
Validate a deserialized quantizer’s structural invariants.
Must be called once on every quantizer loaded from untrusted bytes
(see Self::load_codebook) before it is used for search. After this
returns Ok, the codebook layout matches its declared dimensions and
the rotation matrix (if present) is dimension * dimension, so the
unchecked indexing in apply_rotation operates
only on in-bounds offsets.
§Errors
Returns Error::IndexCorrupted if the centroid table shape, subspace
dimension, or rotation length is inconsistent with the declared
dimension / num_subspaces / num_centroids.
Sourcepub fn validate_codes(&self, pq_vector: &PQVector) -> Result<(), Error>
pub fn validate_codes(&self, pq_vector: &PQVector) -> Result<(), Error>
Verify every code in pq_vector is a valid centroid index < num_centroids
and that the code count matches num_subspaces.
This is the precondition the unsafe SIMD ADC kernels rely on: once it
returns Ok, code[i] < num_centroids for all i, so every gather
index subspace * num_centroids + code stays within the LUT bounds.
§Errors
Returns Error::IndexCorrupted if a code is out of range or the code
count is wrong.
Sourcepub fn precompute_lut(&self, query: &[f32]) -> Vec<f32>
pub fn precompute_lut(&self, query: &[f32]) -> Vec<f32>
Precompute ADC lookup table for a query vector.
Returns flat [m * k] table indexed as lut[subspace * k + centroid_id].
Applies OPQ rotation if present.
Source§impl ProductQuantizer
Persistence methods for codebook and rotation matrix storage.
impl ProductQuantizer
Persistence methods for codebook and rotation matrix storage.
Sourcepub fn save_codebook(&self, dir: &Path) -> Result<(), Error>
pub fn save_codebook(&self, dir: &Path) -> Result<(), Error>
Save trained codebook to <dir>/codebook.pq using postcard.
Uses atomic write (write to .tmp, then rename).
§Errors
Returns Error::Io if serialization or file I/O fails.
Sourcepub fn load_codebook(dir: &Path) -> Result<Option<Self>, Error>
pub fn load_codebook(dir: &Path) -> Result<Option<Self>, Error>
Load codebook from <dir>/codebook.pq. Returns None if file doesn’t exist.
The decoded quantizer is structurally validated (Self::validate_loaded)
before being returned, so a corrupt or tampered codebook is rejected here
rather than producing out-of-bounds indexing during search.
§Errors
Returns Error::Io if deserialization or file I/O fails, or
Error::IndexCorrupted if the decoded codebook/rotation is inconsistent
or the file exceeds MAX_PQ_ARTIFACT_BYTES.
Trait Implementations§
Source§impl Clone for ProductQuantizer
impl Clone for ProductQuantizer
Source§fn clone(&self) -> ProductQuantizer
fn clone(&self) -> ProductQuantizer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ProductQuantizer
impl Debug for ProductQuantizer
Source§impl<'de> Deserialize<'de> for ProductQuantizer
impl<'de> Deserialize<'de> for ProductQuantizer
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for ProductQuantizer
impl RefUnwindSafe for ProductQuantizer
impl Send for ProductQuantizer
impl Sync for ProductQuantizer
impl Unpin for ProductQuantizer
impl UnsafeUnpin for ProductQuantizer
impl UnwindSafe for ProductQuantizer
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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 moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);