Skip to main content

KvCache

Struct KvCache 

Source
pub struct KvCache { /* private fields */ }
Expand description

Per-layer, growable key/value cache for one generation session.

A fresh KvCache holds zero cached positions. Every call to crate::traits::Model::forward appends that call’s new positions to every layer’s cache (via KvCache::append, one call per layer) and reads back the full accumulated K/V for attention. The cache is specific to one generation session — call KvCache::new again (or KvCache::clear) to start a new, unrelated prompt.

Implementations§

Source§

impl KvCache

Source

pub fn new(n_layers: usize, max_context: usize) -> Self

A fresh, empty cache for a model with n_layers transformer blocks and a max_context-token context window (from crate::config::QwenConfig::max_context).

Source

pub fn len(&self) -> usize

Number of positions currently cached (the same for every layer, since every KvCache::append call for a given forward pass appends the same number of new positions to each layer in turn). 0 for a fresh cache.

Source

pub fn is_empty(&self) -> bool

Source

pub fn max_context(&self) -> usize

Source

pub fn clear(&mut self)

Drops every layer’s cached K/V, returning this cache to its freshly-KvCache::newd state so it can be reused for a new, unrelated prompt without reallocating the outer Vec.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.