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# 18cmake_minimum_required(VERSION 3.19) 19project(llava) 20 21# Duplicating options as root CMakeLists.txt 22option(EXECUTORCH_BUILD_KERNELS_OPTIMIZED "Build the optimized kernels" OFF) 23 24# This is a temporary hack to get around Torch dep so we can test this on android 25option(LLAVA_RUNNER_NO_TORCH_DUMMY_IMAGE "Hack option to feed dummy image to remove torch.load dep" OFF) 26 27include(CMakeDependentOption) 28# 29# pthreadpool: build pthreadpool library. Disable on unsupported platforms 30# 31cmake_dependent_option( 32 EXECUTORCH_BUILD_PTHREADPOOL "Build pthreadpool library." ON 33 "NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF 34) 35# 36# cpuinfo: build cpuinfo library. Disable on unsupported platforms 37# 38cmake_dependent_option( 39 EXECUTORCH_BUILD_CPUINFO "Build cpuinfo library." ON 40 "NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF 41) 42 43if(NOT PYTHON_EXECUTABLE) 44 set(PYTHON_EXECUTABLE python3) 45endif() 46 47set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..) 48 49include(${EXECUTORCH_ROOT}/build/Utils.cmake) 50 51if(NOT PYTHON_EXECUTABLE) 52 resolve_python_executable() 53endif() 54 55if(NOT CMAKE_CXX_STANDARD) 56 set(CMAKE_CXX_STANDARD 17) 57 # Can't set to 11 due to executor_runner.cpp make_unique 58endif() 59 60if(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$") 61 set(CMAKE_TOOLCHAIN_IOS ON) 62else() 63 set(CMAKE_TOOLCHAIN_IOS OFF) 64endif() 65 66set(_common_compile_options -Wno-deprecated-declarations -fPIC) 67 68# Let files say "include <executorch/path/to/header.h>". 69set(_common_include_directories ${EXECUTORCH_ROOT}/..) 70 71# For some reason android build is not able to find where gflags is and hence 72# cannot find corresponding .cmake file 73set(gflags_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../third-party/gflags) 74find_package(gflags REQUIRED) 75 76# Avoid torch dep from torch.load()-ing the image. 77# This is a temporary hack. 78if(LLAVA_RUNNER_NO_TORCH_DUMMY_IMAGE) 79 add_definitions(-DLLAVA_NO_TORCH_DUMMY_IMAGE=1) 80 message("Buidling the runner without Torch, feeding a dummy image!") 81else() 82 find_package(Torch CONFIG REQUIRED) 83endif() 84add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) 85 86# 87# llava_main: test binary to run llava, with tokenizer and sampler integrated 88# 89 90# find `executorch` libraries Same as for gflags 91set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../lib/cmake/ExecuTorch) 92find_package(executorch CONFIG REQUIRED) 93if(CMAKE_TOOLCHAIN_IOS OR ANDROID) 94 target_link_options_shared_lib(executorch) 95endif() 96 97# custom ops library 98if(EXECUTORCH_BUILD_KERNELS_CUSTOM) 99 add_subdirectory( 100 ${EXECUTORCH_ROOT}/extension/llm/custom_ops 101 ${CMAKE_CURRENT_BINARY_DIR}/../../../extension/llm/custom_ops 102 ) 103endif() 104 105# llava_runner library 106add_subdirectory(runner) 107 108set(LINK_LIBS gflags) 109if(NOT LLAVA_RUNNER_NO_TORCH_DUMMY_IMAGE) 110 list(APPEND LINK_LIBS torch) 111endif() 112set(link_libraries ${LINK_LIBS}) 113set(_srcs main.cpp) 114 115if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED) 116 list( 117 APPEND 118 link_libraries 119 optimized_native_cpu_ops_lib 120 optimized_kernels 121 portable_kernels 122 cpublas 123 eigen_blas 124 ) 125 target_link_options_shared_lib(optimized_native_cpu_ops_lib) 126else() 127 list(APPEND link_libraries portable_ops_lib portable_kernels) 128 target_link_options_shared_lib(portable_ops_lib) 129endif() 130 131# quantized_ops_lib: Register quantized op kernels into the runtime 132target_link_options_shared_lib(quantized_ops_lib) 133list(APPEND link_libraries quantized_kernels quantized_ops_lib) 134 135if(EXECUTORCH_BUILD_KERNELS_CUSTOM) 136 target_link_options_shared_lib(custom_ops) 137 list(APPEND link_libraries custom_ops) 138endif() 139 140set(XNNPACK_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack) 141# Extra compile option and include dir for pthreadpool 142if(EXECUTORCH_BUILD_PTHREADPOOL) 143 list(APPEND _common_compile_options -DET_USE_THREADPOOL) 144 list(APPEND link_libraries extension_threadpool pthreadpool) 145 list(APPEND _common_include_directories 146 ${XNNPACK_ROOT}/third-party/pthreadpool/include 147 ) 148endif() 149 150# Extra sources for cpuinfo 151if(EXECUTORCH_BUILD_CPUINFO) 152 list(APPEND link_libraries extension_threadpool cpuinfo) 153 list(APPEND _common_include_directories 154 ${XNNPACK_ROOT}/third-party/cpuinfo/include 155 ) 156endif() 157 158# XNNPACK 159if(TARGET xnnpack_backend) 160 set(xnnpack_backend_libs xnnpack_backend XNNPACK microkernels-prod) 161 if(TARGET kleidiai) 162 list(APPEND xnnpack_backend_libs kleidiai) 163 endif() 164 list(APPEND link_libraries ${xnnpack_backend_libs}) 165 target_link_options_shared_lib(xnnpack_backend) 166endif() 167 168# Vulkan backend 169if(TARGET vulkan_backend) 170 list(APPEND link_libraries vulkan_backend) 171 target_link_options_shared_lib(vulkan_backend) 172endif() 173 174# Qnn backend 175if(TARGET qnn_executorch_backend) 176 list(APPEND link_libraries qnn_executorch_backend) 177 target_link_options_shared_lib(qnn_executorch_backend) 178endif() 179 180# MPS backend 181if(TARGET mpsdelegate) 182 list( 183 APPEND 184 link_libraries 185 mpsdelegate 186 "-framework Foundation" 187 "-weak_framework MetalPerformanceShaders" 188 "-weak_framework MetalPerformanceShadersGraph" 189 "-weak_framework Metal" 190 ) 191 target_link_options_shared_lib(mpsdelegate) 192endif() 193 194if(TARGET coremldelegate) 195 find_library(SQLITE_LIBRARY sqlite3) 196 list( 197 APPEND 198 link_libraries 199 coremldelegate 200 sqlite3 201 "-framework Foundation" 202 "-framework CoreML" 203 "-framework Accelerate" 204 ) 205 target_link_options_shared_lib(coremldelegate) 206endif() 207 208# This one is needed for cpuinfo where it uses android specific log lib 209if(ANDROID) 210 list(APPEND link_libraries log) 211endif() 212 213add_executable(llava_main ${_srcs}) 214if(CMAKE_BUILD_TYPE STREQUAL "Release") 215 if(APPLE) 216 target_link_options(llava_main PRIVATE "LINKER:-dead_strip,-s") 217 else() 218 target_link_options(llava_main PRIVATE "LINKER:--gc-sections,-s") 219 endif() 220endif() 221 222target_include_directories(llava_main PUBLIC ${_common_include_directories}) 223target_link_libraries(llava_main PUBLIC llava_runner ${link_libraries}) 224target_compile_options(llava_main PUBLIC ${_common_compile_options}) 225 226if(APPLE) 227 target_link_options_shared_lib(executorch) 228endif() 229 230# Print all summary 231executorch_print_configuration_summary() 232