pub struct LatentCache { /* private fields */ }Expand description
Per-forward-pass, N-slot latent cache. See module docs for the shape contract and lifecycle.
Implementations§
Source§impl LatentCache
impl LatentCache
Sourcepub fn new(
anchor: &Tensor,
n_layers: usize,
max_seq_len: usize,
slot_trailing: Vec<Vec<usize>>,
dtype: DType,
) -> Result<Self, Error>
pub fn new( anchor: &Tensor, n_layers: usize, max_seq_len: usize, slot_trailing: Vec<Vec<usize>>, dtype: DType, ) -> Result<Self, Error>
Allocate a zero-filled cache on the same graph as anchor.
slot_trailing gives the trailing shape (past the seq axis) of each
slot; its length is the number of slots per layer and must be ≥ 1.
A slot’s buffer is [max_seq_len, …trailing] (an empty trailing ⇒
a [max_seq_len] per-token-scalar slot). dtype selects the element
type (typically the latent’s — F32 / BF16 / F16 for inference).
Sourcepub fn append(self, layer: usize, new_slots: &[&Tensor]) -> Result<Self>
pub fn append(self, layer: usize, new_slots: &[&Tensor]) -> Result<Self>
Append fresh latents for layer at the cache’s current position.
Consumes self and returns the updated cache (option (b), the
same functional shape as LazyKvCache::append).
new_slots must have exactly n_slots entries, in slot order; entry
s must be [seqlen_new, …slot_trailing[s]], and all entries must
agree on seqlen_new (the number of tokens appended this step: 1 for
a decode step, prefill_len for prefill). Advances the cache’s
position implicitly is not done here — call Self::advance_by
after the last layer’s append in each step, matching LazyKvCache.
Errors (typed, at build time): layer out of bounds, wrong slot
count, a slot shape mismatch, mismatched seqlen_new across slots,
or an append that would exceed max_seq_len.
Sourcepub fn advance_by(self, n: usize) -> Self
pub fn advance_by(self, n: usize) -> Self
Advance current_seq_len by n. Call after the last layer’s
Self::append in each generation step (mirrors
LazyKvCache::advance_by).
Sourcepub fn slot(&self, layer: usize, slot: usize) -> Tensor
pub fn slot(&self, layer: usize, slot: usize) -> Tensor
Active slice of layer’s slot s: [0..current_seq_len] on dim 0
(clamped to ≥ 1 so a fresh cache still yields a valid rank).
Sourcepub fn slot_buffer_full(&self, layer: usize, slot: usize) -> Tensor
pub fn slot_buffer_full(&self, layer: usize, slot: usize) -> Tensor
Full-capacity buffer for layer’s slot s ([max_seq, …trailing])
— escape hatch mirroring LazyKvCache::k_buffer_full.
Sourcepub fn current_seq_len(&self) -> usize
pub fn current_seq_len(&self) -> usize
Tokens written so far.
Sourcepub fn max_seq_len(&self) -> usize
pub fn max_seq_len(&self) -> usize
Pre-allocated per-slot capacity along the sequence axis.
Sourcepub fn slot_trailing(&self, slot: usize) -> &[usize]
pub fn slot_trailing(&self, slot: usize) -> &[usize]
Trailing shape of slot s (past the seq axis).
Trait Implementations§
Source§impl Clone for LatentCache
impl Clone for LatentCache
Auto Trait Implementations§
impl Freeze for LatentCache
impl RefUnwindSafe for LatentCache
impl Send for LatentCache
impl Sync for LatentCache
impl Unpin for LatentCache
impl UnsafeUnpin for LatentCache
impl UnwindSafe for LatentCache
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> ErasedDestructor for Twhere
T: 'static,
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