Skip to main content

KvCache

Struct KvCache 

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

Per-layer KV cache storing FP32 key and value vectors.

Implementations§

Source§

impl KvCache

Source

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

Create a new KV cache.

Source

pub fn seq_len(&self) -> usize

Current number of cached tokens.

Source

pub fn max_seq_len(&self) -> usize

Maximum sequence length.

Source

pub fn store_key(&mut self, layer: usize, head: usize, pos: usize, key: &[f32])

Store a key vector for a specific layer, head, and position.

Source

pub fn store_value( &mut self, layer: usize, head: usize, pos: usize, value: &[f32], )

Store a value vector for a specific layer, head, and position.

Source

pub fn keys_for(&self, layer: usize, head: usize, seq_len: usize) -> &[f32]

Get all cached keys for a layer and head up to seq_len.

Returns a slice of [seq_len × head_dim] in row-major order.

Source

pub fn values_for(&self, layer: usize, head: usize, seq_len: usize) -> &[f32]

Get all cached values for a layer and head up to seq_len.

Source

pub fn advance(&mut self)

Advance the sequence position by one token.

Source

pub fn clear(&mut self)

Reset the cache (clear all stored KV pairs).

Source

pub fn memory_bytes(&self) -> usize

Total memory used by this cache in bytes.

Source

pub fn utilization_ratio(&self) -> f64

Utilization ratio: fraction of cache capacity currently used.

Returns a value in [0.0, 1.0].

Source

pub fn num_layers(&self) -> usize

Number of layers in this cache.

Source

pub fn num_kv_heads(&self) -> usize

Number of KV heads per layer.

Source

pub fn head_dim(&self) -> usize

Head dimension.

Source

pub fn set_seq_len(&mut self, n: usize)

Manually set the cached sequence length.

Used by the prefix-cache integration when restoring previously computed KV blocks: after inject_block writes the block contents, the consumer must call this to advertise the number of valid positions to subsequent attention computations.

n is clamped to max_seq_len.

Source

pub fn extract_block( &self, layer: usize, start_pos: usize, block_size: usize, ) -> (Vec<f32>, Vec<f32>)

Extract one prefix-cache block worth of KV for a single layer.

Reads block_size consecutive positions starting at start_pos for every KV head in layer and returns them in [head][pos_in_block][dim] order, packed as a flat Vec<f32> of length num_kv_heads * block_size * head_dim.

Mirrors the layout used by crate::prefix_cache::CacheBlock.

Returns (keys, values). If the requested range exceeds max_seq_len, the trailing positions are returned as zeros.

Source

pub fn inject_block( &mut self, layer: usize, start_pos: usize, block_size: usize, keys: &[f32], values: &[f32], )

Inject a previously extracted block back into the cache for a single layer.

keys and values must have the same [head][pos_in_block][dim] layout produced by extract_block; they are expected to be of length num_kv_heads * block_size * head_dim. Positions outside max_seq_len are silently skipped.

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more