|
| 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(llava) |
| 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 | + |
| 47 | +include(${EXECUTORCH_ROOT}/build/Utils.cmake) |
| 48 | + |
| 49 | +if(NOT PYTHON_EXECUTABLE) |
| 50 | + resolve_python_executable() |
| 51 | +endif() |
| 52 | + |
| 53 | +if(NOT CMAKE_CXX_STANDARD) |
| 54 | + set(CMAKE_CXX_STANDARD 17) |
| 55 | + # Can't set to 11 due to executor_runner.cpp make_unique |
| 56 | +endif() |
| 57 | + |
| 58 | +if(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$") |
| 59 | + set(CMAKE_TOOLCHAIN_IOS ON) |
| 60 | +else() |
| 61 | + set(CMAKE_TOOLCHAIN_IOS OFF) |
| 62 | +endif() |
| 63 | + |
| 64 | +set(_common_compile_options -Wno-deprecated-declarations -fPIC) |
| 65 | + |
| 66 | +# Let files say "include <executorch/path/to/header.h>". |
| 67 | +set(_common_include_directories ${EXECUTORCH_ROOT}/..) |
| 68 | + |
| 69 | +# For some reason android build is not able to find where gflags is and hence |
| 70 | +# cannot find corresponding .cmake file |
| 71 | +set(gflags_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../third-party/gflags) |
| 72 | +find_package(gflags REQUIRED) |
| 73 | + |
| 74 | +find_package(Torch CONFIG REQUIRED) |
| 75 | +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) |
| 76 | + |
| 77 | +# |
| 78 | +# llava_main: test binary to run llava, with tokenizer and sampler integrated |
| 79 | +# |
| 80 | + |
| 81 | +# find `executorch` libraries Same as for gflags |
| 82 | +set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../lib/cmake/ExecuTorch) |
| 83 | +find_package(executorch CONFIG REQUIRED) |
| 84 | +if(CMAKE_TOOLCHAIN_IOS OR ANDROID) |
| 85 | + target_link_options_shared_lib(executorch) |
| 86 | +endif() |
| 87 | + |
| 88 | +# custom ops library |
| 89 | +if(EXECUTORCH_BUILD_KERNELS_CUSTOM) |
| 90 | + add_subdirectory( |
| 91 | + ${EXECUTORCH_ROOT}/extension/llm/custom_ops |
| 92 | + ${CMAKE_CURRENT_BINARY_DIR}/../../../extension/llm/custom_ops |
| 93 | + ) |
| 94 | +endif() |
| 95 | + |
| 96 | +# llava_runner library |
| 97 | +add_subdirectory(runner) |
| 98 | + |
| 99 | +set(link_libraries gflags torch) |
| 100 | +set(_srcs main.cpp) |
| 101 | + |
| 102 | +if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED) |
| 103 | + list( |
| 104 | + APPEND |
| 105 | + link_libraries |
| 106 | + optimized_native_cpu_ops_lib |
| 107 | + optimized_kernels |
| 108 | + portable_kernels |
| 109 | + cpublas |
| 110 | + eigen_blas |
| 111 | + ) |
| 112 | + target_link_options_shared_lib(optimized_native_cpu_ops_lib) |
| 113 | +else() |
| 114 | + list(APPEND link_libraries portable_ops_lib portable_kernels) |
| 115 | + target_link_options_shared_lib(portable_ops_lib) |
| 116 | +endif() |
| 117 | + |
| 118 | +# quantized_ops_lib: Register quantized op kernels into the runtime |
| 119 | +target_link_options_shared_lib(quantized_ops_lib) |
| 120 | +list(APPEND link_libraries quantized_kernels quantized_ops_lib) |
| 121 | + |
| 122 | +if(EXECUTORCH_BUILD_KERNELS_CUSTOM) |
| 123 | + target_link_options_shared_lib(custom_ops) |
| 124 | + list(APPEND link_libraries custom_ops) |
| 125 | +endif() |
| 126 | + |
| 127 | +set(XNNPACK_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack) |
| 128 | +# Extra compile option and include dir for pthreadpool |
| 129 | +if(EXECUTORCH_BUILD_PTHREADPOOL) |
| 130 | + list(APPEND _common_compile_options -DET_USE_THREADPOOL) |
| 131 | + list(APPEND link_libraries pthreadpool) |
| 132 | + # These 2 source files are included in xnnpack_backend |
| 133 | + if(NOT TARGET xnnpack_backend) |
| 134 | + list(APPEND _srcs ${XNNPACK_ROOT}/threadpool/threadpool.cpp |
| 135 | + ${XNNPACK_ROOT}/threadpool/threadpool_guard.cpp |
| 136 | + ) |
| 137 | + endif() |
| 138 | + list(APPEND _common_include_directories |
| 139 | + ${XNNPACK_ROOT}/third-party/pthreadpool/include |
| 140 | + ) |
| 141 | +endif() |
| 142 | + |
| 143 | +# Extra sources for cpuinfo |
| 144 | +if(EXECUTORCH_BUILD_CPUINFO) |
| 145 | + list(APPEND link_libraries cpuinfo) |
| 146 | + list(APPEND _srcs ${XNNPACK_ROOT}/threadpool/cpuinfo_utils.cpp) |
| 147 | + list(APPEND _common_include_directories |
| 148 | + ${XNNPACK_ROOT}/third-party/cpuinfo/include |
| 149 | + ) |
| 150 | +endif() |
| 151 | + |
| 152 | +# XNNPACK |
| 153 | +if(TARGET xnnpack_backend) |
| 154 | + set(xnnpack_backend_libs xnnpack_backend XNNPACK) |
| 155 | + list(APPEND link_libraries ${xnnpack_backend_libs}) |
| 156 | + target_link_options_shared_lib(xnnpack_backend) |
| 157 | +endif() |
| 158 | + |
| 159 | +# Vulkan backend |
| 160 | +if(TARGET vulkan_backend) |
| 161 | + list(APPEND link_libraries vulkan_backend) |
| 162 | + target_link_options_shared_lib(vulkan_backend) |
| 163 | +endif() |
| 164 | + |
| 165 | +# Qnn backend |
| 166 | +if(TARGET qnn_executorch_backend) |
| 167 | + list(APPEND link_libraries qnn_executorch_backend) |
| 168 | + target_link_options_shared_lib(qnn_executorch_backend) |
| 169 | +endif() |
| 170 | + |
| 171 | +# MPS backend |
| 172 | +if(TARGET mpsdelegate) |
| 173 | + list( |
| 174 | + APPEND |
| 175 | + link_libraries |
| 176 | + mpsdelegate |
| 177 | + "-framework Foundation" |
| 178 | + "-weak_framework MetalPerformanceShaders" |
| 179 | + "-weak_framework MetalPerformanceShadersGraph" |
| 180 | + "-weak_framework Metal" |
| 181 | + ) |
| 182 | + target_link_options_shared_lib(mpsdelegate) |
| 183 | +endif() |
| 184 | + |
| 185 | +if(TARGET coremldelegate) |
| 186 | + find_library(SQLITE_LIBRARY sqlite3) |
| 187 | + list( |
| 188 | + APPEND |
| 189 | + link_libraries |
| 190 | + coremldelegate |
| 191 | + sqlite3 |
| 192 | + "-framework Foundation" |
| 193 | + "-framework CoreML" |
| 194 | + "-framework Accelerate" |
| 195 | + ) |
| 196 | + target_link_options_shared_lib(coremldelegate) |
| 197 | +endif() |
| 198 | + |
| 199 | +# This one is needed for cpuinfo where it uses android specific log lib |
| 200 | +if(ANDROID) |
| 201 | + list(APPEND link_libraries log) |
| 202 | +endif() |
| 203 | + |
| 204 | +add_executable(llava_main ${_srcs}) |
| 205 | +if(CMAKE_BUILD_TYPE STREQUAL "Release") |
| 206 | + target_link_options(llava_main PRIVATE "LINKER:--gc-sections,-s") |
| 207 | +endif() |
| 208 | + |
| 209 | +target_include_directories(llava_main PUBLIC ${_common_include_directories}) |
| 210 | +target_link_libraries(llava_main PUBLIC llava_runner ${link_libraries}) |
| 211 | +target_compile_options(llava_main PUBLIC ${_common_compile_options}) |
| 212 | + |
| 213 | +if(APPLE) |
| 214 | + target_link_options_shared_lib(executorch) |
| 215 | +endif() |
| 216 | + |
| 217 | +# Print all summary |
| 218 | +executorch_print_configuration_summary() |
0 commit comments