pub struct LayerKvCache {
pub config: KVCacheConfig,
/* private fields */
}Expand description
Cheap to clone (an Arc around the shared, lock-guarded buffer) — matches
the shared-state pattern used elsewhere in this crate (e.g. ClusterState).
Fields§
§config: KVCacheConfigImplementations§
Source§impl LayerKvCache
impl LayerKvCache
Sourcepub fn new(config: KVCacheConfig) -> Self
pub fn new(config: KVCacheConfig) -> Self
Creates an empty cache. Does not allocate the backing buffer yet —
initialize() performs the single upfront allocation, sized once for
config.max_tokens_per_seq.
Sourcepub fn initialize(&self, seq_len: usize) -> Result<(), String>
pub fn initialize(&self, seq_len: usize) -> Result<(), String>
Ensures the backing buffer exists (allocating it on the first call
only) and marks the first seq_len tokens (clamped to
config.max_tokens_per_seq) as valid.
Safe to call again on the same cache — e.g. to grow current_len as
more tokens are generated — without losing previously written data:
the buffer is allocated once and never reallocated by this method.
Sourcepub fn write_kv(
&self,
token_idx: usize,
keys: &[f32],
values: &[f32],
) -> Result<(), String>
pub fn write_kv( &self, token_idx: usize, keys: &[f32], values: &[f32], ) -> Result<(), String>
Writes one token’s key/value vectors into the cache at token_idx.
Copies directly into the pre-allocated buffer slice — no allocation.
Sourcepub fn write_kv_batch(
&self,
entries: &[(usize, &[f32], &[f32])],
) -> Result<(), String>
pub fn write_kv_batch( &self, entries: &[(usize, &[f32], &[f32])], ) -> Result<(), String>
Writes multiple tokens’ key/value vectors under a single write-lock
acquisition, validating every entry upfront (before taking the lock)
so a bad entry anywhere in the batch fails the whole call without
partially writing it. Same per-token semantics as calling write_kv
in a loop — this only changes how many times the lock is acquired.
Sourcepub fn with_read_kv<F, R>(&self, token_idx: usize, f: F) -> Result<R, String>
pub fn with_read_kv<F, R>(&self, token_idx: usize, f: F) -> Result<R, String>
Inspects one token’s cached key/value slices directly under a read lock via a closure. Zero heap allocation on this path.
Sourcepub fn with_read_range<F, R>(
&self,
start_token: usize,
end_token: usize,
f: F,
) -> Result<R, String>
pub fn with_read_range<F, R>( &self, start_token: usize, end_token: usize, f: F, ) -> Result<R, String>
Inspects keys/values for a contiguous token range ([start_token, end_token))
directly under a read lock via a closure that accepts a KVSpan.
Zero heap allocation on this path.
Sourcepub fn read_range_into(
&self,
start_token: usize,
end_token: usize,
keys_out: &mut [f32],
values_out: &mut [f32],
) -> Result<(), String>
pub fn read_range_into( &self, start_token: usize, end_token: usize, keys_out: &mut [f32], values_out: &mut [f32], ) -> Result<(), String>
Copies keys and values for a contiguous token range into pre-allocated
caller slices keys_out and values_out.
Rejects output buffers if they are too small for the requested token range.
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Trait Implementations§
Source§impl Clone for LayerKvCache
impl Clone for LayerKvCache
Source§impl Debug for LayerKvCache
impl Debug for LayerKvCache
Auto Trait Implementations§
impl Freeze for LayerKvCache
impl RefUnwindSafe for LayerKvCache
impl Send for LayerKvCache
impl Sync for LayerKvCache
impl Unpin for LayerKvCache
impl UnsafeUnpin for LayerKvCache
impl UnwindSafe for LayerKvCache
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,
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