Skip to content

armgit / ente

Public Imported from GitHub
0

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

Downgrade ort crate to be compatible with onnx runtime 1.18

laurenspriem4434d8f146e0
diff --git a/mobile/apps/photos/rust/Cargo.lock b/mobile/apps/photos/rust/Cargo.lock
index 1ad86e5623..772bfd6187 100644
--- a/mobile/apps/photos/rust/Cargo.lock
+++ b/mobile/apps/photos/rust/Cargo.lock
@@ -168,7 +168,7 @@ dependencies = [
"num-traits",
"pastey",
"rayon",
- "thiserror",
+ "thiserror 2.0.18",
"v_frame",
"y4m",
]
@@ -548,7 +548,7 @@ dependencies = [
"ndarray 0.16.1",
"once_cell",
"ort",
- "thiserror",
+ "thiserror 2.0.18",
"usearch",
]
@@ -615,7 +615,7 @@ dependencies = [
"image",
"num-traits",
"rayon",
- "thiserror",
+ "thiserror 2.0.18",
]
[[package]]
@@ -1042,9 +1042,9 @@ dependencies = [
[[package]]
name = "libloading"
-version = "0.9.0"
+version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "754ca22de805bb5744484a5b151a9e1a8e837d5dc232c2d7d8c2e3492edc8b60"
+checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55"
dependencies = [
"cfg-if",
"windows-link",
@@ -1201,24 +1201,22 @@ dependencies = [
[[package]]
name = "ndarray"
-version = "0.16.1"
+version = "0.15.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "882ed72dce9365842bf196bdeedf5055305f11fc8c03dee7bb0194a6cad34841"
+checksum = "adb12d4e967ec485a5f71c6311fe28158e9d6f4bc4a447b474184d0f91a8fa32"
dependencies = [
"matrixmultiply",
"num-complex",
"num-integer",
"num-traits",
- "portable-atomic",
- "portable-atomic-util",
"rawpointer",
]
[[package]]
name = "ndarray"
-version = "0.17.2"
+version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "520080814a7a6b4a6e9070823bb24b4531daac8c4627e08ba5de8c5ef2f2752d"
+checksum = "882ed72dce9365842bf196bdeedf5055305f11fc8c03dee7bb0194a6cad34841"
dependencies = [
"matrixmultiply",
"num-complex",
@@ -1362,22 +1360,27 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "ort"
-version = "2.0.0-rc.11"
+version = "2.0.0-rc.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4a5df903c0d2c07b56950f1058104ab0c8557159f2741782223704de9be73c3c"
+checksum = "86d83095ae3c1258738d70ae7a06195c94d966a8e546f0d3609dc90885fb61f5"
dependencies = [
+ "js-sys",
"libloading",
- "ndarray 0.17.2",
+ "ndarray 0.15.6",
"ort-sys",
- "smallvec",
+ "thiserror 1.0.69",
"tracing",
+ "web-sys",
]
[[package]]
name = "ort-sys"
-version = "2.0.0-rc.11"
+version = "2.0.0-rc.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "06503bb33f294c5f1ba484011e053bfa6ae227074bdb841e9863492dc5960d4b"
+checksum = "0f2f6427193c808010b126bef45ebd33f8dee43770223a1200f84d3734d6c656"
+dependencies = [
+ "pkg-config",
+]
[[package]]
name = "oslog"
@@ -1436,6 +1439,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
+[[package]]
+name = "pkg-config"
+version = "0.3.32"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
+
[[package]]
name = "png"
version = "0.18.1"
@@ -1648,7 +1657,7 @@ dependencies = [
"rand 0.9.2",
"rand_chacha 0.9.0",
"simd_helpers",
- "thiserror",
+ "thiserror 2.0.18",
"v_frame",
"wasm-bindgen",
]
@@ -1892,13 +1901,33 @@ dependencies = [
"winapi-util",
]
+[[package]]
+name = "thiserror"
+version = "1.0.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
+dependencies = [
+ "thiserror-impl 1.0.69",
+]
+
[[package]]
name = "thiserror"
version = "2.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4"
dependencies = [
- "thiserror-impl",
+ "thiserror-impl 2.0.18",
+]
+
+[[package]]
+name = "thiserror-impl"
+version = "1.0.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
]
[[package]]
@@ -1951,9 +1980,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
dependencies = [
"pin-project-lite",
+ "tracing-attributes",
"tracing-core",
]
+[[package]]
+name = "tracing-attributes"
+version = "0.1.31"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
[[package]]
name = "tracing-core"
version = "0.1.36"
diff --git a/mobile/apps/photos/rust/Cargo.toml b/mobile/apps/photos/rust/Cargo.toml
index 03690c2f8d..c76128e16a 100644
--- a/mobile/apps/photos/rust/Cargo.toml
+++ b/mobile/apps/photos/rust/Cargo.toml
@@ -20,10 +20,10 @@ once_cell = "1.20.2"
thiserror = "2.0.11"
[target.'cfg(target_os = "ios")'.dependencies]
-ort = { version = "2.0.0-rc.9", default-features = false, features = ["std", "ndarray", "coreml"] }
+ort = { version = "=2.0.0-rc.4", default-features = false, features = ["ndarray", "coreml"] }
[target.'cfg(not(target_os = "ios"))'.dependencies]
-ort = { version = "2.0.0-rc.9", default-features = false, features = ["ndarray", "load-dynamic", "copy-dylibs", "coreml", "nnapi"] }
+ort = { version = "=2.0.0-rc.4", default-features = false, features = ["ndarray", "load-dynamic", "copy-dylibs", "coreml", "nnapi"] }
[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(frb_expand)'] }
diff --git a/mobile/apps/photos/rust/src/ml/onnx.rs b/mobile/apps/photos/rust/src/ml/onnx.rs
index 4f2d4f1bf0..d390693ba5 100644
--- a/mobile/apps/photos/rust/src/ml/onnx.rs
+++ b/mobile/apps/photos/rust/src/ml/onnx.rs
@@ -1,9 +1,12 @@
use ort::{
- ep,
- session::{Session, builder::GraphOptimizationLevel},
- value::Tensor,
+ CPUExecutionProvider, ExecutionProviderDispatch, GraphOptimizationLevel, Session, Tensor,
};
+#[cfg(target_vendor = "apple")]
+use ort::CoreMLExecutionProvider;
+#[cfg(target_os = "android")]
+use ort::NNAPIExecutionProvider;
+
use crate::ml::{
error::{MlError, MlResult},
runtime::ExecutionProviderPolicy,
@@ -11,24 +14,24 @@ use crate::ml::{
pub fn build_session(model_path: &str, policy: &ExecutionProviderPolicy) -> MlResult<Session> {
let mut builder = Session::builder()?
- .with_optimization_level(GraphOptimizationLevel::All)?
+ .with_optimization_level(GraphOptimizationLevel::Level3)?
.with_intra_threads(1)?
.with_inter_threads(1)?;
- let mut providers = Vec::new();
+ let mut providers: Vec<ExecutionProviderDispatch> = Vec::new();
#[cfg(target_vendor = "apple")]
if policy.prefer_coreml {
- providers.push(ep::CoreML::default().build());
+ providers.push(CoreMLExecutionProvider::default().build());
}
#[cfg(target_os = "android")]
if policy.prefer_nnapi {
- providers.push(ep::NNAPI::default().build());
+ providers.push(NNAPIExecutionProvider::default().build());
}
if policy.allow_cpu_fallback {
- providers.push(ep::CPU::default().build());
+ providers.push(CPUExecutionProvider::default().build());
}
if !providers.is_empty() {
@@ -45,11 +48,13 @@ pub fn run_f32(
input_shape: Vec<i64>,
) -> MlResult<(Vec<i64>, Vec<f32>)> {
let input_tensor = Tensor::<f32>::from_array((input_shape, input))?;
- let outputs = session.run(ort::inputs![input_tensor])?;
+ 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 (shape, data) = output.try_extract_tensor::<f32>()?;
- Ok((shape.to_vec(), data.to_vec()))
+ let tensor = output.try_extract_tensor::<f32>()?;
+ let shape = tensor.shape().iter().map(|d| *d as i64).collect::<Vec<_>>();
+ let data = tensor.iter().copied().collect::<Vec<_>>();
+ Ok((shape, data))
}
diff --git a/mobile/apps/photos/rust/src/ml/runtime.rs b/mobile/apps/photos/rust/src/ml/runtime.rs
index 65762d051a..112224e285 100644
--- a/mobile/apps/photos/rust/src/ml/runtime.rs
+++ b/mobile/apps/photos/rust/src/ml/runtime.rs
@@ -1,7 +1,7 @@
use std::sync::Mutex;
use once_cell::sync::Lazy;
-use ort::session::Session;
+use ort::Session;
use crate::ml::{
error::{MlError, MlResult},
@@ -66,20 +66,30 @@ fn create_runtime(config: &MlRuntimeConfig) -> MlResult<MlRuntime> {
})
}
-fn lock_runtime() -> MlResult<std::sync::MutexGuard<'static, Option<RuntimeState>>> {
- GLOBAL_RUNTIME
- .lock()
- .map_err(|e| MlError::Runtime(format!("runtime mutex poisoned: {e}")))
+fn lock_runtime() -> std::sync::MutexGuard<'static, Option<RuntimeState>> {
+ match GLOBAL_RUNTIME.lock() {
+ Ok(guard) => guard,
+ Err(poisoned) => {
+ // Recover from a previous panic by clearing runtime state.
+ let mut guard = poisoned.into_inner();
+ *guard = None;
+ guard
+ }
+ }
}
pub fn ensure_runtime(config: &MlRuntimeConfig) -> MlResult<()> {
- let mut guard = lock_runtime()?;
- let should_rebuild = match guard.as_ref() {
- Some(existing) => existing.config != *config,
- None => true,
+ let should_rebuild = {
+ let guard = lock_runtime();
+ match guard.as_ref() {
+ Some(existing) => existing.config != *config,
+ None => true,
+ }
};
+
if should_rebuild {
let runtime = create_runtime(config)?;
+ let mut guard = lock_runtime();
*guard = Some(RuntimeState {
config: config.clone(),
runtime,
@@ -93,7 +103,7 @@ where
F: FnOnce(&mut MlRuntime) -> MlResult<R>,
{
ensure_runtime(config)?;
- let mut guard = lock_runtime()?;
+ let mut guard = lock_runtime();
let state = guard
.as_mut()
.ok_or_else(|| MlError::Runtime("runtime is not initialized".to_string()))?;
@@ -101,7 +111,7 @@ where
}
pub fn release_runtime() -> MlResult<()> {
- let mut guard = lock_runtime()?;
+ let mut guard = lock_runtime();
*guard = None;
Ok(())
}