Skip to main content

KvCache

Struct KvCache 

Source
pub struct KvCache {
    pub seq_len: usize,
    /* private fields */
}
Expand description

Host-side KV cache with one K and one V buffer per transformer layer.

Layout per layer: contiguous f32 slice of length context_len × num_kv_heads × head_dim. Valid data occupies [0, seq_len × row_size).

Fields§

§seq_len: usize

Implementations§

Source§

impl KvCache

Source

pub fn new( num_layers: u32, context_len: u32, num_kv_heads: u32, head_dim: u32, ) -> Self

Allocate a zeroed cache for num_layers layers.

Source

pub fn append(&mut self, layer: usize, k_row: &[f32], v_row: &[f32])

Append one token’s K and V vectors for a given layer.

When the cache is full, the oldest token is evicted (ring-buffer shift) before the new vectors are written.

Source

pub fn get_k(&self, layer: usize) -> &[f32]

Return the valid slice of K vectors for layer as [seq_len, row_size] data.

Source

pub fn get_v(&self, layer: usize) -> &[f32]

Return the valid slice of V vectors for layer as [seq_len, row_size] data.

Source

pub fn get_k_at(&self, layer: usize, count: usize) -> &[f32]

Return the K slice for layer at an explicit token count.

Use this after append to include the just-written token whose position has been filled but seq_len has not yet been incremented (because seq_len only advances after the final layer has appended).

Source

pub fn get_v_at(&self, layer: usize, count: usize) -> &[f32]

Return the V slice for layer at an explicit token count.

Source

pub fn reset(&mut self)

Zero out all K/V data and reset the sequence counter.

Source

pub fn num_layers(&self) -> usize

Total number of layers.

Source

pub fn context_len(&self) -> usize

Maximum sequence length before eviction occurs.

Trait Implementations§

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.