1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ # All rights reserved.
3
+ #
4
+ # This source code is licensed under the BSD-style license found in the
5
+ # LICENSE file in the root directory of this source tree.
6
+
7
+ #
8
+ # Simple CMake build system for llava runner.
9
+ #
10
+ # ### Editing this file ###
11
+ #
12
+ # This file should be formatted with
13
+ # ~~~
14
+ # cmake-format -i CMakeLists.txt
15
+ # ~~~
16
+ # It should also be cmake-lint clean.
17
+ #
18
+ cmake_minimum_required (VERSION 3.19 )
19
+ project (multimodal )
20
+
21
+ # Duplicating options as root CMakeLists.txt
22
+ option (EXECUTORCH_BUILD_KERNELS_OPTIMIZED "Build the optimized kernels" OFF )
23
+
24
+
25
+ include (CMakeDependentOption )
26
+ #
27
+ # pthreadpool: build pthreadpool library. Disable on unsupported platforms
28
+ #
29
+ cmake_dependent_option (
30
+ EXECUTORCH_BUILD_PTHREADPOOL "Build pthreadpool library." ON
31
+ "NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF
32
+ )
33
+ #
34
+ # cpuinfo: build cpuinfo library. Disable on unsupported platforms
35
+ #
36
+ cmake_dependent_option (
37
+ EXECUTORCH_BUILD_CPUINFO "Build cpuinfo library." ON
38
+ "NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF
39
+ )
40
+
41
+ if (NOT PYTHON_EXECUTABLE )
42
+ set (PYTHON_EXECUTABLE python3 )
43
+ endif ()
44
+
45
+ set (EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR} /../../.. )
46
+ set (TORCH_ROOT ${EXECUTORCH_ROOT} /third-party/pytorch )
47
+
48
+ include (${EXECUTORCH_ROOT} /build/Utils.cmake )
49
+
50
+ if (NOT PYTHON_EXECUTABLE )
51
+ resolve_python_executable ()
52
+ endif ()
53
+
54
+ if (NOT CMAKE_CXX_STANDARD )
55
+ set (CMAKE_CXX_STANDARD 17 )
56
+ # Can't set to 11 due to executor_runner.cpp make_unique
57
+ endif ()
58
+
59
+ if (CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\. toolchain)\. cmake$" )
60
+ set (CMAKE_TOOLCHAIN_IOS ON )
61
+ else ()
62
+ set (CMAKE_TOOLCHAIN_IOS OFF )
63
+ endif ()
64
+
65
+ set (_common_compile_options -Wno-deprecated-declarations -fPIC )
66
+
67
+ # Let files say "include <executorch/path/to/header.h>".
68
+ set (_common_include_directories ${EXECUTORCH_ROOT} /.. )
69
+
70
+ # For some reason android build is not able to find where gflags is and hence
71
+ # cannot find corresponding .cmake file
72
+ set (gflags_DIR ${CMAKE_CURRENT_BINARY_DIR} /../../../third-party/gflags )
73
+ find_package (gflags REQUIRED )
74
+
75
+ find_package (Torch CONFIG REQUIRED )
76
+ add_definitions (-D_GLIBCXX_USE_CXX11_ABI=0 )
77
+
78
+ #
79
+ # llava_main: test binary to run llava, with tokenizer and sampler integrated
80
+ #
81
+
82
+ # find `executorch` libraries Same as for gflags
83
+ set (executorch_DIR ${CMAKE_CURRENT_BINARY_DIR} /../../../lib/cmake/ExecuTorch )
84
+ find_package (executorch CONFIG REQUIRED )
85
+ if (CMAKE_TOOLCHAIN_IOS OR ANDROID )
86
+ target_link_options_shared_lib (executorch )
87
+ endif ()
88
+
89
+ # custom ops library
90
+ if (EXECUTORCH_BUILD_KERNELS_CUSTOM )
91
+ add_subdirectory (
92
+ ${EXECUTORCH_ROOT} /extension/llm/custom_ops
93
+ ${CMAKE_CURRENT_BINARY_DIR} /../../../extension/llm/custom_ops
94
+ )
95
+ endif ()
96
+
97
+ # llava_runner library
98
+ add_subdirectory (runner )
99
+
100
+ set (link_libraries gflags torch )
101
+ set (_srcs main.cpp )
102
+
103
+ if (EXECUTORCH_BUILD_KERNELS_OPTIMIZED )
104
+ list (
105
+ APPEND
106
+ link_libraries
107
+ optimized_native_cpu_ops_lib
108
+ optimized_kernels
109
+ portable_kernels
110
+ cpublas
111
+ eigen_blas
112
+ )
113
+ target_link_options_shared_lib (optimized_native_cpu_ops_lib )
114
+ else ()
115
+ list (APPEND link_libraries portable_ops_lib portable_kernels )
116
+ target_link_options_shared_lib (portable_ops_lib )
117
+ endif ()
118
+
119
+ # quantized_ops_lib: Register quantized op kernels into the runtime
120
+ target_link_options_shared_lib (quantized_ops_lib )
121
+ list (APPEND link_libraries quantized_kernels quantized_ops_lib )
122
+
123
+ if (EXECUTORCH_BUILD_KERNELS_CUSTOM )
124
+ target_link_options_shared_lib (custom_ops )
125
+ list (APPEND link_libraries custom_ops )
126
+ endif ()
127
+
128
+ set (XNNPACK_ROOT ${CMAKE_CURRENT_SOURCE_DIR} /../../../backends/xnnpack )
129
+ # Extra compile option and include dir for pthreadpool
130
+ if (EXECUTORCH_BUILD_PTHREADPOOL )
131
+ list (APPEND _common_compile_options -DET_USE_THREADPOOL )
132
+ list (APPEND link_libraries pthreadpool )
133
+ # These 2 source files are included in xnnpack_backend
134
+ if (NOT TARGET xnnpack_backend )
135
+ list (APPEND _srcs ${XNNPACK_ROOT} /threadpool/threadpool.cpp
136
+ ${XNNPACK_ROOT} /threadpool/threadpool_guard.cpp
137
+ )
138
+ endif ()
139
+ list (APPEND _common_include_directories
140
+ ${XNNPACK_ROOT} /third-party/pthreadpool/include
141
+ )
142
+ endif ()
143
+
144
+ # Extra sources for cpuinfo
145
+ if (EXECUTORCH_BUILD_CPUINFO )
146
+ list (APPEND link_libraries cpuinfo )
147
+ list (APPEND _srcs ${XNNPACK_ROOT} /threadpool/cpuinfo_utils.cpp )
148
+ list (APPEND _common_include_directories
149
+ ${XNNPACK_ROOT} /third-party/cpuinfo/include
150
+ )
151
+ endif ()
152
+
153
+ # XNNPACK
154
+ if (TARGET xnnpack_backend )
155
+ set (xnnpack_backend_libs xnnpack_backend XNNPACK )
156
+ list (APPEND link_libraries ${xnnpack_backend_libs} )
157
+ target_link_options_shared_lib (xnnpack_backend )
158
+ endif ()
159
+
160
+ # Vulkan backend
161
+ if (TARGET vulkan_backend )
162
+ list (APPEND link_libraries vulkan_backend )
163
+ target_link_options_shared_lib (vulkan_backend )
164
+ endif ()
165
+
166
+ # Qnn backend
167
+ if (TARGET qnn_executorch_backend )
168
+ list (APPEND link_libraries qnn_executorch_backend )
169
+ target_link_options_shared_lib (qnn_executorch_backend )
170
+ endif ()
171
+
172
+ # MPS backend
173
+ if (TARGET mpsdelegate )
174
+ list (
175
+ APPEND
176
+ link_libraries
177
+ mpsdelegate
178
+ "-framework Foundation"
179
+ "-weak_framework MetalPerformanceShaders"
180
+ "-weak_framework MetalPerformanceShadersGraph"
181
+ "-weak_framework Metal"
182
+ )
183
+ target_link_options_shared_lib (mpsdelegate )
184
+ endif ()
185
+
186
+ if (TARGET coremldelegate )
187
+ find_library (SQLITE_LIBRARY sqlite3 )
188
+ list (
189
+ APPEND
190
+ link_libraries
191
+ coremldelegate
192
+ sqlite3
193
+ "-framework Foundation"
194
+ "-framework CoreML"
195
+ "-framework Accelerate"
196
+ )
197
+ target_link_options_shared_lib (coremldelegate )
198
+ endif ()
199
+
200
+ # This one is needed for cpuinfo where it uses android specific log lib
201
+ if (ANDROID )
202
+ list (APPEND link_libraries log )
203
+ endif ()
204
+
205
+ add_executable (llava_main ${_srcs} )
206
+ if (CMAKE_BUILD_TYPE STREQUAL "Release" )
207
+ target_link_options (llava_main PRIVATE "LINKER:--gc-sections,-s" )
208
+ endif ()
209
+
210
+ target_include_directories (llava_main PUBLIC ${_common_include_directories} )
211
+ target_link_libraries (llava_main PUBLIC llava_runner ${link_libraries} )
212
+ target_compile_options (llava_main PUBLIC ${_common_compile_options} )
213
+
214
+ if (APPLE )
215
+ target_link_options_shared_lib (executorch )
216
+ endif ()
217
+
218
+ # Print all summary
219
+ executorch_print_configuration_summary ()
0 commit comments