Skip to main content

KVCache

Struct KVCache 

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

Key-Value cache for efficient transformer inference

Implementations§

Source§

impl KVCache

Source

pub fn new(num_layers: usize, num_heads: usize, head_dim: usize) -> Self

Create a new KV-cache

Source

pub fn from_config(config: KVCacheConfig) -> Result<Self>

Create KV-cache from configuration

Source

pub fn config(&self) -> &KVCacheConfig

Get configuration

Source

pub fn is_enabled(&self) -> bool

Check if cache is enabled

Source

pub fn step(&self) -> usize

Get current generation step

Source

pub fn init_layer(&mut self, layer_idx: usize, batch_size: usize) -> Result<()>

Initialize cache for a layer

Source

pub fn update_layer( &mut self, layer_idx: usize, new_keys: Vec<f32>, new_values: Vec<f32>, ) -> Result<()>

Update cache for a layer with new keys and values

Source

pub fn get_layer(&self, layer_idx: usize) -> Result<(&[f32], &[f32])>

Get cached keys and values for a layer

Source

pub fn get_seq_len(&self, layer_idx: usize) -> Result<usize>

Get sequence length for a layer

Source

pub fn clear_layer(&mut self, layer_idx: usize)

Clear cache for a specific layer

Source

pub fn clear_all(&mut self)

Clear all cache entries

Source

pub fn next_step(&mut self)

Increment generation step

Source

pub fn reset(&mut self)

Reset to initial state

Source

pub fn num_cached_layers(&self) -> usize

Get number of cached layers

Source

pub fn current_memory_usage(&self) -> usize

Calculate current memory usage

Source

pub fn current_memory_usage_mb(&self) -> f64

Calculate memory usage in MB

Source

pub fn stats(&self) -> CacheStats

Get cache statistics

Trait Implementations§

Source§

impl Clone for KVCache

Source§

fn clone(&self) -> KVCache

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KVCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.