Skip to main content

KvWindow

Struct KvWindow 

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

How many rows a contiguous windowed KV cache keeps resident, and when it drops the ones behind the window.

BlockLayout above is the same question for the block/paged tier, where the eviction unit is a block. This is the answer for ferrox_core::cache::KvCache, where the eviction unit is a row and the only thing stopping a per-token drain is arithmetic.

§Why there is slack

Dropping exactly one row per push moves every remaining row down by one every single token: window * n_kv_heads * head_dim floats per layer per token, about a megabyte per token per layer on Gemma-3-4B. So the cache is allowed to run slack rows past the window and then drops slack + 1 at once, which amortises the move to roughly window / (slack + 1) rows per token.

§Why this is a type and not two lines in push

The store keeps these rows and the budget (ferrox_models::kv_budget) has to price exactly what the store keeps. #33 is the record of what happens when those two are separate statements of the same rule: the budget capped a sliding layer that no store ever capped, -c auto approved a context that did not fit, and the failure arrived as an OOM. KvWindow::rows_after is the single rule; the store calls it to decide and the budget calls it to price, so there is nothing left for them to disagree about.

Implementations§

Source§

impl KvWindow

Source

pub fn new(window: usize, slack: usize) -> Option<Self>

None for a zero window, for BlockLayoutError::ZeroWindow’s reason: a query that attends to nothing is not a model.

Source

pub fn with_default_slack(window: usize) -> Option<Self>

The slack a caller gets when it has no opinion: half a window, so the cache peaks at 1.5x the window and moves roughly two rows per token instead of window of them.

Source

pub fn window(&self) -> usize

Source

pub fn slack(&self) -> usize

Source

pub fn max_rows(&self) -> usize

The most rows this window ever leaves resident.

Source

pub fn rows_after(&self, positions: usize) -> usize

The rule. Rows resident once the sequence has consumed positions positions and the holder has evicted at every step.

Grows one per position up to window + slack, then drops back to window and climbs again, so the resident count cycles through [window, window + slack] with period slack + 1.

The invariant every reader depends on is rows_after(p) >= min(p, window): the rows kept are always at least the last window positions, which is exactly the set a windowed attention kernel reads. Everything above that is slack the kernel skips, so evicting can only ever change where a row sits, never whether it is read. That is why turning eviction on is token-identical rather than approximately so, and it is asserted in this module’s tests rather than argued here.

Trait Implementations§

Source§

impl Clone for KvWindow

Source§

fn clone(&self) -> KvWindow

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for KvWindow

Source§

impl Debug for KvWindow

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Eq for KvWindow

Source§

impl PartialEq for KvWindow

Source§

fn eq(&self, other: &KvWindow) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for KvWindow

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

Source§

type Output = T

Should always be Self
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 = !

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.