π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Fix EP fallback rebuild loop and body detection dual-class emission
Amrithesh Kakkoth Ente1897d08f6601
diff --git a/mobile/apps/photos/rust/src/ml/pet/detect.rs b/mobile/apps/photos/rust/src/ml/pet/detect.rsindex dcbe4759f1..1dd9d67d07 100644--- a/mobile/apps/photos/rust/src/ml/pet/detect.rs+++ b/mobile/apps/photos/rust/src/ml/pet/detect.rs@@ -165,39 +165,43 @@ pub fn run_pet_body_detection(let row = &output_data[start..(start + row_len)];let obj_conf = row[4];- // Check cat (class 15) and dog (class 16) scores- for &class_id in &[COCO_CAT, COCO_DOG] {- let class_score = row[5 + class_id as usize] * obj_conf;- if class_score < BODY_MIN_SCORE {- continue;- }-- let x_min_abs = row[0] - row[2] / 2.0;- let y_min_abs = row[1] - row[3] / 2.0;- let x_max_abs = row[0] + row[2] / 2.0;- let y_max_abs = row[1] + row[3] / 2.0;-- let mut box_xyxy = [- x_min_abs / INPUT_WIDTH,- y_min_abs / INPUT_HEIGHT,- x_max_abs / INPUT_WIDTH,- y_max_abs / INPUT_HEIGHT,- ];-- correct_box_for_aspect_ratio(- &mut box_xyxy,- scaled_width,- scaled_height,- pad_left,- pad_top,- );-- detections.push(PetBodyDetection {- score: class_score,- box_xyxy,- coco_class: class_id,- });+ // Each anchor represents a single object β pick the best pet class.+ let cat_score = row[5 + COCO_CAT as usize] * obj_conf;+ let dog_score = row[5 + COCO_DOG as usize] * obj_conf;+ let (class_score, class_id) = if dog_score >= cat_score {+ (dog_score, COCO_DOG)+ } else {+ (cat_score, COCO_CAT)+ };+ if class_score < BODY_MIN_SCORE {+ continue;}++ let x_min_abs = row[0] - row[2] / 2.0;+ let y_min_abs = row[1] - row[3] / 2.0;+ let x_max_abs = row[0] + row[2] / 2.0;+ let y_max_abs = row[1] + row[3] / 2.0;++ let mut box_xyxy = [+ x_min_abs / INPUT_WIDTH,+ y_min_abs / INPUT_HEIGHT,+ x_max_abs / INPUT_WIDTH,+ y_max_abs / INPUT_HEIGHT,+ ];++ correct_box_for_aspect_ratio(+ &mut box_xyxy,+ scaled_width,+ scaled_height,+ pad_left,+ pad_top,+ );++ detections.push(PetBodyDetection {+ score: class_score,+ box_xyxy,+ coco_class: class_id,+ });}Ok(naive_nms_pet_body(detections, BODY_IOU_THRESHOLD))diff --git a/mobile/apps/photos/rust/src/ml/runtime.rs b/mobile/apps/photos/rust/src/ml/runtime.rsindex a0865310fe..7ddf7d64c9 100644--- a/mobile/apps/photos/rust/src/ml/runtime.rs+++ b/mobile/apps/photos/rust/src/ml/runtime.rs@@ -164,6 +164,12 @@ pub struct MlRuntime {struct RuntimeState {config: MlRuntimeConfig,runtime: MlRuntime,+ /// Set after a hardware EP failure triggers a successful CPU-only fallback.+ /// While true, `ensure_runtime` treats a CPU-only runtime as compatible+ /// with requests that prefer hardware acceleration (when+ /// `allow_cpu_fallback` is set), avoiding repeated rebuild/retry cycles+ /// on devices where NNAPI/CoreML is broken.+ fell_back_to_cpu: bool,}static GLOBAL_RUNTIME: Lazy<RwLock<Option<RuntimeState>>> = Lazy::new(|| RwLock::new(None));@@ -268,12 +274,19 @@ fn write_runtime() -> std::sync::RwLockWriteGuard<'static, Option<RuntimeState>>}}+/// A CPU-only runtime satisfies a hardware-preferring request when the+/// runtime was installed via EP fallback and the caller allows CPU fallback.+fn policy_compatible(existing: &RuntimeState, requested: &ExecutionProviderPolicy) -> bool {+ existing.config.provider_policy == *requested+ || (existing.fell_back_to_cpu && requested.allow_cpu_fallback)+}+pub fn ensure_runtime(config: &MlRuntimeConfig) -> MlResult<()> {// Fast path: check under read lock whether the current runtime can serve.{let guard = read_runtime();if let Some(existing) = guard.as_ref()- && existing.config.provider_policy == config.provider_policy+ && policy_compatible(existing, &config.provider_policy)&& existing.config.model_paths.can_serve(&config.model_paths){return Ok(()); // existing runtime handles this request@@ -283,27 +296,39 @@ pub fn ensure_runtime(config: &MlRuntimeConfig) -> MlResult<()> {// Slow path: acquire write lock, re-check (another thread may have// already merged), compute the merged config, build, and install.let mut guard = write_runtime();- let merged = match guard.as_ref() {+ let (merged, preserve_fallback) = match guard.as_ref() {Some(existing) => {// Re-check: a concurrent caller may have updated since the// read-lock check above.- if existing.config.provider_policy == config.provider_policy+ if policy_compatible(existing, &config.provider_policy)&& existing.config.model_paths.can_serve(&config.model_paths){return Ok(());}- MlRuntimeConfig {- model_paths: existing.config.model_paths.merge(&config.model_paths),- provider_policy: config.provider_policy.clone(),- }+ // If we previously fell back to CPU, keep the CPU-only policy+ // for the merged config instead of re-trying the broken EP.+ let effective_policy =+ if existing.fell_back_to_cpu && config.provider_policy.allow_cpu_fallback {+ existing.config.provider_policy.clone()+ } else {+ config.provider_policy.clone()+ };+ (+ MlRuntimeConfig {+ model_paths: existing.config.model_paths.merge(&config.model_paths),+ provider_policy: effective_policy,+ },+ existing.fell_back_to_cpu,+ )}- None => config.clone(),+ None => (config.clone(), false),};let runtime = create_runtime(&merged);*guard = Some(RuntimeState {config: merged,runtime,+ fell_back_to_cpu: preserve_fallback,});Ok(())}@@ -337,6 +362,16 @@ wherelet fallback_config = cpu_only_runtime_config(config);rt_log(&format!("execution provider failed, retrying with CPU-only runtime: {first_error}"));ensure_runtime(&fallback_config)?;++ // Mark that the hardware EP failed so future ensure_runtime+ // calls reuse the CPU-only runtime instead of rebuilding.+ {+ let mut guard = write_runtime();+ if let Some(state) = guard.as_mut() {+ state.fell_back_to_cpu = true;+ }+ }+{let guard = read_runtime();let state = guard