Skip to content

Commit 9ad4497

Browse files
committed
use a patch
1 parent 6bfa8b7 commit 9ad4497

5 files changed

Lines changed: 134 additions & 1 deletion

File tree

.github/workflows/build.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ jobs:
3838
java-version: '21'
3939
distribution: 'temurin'
4040

41+
- name: Apply llama mtmd patch
42+
shell: bash
43+
run: |
44+
bash script/apply-llama-mtmd-patch
45+
4146
- name: Cache embedding model
4247
uses: actions/cache@v4
4348
with:

patches/llama-mtmd-build.patch

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
diff --git a/CMakeLists.txt b/CMakeLists.txt
2+
index 69da97dc1..e904df95b 100644
3+
--- a/CMakeLists.txt
4+
+++ b/CMakeLists.txt
5+
@@ -106,6 +106,8 @@ option(LLAMA_BUILD_COMMON "llama: build common utils library" ${LLAMA_STANDALONE
6+
# extra artifacts
7+
option(LLAMA_BUILD_TESTS "llama: build tests" ${LLAMA_STANDALONE})
8+
option(LLAMA_BUILD_TOOLS "llama: build tools" ${LLAMA_STANDALONE})
9+
+option(LLAMA_BUILD_MTMD "llama: build multimodal library" ${LLAMA_STANDALONE})
10+
+option(LLAMA_BUILD_MTMD_CLI "llama: build multimodal CLI tools" ${LLAMA_STANDALONE})
11+
option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE})
12+
option(LLAMA_BUILD_SERVER "llama: build server example" ${LLAMA_STANDALONE})
13+
option(LLAMA_TOOLS_INSTALL "llama: install tools" ${LLAMA_TOOLS_INSTALL_DEFAULT})
14+
@@ -212,6 +214,8 @@ endif()
15+
16+
if (LLAMA_BUILD_COMMON AND LLAMA_BUILD_TOOLS)
17+
add_subdirectory(tools)
18+
+elseif (LLAMA_BUILD_MTMD)
19+
+ add_subdirectory(tools/mtmd)
20+
endif()
21+
22+
# Automatically add all files from the 'licenses' directory
23+
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
24+
index b433c91d8..eaa538127 100644
25+
--- a/tools/CMakeLists.txt
26+
+++ b/tools/CMakeLists.txt
27+
@@ -28,7 +28,9 @@ else()
28+
add_subdirectory(tokenize)
29+
add_subdirectory(parser)
30+
add_subdirectory(tts)
31+
- add_subdirectory(mtmd)
32+
+ if (LLAMA_BUILD_MTMD)
33+
+ add_subdirectory(mtmd)
34+
+ endif()
35+
if (GGML_RPC)
36+
add_subdirectory(rpc)
37+
endif()
38+
diff --git a/tools/mtmd/CMakeLists.txt b/tools/mtmd/CMakeLists.txt
39+
index 3be3c27e8..2c76babd5 100644
40+
--- a/tools/mtmd/CMakeLists.txt
41+
+++ b/tools/mtmd/CMakeLists.txt
42+
@@ -62,7 +62,7 @@ set_target_properties(mtmd
43+
PROPERTIES
44+
PUBLIC_HEADER "${MTMD_PUBLIC_HEADERS}")
45+
46+
-install(TARGETS mtmd LIBRARY PUBLIC_HEADER)
47+
+install(TARGETS mtmd LIBRARY ARCHIVE PUBLIC_HEADER)
48+
49+
if (NOT MSVC)
50+
# for stb_image.h and miniaudio.h
51+
@@ -71,7 +71,6 @@ endif()
52+
53+
if (TARGET BUILD_INFO)
54+
add_dependencies(mtmd BUILD_INFO)
55+
- add_dependencies(mtmd-helper BUILD_INFO)
56+
endif()
57+
58+
# if mtmd is linked against common, we throw an error
59+
@@ -83,16 +82,22 @@ if (TARGET mtmd)
60+
endif()
61+
endif()
62+
63+
-add_executable(llama-llava-cli deprecation-warning.cpp)
64+
-add_executable(llama-gemma3-cli deprecation-warning.cpp)
65+
-add_executable(llama-minicpmv-cli deprecation-warning.cpp)
66+
-add_executable(llama-qwen2vl-cli deprecation-warning.cpp)
67+
+if (LLAMA_BUILD_MTMD_CLI)
68+
+ if (NOT TARGET common)
69+
+ message(FATAL_ERROR "llama-mtmd-cli requires the common target; enable LLAMA_BUILD_COMMON or disable LLAMA_BUILD_MTMD_CLI")
70+
+ endif()
71+
72+
-set(TARGET llama-mtmd-cli)
73+
-add_executable (${TARGET} mtmd-cli.cpp)
74+
-set_target_properties (${TARGET} PROPERTIES OUTPUT_NAME llama-mtmd-cli)
75+
-if(LLAMA_TOOLS_INSTALL)
76+
- install(TARGETS ${TARGET} RUNTIME)
77+
+ add_executable(llama-llava-cli deprecation-warning.cpp)
78+
+ add_executable(llama-gemma3-cli deprecation-warning.cpp)
79+
+ add_executable(llama-minicpmv-cli deprecation-warning.cpp)
80+
+ add_executable(llama-qwen2vl-cli deprecation-warning.cpp)
81+
+
82+
+ set(TARGET llama-mtmd-cli)
83+
+ add_executable (${TARGET} mtmd-cli.cpp)
84+
+ set_target_properties (${TARGET} PROPERTIES OUTPUT_NAME llama-mtmd-cli)
85+
+ if(LLAMA_TOOLS_INSTALL)
86+
+ install(TARGETS ${TARGET} RUNTIME)
87+
+ endif()
88+
+ target_link_libraries (${TARGET} PRIVATE common mtmd Threads::Threads)
89+
+ target_compile_features(${TARGET} PRIVATE cxx_std_17)
90+
endif()
91+
-target_link_libraries (${TARGET} PRIVATE common mtmd Threads::Threads)
92+
-target_compile_features(${TARGET} PRIVATE cxx_std_17)

