Skip to content

armgit / ente

Public Imported from GitHub
0

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

Require only model paths per requested pipeline

laurenspriemc94aab016841
diff --git a/mobile/apps/photos/lib/utils/ml_util.dart b/mobile/apps/photos/lib/utils/ml_util.dart
index ded90f69f7..ef6ad99b23 100644
--- a/mobile/apps/photos/lib/utils/ml_util.dart
+++ b/mobile/apps/photos/lib/utils/ml_util.dart
@@ -609,16 +609,31 @@ Future<MLResult> analyzeImageRust(Map args) async {
final bool preferNnapi = args["preferNnapi"] as bool? ?? true;
final bool allowCpuFallback = args["allowCpuFallback"] as bool? ?? true;
- if (runFaces || runClip) {
- if (faceDetectionModelPath == null ||
- faceEmbeddingModelPath == null ||
- clipImageModelPath == null) {
- throw Exception(
- "RustMLMissingModelPath: Expected all model paths to be provided",
- );
+ bool isMissingModelPath(String? path) =>
+ path == null || path.trim().isEmpty;
+ final missingModelPaths = <String>[];
+ if (runFaces) {
+ if (isMissingModelPath(faceDetectionModelPath)) {
+ missingModelPaths.add("faceDetectionModelPath");
}
+ if (isMissingModelPath(faceEmbeddingModelPath)) {
+ missingModelPaths.add("faceEmbeddingModelPath");
+ }
+ }
+ if (runClip && isMissingModelPath(clipImageModelPath)) {
+ missingModelPaths.add("clipImageModelPath");
+ }
+ if (missingModelPaths.isNotEmpty) {
+ throw Exception(
+ "RustMLMissingModelPath: Missing required model paths: ${missingModelPaths.join(', ')}",
+ );
}
+ final modelPaths = rust_ml.RustModelPaths(
+ faceDetection: faceDetectionModelPath ?? "",
+ faceEmbedding: faceEmbeddingModelPath ?? "",
+ clipImage: clipImageModelPath ?? "",
+ );
final providerPolicy = rust_ml.RustExecutionProviderPolicy(
preferCoreml: preferCoreml,
preferNnapi: preferNnapi,
@@ -626,11 +641,7 @@ Future<MLResult> analyzeImageRust(Map args) async {
);
await rust_ml.initMlRuntime(
config: rust_ml.RustMlRuntimeConfig(
- modelPaths: rust_ml.RustModelPaths(
- faceDetection: faceDetectionModelPath ?? "",
- faceEmbedding: faceEmbeddingModelPath ?? "",
- clipImage: clipImageModelPath ?? "",
- ),
+ modelPaths: modelPaths,
providerPolicy: providerPolicy,
),
);
@@ -641,11 +652,7 @@ Future<MLResult> analyzeImageRust(Map args) async {
imagePath: imagePath,
runFaces: runFaces,
runClip: runClip,
- modelPaths: rust_ml.RustModelPaths(
- faceDetection: faceDetectionModelPath ?? "",
- faceEmbedding: faceEmbeddingModelPath ?? "",
- clipImage: clipImageModelPath ?? "",
- ),
+ modelPaths: modelPaths,
providerPolicy: providerPolicy,
),
);
diff --git a/mobile/apps/photos/rust/src/api/ml_indexing_api.rs b/mobile/apps/photos/rust/src/api/ml_indexing_api.rs
index 070e35e3fb..6bdfb48497 100644
--- a/mobile/apps/photos/rust/src/api/ml_indexing_api.rs
+++ b/mobile/apps/photos/rust/src/api/ml_indexing_api.rs
@@ -1,7 +1,7 @@
use crate::ml::{
clip::image::run_clip_image,
decode::decode_image_from_path,
- error::MlResult,
+ error::{MlError, MlResult},
face::{align::run_face_alignment, detect::run_face_detection, embed::run_face_embedding},
runtime::{self, ExecutionProviderPolicy, MlRuntimeConfig, ModelPaths},
};
@@ -102,6 +102,8 @@ pub fn analyze_image_rust(req: AnalyzeImageRequest) -> Result<AnalyzeImageResult
}
fn analyze_image_rust_inner(req: AnalyzeImageRequest) -> MlResult<AnalyzeImageResult> {
+ validate_request_model_paths(&req)?;
+
let runtime_config = MlRuntimeConfig {
model_paths: to_model_paths(&req.model_paths),
provider_policy: to_provider_policy(&req.provider_policy),
@@ -146,6 +148,29 @@ fn analyze_image_rust_inner(req: AnalyzeImageRequest) -> MlResult<AnalyzeImageRe
})
}
+fn validate_request_model_paths(req: &AnalyzeImageRequest) -> MlResult<()> {
+ let mut missing = Vec::new();
+ if req.run_faces {
+ if req.model_paths.face_detection.trim().is_empty() {
+ missing.push("faceDetectionModelPath");
+ }
+ if req.model_paths.face_embedding.trim().is_empty() {
+ missing.push("faceEmbeddingModelPath");
+ }
+ }
+ if req.run_clip && req.model_paths.clip_image.trim().is_empty() {
+ missing.push("clipImageModelPath");
+ }
+ if missing.is_empty() {
+ return Ok(());
+ }
+
+ Err(MlError::InvalidRequest(format!(
+ "missing required model paths: {}",
+ missing.join(", ")
+ )))
+}
+
fn to_runtime_config(config: &RustMlRuntimeConfig) -> MlRuntimeConfig {
MlRuntimeConfig {
model_paths: to_model_paths(&config.model_paths),
diff --git a/mobile/apps/photos/rust/src/ml/clip/image.rs b/mobile/apps/photos/rust/src/ml/clip/image.rs
index 4f1ffccccc..a6342f9243 100644
--- a/mobile/apps/photos/rust/src/ml/clip/image.rs
+++ b/mobile/apps/photos/rust/src/ml/clip/image.rs
@@ -7,7 +7,8 @@ use crate::ml::{
pub fn run_clip_image(runtime: &mut MlRuntime, decoded: &DecodedImage) -> MlResult<ClipResult> {
let input = preprocess::preprocess_clip(decoded)?;
- let (shape, output) = onnx::run_f32(&mut runtime.clip_image, input, vec![1, 3, 256, 256])?;
+ let clip_image = runtime.clip_image_session_mut()?;
+ let (shape, output) = onnx::run_f32(clip_image, input, vec![1, 3, 256, 256])?;
let mut embedding = if shape.len() == 2 {
if shape[0] != 1 {
diff --git a/mobile/apps/photos/rust/src/ml/face/detect.rs b/mobile/apps/photos/rust/src/ml/face/detect.rs
index 77265691ad..2b4f5b499d 100644
--- a/mobile/apps/photos/rust/src/ml/face/detect.rs
+++ b/mobile/apps/photos/rust/src/ml/face/detect.rs
@@ -15,8 +15,9 @@ pub fn run_face_detection(
decoded: &DecodedImage,
) -> MlResult<Vec<FaceDetection>> {
let (input, scaled_width, scaled_height) = preprocess::preprocess_yolo(decoded)?;
+ let face_detection = runtime.face_detection_session_mut()?;
let (output_shape, output_data) = onnx::run_f32(
- &mut runtime.face_detection,
+ face_detection,
input,
vec![1, 3, INPUT_HEIGHT as i64, INPUT_WIDTH as i64],
)?;
diff --git a/mobile/apps/photos/rust/src/ml/face/embed.rs b/mobile/apps/photos/rust/src/ml/face/embed.rs
index b05cd90c33..508c0a4bbd 100644
--- a/mobile/apps/photos/rust/src/ml/face/embed.rs
+++ b/mobile/apps/photos/rust/src/ml/face/embed.rs
@@ -38,8 +38,9 @@ pub fn run_face_embedding(
input.extend_from_slice(aligned);
}
+ let face_embedding = runtime.face_embedding_session_mut()?;
let (shape, output) = onnx::run_f32(
- &mut runtime.face_embedding,
+ face_embedding,
input,
vec![
aligned_faces.len() as i64,
diff --git a/mobile/apps/photos/rust/src/ml/runtime.rs b/mobile/apps/photos/rust/src/ml/runtime.rs
index 112224e285..83bb0ba4c7 100644
--- a/mobile/apps/photos/rust/src/ml/runtime.rs
+++ b/mobile/apps/photos/rust/src/ml/runtime.rs
@@ -40,9 +40,9 @@ pub struct MlRuntimeConfig {
#[derive(Debug)]
pub struct MlRuntime {
- pub face_detection: Session,
- pub face_embedding: Session,
- pub clip_image: Session,
+ face_detection: Option<Session>,
+ face_embedding: Option<Session>,
+ clip_image: Option<Session>,
}
#[derive(Debug)]
@@ -55,10 +55,11 @@ static GLOBAL_RUNTIME: Lazy<Mutex<Option<RuntimeState>>> = Lazy::new(|| Mutex::n
fn create_runtime(config: &MlRuntimeConfig) -> MlResult<MlRuntime> {
let face_detection =
- onnx::build_session(&config.model_paths.face_detection, &config.provider_policy)?;
+ build_optional_session(&config.model_paths.face_detection, &config.provider_policy)?;
let face_embedding =
- onnx::build_session(&config.model_paths.face_embedding, &config.provider_policy)?;
- let clip_image = onnx::build_session(&config.model_paths.clip_image, &config.provider_policy)?;
+ build_optional_session(&config.model_paths.face_embedding, &config.provider_policy)?;
+ let clip_image =
+ build_optional_session(&config.model_paths.clip_image, &config.provider_policy)?;
Ok(MlRuntime {
face_detection,
face_embedding,
@@ -66,6 +67,45 @@ fn create_runtime(config: &MlRuntimeConfig) -> MlResult<MlRuntime> {
})
}
+fn build_optional_session(
+ model_path: &str,
+ provider_policy: &ExecutionProviderPolicy,
+) -> MlResult<Option<Session>> {
+ if model_path.trim().is_empty() {
+ return Ok(None);
+ }
+ Ok(Some(onnx::build_session(model_path, provider_policy)?))
+}
+
+impl MlRuntime {
+ pub fn face_detection_session_mut(&mut self) -> MlResult<&mut Session> {
+ self.face_detection.as_mut().ok_or_else(|| {
+ MlError::InvalidRequest(
+ "missing model path: faceDetectionModelPath is required when runFaces is true"
+ .to_string(),
+ )
+ })
+ }
+
+ pub fn face_embedding_session_mut(&mut self) -> MlResult<&mut Session> {
+ self.face_embedding.as_mut().ok_or_else(|| {
+ MlError::InvalidRequest(
+ "missing model path: faceEmbeddingModelPath is required when runFaces is true"
+ .to_string(),
+ )
+ })
+ }
+
+ pub fn clip_image_session_mut(&mut self) -> MlResult<&mut Session> {
+ self.clip_image.as_mut().ok_or_else(|| {
+ MlError::InvalidRequest(
+ "missing model path: clipImageModelPath is required when runClip is true"
+ .to_string(),
+ )
+ })
+ }
+}
+
fn lock_runtime() -> std::sync::MutexGuard<'static, Option<RuntimeState>> {
match GLOBAL_RUNTIME.lock() {
Ok(guard) => guard,