Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Return initialized model contexts directly

Manav Rathi1f60cec7a8a8
diff --git a/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rs b/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rs
index 5634c6f967..1651e7db1d 100644
--- a/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rs
+++ b/rust/apps/ensu/src-tauri/src/commands/notes/indexing.rs
@@ -56,15 +56,13 @@ pub(super) fn index_collection(
"The knowledge embedding model is not downloaded",
)
})?;
- if embedding_context.is_none() {
- embedding_context = Some(crate::commands::llm::load_knowledge_embedding_context(
+ let context = match &mut embedding_context {
+ Some(context) => context,
+ slot => slot.insert(crate::commands::llm::load_knowledge_embedding_context(
embedding_path,
|| check_cancelled(cancellation_epoch, retrieval_epoch),
- )?);
- }
- let context = embedding_context
- .as_ref()
- .expect("embedding context was initialized");
+ )?),
+ };
prepared
.embed(|title, text| {
check_cancelled(cancellation_epoch, retrieval_epoch)
diff --git a/rust/crates/ml/src/runtime.rs b/rust/crates/ml/src/runtime.rs
index f239fdda6c..d9cd3b19a9 100644
--- a/rust/crates/ml/src/runtime.rs
+++ b/rust/crates/ml/src/runtime.rs
@@ -98,25 +98,26 @@ impl ModelRuntime {
}
let mut session = self.lock_session();
- self.configure_locked(&mut session, path);
- session
- .as_mut()
- .expect("non-empty model path must configure a session")
- .run(operation)
+ self.configure_locked(&mut session, path).run(operation)
}
- fn configure_locked(&self, session: &mut Option<onnx::OnnxSession>, path: &str) {
- if session
- .as_ref()
- .is_some_and(|session| session.model_path() == path)
- {
- return;
+ fn configure_locked<'a>(
+ &self,
+ session: &'a mut Option<onnx::OnnxSession>,
+ path: &str,
+ ) -> &'a mut onnx::OnnxSession {
+ let new_session = || {
+ onnx::OnnxSession::new(
+ path,
+ self.model.namespace(),
+ default_execution_mode(self.model),
+ )
+ };
+ let session = session.get_or_insert_with(new_session);
+ if session.model_path() != path {
+ *session = new_session();
}
- *session = Some(onnx::OnnxSession::new(
- path,
- self.model.namespace(),
- default_execution_mode(self.model),
- ));
+ session
}
}