Skip to main content

SharedPagedKv

Struct SharedPagedKv 

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

Per-layer PagedKvStores that many concurrent requests share.

§Why a lock per layer, and why two phases

ferrox-server runs generation on spawn_blocking with, in its own words, “no I/O and no shared lock”. KvBlockPool survives that because it only bounds a count: each KvCache owns a private Vec, and the pool mutex is taken briefly at acquire and release, never during a forward. A PagedKvStore is the opposite – it IS the backing memory – so sharing one across concurrent requests needs an answer to “who may touch these bytes when”.

The answer the API already implies: attention takes &PagedKvStore and only push takes &mut. So the accesses split cleanly into many concurrent readers and one short exclusive write per position, which is exactly an RwLock – and one per LAYER rather than one for the whole model, so two requests contend only when both are writing the same layer at the same instant.

A caller must therefore take the write guard for the push alone and drop it before attending under a read guard. Holding the write guard across attention would serialise the expensive half and give back a global lock with extra steps. Nothing breaks in the gap: a sequence’s block table and length are its own, and another request’s push in between only touches blocks it exclusively holds.

§Deadlock

Self::write_all is the one place several layers are held at once, and it takes them in ascending layer order. Every caller getting the same order is what makes that safe; there is no other multi-layer acquisition in the codebase, and a new one must follow the same rule.

§Poisoning

A panic while holding a store leaves the KV mid-write, which is not recoverable state, but it is also not unsound – the bytes are plain f32. Poison is stepped over with into_inner, matching how ferrox-server already treats its pool mutex: a poisoned lock should not turn one request’s panic into a permanently dead server.

Implementations§

Source§

impl SharedPagedKv

Source

pub fn new( n_layers: usize, block_size: usize, blocks_per_layer: usize, n_kv_heads: usize, head_dim: usize, ) -> Self

One store per layer, each with blocks_per_layer blocks.

Source

pub fn from_stores(stores: Vec<PagedKvStore>) -> Self

Wraps stores the caller built, for tests and for callers that size layers differently.

Source

pub fn layer_count(&self) -> usize

Source

pub fn read(&self, layer: usize) -> RwLockReadGuard<'_, PagedKvStore>

Shared access to one layer, for attention.

Source

pub fn write(&self, layer: usize) -> RwLockWriteGuard<'_, PagedKvStore>

Exclusive access to one layer, for a push. Hold it for the push and nothing else – see the type docs.

Source

pub fn write_all(&self) -> Vec<RwLockWriteGuard<'_, PagedKvStore>>

Every layer at once, in ascending order, so a multi-layer append is atomic against other requests.

This is what makes “all layers advance or none do” hold under concurrency rather than only single-threaded: checking free space and then appending are separate steps, and without the guards spanning both, another request can take the blocks in between and leave this one half-written.

Ascending order is the deadlock rule; see the type docs.

Source

pub fn free_blocks(&self, layer: usize) -> usize

Free blocks in one layer, for admission control. A snapshot: by the time a caller acts on it another request may have taken them, which is why the append itself re-checks under the guard.

Source

pub fn acquire_group(&self) -> Option<PageGroup>

Takes one block from EVERY layer as a single group, refcount 1.

All layers or none: a group that existed in some layers and not others could not answer “which block holds position p in layer l”, which is the only question it exists to answer.

Source

pub fn retain_group(&self, group: PageGroup)

One more holder of group.

Called when a second sequence adopts a cached prefix. Without it, the first sequence to finish frees pages the second is still attending over – a use-after-free that shows up as another conversation’s tokens rather than as a crash.

Source

pub fn release_group(&self, group: PageGroup) -> bool

One fewer holder. At zero the blocks go back to their layers.

Returns whether this was the last holder, so a caller can assert on it rather than guess.

Source

pub fn group_blocks(&self, group: PageGroup) -> Vec<usize>

Which block in each layer this group owns, indexed by layer.

Source

pub fn group_refs(&self, group: PageGroup) -> u32

How many holders group has. Zero means it does not exist.

Source

pub fn free_groups(&self) -> usize

Groups that could still be allocated, bounded by the layer with the fewest free blocks: a group needs one from each.

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> 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, 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, <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.