pub struct ModelLoader { /* private fields */ }Expand description
Model loader with lazy loading and caching
§Thread Safety
ModelLoader uses Arc + RwLock internally for thread-safe access. Multiple threads can safely load and access models concurrently.
§Caching
Once a model is loaded, it stays in memory until explicitly unloaded.
Subsequent calls to load() with the same model type/variant return
the cached session.
Implementations§
Source§impl ModelLoader
impl ModelLoader
Sourcepub fn new(registry: Arc<ModelRegistry>) -> Self
pub fn new(registry: Arc<ModelRegistry>) -> Self
Sourcepub fn with_registry(registry: Arc<ModelRegistry>) -> Self
pub fn with_registry(registry: Arc<ModelRegistry>) -> Self
Create a new model loader (alias for new)
Sourcepub async fn load(
&self,
model_type: ModelType,
variant: ModelVariant,
) -> Result<Arc<Mutex<Session>>>
pub async fn load( &self, model_type: ModelType, variant: ModelVariant, ) -> Result<Arc<Mutex<Session>>>
Load a model (lazily, with caching)
If the model is already loaded, returns the cached session. Otherwise, loads the model from disk using the registry.
§Arguments
model_type- Type of model to loadvariant- Model variant (precision)
§Returns
Arc to ONNX Runtime session
§Example
use llm_shield_models::{ModelLoader, ModelRegistry, ModelType, ModelVariant};
use std::sync::Arc;
let session = loader.load(ModelType::PromptInjection, ModelVariant::FP16).await?;Sourcepub async fn load_with_config(
&self,
config: ModelConfig,
) -> Result<Arc<Mutex<Session>>>
pub async fn load_with_config( &self, config: ModelConfig, ) -> Result<Arc<Mutex<Session>>>
Sourcepub async fn preload(
&self,
models: Vec<(ModelType, ModelVariant)>,
) -> Result<()>
pub async fn preload( &self, models: Vec<(ModelType, ModelVariant)>, ) -> Result<()>
Preload multiple models
Useful for warming up the cache before first use.
§Arguments
models- List of (ModelType, ModelVariant) tuples to preload
§Example
use llm_shield_models::{ModelLoader, ModelRegistry, ModelType, ModelVariant};
use std::sync::Arc;
let models = vec![
(ModelType::PromptInjection, ModelVariant::FP16),
(ModelType::Toxicity, ModelVariant::FP16),
];
loader.preload(models).await?;Sourcepub fn is_loaded(&self, model_type: ModelType, variant: ModelVariant) -> bool
pub fn is_loaded(&self, model_type: ModelType, variant: ModelVariant) -> bool
Check if a model is loaded
Sourcepub fn unload(&self, model_type: ModelType, variant: ModelVariant)
pub fn unload(&self, model_type: ModelType, variant: ModelVariant)
Unload a specific model
Removes the model from cache, freeing memory.
Sourcepub fn unload_all(&self)
pub fn unload_all(&self)
Unload all models
Clears the entire cache, freeing all memory.
Sourcepub fn loaded_models(&self) -> Vec<(ModelType, ModelVariant)>
pub fn loaded_models(&self) -> Vec<(ModelType, ModelVariant)>
Get list of loaded models
Sourcepub fn model_info(
&self,
model_type: ModelType,
variant: ModelVariant,
) -> Option<String>
pub fn model_info( &self, model_type: ModelType, variant: ModelVariant, ) -> Option<String>
Get information about a loaded model
Returns None if model is not loaded.
Sourcepub fn stats(&self) -> LoaderStats
pub fn stats(&self) -> LoaderStats
Get loader statistics
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ModelLoader
impl RefUnwindSafe for ModelLoader
impl Send for ModelLoader
impl Sync for ModelLoader
impl Unpin for ModelLoader
impl UnwindSafe for ModelLoader
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> 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