Skip to content

armgit / ente

Public Imported from GitHub
0

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

update rust patch and readme

Prateek Sunal7d3937e7cd4f
diff --git a/mobile/native/android/apps/ensu/README.md b/mobile/native/android/apps/ensu/README.md
index d65a242408..e29ee67067 100644
--- a/mobile/native/android/apps/ensu/README.md
+++ b/mobile/native/android/apps/ensu/README.md
@@ -7,12 +7,13 @@
- Android SDK with API level 35 (install in a default location or set `ANDROID_HOME`/`ANDROID_SDK_ROOT`)
- Android NDK (install via `sdkmanager "ndk;<version>"`; optionally set `NDK_VERSION` to select a specific version)
- Rust toolchain and `cargo-ndk` (`cargo install cargo-ndk`)
+- Python 3 (used to patch llama.cpp mtmd sources)
## Building
### 1. Build Rust Libraries
-Before building the Android app, compile the Rust native libraries. Ensure the SDK is installed in a default location or set `ANDROID_HOME`/`ANDROID_SDK_ROOT` to the SDK containing an NDK. Optionally set `NDK_VERSION` to select a specific version.
+Before building the Android app, compile the Rust native libraries. Ensure the SDK is installed in a default location or set `ANDROID_HOME`/`ANDROID_SDK_ROOT` to the SDK containing an NDK. Optionally set `NDK_VERSION` to select a specific version. The build script applies the llama.cpp mtmd patch; set `APPLY_LLAMA_MTMD_PATCH=0` to skip it.
```bash
cd mobile/native/android/packages/rust/tool
diff --git a/mobile/native/android/apps/ensu/app-ui/build.gradle.kts b/mobile/native/android/apps/ensu/app-ui/build.gradle.kts
index 22c332a0fe..95ab9ed473 100644
--- a/mobile/native/android/apps/ensu/app-ui/build.gradle.kts
+++ b/mobile/native/android/apps/ensu/app-ui/build.gradle.kts
@@ -47,7 +47,7 @@ android {
applicationId = "io.ente.ensu"
minSdk = 23
targetSdk = 35
- versionCode = 6
+ versionCode = 7
versionName = "0.1.2"
buildConfigField("String", "API_ENDPOINT", "\"$apiEndpointOverride\"")
}
diff --git a/mobile/native/android/packages/rust/tool/build_android.sh b/mobile/native/android/packages/rust/tool/build_android.sh
index 67b0b75689..679bbbdc9e 100755
--- a/mobile/native/android/packages/rust/tool/build_android.sh
+++ b/mobile/native/android/packages/rust/tool/build_android.sh
@@ -6,8 +6,9 @@ REPO_ROOT="$(cd "$ROOT/../../../../.." && pwd)"
INFERENCE_UNIFFI_RUST_DIR="$REPO_ROOT/rust/inference_rs_uniffi"
CHATDB_UNIFFI_RUST_DIR="$REPO_ROOT/rust/llmchat_db_uniffi"
CHAT_SYNC_UNIFFI_RUST_DIR="$REPO_ROOT/rust/llmchat_sync_uniffi"
-# Note: Older builds used to patch llama-cpp sources; this repo no longer ships that patch script.
-PATCH_SCRIPT=""
+# Patch llama.cpp mtmd sources so mmproj models load correctly.
+PATCH_SCRIPT="$REPO_ROOT/rust/inference_rs/tool/patch_llama_mtmd.sh"
+APPLY_LLAMA_MTMD_PATCH="${APPLY_LLAMA_MTMD_PATCH:-1}"
OUT_DIR="$ROOT/src/main/jniLibs"
SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}}"
NDK_VERSION="${NDK_VERSION:-}"
@@ -40,6 +41,14 @@ if [[ -z "$NDK_ROOT_PATH" || ! -d "$NDK_ROOT_PATH" ]]; then
exit 1
fi
+if [[ "$APPLY_LLAMA_MTMD_PATCH" != "0" && -f "$PATCH_SCRIPT" ]]; then
+ if ! command -v python3 >/dev/null 2>&1; then
+ echo "python3 is required to apply the llama mtmd patch." >&2
+ exit 1
+ fi
+ echo "Applying llama mtmd patch..."
+ bash "$PATCH_SCRIPT"
+fi
export ANDROID_NDK="$NDK_ROOT_PATH"
export ANDROID_NDK_ROOT="$NDK_ROOT_PATH"
diff --git a/mobile/native/darwin/Packages/Rust/README.md b/mobile/native/darwin/Packages/Rust/README.md
index 175e106eed..629fdddffc 100644
--- a/mobile/native/darwin/Packages/Rust/README.md
+++ b/mobile/native/darwin/Packages/Rust/README.md
@@ -14,6 +14,9 @@ cd swift
This writes the Swift sources plus the UniFFI header/modulemap into
`swift/Sources/InferenceRS`.
+Note: The script applies the llama.cpp mtmd patch (requires `python3`). Set
+`APPLY_LLAMA_MTMD_PATCH=0` to skip it.
+
## Build the xcframework
From the repo root:
@@ -26,6 +29,9 @@ cd swift
This produces `swift/InferenceRSFFI.xcframework`, which the Swift package
references as a binary target.
+Note: The build applies the llama.cpp mtmd patch (requires `python3`). Set
+`APPLY_LLAMA_MTMD_PATCH=0` to skip it.
+
## Use in an app (SwiftPM)
Add the package to your app and import `InferenceRS`:
diff --git a/mobile/native/darwin/Packages/Rust/tool/build_xcframework.sh b/mobile/native/darwin/Packages/Rust/tool/build_xcframework.sh
index cd6a10c807..48710938ab 100755
--- a/mobile/native/darwin/Packages/Rust/tool/build_xcframework.sh
+++ b/mobile/native/darwin/Packages/Rust/tool/build_xcframework.sh
@@ -6,9 +6,17 @@ REPO_ROOT="$(cd "$ROOT/../../../../.." && pwd)"
RUST_DIR="$ROOT/rust"
CORE_ROOT="$REPO_ROOT/rust/inference_rs"
PATCH_SCRIPT="$CORE_ROOT/tool/patch_llama_mtmd.sh"
+APPLY_LLAMA_MTMD_PATCH="${APPLY_LLAMA_MTMD_PATCH:-1}"
LIB_NAME="libinference_rs_uniffi.a"
-"$PATCH_SCRIPT"
+if [[ "$APPLY_LLAMA_MTMD_PATCH" != "0" && -f "$PATCH_SCRIPT" ]]; then
+ if ! command -v python3 >/dev/null 2>&1; then
+ echo "python3 is required to apply the llama mtmd patch." >&2
+ exit 1
+ fi
+ echo "Applying llama mtmd patch..."
+ bash "$PATCH_SCRIPT"
+fi
"$ROOT/tool/generate_bindings.sh"
diff --git a/mobile/native/darwin/Packages/Rust/tool/generate_bindings.sh b/mobile/native/darwin/Packages/Rust/tool/generate_bindings.sh
index edb43f9418..b06200f41f 100755
--- a/mobile/native/darwin/Packages/Rust/tool/generate_bindings.sh
+++ b/mobile/native/darwin/Packages/Rust/tool/generate_bindings.sh
@@ -6,9 +6,17 @@ REPO_ROOT="$(cd "$ROOT/../../../../.." && pwd)"
RUST_DIR="$ROOT/rust"
CORE_ROOT="$REPO_ROOT/rust/inference_rs"
PATCH_SCRIPT="$CORE_ROOT/tool/patch_llama_mtmd.sh"
+APPLY_LLAMA_MTMD_PATCH="${APPLY_LLAMA_MTMD_PATCH:-1}"
OUT_DIR="$ROOT/Sources/InferenceRS"
-"$PATCH_SCRIPT"
+if [[ "$APPLY_LLAMA_MTMD_PATCH" != "0" && -f "$PATCH_SCRIPT" ]]; then
+ if ! command -v python3 >/dev/null 2>&1; then
+ echo "python3 is required to apply the llama mtmd patch." >&2
+ exit 1
+ fi
+ echo "Applying llama mtmd patch..."
+ bash "$PATCH_SCRIPT"
+fi
cargo build --manifest-path "$RUST_DIR/Cargo.toml" --release
diff --git a/rust/inference_rs/tool/patch_llama_mtmd.sh b/rust/inference_rs/tool/patch_llama_mtmd.sh
index ce1850e278..2df45e2593 100755
--- a/rust/inference_rs/tool/patch_llama_mtmd.sh
+++ b/rust/inference_rs/tool/patch_llama_mtmd.sh
@@ -2,12 +2,38 @@
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
-VERSION=$(grep -E "llama-cpp-2" "$ROOT/Cargo.toml" | head -n 1 | grep -Eo 'version *= *"[^"]+"|"[0-9]+\.[0-9]+\.[0-9]+"' | head -n 1 | sed -E 's/.*"([^"]+)".*/\1/')
-CRATE_DIR=$(ls -d "$HOME/.cargo/registry/src/"*/llama-cpp-sys-2-$VERSION 2>/dev/null | head -n 1 || true)
+CRATE_DIR="$(
+ python3 - <<PY
+import json
+import os
+import subprocess
+import sys
+from pathlib import Path
+
+root = os.path.abspath("${ROOT}")
+try:
+ meta = subprocess.check_output(["cargo", "metadata", "--format-version", "1"], cwd=root)
+except Exception:
+ sys.exit(1)
+
+data = json.loads(meta)
+for pkg in data.get("packages", []):
+ if pkg.get("name") == "llama-cpp-sys-2":
+ print(Path(pkg["manifest_path"]).parent)
+ sys.exit(0)
+
+sys.exit(1)
+PY
+)" || true
if [[ -z "$CRATE_DIR" ]]; then
- echo "llama-cpp-sys-2 $VERSION not found in cargo registry" >&2
- exit 0
+ VERSION=$(grep -E "llama-cpp-2" "$ROOT/Cargo.toml" | head -n 1 | grep -Eo 'version *= *"[^"]+"|"[0-9]+\.[0-9]+\.[0-9]+"' | head -n 1 | sed -E 's/.*"([^"]+)".*/\1/' | sed -E 's/^=//')
+ CRATE_DIR=$(ls -d "$HOME/.cargo/registry/src/"*/llama-cpp-sys-2-$VERSION 2>/dev/null | head -n 1 || true)
+fi
+
+if [[ -z "$CRATE_DIR" ]]; then
+ echo "llama-cpp-sys-2 not found in cargo metadata or registry. Run 'cargo fetch' in $ROOT and try again." >&2
+ exit 1
fi
TOOLS_DIR="$CRATE_DIR/llama.cpp/tools"
@@ -46,6 +72,8 @@ CLIP_CPP="$MTMD_DIR/clip.cpp"
if [[ -f "$CLIP_CPP" ]]; then
python3 - <<PY
from pathlib import Path
+import sys
+
path = Path("$CLIP_CPP")
text = path.read_text()
@@ -57,10 +85,37 @@ replacements = {
}
updated = text
+applied = []
+missing = []
+
for old, new in replacements.items():
- updated = updated.replace(old, new)
+ if old in text:
+ updated = updated.replace(old, new)
+ applied.append(old)
+ elif new in text:
+ continue
+ else:
+ missing.append(old)
if updated != text:
path.write_text(updated)
+
+if missing:
+ sys.stderr.write("mtmd clip.cpp patch patterns not found; llama.cpp may have changed.\n")
+ sys.stderr.write(f"File: {path}\n")
+ sys.exit(1)
+
+marker = Path("$CRATE_DIR") / "llama.cpp" / "CMakeMtmdPatch.txt"
+needs_rebuild = applied or not marker.exists()
+if needs_rebuild:
+ marker.write_text("# mtmd patch applied\n")
+ cmake_root = Path("$CRATE_DIR") / "llama.cpp" / "CMakeLists.txt"
+ if cmake_root.exists():
+ cmake_root.touch()
+
+if applied:
+ print(f"Patched mtmd clip.cpp ({len(applied)} replacements).")
+else:
+ print("mtmd clip.cpp already patched.")
PY
fi