π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Refactor ONNX support modules
laurenspriemde29261a9a77
diff --git a/rust/crates/photos/src/ml/indexing.rs b/rust/crates/photos/src/ml/indexing.rsindex 1bb361660b..4e9b2f786c 100644--- a/rust/crates/photos/src/ml/indexing.rs+++ b/rust/crates/photos/src/ml/indexing.rs@@ -339,21 +339,6 @@ fn validate_request_model_paths(req: &AnalyzeImageRequest) -> MlResult<()> {mod tests {use super::*;- fn empty_model_paths() -> ModelPaths {- ModelPaths {- face_detection: String::new(),- face_embedding: String::new(),- clip_image: String::new(),- clip_text: String::new(),- pet_face_detection: String::new(),- pet_face_embedding_dog: String::new(),- pet_face_embedding_cat: String::new(),- pet_body_detection: String::new(),- pet_body_embedding_dog: String::new(),- pet_body_embedding_cat: String::new(),- }- }-fn png_bytes(width: u32, height: u32) -> Vec<u8> {let img =image::RgbImage::from_fn(width, height, |x, y| image::Rgb([x as u8, y as u8, 128]));@@ -373,7 +358,7 @@ mod tests {run_clip: false,run_pets: false,generate_face_crops: false,- model_paths: empty_model_paths(),+ model_paths: ModelPaths::default(),};let result = analyze_image(req).expect("analysis without models succeeds");@@ -393,7 +378,7 @@ mod tests {run_clip: false,run_pets: false,generate_face_crops: false,- model_paths: empty_model_paths(),+ model_paths: ModelPaths::default(),};match analyze_image(req) {diff --git a/rust/crates/photos/src/ml/onnx.rs b/rust/crates/photos/src/ml/onnx.rsindex 014659f1f0..e494839557 100644--- a/rust/crates/photos/src/ml/onnx.rs+++ b/rust/crates/photos/src/ml/onnx.rs@@ -1,14 +1,8 @@use ort::{ep::{CPU, ExecutionProviderDispatch},session::{Session, builder::GraphOptimizationLevel},- value::{Tensor, TensorElementType, TensorRef, ValueType},};-use std::{- cell::OnceCell,- path::{Path, PathBuf},-};--use half::prelude::{HalfFloatSliceExt, HalfFloatVecExt};+use std::path::{Path, PathBuf};#[cfg(target_os = "android")]use ort::ep::XNNPACK;@@ -30,70 +24,16 @@ use crate::ml::golden;#[cfg(any(target_os = "android", target_os = "linux", target_os = "windows"))]use crate::ml::webgpu;-#[cfg(any(target_os = "ios", target_os = "macos", test))]-const COREML_CACHE_SCHEMA: &str = "ort-1_28-mlprogram-all-default-v1";-const COREML_CACHE_COMPLETE_MARKER: &str = ".ente-cache-complete";-// The name ONNX Runtime's CoreML EP gives the compiled model it stores-// inside each generated MLProgram package directory in the cache-// (`model.mm` `CompileOrReadCachedModel`).-const COREML_CACHE_COMPILED_MODEL: &str = "compiled_model.mlmodelc";-// The weight blob file name inside an ORT-generated `.mlpackage`-// (`model_builder.cc` writes `@model_path/weights/weight.bin`).-const COREML_PACKAGE_WEIGHT_BLOB: &str = "weight.bin";-#[cfg(any(target_os = "ios", target_os = "macos"))]-const ENABLE_PERSISTENT_COREML_CACHE: bool = true;--pub(crate) struct PreparedF32Input {- f32_data: Vec<f32>,- f16_data: OnceCell<Vec<half::f16>>,-}--#[derive(Clone, Copy)]-pub(crate) enum BorrowedFloatTensor<'a> {- F32(&'a [f32]),- F16(&'a [half::f16]),-}--pub(crate) trait FloatTensorData: Copy {- fn len(self) -> usize;- fn value(self, index: usize) -> f32;-}--impl FloatTensorData for &[f32] {- fn len(self) -> usize {- <[f32]>::len(self)- }-- #[inline]- fn value(self, index: usize) -> f32 {- self[index]- }-}--impl FloatTensorData for &[half::f16] {- fn len(self) -> usize {- <[half::f16]>::len(self)- }+mod coreml_cache;+mod tensor;- #[inline]- fn value(self, index: usize) -> f32 {- self[index].to_f32()- }-}--impl PreparedF32Input {- pub(crate) fn new(data: Vec<f32>) -> Self {- Self {- f32_data: data,- f16_data: OnceCell::new(),- }- }+pub(crate) use tensor::{+ BorrowedFloatTensor, FloatTensorData, PreparedF32Input, run_f32, run_golden_tensor,+ run_i32_f32, with_prepared_float_output,+};- fn f16_data(&self) -> &[half::f16] {- self.f16_data- .get_or_init(|| Vec::<half::f16>::from_f32_slice(&self.f32_data))- }-}+#[cfg(any(target_os = "ios", target_os = "macos"))]+const ENABLE_PERSISTENT_COREML_CACHE: bool = true;#[derive(Clone, Copy, Debug, PartialEq, Eq)]pub(crate) enum ExecutionMode {@@ -131,9 +71,9 @@ impl ExecutionMode {pub(crate) fn build_session(model_path: &str,mode: ExecutionMode,- coreml_cache_namespace: &str,+ model_namespace: &str,) -> MlResult<(Session, ExecutionProvider)> {- let attempts = provider_attempts(mode, model_path, coreml_cache_namespace);+ let attempts = provider_attempts(mode, model_path, model_namespace);let mut errors = Vec::new();for attempt in attempts {@@ -141,8 +81,7 @@ pub(crate) fn build_session(if attempt.uses_webgpu {#[cfg(any(target_os = "android", target_os = "linux", target_os = "windows"))]{- match build_webgpu_session_with_canary(model_path, coreml_cache_namespace, attempt)- {+ match build_webgpu_session_with_canary(model_path, model_namespace, attempt) {Ok(session) => return Ok((session, execution_provider)),Err(error) => errors.push(format!("{error}")),}@@ -156,13 +95,13 @@ pub(crate) fn build_session(match build_and_validate_session(model_path, attempt) {Ok(session) => {if let Some(cache_dir) = coreml_cache_dir {- finalize_coreml_cache(&cache_dir, model_path);+ coreml_cache::finalize(&cache_dir, model_path);}return Ok((session, execution_provider));}Err(error) => {if let Some(cache_dir) = coreml_cache_dir- && let Err(cleanup_error) = invalidate_coreml_cache(&cache_dir)+ && let Err(cleanup_error) = coreml_cache::invalidate(&cache_dir){log::warn!("failed to invalidate CoreML cache for '{}' after session construction failed: {cleanup_error}",@@ -422,46 +361,6 @@ fn model_file_label(model_path: &str) -> &str {.unwrap_or(model_path)}-// Shared with the generator so its inference path stays identical to devices.-pub(crate) fn run_golden_tensor(- session: &mut Session,- input_shape: &[i64],- input: &golden::PreparedGoldenInput,-) -> MlResult<Vec<f32>> {- let outputs = match input {- golden::PreparedGoldenInput::F32(data) => {- if session_expects_f16(session) {- let input_tensor = Tensor::<half::f16>::from_array((- input_shape,- Vec::<half::f16>::from_f32_slice(data),- ))?;- session.run(ort::inputs![input_tensor])?- } else {- let input_tensor =- TensorRef::<f32>::from_array_view((input_shape, data.as_slice()))?;- session.run(ort::inputs![input_tensor])?- }- }- golden::PreparedGoldenInput::I32(data) => {- let input_tensor = TensorRef::<i32>::from_array_view((input_shape, data.as_slice()))?;- session.run(ort::inputs![input_tensor])?- }- };-- if outputs.len() == 0 {- return Err(MlError::Ort("missing first output tensor".to_string()));- }- let output = &outputs[0];- if let Ok((_, tensor_data)) = output.try_extract_tensor::<f32>() {- Ok(tensor_data.to_vec())- } else {- let (_, tensor_data) = output.try_extract_tensor::<half::f16>()?;- let mut data = vec![0.0; tensor_data.len()];- tensor_data.convert_to_f32_slice(&mut data);- Ok(data)- }-}-fn has_protobuf_parse_failure(errors: &[String]) -> bool {errors.iter().any(|error| {error@@ -493,12 +392,10 @@ impl ProviderAttempt {fn provider_attempts(mode: ExecutionMode,model_path: &str,- coreml_cache_namespace: &str,+ model_namespace: &str,) -> Vec<ProviderAttempt> {match mode {- ExecutionMode::PlatformDefault => {- platform_default_attempts(model_path, coreml_cache_namespace)- }+ ExecutionMode::PlatformDefault => platform_default_attempts(model_path, model_namespace),#[cfg(target_os = "android")]ExecutionMode::Xnnpack => vec![xnnpack_attempt(), ProviderAttempt::cpu_only()],ExecutionMode::CpuOnly => vec![ProviderAttempt::cpu_only()],@@ -506,14 +403,10 @@ fn provider_attempts(}#[cfg(any(target_os = "ios", target_os = "macos"))]-fn platform_default_attempts(- model_path: &str,- coreml_cache_namespace: &str,-) -> Vec<ProviderAttempt> {+fn platform_default_attempts(model_path: &str, model_namespace: &str) -> Vec<ProviderAttempt> {let mut attempts = Vec::new();if golden_entry_required(model_path, "CoreML") {- let (coreml_provider, coreml_cache_dir) =- coreml_provider(model_path, coreml_cache_namespace);+ let (coreml_provider, coreml_cache_dir) = coreml_provider(model_path, model_namespace);attempts.push(ProviderAttempt {providers: vec![coreml_provider,@@ -532,7 +425,7 @@ fn platform_default_attempts(#[cfg(any(target_os = "ios", target_os = "macos"))]fn coreml_provider(model_path: &str,- cache_namespace: &str,+ model_namespace: &str,) -> (ExecutionProviderDispatch, Option<PathBuf>) {let mut provider = CoreML::default().with_model_format(ModelFormat::MLProgram)@@ -541,7 +434,7 @@ fn coreml_provider(let mut prepared_cache_dir = None;if ENABLE_PERSISTENT_COREML_CACHE {- match prepare_coreml_cache_directory(model_path, cache_namespace) {+ match coreml_cache::prepare_directory(model_path, model_namespace) {Ok(cache_dir) => {provider = provider.with_model_cache_dir(cache_dir.to_string_lossy());prepared_cache_dir = Some(cache_dir);@@ -554,33 +447,14 @@ fn coreml_provider(}}} else {- remove_persistent_coreml_cache(model_path);+ coreml_cache::remove(model_path);}(provider.build().error_on_failure(), prepared_cache_dir)}-// Disabling the feature must also reclaim caches created by earlier releases.-#[cfg(any(target_os = "ios", target_os = "macos"))]-fn remove_persistent_coreml_cache(model_path: &str) {- let Some(coreml_root) = coreml_cache_root(Path::new(model_path))- .parent()- .map(Path::to_path_buf)- else {- return;- };- match std::fs::remove_dir_all(&coreml_root) {- Ok(()) => log::info!("removed persistent CoreML cache (feature disabled)"),- Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}- Err(error) => log::warn!("failed to remove disabled persistent CoreML cache: {error}"),- }-}-#[cfg(target_os = "android")]-fn platform_default_attempts(- model_path: &str,- _coreml_cache_namespace: &str,-) -> Vec<ProviderAttempt> {+fn platform_default_attempts(model_path: &str, _model_namespace: &str) -> Vec<ProviderAttempt> {let mut attempts = Vec::new();if webgpu::attempt_permitted(model_path) && golden_entry_required(model_path, "WebGPU") {attempts.push(ProviderAttempt {@@ -603,10 +477,7 @@ fn platform_default_attempts(}#[cfg(any(target_os = "linux", target_os = "windows"))]-fn platform_default_attempts(- model_path: &str,- _coreml_cache_namespace: &str,-) -> Vec<ProviderAttempt> {+fn platform_default_attempts(model_path: &str, _model_namespace: &str) -> Vec<ProviderAttempt> {let mut attempts = Vec::new();if webgpu::attempt_permitted(model_path) && golden_entry_required(model_path, "WebGPU") {attempts.push(ProviderAttempt {@@ -661,239 +532,10 @@ fn webgpu_provider() -> ExecutionProviderDispatch {target_os = "macos",target_os = "windows")))]-fn platform_default_attempts(- _model_path: &str,- _coreml_cache_namespace: &str,-) -> Vec<ProviderAttempt> {+fn platform_default_attempts(_model_path: &str, _model_namespace: &str) -> Vec<ProviderAttempt> {vec![ProviderAttempt::cpu_only()]}-#[cfg(any(target_os = "ios", target_os = "macos"))]-fn prepare_coreml_cache_directory(- model_path: &str,- cache_namespace: &str,-) -> std::io::Result<PathBuf> {- let model_path = Path::new(model_path);- let schema_root = coreml_cache_root(model_path);-- if let Some(coreml_root) = schema_root.parent() {- match prune_stale_coreml_schema_directories(coreml_root, COREML_CACHE_SCHEMA) {- Ok(removed) => {- for schema in removed {- log::info!("removed stale CoreML cache schema '{schema}'");- }- }- Err(error) => log::warn!("failed to prune stale CoreML cache schemas: {error}"),- }- }-- let model_cache_root = schema_root.join(sanitize_cache_component(cache_namespace));- std::fs::create_dir_all(&model_cache_root)?;-- let cache_key = coreml_model_cache_key(model_path)?;- prune_superseded_coreml_cache_directories(&model_cache_root, &cache_key)?;-- let cache_dir = model_cache_root.join(cache_key);- prepare_coreml_cache_entry(&cache_dir)?;- Ok(cache_dir)-}--// Rebuild unmarked entries so ORT cannot reuse a package interrupted mid-build.-#[cfg(any(target_os = "ios", target_os = "macos", test))]-fn prepare_coreml_cache_entry(cache_dir: &Path) -> std::io::Result<()> {- if cache_dir.exists() && !coreml_cache_complete_marker(cache_dir).is_file() {- std::fs::remove_dir_all(cache_dir)?;- }- std::fs::create_dir_all(cache_dir)-}--// Trim before marking complete so a crash mid-trim forces a clean rebuild.-// Trim failures only waste disk space and therefore do not fail the load.-fn finalize_coreml_cache(cache_dir: &Path, model_path: &str) {- match trim_coreml_cache_weights(cache_dir) {- Ok(0) => {}- Ok(reclaimed) => log::info!(- "primed CoreML cache for '{}': trimmed {} MiB of generated package weights",- model_file_label(model_path),- reclaimed / (1024 * 1024)- ),- Err(error) => log::warn!(- "failed to trim CoreML cache weights for '{}': {error}",- model_file_label(model_path)- ),- }-- if let Err(error) = mark_coreml_cache_complete(cache_dir) {- log::warn!(- "failed to mark CoreML cache complete for '{}': {error}",- model_file_label(model_path)- );- }-}--// On a warm cache hit ONNX Runtime 1.28 only checks that the generated-// package directory and loads `compiled_model.mlmodelc`, making the package's-// own weights redundant. Uncompiled packages remain intact; incompatible-// future runtimes will fail construction and trigger cache invalidation.-fn trim_coreml_cache_weights(cache_dir: &Path) -> std::io::Result<u64> {- let mut reclaimed = 0;- // ORT 1.28 stores the compiled MLProgram inside- // <cache_dir>/<model_hash>/<partition>/model/compiled_model.mlmodelc.- for model_hash_entry in std::fs::read_dir(cache_dir)? {- let model_hash_entry = model_hash_entry?;- if !model_hash_entry.file_type()?.is_dir() {- continue;- }- for partition_entry in std::fs::read_dir(model_hash_entry.path())? {- let partition_entry = partition_entry?;- if !partition_entry.file_type()?.is_dir() {- continue;- }- let package_dir = partition_entry.path().join("model");- if !package_dir.is_dir() || !package_dir.join(COREML_CACHE_COMPILED_MODEL).exists() {- continue;- }- reclaimed += truncate_weight_blobs(&package_dir)?;- }- }- Ok(reclaimed)-}--// Truncates `weight.bin` files under `dir`, never descending into-// `.mlmodelc` bundles: the compiled model keeps its own `weights/weight.bin`-// copy, and that one is exactly what warm loads read, so it must survive.-fn truncate_weight_blobs(dir: &Path) -> std::io::Result<u64> {- let mut reclaimed = 0;- for entry in std::fs::read_dir(dir)? {- let entry = entry?;- let file_type = entry.file_type()?;- if file_type.is_dir() {- if entry.path().extension() == Some(std::ffi::OsStr::new("mlmodelc")) {- continue;- }- reclaimed += truncate_weight_blobs(&entry.path())?;- } else if file_type.is_file() && entry.file_name() == COREML_PACKAGE_WEIGHT_BLOB {- let size = entry.metadata()?.len();- if size > 0 {- std::fs::write(entry.path(), [])?;- reclaimed += size;- }- }- }- Ok(reclaimed)-}--fn mark_coreml_cache_complete(cache_dir: &Path) -> std::io::Result<()> {- std::fs::write(coreml_cache_complete_marker(cache_dir), [])-}--fn invalidate_coreml_cache(cache_dir: &Path) -> std::io::Result<()> {- match std::fs::remove_dir_all(cache_dir) {- Ok(()) => Ok(()),- Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),- Err(error) => Err(error),- }-}--fn coreml_cache_complete_marker(cache_dir: &Path) -> PathBuf {- cache_dir.join(COREML_CACHE_COMPLETE_MARKER)-}--// Use Library/Caches for eviction and backup exclusion, or temp for unusual-// layouts.-#[cfg(any(target_os = "ios", target_os = "macos", test))]-fn coreml_cache_root(model_path: &Path) -> PathBuf {- let cache_base = model_path- .ancestors()- .find(|ancestor| ancestor.file_name().is_some_and(|name| name == "Library"))- .map(|library| library.join("Caches"))- .unwrap_or_else(std::env::temp_dir);-- cache_base- .join("ente")- .join("ml")- .join("coreml")- .join(COREML_CACHE_SCHEMA)-}--// Include the filename and file metadata in the directory name because ONNX-// Runtime's internal CoreML cache key does not necessarily change when only-// model weights change.-#[cfg(any(target_os = "ios", target_os = "macos", test))]-fn coreml_model_cache_key(model_path: &Path) -> std::io::Result<String> {- use std::time::UNIX_EPOCH;-- let metadata = std::fs::metadata(model_path)?;- let modified = metadata- .modified()?- .duration_since(UNIX_EPOCH)- .unwrap_or_default();- Ok(format!(- "{}-{}-{}-{}",- sanitize_cache_component(- model_path- .file_name()- .and_then(|name| name.to_str())- .unwrap_or("model")- ),- metadata.len(),- modified.as_secs(),- modified.subsec_nanos()- ))-}--#[cfg(any(target_os = "ios", target_os = "macos", test))]-fn sanitize_cache_component(value: &str) -> String {- value- .chars()- .map(|character| {- if character.is_ascii_alphanumeric() || matches!(character, '-' | '_' | '.') {- character- } else {- '_'- }- })- .collect()-}--// Schema bumps must not orphan the previous version's large cache trees.-#[cfg(any(target_os = "ios", target_os = "macos", test))]-fn prune_stale_coreml_schema_directories(- coreml_root: &Path,- current_schema: &str,-) -> std::io::Result<Vec<String>> {- let mut removed = Vec::new();- let entries = match std::fs::read_dir(coreml_root) {- Ok(entries) => entries,- Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(removed),- Err(error) => return Err(error),- };- for entry in entries {- let entry = entry?;- if entry.file_name() == current_schema || !entry.file_type()?.is_dir() {- continue;- }- std::fs::remove_dir_all(entry.path())?;- removed.push(entry.file_name().to_string_lossy().into_owned());- }- Ok(removed)-}--#[cfg(any(target_os = "ios", target_os = "macos", test))]-fn prune_superseded_coreml_cache_directories(- model_cache_root: &Path,- current_cache_key: &str,-) -> std::io::Result<()> {- for entry in std::fs::read_dir(model_cache_root)? {- let entry = entry?;- if entry.file_name() == current_cache_key || !entry.file_type()?.is_dir() {- continue;- }- std::fs::remove_dir_all(entry.path())?;- }- Ok(())-}-#[cfg(target_os = "android")]fn xnnpack_provider() -> ExecutionProviderDispatch {XNNPACK::default()@@ -931,148 +573,9 @@ fn build_session_with_providers(model_path: &str, attempt: ProviderAttempt) -> MOk(session)}-// Guard against execution providers that return NaN or infinity instead of-// failing. The error message matches `is_execution_provider_failure` so the-// runtime advances to the next execution provider fallback.-fn ensure_finite_f32(data: &[f32]) -> MlResult<()> {- if data.iter().copied().all(f32::is_finite) {- return Ok(());- }- Err(MlError::Ort(- "model produced non-finite output values".to_string(),- ))-}--fn ensure_finite_f16(data: &[half::f16]) -> MlResult<()> {- if data.iter().all(|value| value.is_finite()) {- return Ok(());- }- Err(MlError::Ort(- "model produced non-finite output values".to_string(),- ))-}--fn session_expects_f16(session: &Session) -> bool {- session- .inputs()- .first()- .and_then(|i| match i.dtype() {- ValueType::Tensor { ty, .. } => Some(*ty == TensorElementType::Float16),- _ => None,- })- .unwrap_or(false)-}--pub(crate) fn run_f32<const N: usize>(- session: &mut Session,- input: &PreparedF32Input,- input_shape: [i64; N],-) -> MlResult<(Vec<i64>, Vec<f32>)> {- let outputs = if session_expects_f16(session) {- let input_tensor =- TensorRef::<half::f16>::from_array_view((input_shape, input.f16_data()))?;- session.run(ort::inputs![input_tensor])?- } else {- let input_tensor =- TensorRef::<f32>::from_array_view((input_shape, input.f32_data.as_slice()))?;- session.run(ort::inputs![input_tensor])?- };-- if outputs.len() == 0 {- return Err(MlError::Ort("missing first output tensor".to_string()));- }- let output = &outputs[0];-- if let Ok((tensor_shape, tensor_data)) = output.try_extract_tensor::<f32>() {- let shape = tensor_shape.iter().copied().collect::<Vec<_>>();- let data = tensor_data.to_vec();- ensure_finite_f32(&data)?;- Ok((shape, data))- } else {- let (tensor_shape, tensor_data) = output.try_extract_tensor::<half::f16>()?;- let shape = tensor_shape.iter().copied().collect::<Vec<_>>();- let mut data = vec![0.0; tensor_data.len()];- tensor_data.convert_to_f32_slice(&mut data);- ensure_finite_f32(&data)?;- Ok((shape, data))- }-}--pub(crate) fn with_prepared_float_output<const N: usize, T>(- session: &mut Session,- input: &PreparedF32Input,- input_shape: [i64; N],- consume: impl FnOnce(&[i64], BorrowedFloatTensor<'_>) -> MlResult<T>,-) -> MlResult<T> {- let outputs = if session_expects_f16(session) {- let input_tensor =- TensorRef::<half::f16>::from_array_view((input_shape, input.f16_data()))?;- session.run(ort::inputs![input_tensor])?- } else {- let input_tensor =- TensorRef::<f32>::from_array_view((input_shape, input.f32_data.as_slice()))?;- session.run(ort::inputs![input_tensor])?- };-- if outputs.len() == 0 {- return Err(MlError::Ort("missing first output tensor".to_string()));- }- let output = &outputs[0];- if let Ok((tensor_shape, tensor_data)) = output.try_extract_tensor::<f32>() {- ensure_finite_f32(tensor_data)?;- consume(tensor_shape, BorrowedFloatTensor::F32(tensor_data))- } else {- let (tensor_shape, tensor_data) = output.try_extract_tensor::<half::f16>()?;- ensure_finite_f16(tensor_data)?;- consume(tensor_shape, BorrowedFloatTensor::F16(tensor_data))- }-}--pub(crate) fn run_i32_f32<const N: usize>(- session: &mut Session,- input: &[i32],- input_shape: [i64; N],-) -> MlResult<(Vec<i64>, Vec<f32>)> {- let input_tensor = TensorRef::<i32>::from_array_view((input_shape, input))?;- let outputs = session.run(ort::inputs![input_tensor])?;- if outputs.len() == 0 {- return Err(MlError::Ort("missing first output tensor".to_string()));- }- let output = &outputs[0];- let (tensor_shape, tensor_data) = output.try_extract_tensor::<f32>()?;- let shape = tensor_shape.iter().copied().collect::<Vec<_>>();- let data = tensor_data.to_vec();- ensure_finite_f32(&data)?;- Ok((shape, data))-}-#[cfg(test)]mod tests {- use std::{io::Write, path::Path};-- use super::{- ExecutionProvider, coreml_cache_root, coreml_model_cache_key, ensure_finite_f16,- ensure_finite_f32, has_protobuf_parse_failure, invalidate_coreml_cache,- mark_coreml_cache_complete, prepare_coreml_cache_entry, provider_attempt_failure_message,- prune_stale_coreml_schema_directories, prune_superseded_coreml_cache_directories,- sanitize_cache_component, trim_coreml_cache_weights,- };-- #[test]- fn accepts_finite_model_outputs() {- assert!(ensure_finite_f32(&[0.0, -1.5, f32::MAX]).is_ok());- assert!(ensure_finite_f16(&[half::f16::from_f32(0.25)]).is_ok());- }-- #[test]- fn rejects_non_finite_model_outputs() {- for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {- let error = ensure_finite_f32(&[1.0, bad]).unwrap_err();- assert!(error.to_string().contains("non-finite"));- }- let error = ensure_finite_f16(&[half::f16::NAN]).unwrap_err();- assert!(error.to_string().contains("non-finite"));- }+ use super::{ExecutionProvider, has_protobuf_parse_failure, provider_attempt_failure_message};#[test]fn detects_protobuf_parse_failure() {@@ -1119,186 +622,4 @@ mod tests {.is_none());}-- #[test]- fn places_coreml_cache_in_library_caches() {- let model = Path::new(- "/var/mobile/Containers/Data/Application/APP/Library/Application Support/assets/model.onnx",- );-- assert_eq!(- coreml_cache_root(model),- Path::new(- "/var/mobile/Containers/Data/Application/APP/Library/Caches/ente/ml/coreml/ort-1_28-mlprogram-all-default-v1"- )- );- }-- #[test]- fn coreml_cache_key_changes_when_model_file_changes() {- let mut model = tempfile::NamedTempFile::new().unwrap();- model.write_all(b"first").unwrap();- model.flush().unwrap();- let first_key = coreml_model_cache_key(model.path()).unwrap();-- model.write_all(b" version").unwrap();- model.flush().unwrap();- let second_key = coreml_model_cache_key(model.path()).unwrap();-- assert_ne!(first_key, second_key);- }-- #[test]- fn sanitizes_coreml_cache_component() {- assert_eq!(- sanitize_cache_component("model name.onnx"),- "model_name.onnx"- );- }-- #[test]- fn prunes_only_superseded_directories_for_one_model() {- let root = tempfile::tempdir().unwrap();- let old_cache = root.path().join("old");- let current_cache = root.path().join("current");- std::fs::create_dir(&old_cache).unwrap();- std::fs::create_dir(¤t_cache).unwrap();- std::fs::write(root.path().join("marker"), b"keep").unwrap();-- prune_superseded_coreml_cache_directories(root.path(), "current").unwrap();-- assert!(!old_cache.exists());- assert!(current_cache.exists());- assert!(root.path().join("marker").exists());- }-- #[test]- fn prunes_stale_schema_directories_keeping_current_and_files() {- let coreml_root = tempfile::tempdir().unwrap();- let stale = coreml_root.path().join("ort-1_27-mlprogram-all-default-v1");- let current = coreml_root.path().join("ort-1_28-mlprogram-all-default-v1");- std::fs::create_dir(&stale).unwrap();- std::fs::write(stale.join("cached"), b"stale").unwrap();- std::fs::create_dir(¤t).unwrap();- std::fs::write(coreml_root.path().join("stray-file"), b"keep").unwrap();-- let removed = prune_stale_coreml_schema_directories(- coreml_root.path(),- "ort-1_28-mlprogram-all-default-v1",- )- .unwrap();-- assert_eq!(removed, vec!["ort-1_27-mlprogram-all-default-v1"]);- assert!(!stale.exists());- assert!(current.exists());- assert!(coreml_root.path().join("stray-file").exists());- }-- #[test]- fn schema_pruning_treats_a_missing_root_as_empty() {- let parent = tempfile::tempdir().unwrap();- let removed =- prune_stale_coreml_schema_directories(&parent.path().join("missing"), "current")- .unwrap();- assert!(removed.is_empty());- }-- fn write_cache_partition(- cache_dir: &Path,- partition: &str,- compiled: bool,- ) -> std::path::PathBuf {- let package_dir = cache_dir.join("modelhash").join(partition).join("model");- let weights_dir = package_dir- .join("Data")- .join("com.microsoft.OnnxRuntime")- .join("weights");- std::fs::create_dir_all(&weights_dir).unwrap();- let weight_blob = weights_dir.join("weight.bin");- std::fs::write(&weight_blob, b"weights").unwrap();- std::fs::write(package_dir.join("Manifest.json"), b"manifest").unwrap();- if compiled {- let compiled_weights_dir = package_dir.join("compiled_model.mlmodelc").join("weights");- std::fs::create_dir_all(&compiled_weights_dir).unwrap();- std::fs::write(compiled_weights_dir.join("weight.bin"), b"compiled-weights").unwrap();- }- weight_blob- }-- #[test]- fn trims_package_weights_only_where_a_compiled_model_exists() {- let cache_dir = tempfile::tempdir().unwrap();- let compiled_blob = write_cache_partition(cache_dir.path(), "0_static_mlprogram", true);- let uncompiled_blob = write_cache_partition(cache_dir.path(), "1_static_mlprogram", false);- mark_coreml_cache_complete(cache_dir.path()).unwrap();-- let reclaimed = trim_coreml_cache_weights(cache_dir.path()).unwrap();-- assert_eq!(reclaimed, b"weights".len() as u64);- assert!(compiled_blob.exists());- assert_eq!(std::fs::metadata(&compiled_blob).unwrap().len(), 0);- let package_dir = compiled_blob.ancestors().nth(4).unwrap();- assert!(package_dir.ends_with("model"));- assert!(package_dir.is_dir());- assert_eq!(- std::fs::read(- package_dir- .join("compiled_model.mlmodelc")- .join("weights")- .join("weight.bin")- )- .unwrap(),- b"compiled-weights".to_vec()- );- assert_eq!(std::fs::read(uncompiled_blob).unwrap(), b"weights".to_vec());- }-- #[test]- fn trimming_an_already_trimmed_cache_reclaims_nothing() {- let cache_dir = tempfile::tempdir().unwrap();- write_cache_partition(cache_dir.path(), "0_static_mlprogram", true);-- assert!(trim_coreml_cache_weights(cache_dir.path()).unwrap() > 0);- assert_eq!(trim_coreml_cache_weights(cache_dir.path()).unwrap(), 0);- }-- #[test]- fn replaces_an_incomplete_coreml_cache_entry() {- let root = tempfile::tempdir().unwrap();- let cache_dir = root.path().join("current");- std::fs::create_dir(&cache_dir).unwrap();- let partial_artifact = cache_dir.join("partial.mlmodelc");- std::fs::write(&partial_artifact, b"partial").unwrap();-- prepare_coreml_cache_entry(&cache_dir).unwrap();-- assert!(cache_dir.is_dir());- assert!(!partial_artifact.exists());- }-- #[test]- fn preserves_a_completed_coreml_cache_entry() {- let root = tempfile::tempdir().unwrap();- let cache_dir = root.path().join("current");- std::fs::create_dir(&cache_dir).unwrap();- let artifact = cache_dir.join("model.mlmodelc");- std::fs::write(&artifact, b"complete").unwrap();- mark_coreml_cache_complete(&cache_dir).unwrap();-- prepare_coreml_cache_entry(&cache_dir).unwrap();-- assert!(artifact.exists());- }-- #[test]- fn invalidates_a_failed_coreml_cache_entry() {- let root = tempfile::tempdir().unwrap();- let cache_dir = root.path().join("current");- std::fs::create_dir(&cache_dir).unwrap();-- invalidate_coreml_cache(&cache_dir).unwrap();-- assert!(!cache_dir.exists());- invalidate_coreml_cache(&cache_dir).unwrap();- }}diff --git a/rust/crates/photos/src/ml/onnx/coreml_cache.rs b/rust/crates/photos/src/ml/onnx/coreml_cache.rsnew file mode 100644index 0000000000..20ecc25116--- /dev/null+++ b/rust/crates/photos/src/ml/onnx/coreml_cache.rs@@ -0,0 +1,418 @@+use std::path::{Path, PathBuf};++#[cfg(any(target_os = "ios", target_os = "macos", test))]+const SCHEMA: &str = "ort-1_28-mlprogram-all-default-v1";+const COMPLETE_MARKER: &str = ".ente-cache-complete";+const COMPILED_MODEL: &str = "compiled_model.mlmodelc";+const PACKAGE_WEIGHT_BLOB: &str = "weight.bin";++#[cfg(any(target_os = "ios", target_os = "macos"))]+pub(super) fn prepare_directory(+ model_path: &str,+ model_namespace: &str,+) -> std::io::Result<PathBuf> {+ let model_path = Path::new(model_path);+ let schema_root = cache_root(model_path);++ if let Some(coreml_root) = schema_root.parent() {+ match prune_stale_schema_directories(coreml_root, SCHEMA) {+ Ok(removed) => {+ for schema in removed {+ log::info!("removed stale CoreML cache schema '{schema}'");+ }+ }+ Err(error) => log::warn!("failed to prune stale CoreML cache schemas: {error}"),+ }+ }++ let model_cache_root = schema_root.join(sanitize_component(model_namespace));+ std::fs::create_dir_all(&model_cache_root)?;++ let cache_key = model_cache_key(model_path)?;+ prune_superseded_directories(&model_cache_root, &cache_key)?;++ let cache_dir = model_cache_root.join(cache_key);+ prepare_entry(&cache_dir)?;+ Ok(cache_dir)+}++#[cfg(any(target_os = "ios", target_os = "macos"))]+pub(super) fn remove(model_path: &str) {+ let Some(coreml_root) = cache_root(Path::new(model_path))+ .parent()+ .map(Path::to_path_buf)+ else {+ return;+ };+ match std::fs::remove_dir_all(&coreml_root) {+ Ok(()) => log::info!("removed persistent CoreML cache (feature disabled)"),+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}+ Err(error) => log::warn!("failed to remove disabled persistent CoreML cache: {error}"),+ }+}++pub(super) fn finalize(cache_dir: &Path, model_path: &str) {+ match trim_weights(cache_dir) {+ Ok(0) => {}+ Ok(reclaimed) => log::info!(+ "primed CoreML cache for '{}': trimmed {} MiB of generated package weights",+ super::model_file_label(model_path),+ reclaimed / (1024 * 1024)+ ),+ Err(error) => log::warn!(+ "failed to trim CoreML cache weights for '{}': {error}",+ super::model_file_label(model_path)+ ),+ }++ if let Err(error) = mark_complete(cache_dir) {+ log::warn!(+ "failed to mark CoreML cache complete for '{}': {error}",+ super::model_file_label(model_path)+ );+ }+}++pub(super) fn invalidate(cache_dir: &Path) -> std::io::Result<()> {+ match std::fs::remove_dir_all(cache_dir) {+ Ok(()) => Ok(()),+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),+ Err(error) => Err(error),+ }+}++#[cfg(any(target_os = "ios", target_os = "macos", test))]+fn prepare_entry(cache_dir: &Path) -> std::io::Result<()> {+ if cache_dir.exists() && !complete_marker(cache_dir).is_file() {+ std::fs::remove_dir_all(cache_dir)?;+ }+ std::fs::create_dir_all(cache_dir)+}++fn trim_weights(cache_dir: &Path) -> std::io::Result<u64> {+ let mut reclaimed = 0;+ for model_hash_entry in std::fs::read_dir(cache_dir)? {+ let model_hash_entry = model_hash_entry?;+ if !model_hash_entry.file_type()?.is_dir() {+ continue;+ }+ for partition_entry in std::fs::read_dir(model_hash_entry.path())? {+ let partition_entry = partition_entry?;+ if !partition_entry.file_type()?.is_dir() {+ continue;+ }+ let package_dir = partition_entry.path().join("model");+ if !package_dir.is_dir() || !package_dir.join(COMPILED_MODEL).exists() {+ continue;+ }+ reclaimed += truncate_weight_blobs(&package_dir)?;+ }+ }+ Ok(reclaimed)+}++fn truncate_weight_blobs(dir: &Path) -> std::io::Result<u64> {+ let mut reclaimed = 0;+ for entry in std::fs::read_dir(dir)? {+ let entry = entry?;+ let file_type = entry.file_type()?;+ if file_type.is_dir() {+ // Compiled bundles keep the weights needed by warm cache loads.+ if entry.path().extension() == Some(std::ffi::OsStr::new("mlmodelc")) {+ continue;+ }+ reclaimed += truncate_weight_blobs(&entry.path())?;+ } else if file_type.is_file() && entry.file_name() == PACKAGE_WEIGHT_BLOB {+ let size = entry.metadata()?.len();+ if size > 0 {+ std::fs::write(entry.path(), [])?;+ reclaimed += size;+ }+ }+ }+ Ok(reclaimed)+}++fn mark_complete(cache_dir: &Path) -> std::io::Result<()> {+ std::fs::write(complete_marker(cache_dir), [])+}++fn complete_marker(cache_dir: &Path) -> PathBuf {+ cache_dir.join(COMPLETE_MARKER)+}++#[cfg(any(target_os = "ios", target_os = "macos", test))]+fn cache_root(model_path: &Path) -> PathBuf {+ let cache_base = model_path+ .ancestors()+ .find(|ancestor| ancestor.file_name().is_some_and(|name| name == "Library"))+ .map(|library| library.join("Caches"))+ .unwrap_or_else(std::env::temp_dir);++ cache_base+ .join("ente")+ .join("ml")+ .join("coreml")+ .join(SCHEMA)+}++#[cfg(any(target_os = "ios", target_os = "macos", test))]+fn model_cache_key(model_path: &Path) -> std::io::Result<String> {+ use std::time::UNIX_EPOCH;++ let metadata = std::fs::metadata(model_path)?;+ let modified = metadata+ .modified()?+ .duration_since(UNIX_EPOCH)+ .unwrap_or_default();+ Ok(format!(+ "{}-{}-{}-{}",+ sanitize_component(+ model_path+ .file_name()+ .and_then(|name| name.to_str())+ .unwrap_or("model")+ ),+ metadata.len(),+ modified.as_secs(),+ modified.subsec_nanos()+ ))+}++#[cfg(any(target_os = "ios", target_os = "macos", test))]+fn sanitize_component(value: &str) -> String {+ value+ .chars()+ .map(|character| {+ if character.is_ascii_alphanumeric() || matches!(character, '-' | '_' | '.') {+ character+ } else {+ '_'+ }+ })+ .collect()+}++#[cfg(any(target_os = "ios", target_os = "macos", test))]+fn prune_stale_schema_directories(+ coreml_root: &Path,+ current_schema: &str,+) -> std::io::Result<Vec<String>> {+ let mut removed = Vec::new();+ let entries = match std::fs::read_dir(coreml_root) {+ Ok(entries) => entries,+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(removed),+ Err(error) => return Err(error),+ };+ for entry in entries {+ let entry = entry?;+ if entry.file_name() == current_schema || !entry.file_type()?.is_dir() {+ continue;+ }+ std::fs::remove_dir_all(entry.path())?;+ removed.push(entry.file_name().to_string_lossy().into_owned());+ }+ Ok(removed)+}++#[cfg(any(target_os = "ios", target_os = "macos", test))]+fn prune_superseded_directories(+ model_cache_root: &Path,+ current_cache_key: &str,+) -> std::io::Result<()> {+ for entry in std::fs::read_dir(model_cache_root)? {+ let entry = entry?;+ if entry.file_name() == current_cache_key || !entry.file_type()?.is_dir() {+ continue;+ }+ std::fs::remove_dir_all(entry.path())?;+ }+ Ok(())+}++#[cfg(test)]+mod tests {+ use std::{io::Write, path::Path};++ use super::{+ cache_root, invalidate, mark_complete, model_cache_key, prepare_entry,+ prune_stale_schema_directories, prune_superseded_directories, sanitize_component,+ trim_weights,+ };++ #[test]+ fn places_cache_in_library_caches() {+ let model = Path::new(+ "/var/mobile/Containers/Data/Application/APP/Library/Application Support/assets/model.onnx",+ );++ assert_eq!(+ cache_root(model),+ Path::new(+ "/var/mobile/Containers/Data/Application/APP/Library/Caches/ente/ml/coreml/ort-1_28-mlprogram-all-default-v1"+ )+ );+ }++ #[test]+ fn cache_key_changes_when_model_file_changes() {+ let mut model = tempfile::NamedTempFile::new().unwrap();+ model.write_all(b"first").unwrap();+ model.flush().unwrap();+ let first_key = model_cache_key(model.path()).unwrap();++ model.write_all(b" version").unwrap();+ model.flush().unwrap();+ let second_key = model_cache_key(model.path()).unwrap();++ assert_ne!(first_key, second_key);+ }++ #[test]+ fn sanitizes_cache_component() {+ assert_eq!(sanitize_component("model name.onnx"), "model_name.onnx");+ }++ #[test]+ fn prunes_only_superseded_directories_for_one_model() {+ let root = tempfile::tempdir().unwrap();+ let old_cache = root.path().join("old");+ let current_cache = root.path().join("current");+ std::fs::create_dir(&old_cache).unwrap();+ std::fs::create_dir(¤t_cache).unwrap();+ std::fs::write(root.path().join("marker"), b"keep").unwrap();++ prune_superseded_directories(root.path(), "current").unwrap();++ assert!(!old_cache.exists());+ assert!(current_cache.exists());+ assert!(root.path().join("marker").exists());+ }++ #[test]+ fn prunes_stale_schema_directories_keeping_current_and_files() {+ let coreml_root = tempfile::tempdir().unwrap();+ let stale = coreml_root.path().join("ort-1_27-mlprogram-all-default-v1");+ let current = coreml_root.path().join("ort-1_28-mlprogram-all-default-v1");+ std::fs::create_dir(&stale).unwrap();+ std::fs::write(stale.join("cached"), b"stale").unwrap();+ std::fs::create_dir(¤t).unwrap();+ std::fs::write(coreml_root.path().join("stray-file"), b"keep").unwrap();++ let removed =+ prune_stale_schema_directories(coreml_root.path(), "ort-1_28-mlprogram-all-default-v1")+ .unwrap();++ assert_eq!(removed, vec!["ort-1_27-mlprogram-all-default-v1"]);+ assert!(!stale.exists());+ assert!(current.exists());+ assert!(coreml_root.path().join("stray-file").exists());+ }++ #[test]+ fn schema_pruning_treats_a_missing_root_as_empty() {+ let parent = tempfile::tempdir().unwrap();+ let removed =+ prune_stale_schema_directories(&parent.path().join("missing"), "current").unwrap();+ assert!(removed.is_empty());+ }++ fn write_cache_partition(+ cache_dir: &Path,+ partition: &str,+ compiled: bool,+ ) -> std::path::PathBuf {+ let package_dir = cache_dir.join("modelhash").join(partition).join("model");+ let weights_dir = package_dir+ .join("Data")+ .join("com.microsoft.OnnxRuntime")+ .join("weights");+ std::fs::create_dir_all(&weights_dir).unwrap();+ let weight_blob = weights_dir.join("weight.bin");+ std::fs::write(&weight_blob, b"weights").unwrap();+ std::fs::write(package_dir.join("Manifest.json"), b"manifest").unwrap();+ if compiled {+ let compiled_weights_dir = package_dir.join("compiled_model.mlmodelc").join("weights");+ std::fs::create_dir_all(&compiled_weights_dir).unwrap();+ std::fs::write(compiled_weights_dir.join("weight.bin"), b"compiled-weights").unwrap();+ }+ weight_blob+ }++ #[test]+ fn trims_package_weights_only_where_a_compiled_model_exists() {+ let cache_dir = tempfile::tempdir().unwrap();+ let compiled_blob = write_cache_partition(cache_dir.path(), "0_static_mlprogram", true);+ let uncompiled_blob = write_cache_partition(cache_dir.path(), "1_static_mlprogram", false);+ mark_complete(cache_dir.path()).unwrap();++ let reclaimed = trim_weights(cache_dir.path()).unwrap();++ assert_eq!(reclaimed, b"weights".len() as u64);+ assert!(compiled_blob.exists());+ assert_eq!(std::fs::metadata(&compiled_blob).unwrap().len(), 0);+ let package_dir = compiled_blob.ancestors().nth(4).unwrap();+ assert!(package_dir.ends_with("model"));+ assert!(package_dir.is_dir());+ assert_eq!(+ std::fs::read(+ package_dir+ .join("compiled_model.mlmodelc")+ .join("weights")+ .join("weight.bin")+ )+ .unwrap(),+ b"compiled-weights".to_vec()+ );+ assert_eq!(std::fs::read(uncompiled_blob).unwrap(), b"weights".to_vec());+ }++ #[test]+ fn trimming_an_already_trimmed_cache_reclaims_nothing() {+ let cache_dir = tempfile::tempdir().unwrap();+ write_cache_partition(cache_dir.path(), "0_static_mlprogram", true);++ assert!(trim_weights(cache_dir.path()).unwrap() > 0);+ assert_eq!(trim_weights(cache_dir.path()).unwrap(), 0);+ }++ #[test]+ fn replaces_an_incomplete_cache_entry() {+ let root = tempfile::tempdir().unwrap();+ let cache_dir = root.path().join("current");+ std::fs::create_dir(&cache_dir).unwrap();+ let partial_artifact = cache_dir.join("partial.mlmodelc");+ std::fs::write(&partial_artifact, b"partial").unwrap();++ prepare_entry(&cache_dir).unwrap();++ assert!(cache_dir.is_dir());+ assert!(!partial_artifact.exists());+ }++ #[test]+ fn preserves_a_completed_cache_entry() {+ let root = tempfile::tempdir().unwrap();+ let cache_dir = root.path().join("current");+ std::fs::create_dir(&cache_dir).unwrap();+ let artifact = cache_dir.join("model.mlmodelc");+ std::fs::write(&artifact, b"complete").unwrap();+ mark_complete(&cache_dir).unwrap();++ prepare_entry(&cache_dir).unwrap();++ assert!(artifact.exists());+ }++ #[test]+ fn invalidates_a_failed_cache_entry() {+ let root = tempfile::tempdir().unwrap();+ let cache_dir = root.path().join("current");+ std::fs::create_dir(&cache_dir).unwrap();++ invalidate(&cache_dir).unwrap();++ assert!(!cache_dir.exists());+ invalidate(&cache_dir).unwrap();+ }+}diff --git a/rust/crates/photos/src/ml/onnx/tensor.rs b/rust/crates/photos/src/ml/onnx/tensor.rsnew file mode 100644index 0000000000..54ea445059--- /dev/null+++ b/rust/crates/photos/src/ml/onnx/tensor.rs@@ -0,0 +1,237 @@+use std::cell::OnceCell;++use half::prelude::{HalfFloatSliceExt, HalfFloatVecExt};+use ort::{+ session::Session,+ value::{Tensor, TensorElementType, TensorRef, ValueType},+};++use crate::ml::{+ error::{MlError, MlResult},+ golden,+};++pub(crate) struct PreparedF32Input {+ f32_data: Vec<f32>,+ f16_data: OnceCell<Vec<half::f16>>,+}++#[derive(Clone, Copy)]+pub(crate) enum BorrowedFloatTensor<'a> {+ F32(&'a [f32]),+ F16(&'a [half::f16]),+}++pub(crate) trait FloatTensorData: Copy {+ fn len(self) -> usize;+ fn value(self, index: usize) -> f32;+}++impl FloatTensorData for &[f32] {+ fn len(self) -> usize {+ <[f32]>::len(self)+ }++ #[inline]+ fn value(self, index: usize) -> f32 {+ self[index]+ }+}++impl FloatTensorData for &[half::f16] {+ fn len(self) -> usize {+ <[half::f16]>::len(self)+ }++ #[inline]+ fn value(self, index: usize) -> f32 {+ self[index].to_f32()+ }+}++impl PreparedF32Input {+ pub(crate) fn new(data: Vec<f32>) -> Self {+ Self {+ f32_data: data,+ f16_data: OnceCell::new(),+ }+ }++ fn f16_data(&self) -> &[half::f16] {+ self.f16_data+ .get_or_init(|| Vec::<half::f16>::from_f32_slice(&self.f32_data))+ }+}++pub(crate) fn run_golden_tensor(+ session: &mut Session,+ input_shape: &[i64],+ input: &golden::PreparedGoldenInput,+) -> MlResult<Vec<f32>> {+ let outputs = match input {+ golden::PreparedGoldenInput::F32(data) => {+ if session_expects_f16(session) {+ let input_tensor = Tensor::<half::f16>::from_array((+ input_shape,+ Vec::<half::f16>::from_f32_slice(data),+ ))?;+ session.run(ort::inputs![input_tensor])?+ } else {+ let input_tensor =+ TensorRef::<f32>::from_array_view((input_shape, data.as_slice()))?;+ session.run(ort::inputs![input_tensor])?+ }+ }+ golden::PreparedGoldenInput::I32(data) => {+ let input_tensor = TensorRef::<i32>::from_array_view((input_shape, data.as_slice()))?;+ session.run(ort::inputs![input_tensor])?+ }+ };++ if outputs.len() == 0 {+ return Err(MlError::Ort("missing first output tensor".to_string()));+ }+ let output = &outputs[0];+ if let Ok((_, tensor_data)) = output.try_extract_tensor::<f32>() {+ Ok(tensor_data.to_vec())+ } else {+ let (_, tensor_data) = output.try_extract_tensor::<half::f16>()?;+ let mut data = vec![0.0; tensor_data.len()];+ tensor_data.convert_to_f32_slice(&mut data);+ Ok(data)+ }+}++fn ensure_finite_f32(data: &[f32]) -> MlResult<()> {+ if data.iter().copied().all(f32::is_finite) {+ return Ok(());+ }+ // Runtime classifies this message as a retryable provider failure.+ Err(MlError::Ort(+ "model produced non-finite output values".to_string(),+ ))+}++fn ensure_finite_f16(data: &[half::f16]) -> MlResult<()> {+ if data.iter().all(|value| value.is_finite()) {+ return Ok(());+ }+ Err(MlError::Ort(+ "model produced non-finite output values".to_string(),+ ))+}++fn session_expects_f16(session: &Session) -> bool {+ session+ .inputs()+ .first()+ .and_then(|i| match i.dtype() {+ ValueType::Tensor { ty, .. } => Some(*ty == TensorElementType::Float16),+ _ => None,+ })+ .unwrap_or(false)+}++pub(crate) fn run_f32<const N: usize>(+ session: &mut Session,+ input: &PreparedF32Input,+ input_shape: [i64; N],+) -> MlResult<(Vec<i64>, Vec<f32>)> {+ let outputs = if session_expects_f16(session) {+ let input_tensor =+ TensorRef::<half::f16>::from_array_view((input_shape, input.f16_data()))?;+ session.run(ort::inputs![input_tensor])?+ } else {+ let input_tensor =+ TensorRef::<f32>::from_array_view((input_shape, input.f32_data.as_slice()))?;+ session.run(ort::inputs![input_tensor])?+ };++ if outputs.len() == 0 {+ return Err(MlError::Ort("missing first output tensor".to_string()));+ }+ let output = &outputs[0];++ if let Ok((tensor_shape, tensor_data)) = output.try_extract_tensor::<f32>() {+ let shape = tensor_shape.iter().copied().collect::<Vec<_>>();+ let data = tensor_data.to_vec();+ ensure_finite_f32(&data)?;+ Ok((shape, data))+ } else {+ let (tensor_shape, tensor_data) = output.try_extract_tensor::<half::f16>()?;+ let shape = tensor_shape.iter().copied().collect::<Vec<_>>();+ let mut data = vec![0.0; tensor_data.len()];+ tensor_data.convert_to_f32_slice(&mut data);+ ensure_finite_f32(&data)?;+ Ok((shape, data))+ }+}++pub(crate) fn with_prepared_float_output<const N: usize, T>(+ session: &mut Session,+ input: &PreparedF32Input,+ input_shape: [i64; N],+ consume: impl FnOnce(&[i64], BorrowedFloatTensor<'_>) -> MlResult<T>,+) -> MlResult<T> {+ let outputs = if session_expects_f16(session) {+ let input_tensor =+ TensorRef::<half::f16>::from_array_view((input_shape, input.f16_data()))?;+ session.run(ort::inputs![input_tensor])?+ } else {+ let input_tensor =+ TensorRef::<f32>::from_array_view((input_shape, input.f32_data.as_slice()))?;+ session.run(ort::inputs![input_tensor])?+ };++ if outputs.len() == 0 {+ return Err(MlError::Ort("missing first output tensor".to_string()));+ }+ let output = &outputs[0];+ if let Ok((tensor_shape, tensor_data)) = output.try_extract_tensor::<f32>() {+ ensure_finite_f32(tensor_data)?;+ consume(tensor_shape, BorrowedFloatTensor::F32(tensor_data))+ } else {+ let (tensor_shape, tensor_data) = output.try_extract_tensor::<half::f16>()?;+ ensure_finite_f16(tensor_data)?;+ consume(tensor_shape, BorrowedFloatTensor::F16(tensor_data))+ }+}++pub(crate) fn run_i32_f32<const N: usize>(+ session: &mut Session,+ input: &[i32],+ input_shape: [i64; N],+) -> MlResult<(Vec<i64>, Vec<f32>)> {+ let input_tensor = TensorRef::<i32>::from_array_view((input_shape, input))?;+ let outputs = session.run(ort::inputs![input_tensor])?;+ if outputs.len() == 0 {+ return Err(MlError::Ort("missing first output tensor".to_string()));+ }+ let output = &outputs[0];+ let (tensor_shape, tensor_data) = output.try_extract_tensor::<f32>()?;+ let shape = tensor_shape.iter().copied().collect::<Vec<_>>();+ let data = tensor_data.to_vec();+ ensure_finite_f32(&data)?;+ Ok((shape, data))+}++#[cfg(test)]+mod tests {+ use super::{ensure_finite_f16, ensure_finite_f32};++ #[test]+ fn accepts_finite_model_outputs() {+ assert!(ensure_finite_f32(&[0.0, -1.5, f32::MAX]).is_ok());+ assert!(ensure_finite_f16(&[half::f16::from_f32(0.25)]).is_ok());+ }++ #[test]+ fn rejects_non_finite_model_outputs() {+ for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {+ let error = ensure_finite_f32(&[1.0, bad]).unwrap_err();+ assert!(error.to_string().contains("non-finite"));+ }+ let error = ensure_finite_f16(&[half::f16::NAN]).unwrap_err();+ assert!(error.to_string().contains("non-finite"));+ }+}diff --git a/rust/crates/photos/src/ml/runtime.rs b/rust/crates/photos/src/ml/runtime.rsindex 681e0e840e..599ee46939 100644--- a/rust/crates/photos/src/ml/runtime.rs+++ b/rust/crates/photos/src/ml/runtime.rs@@ -1,5 +1,6 @@use std::{cell::Cell,+ path::Path,sync::{Mutex, MutexGuard},};@@ -11,7 +12,7 @@ use crate::ml::{onnx,};-#[derive(Clone, Debug, PartialEq, Eq)]+#[derive(Clone, Debug, Default, PartialEq, Eq)]pub struct ModelPaths {pub face_detection: String,pub face_embedding: String,@@ -25,19 +26,23 @@ pub struct ModelPaths {pub pet_body_embedding_cat: String,}+#[derive(Debug)]+struct LoadedSession {+ session: Session,+ execution_provider: onnx::ExecutionProvider,+}+#[derive(Debug)]struct ModelSlotState {path: String,fallback_execution_mode: Option<onnx::ExecutionMode>,- execution_provider: Option<onnx::ExecutionProvider>,- pin_count: usize,- session: Option<Session>,+ loaded: Option<LoadedSession>,}#[derive(Debug)]struct ModelSlot {default_execution_mode: onnx::ExecutionMode,- coreml_cache_namespace: &'static str,+ model_namespace: &'static str,state: Mutex<ModelSlotState>,}@@ -64,19 +69,14 @@ pub(crate) struct MlRuntimeView<'a> {}impl ModelSlot {- fn new(- default_execution_mode: onnx::ExecutionMode,- coreml_cache_namespace: &'static str,- ) -> Self {+ fn new(default_execution_mode: onnx::ExecutionMode, model_namespace: &'static str) -> Self {Self {default_execution_mode,- coreml_cache_namespace,+ model_namespace,state: Mutex::new(ModelSlotState {path: String::new(),fallback_execution_mode: None,- execution_provider: None,- pin_count: 0,- session: None,+ loaded: None,}),}}@@ -104,17 +104,11 @@ impl ModelSlot {}Self::set_config_locked(&mut state, path);- state.pin_count = 1;}fn release_residency(&self) {let mut state = self.lock_state();- if state.pin_count > 0 {- state.pin_count -= 1;- }- if state.pin_count == 0 {- Self::clear_transient_runtime_state_locked(&mut state);- }+ Self::clear_transient_runtime_state_locked(&mut state);}fn advance_provider_fallback_locked(&self, state: &mut ModelSlotState) -> bool {@@ -126,8 +120,7 @@ impl ModelSlot {};state.fallback_execution_mode = Some(fallback_mode);- state.execution_provider = None;- state.session = None;+ state.loaded = None;true}@@ -151,15 +144,12 @@ impl ModelSlot {return Err(error);}- let execution_provider = state- .execution_provider- .expect("a loaded session must record its execution provider");- let result = operation(- state- .session- .as_mut()- .expect("session must be loaded before model execution"),- );+ let loaded = state+ .loaded+ .as_mut()+ .expect("session must be loaded before model execution");+ let execution_provider = loaded.execution_provider;+ let result = operation(&mut loaded.session);match result {Ok(value) => return Ok((value, execution_provider)),Err(error) => {@@ -195,19 +185,16 @@ impl ModelSlot {}state.path = path.to_string();state.fallback_execution_mode = None;- state.execution_provider = None;- state.session = None;+ state.loaded = None;}fn clear_transient_runtime_state_locked(state: &mut ModelSlotState) {state.fallback_execution_mode = None;- state.execution_provider = None;- state.session = None;+ state.loaded = None;}fn reset_slot_locked(state: &mut ModelSlotState) {state.path.clear();- state.pin_count = 0;Self::clear_transient_runtime_state_locked(state);}@@ -221,19 +208,24 @@ impl ModelSlot {if state.path.trim().is_empty() {return Err(MlError::InvalidRequest(error_msg.to_string()));}- if state.session.is_some() {+ if state.loaded.is_some() {return Ok(());}let execution_mode = self.effective_execution_mode(state);- let model_name = state.path.rsplit('/').next().unwrap_or(&state.path);+ let model_name = Path::new(&state.path)+ .file_name()+ .and_then(|name| name.to_str())+ .unwrap_or(&state.path);log::info!("loading {model_name} with {execution_mode:?} execution");let t = std::time::Instant::now();let (session, execution_provider) =- onnx::build_session(&state.path, execution_mode, self.coreml_cache_namespace)?;+ onnx::build_session(&state.path, execution_mode, self.model_namespace)?;log::info!("loaded {model_name} in {:?}", t.elapsed());- state.execution_provider = Some(execution_provider);- state.session = Some(session);+ state.loaded = Some(LoadedSession {+ session,+ execution_provider,+ });Ok(())}}@@ -536,33 +528,18 @@ fn is_execution_provider_failure(error: &MlError) -> bool {mod tests {use super::*;- fn empty_paths() -> ModelPaths {- ModelPaths {- face_detection: String::new(),- face_embedding: String::new(),- clip_image: String::new(),- clip_text: String::new(),- pet_face_detection: String::new(),- pet_face_embedding_dog: String::new(),- pet_face_embedding_cat: String::new(),- pet_body_detection: String::new(),- pet_body_embedding_dog: String::new(),- pet_body_embedding_cat: String::new(),- }- }-#[test]fn configure_requested_models_preserves_unrequested_slots() {let runtime = MlRuntime::new();runtime.configure_requested_models(&ModelPaths {clip_text: "clip_text.onnx".to_string(),- ..empty_paths()+ ..ModelPaths::default()});runtime.configure_requested_models(&ModelPaths {face_detection: "face.onnx".to_string(),- ..empty_paths()+ ..ModelPaths::default()});let clip_text = runtime.clip_text.lock_state();@@ -575,7 +552,7 @@ mod tests {let model_paths = ModelPaths {face_detection: "face.onnx".to_string(),clip_image: "clip.onnx".to_string(),- ..empty_paths()+ ..ModelPaths::default()};runtime.configure_requested_models(&model_paths);@@ -601,7 +578,7 @@ mod tests {fn runtime_accepts_single_use_operation() {let value = String::from("once");- let result = with_runtime(&empty_paths(), move |_| Ok(value));+ let result = with_runtime(&ModelPaths::default(), move |_| Ok(value));assert_eq!(result.unwrap(), "once");}@@ -613,13 +590,11 @@ mod tests {{let mut clip_text = runtime.clip_text.lock_state();clip_text.path = "clip_text.onnx".to_string();- clip_text.pin_count = 0;clip_text.fallback_execution_mode = Some(onnx::ExecutionMode::CpuOnly);}{let mut face_detection = runtime.face_detection.lock_state();face_detection.path = "face.onnx".to_string();- face_detection.pin_count = 1;face_detection.fallback_execution_mode = Some(onnx::ExecutionMode::CpuOnly);}@@ -627,39 +602,37 @@ mod tests {let clip_text = runtime.clip_text.lock_state();assert_eq!(clip_text.path, "clip_text.onnx");- assert_eq!(clip_text.pin_count, 0);assert_eq!(clip_text.fallback_execution_mode,Some(onnx::ExecutionMode::CpuOnly));let face_detection = runtime.face_detection.lock_state();- assert_eq!(face_detection.pin_count, 0);assert_eq!(face_detection.fallback_execution_mode, None);}#[test]- fn prepare_indexing_models_pins_without_loading_sessions() {+ fn prepare_indexing_models_configures_without_loading_sessions() {let runtime = MlRuntime::new();runtime.prepare_indexing_models(&ModelPaths {face_detection: "face.onnx".to_string(),face_embedding: "embed.onnx".to_string(),clip_image: "clip.onnx".to_string(),- ..empty_paths()+ ..ModelPaths::default()});let face_detection = runtime.face_detection.lock_state();- assert_eq!(face_detection.pin_count, 1);- assert!(face_detection.session.is_none());+ assert_eq!(face_detection.path, "face.onnx");+ assert!(face_detection.loaded.is_none());let face_embedding = runtime.face_embedding.lock_state();- assert_eq!(face_embedding.pin_count, 1);- assert!(face_embedding.session.is_none());+ assert_eq!(face_embedding.path, "embed.onnx");+ assert!(face_embedding.loaded.is_none());let clip_image = runtime.clip_image.lock_state();- assert_eq!(clip_image.pin_count, 1);- assert!(clip_image.session.is_none());+ assert_eq!(clip_image.path, "clip.onnx");+ assert!(clip_image.loaded.is_none());}#[test]@@ -669,7 +642,6 @@ mod tests {{let mut state = slot.lock_state();state.path = "pet.onnx".to_string();- state.pin_count = 1;state.fallback_execution_mode = Some(onnx::ExecutionMode::CpuOnly);}@@ -677,9 +649,8 @@ mod tests {let state = slot.lock_state();assert!(state.path.is_empty());- assert_eq!(state.pin_count, 0);assert_eq!(state.fallback_execution_mode, None);- assert!(state.session.is_none());+ assert!(state.loaded.is_none());}#[test]@@ -689,16 +660,14 @@ mod tests {{let mut state = slot.lock_state();state.path = "clip_text.onnx".to_string();- state.pin_count = 1;state.fallback_execution_mode = Some(onnx::ExecutionMode::CpuOnly);}slot.release_residency();let state = slot.lock_state();- assert_eq!(state.pin_count, 0);assert_eq!(state.fallback_execution_mode, None);- assert!(state.session.is_none());+ assert!(state.loaded.is_none());}#[test]