Skip to content

mtmd : drop _shared from libmtmd name, merge helpers into libmtmd (⚠️ breaking change) #13917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 31, 2025

Conversation

ngxson
Copy link
Collaborator

@ngxson ngxson commented May 30, 2025

Supersede #13903

Small breaking changes:

@65a This is made to closely resemble how libllama is compiled:

  • no _shared prefix
  • no OBJECT when add_library
  • no STATIC declaration

So you should be able to link it exactly the same way as libllama. But please lmk if there are other problems.

@ggerganov I'm not very confident about cmake stuff, could you please take a look? Thanks.

@ngxson ngxson requested a review from ggerganov May 30, 2025 10:13
@JamePeng
Copy link

It is recommended to merge them into a dynamic library such as libmtmd.dll to avoid the last submission where mtmd and mtmd_helper were separated, resulting in the library function not being able to find the MTMD_API function in mtmd_helper.

@ggerganov
Copy link
Member

Here is how I think it should build:

diff --git a/tools/mtmd/CMakeLists.txt b/tools/mtmd/CMakeLists.txt
index edea27a63..382d82033 100644
--- a/tools/mtmd/CMakeLists.txt
+++ b/tools/mtmd/CMakeLists.txt
@@ -1,5 +1,7 @@
 # mtmd
 