script/apply-llama-mtmd-patch

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
ROOT_DIR=$(
6+
CDPATH= cd -- "$(dirname "$0")/.." && pwd
7+
)
8+
9+
SUBMODULE_DIR="$ROOT_DIR/src/llama.cpp"
10+
PATCH_FILE="$ROOT_DIR/patches/llama-mtmd-build.patch"
11+
12+
[ -d "$SUBMODULE_DIR/.git" ] || [ -f "$SUBMODULE_DIR/.git" ] || {
13+
echo "Missing llama.cpp submodule checkout at $SUBMODULE_DIR" >&2
14+
exit 1
15+
}
16+
17+
[ -f "$PATCH_FILE" ] || {
18+
echo "Missing patch file: $PATCH_FILE" >&2
19+
exit 1
20+
}
21+
22+
if git -C "$SUBMODULE_DIR" apply --reverse --check "$PATCH_FILE" >/dev/null 2>&1; then
23+
echo "llama.cpp mtmd patch already applied"
24+
exit 0
25+
fi
26+
27+
git -C "$SUBMODULE_DIR" apply --check "$PATCH_FILE"
28+
git -C "$SUBMODULE_DIR" apply "$PATCH_FILE"
29+
echo "Applied llama.cpp mtmd patch"

script/build-freebsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ cmake \
9696
-D LLAMA_BUILD_COMMON=OFF \
9797
-D LLAMA_BUILD_TESTS=OFF \
9898
-D LLAMA_BUILD_TOOLS=OFF \
99+
-D LLAMA_BUILD_MTMD=ON \
100+
-D LLAMA_BUILD_MTMD_CLI=OFF \
99101
-D LLAMA_BUILD_EXAMPLES=OFF \
100102
-D LLAMA_BUILD_SERVER=OFF \
101103
-B build_release

src/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ lib_lmdb := lmdb/libraries/liblmdb
22
lib_usearch := usearch
33
lib_llama := llama.cpp
44
lib_dtlv := libdtlv
5+
mtmd_static := $(firstword $(wildcard $(lib_llama)/build_release/tools/mtmd/libmtmd.a))
56
libraries := $(lib_lmdb) $(lib_usearch) $(lib_llama) $(lib_dtlv)$(SOEXT)
67

78
CPPFLAGS += -Illama.cpp/include -Illama.cpp/ggml/include -Illama.cpp/tools/mtmd
@@ -22,7 +23,11 @@ $(lib_llama):
2223
cp $(lib_llama)/build_release/ggml/src/libggml.a ./libggml.a
2324
cp $(lib_llama)/build_release/ggml/src/libggml-base.a ./libggml-base.a
2425
cp $(lib_llama)/build_release/ggml/src/libggml-cpu.a ./libggml-cpu.a
25-
cp $(lib_llama)/build_release/tools/mtmd/libmtmd.a ./libmtmd.a
26+
test -n "$(mtmd_static)" || { \
27+
echo "Missing $(lib_llama)/build_release/tools/mtmd/libmtmd.a. Build llama.cpp with mtmd enabled before running make." >&2; \
28+
exit 1; \
29+
}
30+
cp $(mtmd_static) ./libmtmd.a
2631

2732
$(lib_dtlv)$(SOEXT): $(lib_lmdb) $(lib_usearch) $(lib_llama)
2833
$(CC) $(CPPFLAGS) -pthread -fPIC -O2 -Wall -g -c dtlv.c -o dtlv.o

0 commit comments

Comments
 (0)