pub struct PQCodebook {
pub num_subvectors: usize,
pub num_centroids: usize,
pub sub_dim: usize,
pub centroids: Vec<Vec<Vec<f32>>>,
}Fields§
§num_subvectors: usizeNumber of sub-vectors (M)
num_centroids: usizeNumber of centroids per sub-space (K, typically 256 so codes fit in u8)
sub_dim: usizeDimensionality of each sub-vector = dim / num_subvectors
centroids: Vec<Vec<Vec<f32>>>Centroids: [num_subvectors][num_centroids][sub_dim]
Implementations§
Source§impl PQCodebook
impl PQCodebook
Sourcepub fn train(
vectors: &[Vec<f32>],
num_subvectors: usize,
num_centroids: usize,
max_iter: usize,
) -> Result<Self, AilakeError>
pub fn train( vectors: &[Vec<f32>], num_subvectors: usize, num_centroids: usize, max_iter: usize, ) -> Result<Self, AilakeError>
Train PQ codebook via k-means on each sub-space independently.
Sourcepub fn train_with_kmeans<F>(
vectors: &[Vec<f32>],
num_subvectors: usize,
num_centroids: usize,
max_iter: usize,
kmeans_fn: F,
) -> Result<Self, AilakeError>
pub fn train_with_kmeans<F>( vectors: &[Vec<f32>], num_subvectors: usize, num_centroids: usize, max_iter: usize, kmeans_fn: F, ) -> Result<Self, AilakeError>
Train PQ codebook with a custom k-means backend (e.g. GPU-accelerated).
kmeans_fn(vecs, k, max_iter) must return exactly k centroids of the
same dimensionality as vecs. The built-in CPU path passes kmeans.
Sourcepub fn encode(&self, vector: &[f32]) -> Vec<u8> ⓘ
pub fn encode(&self, vector: &[f32]) -> Vec<u8> ⓘ
Encode a single vector into num_subvectors u8 codes.
Sourcepub fn decode(&self, codes: &[u8]) -> Vec<f32>
pub fn decode(&self, codes: &[u8]) -> Vec<f32>
Decode codes back into an approximate vector (centroid reconstruction).
Sourcepub fn compute_adc_table(&self, query: &[f32]) -> Vec<Vec<f32>>
pub fn compute_adc_table(&self, query: &[f32]) -> Vec<Vec<f32>>
Precompute query-to-centroid L2 distances for Asymmetric Distance Computation. Returns [num_subvectors][num_centroids] distance table. ADC is O(M*K) per query precomputation, then O(M) per encoded vector — much faster than symmetric distance which would require decoding each vector first.
Trait Implementations§
Source§impl Clone for PQCodebook
impl Clone for PQCodebook
Source§fn clone(&self) -> PQCodebook
fn clone(&self) -> PQCodebook
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 PQCodebook
impl Debug for PQCodebook
Source§impl<'de> Deserialize<'de> for PQCodebook
impl<'de> Deserialize<'de> for PQCodebook
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 PQCodebook
impl RefUnwindSafe for PQCodebook
impl Send for PQCodebook
impl Sync for PQCodebook
impl Unpin for PQCodebook
impl UnsafeUnpin for PQCodebook
impl UnwindSafe for PQCodebook
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> 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 more