+find_package(Threads REQUIRED)
+
 add_library(mtmd
             mtmd.cpp
             mtmd-audio.cpp
@@ -9,46 +11,50 @@ add_library(mtmd
             clip-impl.h
             )
 
-target_link_libraries(mtmd PRIVATE ggml llama ${CMAKE_THREAD_LIBS_INIT})
-target_include_directories(mtmd PUBLIC .)
+target_link_libraries     (mtmd PUBLIC ggml llama)
+target_link_libraries     (mtmd PRIVATE Threads::Threads)
+target_include_directories(mtmd PUBLIC  .)
 target_include_directories(mtmd PRIVATE ../..)
-target_compile_features(mtmd PRIVATE cxx_std_17)
+target_compile_features   (mtmd PRIVATE cxx_std_17)
+
+if (BUILD_SHARED_LIBS)
+    set_target_properties     (mtmd PROPERTIES POSITION_INDEPENDENT_CODE ON)
+    target_compile_definitions(mtmd PRIVATE LLAMA_BUILD)
+    target_compile_definitions(mtmd PUBLIC  LLAMA_SHARED)
+endif()
+
+set(MTMD_PUBLIC_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/mtmd.h)
+
+set_target_properties(mtmd
+    PROPERTIES
+    PUBLIC_HEADER "${MTMD_PUBLIC_HEADERS}")
+
+install(TARGETS mtmd LIBRARY PUBLIC_HEADER)
 
 # compile the helper separately, to avoid long compile times with miniaudio.h and stb_image.h
 
-add_library(mtmd_helper
+add_library(mtmd-helper STATIC
             mtmd-helper.cpp
             mtmd-helper.h
             )
 
-target_link_libraries(mtmd_helper PRIVATE ggml llama mtmd ${CMAKE_THREAD_LIBS_INIT})
-target_include_directories(mtmd_helper PUBLIC .)
-target_include_directories(mtmd_helper PRIVATE ./vendor)
-target_include_directories(mtmd_helper PRIVATE ../..)
-target_compile_features(mtmd_helper PRIVATE cxx_std_17)
+target_link_libraries     (mtmd-helper PRIVATE mtmd Threads::Threads)
+target_include_directories(mtmd-helper PUBLIC  .)
+target_include_directories(mtmd-helper PRIVATE ../..)
+target_compile_features   (mtmd-helper PRIVATE cxx_std_17)
 
 if (BUILD_SHARED_LIBS)
-    set_target_properties(mtmd PROPERTIES POSITION_INDEPENDENT_CODE ON)
-    target_compile_definitions(mtmd PRIVATE LLAMA_SHARED LLAMA_BUILD)
-    target_link_libraries(mtmd PRIVATE ggml llama ${CMAKE_THREAD_LIBS_INIT})
-    set_target_properties(mtmd PROPERTIES PUBLIC_HEADER "mtmd.h")
-    install(TARGETS mtmd LIBRARY PUBLIC_HEADER)
-
-    set_target_properties(mtmd_helper PROPERTIES POSITION_INDEPENDENT_CODE ON)
-    target_compile_definitions(mtmd_helper PRIVATE LLAMA_SHARED LLAMA_BUILD)
-    target_link_libraries(mtmd_helper PRIVATE ggml llama mtmd ${CMAKE_THREAD_LIBS_INIT})
-    set_target_properties(mtmd_helper PROPERTIES PUBLIC_HEADER "mtmd-helper.h")
-    install(TARGETS mtmd_helper LIBRARY PUBLIC_HEADER)
+    set_target_properties     (mtmd-helper PROPERTIES POSITION_INDEPENDENT_CODE ON)
 endif()
 
 if (NOT MSVC)
     # for stb_image.h and miniaudio.h
-    target_compile_options(mtmd_helper PRIVATE -Wno-cast-qual)
+    target_compile_options(mtmd-helper PRIVATE -Wno-cast-qual)
 endif()
 
-if(TARGET BUILD_INFO)
-    add_dependencies(mtmd BUILD_INFO)
-    add_dependencies(mtmd_helper BUILD_INFO)
+if (TARGET BUILD_INFO)
+    add_dependencies(mtmd        BUILD_INFO)
+    add_dependencies(mtmd-helper BUILD_INFO)
 endif()
 
 add_executable(llama-llava-cli    deprecation-warning.cpp)
@@ -57,8 +63,8 @@ add_executable(llama-minicpmv-cli deprecation-warning.cpp)
 add_executable(llama-qwen2vl-cli  deprecation-warning.cpp)
 
 set(TARGET llama-mtmd-cli)
-add_executable(${TARGET} mtmd-cli.cpp)
-set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME llama-mtmd-cli)
-install(TARGETS ${TARGET} RUNTIME)
-target_link_libraries(${TARGET} PRIVATE common mtmd mtmd_helper ${CMAKE_THREAD_LIBS_INIT})
+add_executable         (${TARGET} mtmd-cli.cpp)
+set_target_properties  (${TARGET} PROPERTIES OUTPUT_NAME llama-mtmd-cli)
+install                (TARGETS ${TARGET} RUNTIME)
+target_link_libraries  (${TARGET} PRIVATE common mtmd mtmd-helper Threads::Threads)
 target_compile_features(${TARGET} PRIVATE cxx_std_17)
diff --git a/tools/server/CMakeLists.txt b/tools/server/CMakeLists.txt
index 08597145c..00fdcaca6 100644
--- a/tools/server/CMakeLists.txt
+++ b/tools/server/CMakeLists.txt
@@ -36,7 +36,7 @@ install(TARGETS ${TARGET} RUNTIME)
 
 target_include_directories(${TARGET} PRIVATE ../llava)
 target_include_directories(${TARGET} PRIVATE ${CMAKE_SOURCE_DIR})
-target_link_libraries(${TARGET} PRIVATE common mtmd mtmd_helper ${CMAKE_THREAD_LIBS_INIT})
+target_link_libraries(${TARGET} PRIVATE common mtmd mtmd-helper ${CMAKE_THREAD_LIBS_INIT})
 
 if (LLAMA_SERVER_SSL)
     find_package(OpenSSL REQUIRED)

You want libmtmd-helper to always be statically linked - it's not supposed to be used by 3rd parties. Also added mtmd.h to be installed as a public header.

@ngxson
Copy link
Collaborator Author

ngxson commented May 30, 2025

You want libmtmd-helper to always be statically linked - it's not supposed to be used by 3rd parties. Also added mtmd.h to be installed as a public header.

The helper is supposed to be used by downstream projects. Without it, it would be quite difficult to convince developers to move to libmtmd. In other words, my idea is that helpers can be part of public API, but not part of the "stable" API.

On second thought, I think it will be less confusing for developers if I ship everything in one single lib, while still separate them internally.

I did that in ed4127c , does it looks good to you @ggerganov ?


Also, I realized that my assumption was not correct:

# compile the helper separately, to avoid long compile times with miniaudio.h and stb_image.h

Indeed, they are compiled as different objects:

[ 71%] Building CXX object tools/mtmd/CMakeFiles/mtmd.dir/mtmd.cpp.o
[ 75%] Building CXX object tools/mtmd/CMakeFiles/mtmd.dir/mtmd-audio.cpp.o
[ 78%] Building CXX object common/CMakeFiles/common.dir/arg.cpp.o
[ 81%] Building CXX object tools/mtmd/CMakeFiles/mtmd.dir/mtmd-helper.cpp.o

So for example, if I change the code in mtmd.cpp, the mtmd-helper.cpp won't be re-compiled. Therefore, I think I can revert my change earlier that separated them into 2 different libs

@ngxson ngxson changed the title mtmd : cmake of libmtmd now closely resemble libllama (⚠️ breaking change) mtmd : drop _shared prefix from libmtmdname (⚠️ breaking change) May 30, 2025
@ngxson ngxson changed the title mtmd : drop _shared prefix from libmtmdname (⚠️ breaking change) mtmd : drop _shared from libmtmdname, merge helpers into libmtmd (⚠️ breaking change) May 30, 2025
@ngxson ngxson changed the title mtmd : drop _shared from libmtmdname, merge helpers into libmtmd (⚠️ breaking change) mtmd : drop _shared from libmtmd name, merge helpers into libmtmd (⚠️ breaking change) May 30, 2025
@65a
Copy link
Contributor

65a commented May 30, 2025

Works perfectly from here. Thanks for the fix. Feel free to mark it as fixes 13902, I'll drop my pull.

@ngxson ngxson merged commit 51fa76f into ggml-org:master May 31, 2025
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants