A high-performance inference engine for AI models
reincarnate registry caching
eugenefd3aea749b3b
diff --git a/crates/uzu/src/engine/mod.rs b/crates/uzu/src/engine/mod.rsindex d14ac4f71..674d015c1 100644--- a/crates/uzu/src/engine/mod.rs+++ b/crates/uzu/src/engine/mod.rs@@ -99,6 +99,7 @@ impl Engine {let registry = SharedAccess::new(MergedRegistry::new(vec![]));let storage_config = StorageConfig::new(device.clone(), None, "mirai".to_string()).with_download_manager_type(config.download_manager_type.into());+ let storage_cache_path = storage_config.cache_path();logs::start(storage_config.cache_path(), &storage_config.log_name(), false);let storage = SharedAccess::new(Storage::new(tokio_handle, storage_config).await?);@@ -126,6 +127,7 @@ impl Engine {version: uzu_backend_version.clone(),}],include_traces: false,+ cache_path: storage_cache_path,};let mirai_registry = Box::new(MiraiRegistry::new(mirai_registry_config)?);diff --git a/crates/uzu/src/registry/cached.rs b/crates/uzu/src/registry/cached.rsindex 1dafeb56e..3f60d544b 100644--- a/crates/uzu/src/registry/cached.rs+++ b/crates/uzu/src/registry/cached.rs@@ -37,12 +37,7 @@ impl Registry for CachedRegistry {if let Some(cached_models) = cached_models.as_ref() {Ok(cached_models.clone())} else {- let models = match self.registry.models().await {- Ok(models) => models.clone(),- Err(_) => {- vec![]- },- };+ let models = self.registry.models().await?;*cached_models = Some(models.clone());Ok(models)}diff --git a/crates/uzu/src/registry/mirai/config.rs b/crates/uzu/src/registry/mirai/config.rsindex 75c8e432e..0c264a58c 100644--- a/crates/uzu/src/registry/mirai/config.rs+++ b/crates/uzu/src/registry/mirai/config.rs@@ -1,3 +1,5 @@+use std::path::PathBuf;+use serde::{Deserialize, Serialize};use crate::device::Device;@@ -16,6 +18,7 @@ pub struct Config {pub device: Device,pub backends: Vec<Backend>,pub include_traces: bool,+ pub cache_path: PathBuf,}impl Config {@@ -24,12 +27,14 @@ impl Config {device: Device,backends: Vec<Backend>,include_traces: bool,+ cache_path: PathBuf,) -> Self {Self {api_key,device,backends,include_traces,+ cache_path,}}}diff --git a/crates/uzu/src/registry/mirai/mod.rs b/crates/uzu/src/registry/mirai/mod.rsindex 36d469712..8227675fd 100644--- a/crates/uzu/src/registry/mirai/mod.rs+++ b/crates/uzu/src/registry/mirai/mod.rs@@ -2,7 +2,13 @@ mod api;mod config;mod types;-use std::{future::Future, pin::Pin, time::Duration};+use std::{+ fs::{read_to_string, write},+ future::Future,+ path::PathBuf,+ pin::Pin,+ time::Duration,+};pub use api::Endpoint;pub use config::{Backend, Config};@@ -47,21 +53,68 @@ impl RegistryTrait for Registry {fn models(&self) -> Pin<Box<dyn Future<Output = Result<Vec<Model>, RegistryError>> + Send + '_>> {Box::pin(async {- let response: Response = self- .client- .response(&Endpoint::FetchModels {- device: self.config.device.clone(),- backends: self.config.backends.clone(),- include_traces: self.config.include_traces,- })- .await- .map_err(|error| RegistryError::UnableToGetModels {- message: error.to_string(),- })?;- let models = response.models().ok_or(RegistryError::UnableToGetModels {- message: "Unable to extract from response".to_string(),+ match self.fetch_models().await {+ Ok(models) => {+ if let Err(error) = self.save_registry(&models) {+ tracing::warn!(?error, "failed to save Mirai registry");+ }+ Ok(models)+ },+ Err(fetch_error) => match self.load_registry() {+ Ok(models) => {+ tracing::warn!(?fetch_error, "serving cached Mirai registry after fetch failure");+ Ok(models)+ },+ Err(load_error) => {+ tracing::warn!(?load_error, "failed to load cached Mirai registry");+ Err(fetch_error)+ },+ },+ }+ })+ }+}++impl Registry {+ async fn fetch_models(&self) -> Result<Vec<Model>, RegistryError> {+ let response: Response = self+ .client+ .response(&Endpoint::FetchModels {+ device: self.config.device.clone(),+ backends: self.config.backends.clone(),+ include_traces: self.config.include_traces,+ })+ .await+ .map_err(|error| RegistryError::UnableToGetModels {+ message: error.to_string(),})?;- Ok(models)+ response.models().ok_or(RegistryError::UnableToGetModels {+ message: "Unable to extract from response".to_string(),+ })+ }++ fn registry_path(&self) -> PathBuf {+ self.config.cache_path.join("registry.json")+ }++ fn save_registry(+ &self,+ models: &[Model],+ ) -> Result<(), RegistryError> {+ let contents = serde_json::to_vec_pretty(models).map_err(|error| RegistryError::UnableToGetModels {+ message: format!("Unable to serialize registry: {}", error),+ })?;+ write(self.registry_path(), contents).map_err(|error| RegistryError::UnableToGetModels {+ message: format!("Unable to write registry: {}", error),+ })+ }++ fn load_registry(&self) -> Result<Vec<Model>, RegistryError> {+ let contents = read_to_string(self.registry_path()).map_err(|error| RegistryError::UnableToGetModels {+ message: format!("Unable to read registry: {}", error),+ })?;+ serde_json::from_str(&contents).map_err(|error| RegistryError::UnableToGetModels {+ message: format!("Unable to parse registry: {}", error),})}}