Skip to content

armgit / ente

Public Imported from GitHub
0

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

Bundle custom CoreML ONNX Runtime static archive on iOS

laurensprieme7ebfa5769de
diff --git a/mobile/apps/photos/ios/Podfile.lock b/mobile/apps/photos/ios/Podfile.lock
index 1160a925c4..0a29ad64ed 100644
--- a/mobile/apps/photos/ios/Podfile.lock
+++ b/mobile/apps/photos/ios/Podfile.lock
@@ -466,7 +466,7 @@ SPEC CHECKSUMS:
connectivity_plus: cb623214f4e1f6ef8fe7403d580fdad517d2f7dd
cupertino_http: 94ac07f5ff090b8effa6c5e2c47871d48ab7c86c
device_info_plus: 21fcca2080fbcd348be798aa36c3e5ed849eefbe
- ente_photos_rust: 3ba148cb0582699855b6c154aecd41ca604ba1e8
+ ente_photos_rust: 740b0d5775f04a70cf5463316d20abd39cadb02a
ente_qr: 86beaf5d2644705db9b565b32dd8b82c05fb36af
ente_rust: d0197ebd5460e8f67576579452b073d698404abe
ente_screen_cover: 6792566774f1c83939f9c1d03ed0a1f1ded45ae4
diff --git a/mobile/apps/photos/rust_builder/cargokit/build_pod.sh b/mobile/apps/photos/rust_builder/cargokit/build_pod.sh
index 3714ee190d..9a360d7342 100755
--- a/mobile/apps/photos/rust_builder/cargokit/build_pod.sh
+++ b/mobile/apps/photos/rust_builder/cargokit/build_pod.sh
@@ -35,6 +35,19 @@ export CARGOKIT_TOOL_TEMP_DIR=$TARGET_TEMP_DIR/build_tool
# Directory inside root project. Not necessarily the top level directory of root project.
export CARGOKIT_ROOT_PROJECT_DIR=$SRCROOT
+case "$PLATFORM_NAME" in
+ iphoneos|iphonesimulator)
+ ONNXRUNTIME_CACHE_DIR=${ENTE_ORT_CACHE_DIR:-$HOME/Library/Caches/io.ente/onnxruntime}
+ ORT_LIB_PATH=$(
+ sh "$BASEDIR/../../../../native/onnxruntime/prepare-ios.sh" \
+ "$ONNXRUNTIME_CACHE_DIR" \
+ "$PLATFORM_NAME"
+ )
+ export ORT_LIB_PATH
+ unset ORT_IOS_XCFWK_PATH ORT_IOS_XCFWK_LOCATION
+ ;;
+esac
+
FLUTTER_EXPORT_BUILD_ENVIRONMENT=(
"$PODS_ROOT/../Flutter/ephemeral/flutter_export_environment.sh" # macOS
"$PODS_ROOT/../Flutter/flutter_export_environment.sh" # iOS
diff --git a/mobile/apps/photos/rust_builder/ios/ente_photos_rust.podspec b/mobile/apps/photos/rust_builder/ios/ente_photos_rust.podspec
index 0be03181a4..41bdcd0d21 100644
--- a/mobile/apps/photos/rust_builder/ios/ente_photos_rust.podspec
+++ b/mobile/apps/photos/rust_builder/ios/ente_photos_rust.podspec
@@ -20,7 +20,7 @@ A new Flutter FFI plugin project.
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
- s.platform = :ios, '11.0'
+ s.platform = :ios, '15.1'
# Flutter.framework does not contain an i386 slice, and ONNX Runtime 1.27
# does not publish an x86_64 iOS Simulator binary.
diff --git a/mobile/native/onnxruntime/README.md b/mobile/native/onnxruntime/README.md
new file mode 100644
index 0000000000..bac5381e23
--- /dev/null
+++ b/mobile/native/onnxruntime/README.md
@@ -0,0 +1,14 @@
+# Custom ONNX Runtime binaries
+
+The mobile apps use Ente's pinned custom ONNX Runtime 1.27.0 packaging build:
+
+- Release: `ort-1.27.0-webgpu-pilot.5`
+- Android: WebGPU, XNNPACK, and CPU; ARM64, ARMv7, and x86_64
+- iOS: CoreML and CPU; iOS 15.1+ device and ARM64 Simulator
+
+Android's AAR is downloaded by `prepare-android.sh` into the Gradle user cache.
+iOS's XCFramework ZIP is downloaded by `prepare-ios.sh`; the selected static
+archive slice is then bundled by the Rust build. Both downloads are pinned to
+the SHA-256 digests published with the release.
+
+Desktop ONNX Runtime packaging is intentionally unchanged.
diff --git a/mobile/native/onnxruntime/prepare-ios.sh b/mobile/native/onnxruntime/prepare-ios.sh
new file mode 100755
index 0000000000..1fa84bf78e
--- /dev/null
+++ b/mobile/native/onnxruntime/prepare-ios.sh
@@ -0,0 +1,100 @@
+#!/bin/sh
+set -eu
+
+asset_name="onnxruntime-coreml-ios-1.27.0-pilot.5.zip"
+asset_url="https://github.com/laurens-pilot/ort-packaging/releases/download/ort-1.27.0-webgpu-pilot.5/$asset_name"
+expected_sha256="fc820547f8e328ed501112be06b23d26329676d4e6a78978fc64971f8f05555d"
+
+cache_root=${1:?"usage: prepare-ios.sh <cache-root> <iphoneos|iphonesimulator>"}
+platform=${2:?"usage: prepare-ios.sh <cache-root> <iphoneos|iphonesimulator>"}
+
+case "$platform" in
+ iphoneos)
+ xcframework_slice="ios-arm64"
+ ;;
+ iphonesimulator)
+ xcframework_slice="ios-arm64-simulator"
+ ;;
+ *)
+ echo "Unsupported iOS platform: $platform" >&2
+ exit 1
+ ;;
+esac
+
+sha256_file() {
+ if command -v shasum >/dev/null 2>&1; then
+ shasum -a 256 "$1" | awk '{print $1}'
+ elif command -v sha256sum >/dev/null 2>&1; then
+ sha256sum "$1" | awk '{print $1}'
+ else
+ echo "A SHA-256 utility (shasum or sha256sum) is required" >&2
+ exit 1
+ fi
+}
+
+asset_cache="$cache_root/ort-1.27.0-webgpu-pilot.5"
+archive="$asset_cache/$asset_name"
+extract_root="$asset_cache/$expected_sha256"
+verified_marker="$extract_root/.verified"
+temporary_file=""
+temporary_extract=""
+temporary_library=""
+
+cleanup() {
+ if [ -n "$temporary_file" ]; then
+ rm -f "$temporary_file"
+ fi
+ if [ -n "$temporary_extract" ]; then
+ rm -rf "$temporary_extract"
+ fi
+ if [ -n "$temporary_library" ]; then
+ rm -f "$temporary_library"
+ fi
+}
+trap cleanup EXIT HUP INT TERM
+
+mkdir -p "$asset_cache"
+if [ ! -f "$archive" ] || [ "$(sha256_file "$archive")" != "$expected_sha256" ]; then
+ temporary_file="$archive.download.$$"
+ curl --fail --location --retry 3 --silent --show-error \
+ --output "$temporary_file" \
+ "$asset_url"
+
+ actual_sha256=$(sha256_file "$temporary_file")
+ if [ "$actual_sha256" != "$expected_sha256" ]; then
+ echo "SHA-256 mismatch for $asset_name: got $actual_sha256, expected $expected_sha256" >&2
+ exit 1
+ fi
+
+ mv "$temporary_file" "$archive"
+ temporary_file=""
+fi
+
+framework_binary="$extract_root/onnxruntime.xcframework/$xcframework_slice/onnxruntime.framework/onnxruntime"
+if [ ! -f "$verified_marker" ] || [ ! -f "$framework_binary" ]; then
+ temporary_extract="$extract_root.extract.$$"
+ mkdir -p "$temporary_extract"
+ unzip -q "$archive" -d "$temporary_extract"
+
+ extracted_binary="$temporary_extract/onnxruntime.xcframework/$xcframework_slice/onnxruntime.framework/onnxruntime"
+ if [ ! -f "$extracted_binary" ]; then
+ echo "The $xcframework_slice ONNX Runtime archive is missing from $asset_name" >&2
+ exit 1
+ fi
+
+ printf '%s\n' "$expected_sha256" > "$temporary_extract/.verified"
+ rm -rf "$extract_root"
+ mv "$temporary_extract" "$extract_root"
+ temporary_extract=""
+fi
+
+library_dir="$extract_root/static-lib/$xcframework_slice"
+mkdir -p "$library_dir"
+library="$library_dir/libonnxruntime.a"
+if [ ! -f "$library" ] || [ -L "$library" ]; then
+ temporary_library="$library.thin.$$"
+ xcrun lipo "$framework_binary" -thin arm64 -output "$temporary_library"
+ mv "$temporary_library" "$library"
+ temporary_library=""
+fi
+printf '%s\n' "$library_dir"
diff --git a/rust/crates/photos/Cargo.toml b/rust/crates/photos/Cargo.toml
index a432a71103..74814e4736 100644
--- a/rust/crates/photos/Cargo.toml
+++ b/rust/crates/photos/Cargo.toml
@@ -25,6 +25,7 @@ thiserror.workspace = true
# ORT source and revision are pinned once in the workspace manifest; only the
# per-target feature sets are declared here.
+# The iOS Cargokit build sets ORT_LIB_PATH to the verified custom static archive.
[target.'cfg(target_os = "ios")'.dependencies]
ort = { workspace = true, features = [
"std",
@@ -33,10 +34,6 @@ ort = { workspace = true, features = [
# CoreML is the preferred iOS execution provider for Rust ML sessions.
"coreml",
"api-27",
- # Link the revision-pinned crate's verified 1.27 archive directly into the
- # Rust ML library.
- "download-binaries",
- "tls-native",
] }
[target.'cfg(target_os = "android")'.dependencies]