Skip to content

armgit / ente

Public Imported from GitHub
0

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

Remove unnecessary mtmd patching

Manav Rathi64982045b9ff
diff --git a/.github/workflows/ensu-desktop-lint.yml b/.github/workflows/ensu-desktop-lint.yml
index 7622eae77e..6d2bca8119 100644
--- a/.github/workflows/ensu-desktop-lint.yml
+++ b/.github/workflows/ensu-desktop-lint.yml
@@ -52,10 +52,6 @@ jobs:
- name: Fetch Rust dependencies
run: cargo fetch
- - name: Patch llama.cpp mtmd sources
- shell: bash
- run: ../../../ensu/inference/tool/patch_llama_mtmd.sh
-
- name: Ensure Tauri distDir exists for macro checks
run: mkdir -p ../../../../web/apps/ensu/out
diff --git a/.github/workflows/ensu-release.yml b/.github/workflows/ensu-release.yml
index 0c858ae39f..4f499d72ed 100644
--- a/.github/workflows/ensu-release.yml
+++ b/.github/workflows/ensu-release.yml
@@ -109,10 +109,6 @@ jobs:
working-directory: rust/apps/ensu/src-tauri
run: cargo fetch
- - name: Patch llama.cpp mtmd sources
- shell: bash
- run: rust/ensu/inference/tool/patch_llama_mtmd.sh
-
- name: Install trusted-signing-cli (Windows only)
if: matrix.platform.os == 'windows-latest' && env.HAS_WINDOWS_SIGNING == 'true'
shell: bash
diff --git a/mobile/native/android/apps/ensu/README.md b/mobile/native/android/apps/ensu/README.md
index c13ac49c5a..51ea757957 100644
--- a/mobile/native/android/apps/ensu/README.md
+++ b/mobile/native/android/apps/ensu/README.md
@@ -7,7 +7,6 @@
- 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)
## Quick scripts
@@ -33,7 +32,7 @@ Run `./build.sh --help` or `./run.sh --help` for full options.
### 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. The build script applies the llama.cpp mtmd patch; set `APPLY_LLAMA_MTMD_PATCH=0` to skip it.
+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.
```bash
cd mobile/native/android/packages/rust/tool
diff --git a/mobile/native/android/packages/rust/tool/build_android.sh b/mobile/native/android/packages/rust/tool/build_android.sh
index 21fab39461..8bb0698c0b 100755
--- a/mobile/native/android/packages/rust/tool/build_android.sh
+++ b/mobile/native/android/packages/rust/tool/build_android.sh
@@ -7,9 +7,6 @@ CORE_UNIFFI_RUST_DIR="$REPO_ROOT/rust/uniffi/core"
INFERENCE_UNIFFI_RUST_DIR="$REPO_ROOT/rust/uniffi/ensu/inference"
CHATDB_UNIFFI_RUST_DIR="$REPO_ROOT/rust/uniffi/ensu/db"
CHAT_SYNC_UNIFFI_RUST_DIR="$REPO_ROOT/rust/uniffi/ensu/sync"
-# Patch llama.cpp mtmd sources so mmproj models load correctly.
-PATCH_SCRIPT="$REPO_ROOT/rust/ensu/inference/tool/patch_llama_mtmd.sh"
-APPLY_LLAMA_MTMD_PATCH="${APPLY_LLAMA_MTMD_PATCH:-1}"
OUT_DIR="$ROOT/src/main/jniLibs"
CORE_KOTLIN_OUT_DIR="$REPO_ROOT/mobile/native/android/apps/ensu/crypto-auth-core/src/main/java"
RUST_KOTLIN_OUT_DIR="$ROOT/src/main/kotlin"
@@ -88,15 +85,6 @@ 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"
export NDK_ROOT="$NDK_ROOT_PATH"
diff --git a/mobile/native/darwin/Apps/Ensu/scripts/build-rust.sh b/mobile/native/darwin/Apps/Ensu/scripts/build-rust.sh
index f5da771c87..602b332e89 100755
--- a/mobile/native/darwin/Apps/Ensu/scripts/build-rust.sh
+++ b/mobile/native/darwin/Apps/Ensu/scripts/build-rust.sh
@@ -17,7 +17,6 @@ fi
REPO_ROOT="$(cd "${SRCROOT}/../../../../.." && pwd)"
OUT_DIR="${TARGET_TEMP_DIR}/ensu_rust"
GENERATED_DIR="${SRCROOT}/Ensu/Generated"
-PATCH_SCRIPT="${REPO_ROOT}/rust/ensu/inference/tool/patch_llama_mtmd.sh"
mkdir -p "${OUT_DIR}"
mkdir -p "${GENERATED_DIR}"
@@ -215,11 +214,6 @@ HOST_SDKROOT="$(xcrun --sdk macosx --show-sdk-path)"
# Ensure host builds (build scripts) use the macOS SDK.
export SDKROOT="${HOST_SDKROOT}"
-if [ -f "${PATCH_SCRIPT}" ]; then
- echo "Applying llama mtmd patch..."
- bash "${PATCH_SCRIPT}"
-fi
-
ensure_uniffi_bindgen
ensure_generated_bindings
write_endpoint_config
diff --git a/rust/ensu/inference/tool/patch_llama_mtmd.sh b/rust/ensu/inference/tool/patch_llama_mtmd.sh
deleted file mode 100755
index baf82ef31b..0000000000
--- a/rust/ensu/inference/tool/patch_llama_mtmd.sh
+++ /dev/null
@@ -1,190 +0,0 @@
-#!/usr/bin/env bash
-set -euo pipefail
-
-PYTHON_BIN="${PYTHON_BIN:-python3}"
-if ! command -v "$PYTHON_BIN" >/dev/null 2>&1; then
- PYTHON_BIN=python
-fi
-
-ROOT="$(cd "$(dirname "$0")/.." && pwd)"
-CRATE_DIR="$(
- "$PYTHON_BIN" - <<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
- 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
-
-PY_CRATE_DIR="$CRATE_DIR"
-if command -v cygpath >/dev/null 2>&1; then
- PY_CRATE_DIR="$(cygpath -w "$CRATE_DIR")"
-fi
-
-TOOLS_DIR="$CRATE_DIR/llama.cpp/tools"
-MTMD_DIR="$TOOLS_DIR/mtmd"
-CMAKE_FILE="$TOOLS_DIR/CMakeLists.txt"
-VENDOR_DIR="$CRATE_DIR/llama.cpp/vendor"
-
-if [[ ! -d "$MTMD_DIR" ]]; then
- echo "mtmd tools directory not found at $MTMD_DIR" >&2
- exit 0
-fi
-
-if [[ ! -f "$CMAKE_FILE" ]]; then
- cat > "$CMAKE_FILE" <<'EOF'
-# Auto-generated by inference_rs_dart to enable mtmd sources in llama.cpp builds.
-if (LLAMA_BUILD_TOOLS)
- if (EXISTS "${CMAKE_CURRENT_LIST_DIR}/mtmd")
- set(LLAMA_MTMD_SOURCES
- ${CMAKE_CURRENT_LIST_DIR}/mtmd/clip.cpp
- ${CMAKE_CURRENT_LIST_DIR}/mtmd/mtmd.cpp
- ${CMAKE_CURRENT_LIST_DIR}/mtmd/mtmd-audio.cpp
- ${CMAKE_CURRENT_LIST_DIR}/mtmd/mtmd-helper.cpp
- )
- target_sources(llama PRIVATE ${LLAMA_MTMD_SOURCES})
- target_include_directories(llama PRIVATE
- ${CMAKE_CURRENT_LIST_DIR}/mtmd
- ${CMAKE_CURRENT_LIST_DIR}/../vendor
- )
- endif()
-endif()
-EOF
- echo "Patched $CMAKE_FILE for mtmd."
-fi
-
-CLIP_CPP="$MTMD_DIR/clip.cpp"
-if [[ -f "$CLIP_CPP" ]]; then
- PY_CRATE_DIR="$PY_CRATE_DIR" "$PYTHON_BIN" - <<PY
-import os
-from pathlib import Path
-import sys
-
-crate_dir = os.environ["PY_CRATE_DIR"]
-root = Path(crate_dir) / "llama.cpp" / "tools" / "mtmd"
-
-patches = [
- {
- "label": "legacy clip.cpp LFM2/KIMIVL optional norm tensors",
- "relpath": "clip.cpp",
- "old": """ case PROJECTOR_TYPE_LFM2:\n case PROJECTOR_TYPE_KIMIVL:\n {\n model.mm_input_norm_w = get_tensor(TN_MM_INP_NORM);\n model.mm_input_norm_b = get_tensor(TN_MM_INP_NORM_B);\n model.mm_1_w = get_tensor(string_format(TN_LLAVA_PROJ, 1, \"weight\"));\n model.mm_1_b = get_tensor(string_format(TN_LLAVA_PROJ, 1, \"bias\"));\n model.mm_2_w = get_tensor(string_format(TN_LLAVA_PROJ, 2, \"weight\"));\n model.mm_2_b = get_tensor(string_format(TN_LLAVA_PROJ, 2, \"bias\"));\n } break;\n""",
- "new": """ case PROJECTOR_TYPE_LFM2:\n case PROJECTOR_TYPE_KIMIVL:\n {\n model.mm_input_norm_w = get_tensor(TN_MM_INP_NORM, false);\n model.mm_input_norm_b = get_tensor(TN_MM_INP_NORM_B, false);\n model.mm_1_w = get_tensor(string_format(TN_LLAVA_PROJ, 1, \"weight\"));\n model.mm_1_b = get_tensor(string_format(TN_LLAVA_PROJ, 1, \"bias\"));\n model.mm_2_w = get_tensor(string_format(TN_LLAVA_PROJ, 2, \"weight\"));\n model.mm_2_b = get_tensor(string_format(TN_LLAVA_PROJ, 2, \"bias\"));\n } break;\n""",
- },
- {
- "label": "legacy clip.cpp siglip norm guard",
- "relpath": "clip.cpp",
- "old": """ cur = ggml_norm(ctx0, cur, 1e-5); // default nn.LayerNorm\n cur = ggml_mul(ctx0, cur, model.mm_input_norm_w);\n cur = ggml_add(ctx0, cur, model.mm_input_norm_b);\n\n cur = ggml_mul_mat(ctx0, model.mm_1_w, cur);\n""",
- "new": """ cur = ggml_norm(ctx0, cur, 1e-5); // default nn.LayerNorm\n if (model.mm_input_norm_w && model.mm_input_norm_b) {\n cur = ggml_mul(ctx0, cur, model.mm_input_norm_w);\n cur = ggml_add(ctx0, cur, model.mm_input_norm_b);\n }\n\n cur = ggml_mul_mat(ctx0, model.mm_1_w, cur);\n""",
- },
- {
- "label": "legacy clip.cpp kimivl norm guard",
- "relpath": "clip.cpp",
- "old": """ cur = ggml_norm(ctx0, cur, 1e-5); // default nn.LayerNorm\n cur = ggml_mul(ctx0, cur, model.mm_input_norm_w);\n cur = ggml_add(ctx0, cur, model.mm_input_norm_b);\n cur = ggml_view_2d(ctx0, cur,\n""",
- "new": """ cur = ggml_norm(ctx0, cur, 1e-5); // default nn.LayerNorm\n if (model.mm_input_norm_w && model.mm_input_norm_b) {\n cur = ggml_mul(ctx0, cur, model.mm_input_norm_w);\n cur = ggml_add(ctx0, cur, model.mm_input_norm_b);\n }\n cur = ggml_view_2d(ctx0, cur,\n""",
- },
- {
- "label": "legacy clip.cpp pixtral rms norm guard",
- "relpath": "clip.cpp",
- "old": """ cur = ggml_mul(ctx0, ggml_rms_norm(ctx0, cur, eps), model.mm_input_norm_w);\n""",
- "new": """ cur = ggml_rms_norm(ctx0, cur, eps);\n if (model.mm_input_norm_w) {\n cur = ggml_mul(ctx0, cur, model.mm_input_norm_w);\n }\n""",
- },
- {
- "label": "modern clip.cpp KIMI projector optional norm tensors",
- "relpath": "clip.cpp",
- "old": """ case PROJECTOR_TYPE_KIMIVL:\n case PROJECTOR_TYPE_PADDLEOCR:\n case PROJECTOR_TYPE_KIMIK25:\n {\n model.mm_input_norm_w = get_tensor(TN_MM_INP_NORM);\n model.mm_input_norm_b = get_tensor(TN_MM_INP_NORM_B);\n model.mm_1_w = get_tensor(string_format(TN_LLAVA_PROJ, 1, \"weight\"));\n model.mm_1_b = get_tensor(string_format(TN_LLAVA_PROJ, 1, \"bias\"));\n model.mm_2_w = get_tensor(string_format(TN_LLAVA_PROJ, 2, \"weight\"));\n model.mm_2_b = get_tensor(string_format(TN_LLAVA_PROJ, 2, \"bias\"));\n } break;\n""",
- "new": """ case PROJECTOR_TYPE_KIMIVL:\n case PROJECTOR_TYPE_PADDLEOCR:\n case PROJECTOR_TYPE_KIMIK25:\n {\n model.mm_input_norm_w = get_tensor(TN_MM_INP_NORM, false);\n model.mm_input_norm_b = get_tensor(TN_MM_INP_NORM_B, false);\n model.mm_1_w = get_tensor(string_format(TN_LLAVA_PROJ, 1, \"weight\"));\n model.mm_1_b = get_tensor(string_format(TN_LLAVA_PROJ, 1, \"bias\"));\n model.mm_2_w = get_tensor(string_format(TN_LLAVA_PROJ, 2, \"weight\"));\n model.mm_2_b = get_tensor(string_format(TN_LLAVA_PROJ, 2, \"bias\"));\n } break;\n""",
- },
- {
- "label": "modern kimivl projector norm guard",
- "relpath": "models/kimivl.cpp",
- "old": """ cur = ggml_norm(ctx0, cur, 1e-5); // default nn.LayerNorm\n cur = ggml_mul(ctx0, cur, model.mm_input_norm_w);\n cur = ggml_add(ctx0, cur, model.mm_input_norm_b);\n""",
- "new": """ cur = build_norm(cur,\n model.mm_input_norm_w, model.mm_input_norm_b,\n NORM_TYPE_NORMAL, 1e-5, -1);\n""",
- },
- {
- "label": "modern kimik25 projector norm guard",
- "relpath": "models/kimik25.cpp",
- "old": """ cur = ggml_norm(ctx0, cur, hparams.eps);\n cur = ggml_mul(ctx0, cur, model.mm_input_norm_w);\n cur = ggml_add(ctx0, cur, model.mm_input_norm_b);\n""",
- "new": """ cur = build_norm(cur,\n model.mm_input_norm_w, model.mm_input_norm_b,\n NORM_TYPE_NORMAL, hparams.eps, -1);\n""",
- },
- {
- "label": "modern pixtral projector rms norm guard",
- "relpath": "models/pixtral.cpp",
- "old": """ cur = ggml_mul(ctx0, ggml_rms_norm(ctx0, cur, eps), model.mm_input_norm_w);\n""",
- "new": """ cur = build_norm(cur,\n model.mm_input_norm_w, nullptr,\n NORM_TYPE_RMS, eps, -1);\n""",
- },
-]
-
-applied = []
-already = []
-missing = []
-changed_files = set()
-
-for patch in patches:
- path = root / patch["relpath"]
- if not path.exists():
- missing.append(f'{patch["label"]} (missing file: {path})')
- continue
-
- text = path.read_text(encoding="utf-8", errors="ignore")
- old = patch["old"]
- new = patch["new"]
-
- if new in text:
- already.append(patch["label"])
- continue
-
- if old in text:
- updated = text.replace(old, new)
- if updated != text:
- path.write_text(updated, encoding="utf-8")
- applied.append(patch["label"])
- changed_files.add(path)
- else:
- already.append(patch["label"])
- else:
- missing.append(patch["label"])
-
-marker = Path(crate_dir) / "llama.cpp" / "CMakeMtmdPatch.txt"
-needs_rebuild = bool(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 sources ({len(applied)} replacements).")
-else:
- print("mtmd sources already patched or no compatible patch needed.")
-
-if missing:
- sys.stderr.write("mtmd patch: some patterns were not found (upstream may have changed):\n")
- for item in missing:
- sys.stderr.write(f" - {item}\n")
-PY
-fi