pub struct ModelCache { /* private fields */ }Expand description
Multi-model cache with LRU eviction under VRAM pressure.
Invariants:
- At most one engine has
residency == Gpuat a time (single-GPU inference). lru_ordertracks all entries from least-recently-used (front) to most-recently-used (back).max_cachedlimits total entries (Gpu, Unloaded, and Parked).
Implementations§
Source§impl ModelCache
impl ModelCache
pub fn new(max_cached: usize) -> Self
Sourcepub fn insert(
&mut self,
engine: Box<dyn InferenceEngine>,
vram_bytes: u64,
) -> Option<Box<dyn InferenceEngine>>
pub fn insert( &mut self, engine: Box<dyn InferenceEngine>, vram_bytes: u64, ) -> Option<Box<dyn InferenceEngine>>
Insert an engine into the cache. If the cache is full, the LRU entry is dropped entirely. Returns the evicted engine (if any) for cleanup.
Sourcepub fn get_mut(&mut self, model_name: &str) -> Option<&mut CachedEngine>
pub fn get_mut(&mut self, model_name: &str) -> Option<&mut CachedEngine>
Get a mutable reference to the engine for a model, if cached.
Sourcepub fn remove(&mut self, model_name: &str) -> Option<Box<dyn InferenceEngine>>
pub fn remove(&mut self, model_name: &str) -> Option<Box<dyn InferenceEngine>>
Remove a model from the cache entirely, returning its engine.
Sourcepub fn unload_all(&mut self) -> Vec<String>
pub fn unload_all(&mut self) -> Vec<String>
Unload all models from GPU. Returns names of models that were unloaded. Unloaded models are parked (retain tokenizers/caches for faster reload).
Sourcepub fn unload_active(&mut self) -> Option<String>
pub fn unload_active(&mut self) -> Option<String>
Unload the current GPU-resident model (if any) to make room for a new one. The engine is parked (retains tokenizers/caches) for faster reload. Returns the name of the unloaded model.
Sourcepub fn clear(&mut self) -> Vec<Box<dyn InferenceEngine>>
pub fn clear(&mut self) -> Vec<Box<dyn InferenceEngine>>
Drop all entries, returning all engines for cleanup.
Sourcepub fn active_vram_bytes(&self) -> u64
pub fn active_vram_bytes(&self) -> u64
VRAM footprint of the currently GPU-resident model (0 if none loaded).
Sourcepub fn active_model(&self) -> Option<&str>
pub fn active_model(&self) -> Option<&str>
The currently GPU-loaded model name.
Sourcepub fn cached_model_names(&self) -> Vec<String>
pub fn cached_model_names(&self) -> Vec<String>
All cached model names (any residency).
pub fn is_empty(&self) -> bool
Auto Trait Implementations§
impl Freeze for ModelCache
impl !RefUnwindSafe for ModelCache
impl Send for ModelCache
impl Sync for ModelCache
impl Unpin for ModelCache
impl UnsafeUnpin for ModelCache
impl !UnwindSafe for ModelCache
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
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