pub struct KvDictionary {
pub dim: usize,
pub n_atoms: usize,
pub atoms: Vec<f32>,
pub sparsity: usize,
}Expand description
A learned per-layer KV dictionary: n_atoms atoms, each of length dim, row-major in atoms.
Fields§
§dim: usize§n_atoms: usize§atoms: Vec<f32>n_atoms × dim, row-major; each atom is L2-normalized.
sparsity: usizeSparsity k this dictionary was trained for (atoms per coded vector).
Implementations§
Source§impl KvDictionary
impl KvDictionary
Sourcepub fn normalize_atoms(&mut self) -> bool
pub fn normalize_atoms(&mut self) -> bool
Normalize every dictionary atom in place after loading or training.
Returns false when the declared shape is inconsistent or an atom has
effectively zero magnitude.
Sourcepub fn reconstruct(&self, code: &SparseCode) -> Vec<f32>
pub fn reconstruct(&self, code: &SparseCode) -> Vec<f32>
Reconstruct a vector from its sparse code: Σ coeffs[i] · atom(indices[i]). The read-path
operation the attention shader mirrors.
Sourcepub fn encode(&self, v: &[f32], k: usize) -> SparseCode
pub fn encode(&self, v: &[f32], k: usize) -> SparseCode
Orthogonal Matching Pursuit: encode v with at most k atoms. Greedy selection by maximum
absolute correlation with the residual, re-solving all selected coefficients by least squares
each step (the “orthogonal” in OMP). Stops early if the residual is already ~0.
Source§impl KvDictionary
impl KvDictionary
Sourcepub fn encode_to_words(&self, vec: &[f32], k: usize, out: &mut [f32])
pub fn encode_to_words(&self, vec: &[f32], k: usize, out: &mut [f32])
Encode vec to k contiguous code words in out (len ≥ k). Pads with zero-coeff words if OMP
selected fewer than k atoms (a zero coeff reconstructs to nothing).
Sourcepub fn reconstruct_from_words(&self, words: &[f32], k: usize, out: &mut [f32])
pub fn reconstruct_from_words(&self, words: &[f32], k: usize, out: &mut [f32])
Reconstruct a vector from k contiguous code words (words len ≥ k) into out (len = dim).
The exact f16-coefficient inverse of [encode_to_words] — the compressed-cache read path.
Source§impl KvDictionary
impl KvDictionary
Trait Implementations§
Source§impl Clone for KvDictionary
impl Clone for KvDictionary
Source§fn clone(&self) -> KvDictionary
fn clone(&self) -> KvDictionary
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 KvDictionary
impl Debug for KvDictionary
Source§impl<'de> Deserialize<'de> for KvDictionary
impl<'de> Deserialize<'de> for KvDictionary
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 KvDictionary
impl RefUnwindSafe for KvDictionary
impl Send for KvDictionary
impl Sync for KvDictionary
impl Unpin for KvDictionary
impl UnsafeUnpin for KvDictionary
impl UnwindSafe for KvDictionary
Blanket Implementations§
§impl<S, A> Aggregate<Result<S, Error>> for Awhere
A: Aggregate<S>,
impl<S, A> Aggregate<Result<S, Error>> for Awhere
A: Aggregate<S>,
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> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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 more