pub struct KvCacheMps { /* private fields */ }Expand description
MPS-compressed KV cache backed by independent online low-rank bases for keys and values.
Implementations§
Source§impl KvCacheMps
impl KvCacheMps
Sourcepub fn new(d_k: usize, d_v: usize, chi_max: usize) -> Self
pub fn new(d_k: usize, d_v: usize, chi_max: usize) -> Self
Create an empty cache.
d_k / d_v: key / value head dimensions.
chi_max: maximum bond dimension (compression rank).
Sourcepub fn append(&mut self, k: &[f32], v: &[f32])
pub fn append(&mut self, k: &[f32], v: &[f32])
Append a new (k, v) token pair.
If the respective basis is not yet full (chi < chi_max), a new
orthonormal direction is added via Gram-Schmidt; otherwise the token is
projected onto the existing basis (lossy compression).
Sourcepub fn token_count(&self) -> usize
pub fn token_count(&self) -> usize
Number of tokens stored.
Sourcepub fn max_bond_dim(&self) -> usize
pub fn max_bond_dim(&self) -> usize
Current bond dimension (max of key and value ranks; ≤ chi_max).
Sourcepub fn attend(&self, query: &[f32], scale: f32) -> Vec<f32>
pub fn attend(&self, query: &[f32], scale: f32) -> Vec<f32>
Compute softmax attention over the compressed KV cache.
query has length d_k. Returns a vector of length d_v.
Scores are computed as (B_k^T · query) · c_k[t], which equals the
dot product query · K_approx[t] exactly (the basis is orthonormal).
Sourcepub fn compression_ratio(&self) -> f64
pub fn compression_ratio(&self) -> f64
Compression ratio: flat storage bytes / compressed storage bytes.
Values > 1 mean the cache uses less memory than storing KV pairs flat.
The ratio grows with T; for large T it approaches d_kv / chi_max.
Auto Trait Implementations§
impl Freeze for KvCacheMps
impl RefUnwindSafe for KvCacheMps
impl Send for KvCacheMps
impl Sync for KvCacheMps
impl Unpin for KvCacheMps
impl UnsafeUnpin for KvCacheMps
impl UnwindSafe for KvCacheMps
Blanket Implementations§
impl<T> Allocation for T
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> 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 more