Skip to main content

ExpertPager

Struct ExpertPager 

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

A byte-budgeted, disk-backed cache of MoE experts.

Model-agnostic: it knows byte ranges, not architectures. A model registers where its banks live and asks for the experts a token fired.

Cheap to share — gather takes &self, so several decode threads can page against one cache.

Implementations§

Source§

impl ExpertPager

Source

pub fn new(budget_bytes: u64) -> Self

A pager holding at most budget_bytes of expert data.

A budget of 0 disables caching: every read goes to disk. That is a legitimate configuration (it is what the planner’s cost model assumes, being the pessimistic case) rather than an error.

Source

pub fn register( &mut self, layer: usize, bank: impl Into<String>, loc: BankLocation, ) -> Result<BankIx>

Tell the pager where one bank lives.

Rejects a location whose bytes do not divide evenly into its experts: that means bytes_per_expert was computed from a wrong expert count or a wrong tensor, and every read after the first would be misaligned — which does not fail, it returns plausible-looking garbage.

Source

pub fn bank_ix(&self, layer: usize, bank: &str) -> Option<BankIx>

Handle for a registered bank, or None if it was never registered.

Hoist this out of a per-token loop: the handle is Copy, and every lookup taking one is allocation-free.

Source

pub fn location(&self, ix: BankIx) -> &BankLocation

Where a bank lives.

Source

pub fn bank_count(&self) -> usize

Banks registered so far.

Source

pub fn total_bank_bytes(&self) -> u64

Bytes the whole registered checkpoint would occupy if held resident — what paging is avoiding.

Source

pub fn stats(&self) -> PagerStats

Source

pub fn resident_bytes(&self) -> u64

Bytes currently cached.

Source

pub fn expert( &self, layer: usize, bank: &str, id: usize, ) -> Result<Arc<Vec<u8>>>

One expert’s packed bytes, from cache or disk.

Resolves the bank by name each call; Self::expert_at skips that.

Source

pub fn expert_at(&self, ix: BankIx, id: usize) -> Result<Arc<Vec<u8>>>

One expert’s packed bytes, by handle.

Source

pub fn gather(&self, layer: usize, bank: &str, ids: &[usize]) -> Result<Vec<u8>>

The fired experts’ bytes, concatenated in the order given.

Laid out exactly like the full bank, so the result is a GroupedMatMul operand with num_experts = ids.len(). Order is the caller’s: it has to match the routing weights the caller will apply, so this must not sort or deduplicate. Repeating an id is legal and copies it twice.

Source

pub fn gather_at(&self, ix: BankIx, ids: &[usize]) -> Result<Vec<u8>>

Self::gather by handle — the name is resolved once, not per expert.

Source

pub fn prefetch(&self, layer: usize, bank: &str, ids: &[usize])

Pull experts into the cache ahead of needing them.

Useful when the router for step t+1 is known while step t is still computing. Errors are swallowed: a prefetch that fails costs a later cache miss, and turning a speculative read into a hard failure would be worse than the miss.

Source

pub fn clear(&self)

Drop everything cached, keeping the registrations and the statistics.

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

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.