CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. cmake_minimum_required(VERSION 3.12) # Don't bump this version for no reason
  2. project("llama.cpp" C CXX)
  3. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  4. if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE)
  5. set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
  6. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
  7. endif()
  8. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  9. if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  10. set(LLAMA_STANDALONE ON)
  11. # configure project version
  12. # TODO
  13. else()
  14. set(LLAMA_STANDALONE OFF)
  15. endif()
  16. if (EMSCRIPTEN)
  17. set(BUILD_SHARED_LIBS_DEFAULT OFF)
  18. option(LLAMA_WASM_SINGLE_FILE "llama: embed WASM inside the generated llama.js" ON)
  19. else()
  20. if (MINGW)
  21. set(BUILD_SHARED_LIBS_DEFAULT OFF)
  22. else()
  23. set(BUILD_SHARED_LIBS_DEFAULT ON)
  24. endif()
  25. endif()
  26. #
  27. # Option list
  28. #
  29. # general
  30. option(LLAMA_STATIC "llama: static link libraries" OFF)
  31. option(LLAMA_NATIVE "llama: enable -march=native flag" OFF)
  32. option(LLAMA_LTO "llama: enable link time optimization" OFF)
  33. # debug
  34. option(LLAMA_ALL_WARNINGS "llama: enable all compiler warnings" ON)
  35. option(LLAMA_ALL_WARNINGS_3RD_PARTY "llama: enable all compiler warnings in 3rd party libs" OFF)
  36. option(LLAMA_GPROF "llama: enable gprof" OFF)
  37. # sanitizers
  38. option(LLAMA_SANITIZE_THREAD "llama: enable thread sanitizer" OFF)
  39. option(LLAMA_SANITIZE_ADDRESS "llama: enable address sanitizer" OFF)
  40. option(LLAMA_SANITIZE_UNDEFINED "llama: enable undefined sanitizer" OFF)
  41. # instruction set specific
  42. option(LLAMA_AVX "llama: enable AVX" ON)
  43. option(LLAMA_AVX2 "llama: enable AVX2" ON)
  44. option(LLAMA_AVX512 "llama: enable AVX512" OFF)
  45. option(LLAMA_AVX512_VBMI "llama: enable AVX512-VBMI" OFF)
  46. option(LLAMA_AVX512_VNNI "llama: enable AVX512-VNNI" OFF)
  47. option(LLAMA_FMA "llama: enable FMA" ON)
  48. # in MSVC F16C is implied with AVX2/AVX512
  49. if (NOT MSVC)
  50. option(LLAMA_F16C "llama: enable F16C" ON)
  51. endif()
  52. # 3rd party libs
  53. option(LLAMA_ACCELERATE "llama: enable Accelerate framework" ON)
  54. option(LLAMA_BLAS "llama: use BLAS" OFF)
  55. option(LLAMA_BLAS_VENDOR "llama: BLA_VENDOR from https://cmake.org/cmake/help/latest/module/FindBLAS.html#blas-lapack-vendors" Generic)
  56. option(LLAMA_CUBLAS "llama: use cuBLAS" OFF)
  57. option(LLAMA_CLBLAST "llama: use CLBlast" OFF)
  58. option(LLAMA_BUILD_TESTS "llama: build tests" ${LLAMA_STANDALONE})
  59. option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE})
  60. option(LLAMA_BUILD_SERVER "llama: build server example" OFF)
  61. #
  62. # Build info header
  63. #
  64. # Generate initial build-info.h
  65. include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake)
  66. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
  67. set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.git")
  68. # Is git submodule
  69. if(NOT IS_DIRECTORY "${GIT_DIR}")
  70. file(READ ${GIT_DIR} REAL_GIT_DIR_LINK)
  71. string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" REAL_GIT_DIR ${REAL_GIT_DIR_LINK})
  72. set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${REAL_GIT_DIR}")
  73. endif()
  74. # Add a custom target for build-info.h
  75. add_custom_target(BUILD_INFO ALL DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h")
  76. # Add a custom command to rebuild build-info.h when .git/index changes
  77. add_custom_command(
  78. OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h"
  79. COMMENT "Generating build details from Git"
  80. COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake"
  81. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  82. DEPENDS "${GIT_DIR}/index"
  83. VERBATIM
  84. )
  85. else()
  86. message(WARNING "Git repository not found; to enable automatic generation of build info, make sure Git is installed and the project is a Git repository.")
  87. endif()
  88. #
  89. # Compile flags
  90. #
  91. set(CMAKE_CXX_STANDARD 11)
  92. set(CMAKE_CXX_STANDARD_REQUIRED true)
  93. set(CMAKE_C_STANDARD 11)
  94. set(CMAKE_C_STANDARD_REQUIRED true)
  95. set(THREADS_PREFER_PTHREAD_FLAG ON)
  96. find_package(Threads REQUIRED)
  97. if (NOT MSVC)
  98. if (LLAMA_SANITIZE_THREAD)
  99. add_compile_options(-fsanitize=thread)
  100. link_libraries(-fsanitize=thread)
  101. endif()
  102. if (LLAMA_SANITIZE_ADDRESS)
  103. add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
  104. link_libraries(-fsanitize=address)
  105. endif()
  106. if (LLAMA_SANITIZE_UNDEFINED)
  107. add_compile_options(-fsanitize=undefined)
  108. link_libraries(-fsanitize=undefined)
  109. endif()
  110. endif()
  111. if (APPLE AND LLAMA_ACCELERATE)
  112. find_library(ACCELERATE_FRAMEWORK Accelerate)
  113. if (ACCELERATE_FRAMEWORK)
  114. message(STATUS "Accelerate framework found")
  115. add_compile_definitions(GGML_USE_ACCELERATE)
  116. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK})
  117. else()
  118. message(WARNING "Accelerate framework not found")
  119. endif()
  120. endif()
  121. if (LLAMA_BLAS)
  122. if (LLAMA_STATIC)
  123. set(BLA_STATIC ON)
  124. endif()
  125. if ($(CMAKE_VERSION) VERSION_GREATER_EQUAL 3.22)
  126. set(BLA_SIZEOF_INTEGER 8)
  127. endif()
  128. set(BLA_VENDOR ${LLAMA_BLAS_VENDOR})
  129. find_package(BLAS)
  130. if (BLAS_FOUND)
  131. message(STATUS "BLAS found, Libraries: ${BLAS_LIBRARIES}")
  132. add_compile_options(${BLAS_LINKER_FLAGS})
  133. add_compile_definitions(GGML_USE_OPENBLAS)
  134. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${BLAS_LIBRARIES})
  135. message("${BLAS_LIBRARIES} ${BLAS_INCLUDE_DIRS}")
  136. include_directories(${BLAS_INCLUDE_DIRS})
  137. else()
  138. message(WARNING "BLAS not found, please refer to "
  139. "https://cmake.org/cmake/help/latest/module/FindBLAS.html#blas-lapack-vendors"
  140. " to set correct LLAMA_BLAS_VENDOR")
  141. endif()
  142. endif()
  143. if (LLAMA_CUBLAS)
  144. cmake_minimum_required(VERSION 3.17)
  145. find_package(CUDAToolkit)
  146. if (CUDAToolkit_FOUND)
  147. message(STATUS "cuBLAS found")
  148. enable_language(CUDA)
  149. set(GGML_CUDA_SOURCES ggml-cuda.cu ggml-cuda.h)
  150. add_compile_definitions(GGML_USE_CUBLAS)
  151. if (LLAMA_STATIC)
  152. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} CUDA::cudart_static CUDA::cublas_static CUDA::cublasLt_static)
  153. else()
  154. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} CUDA::cudart CUDA::cublas CUDA::cublasLt)
  155. endif()
  156. else()
  157. message(WARNING "cuBLAS not found")
  158. endif()
  159. endif()
  160. if (LLAMA_CLBLAST)
  161. find_package(CLBlast)
  162. if (CLBlast_FOUND)
  163. message(STATUS "CLBlast found")
  164. set(GGML_OPENCL_SOURCES ggml-opencl.cpp ggml-opencl.h)
  165. add_compile_definitions(GGML_USE_CLBLAST)
  166. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} clblast)
  167. else()
  168. message(WARNING "CLBlast not found")
  169. endif()
  170. endif()
  171. if (LLAMA_ALL_WARNINGS)
  172. if (NOT MSVC)
  173. set(c_flags
  174. -Wall
  175. -Wextra
  176. -Wpedantic
  177. -Wcast-qual
  178. -Wdouble-promotion
  179. -Wshadow
  180. -Wstrict-prototypes
  181. -Wpointer-arith
  182. )
  183. set(cxx_flags
  184. -Wall
  185. -Wextra
  186. -Wpedantic
  187. -Wcast-qual
  188. -Wno-unused-function
  189. -Wno-multichar
  190. )
  191. else()
  192. # todo : msvc
  193. endif()
  194. add_compile_options(
  195. "$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
  196. "$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
  197. )
  198. endif()
  199. if (MSVC)
  200. add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
  201. if (BUILD_SHARED_LIBS)
  202. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  203. endif()
  204. endif()
  205. if (LLAMA_LTO)
  206. include(CheckIPOSupported)
  207. check_ipo_supported(RESULT result OUTPUT output)
  208. if (result)
  209. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
  210. else()
  211. message(WARNING "IPO is not supported: ${output}")
  212. endif()
  213. endif()
  214. # Architecture specific
  215. # TODO: probably these flags need to be tweaked on some architectures
  216. # feel free to update the Makefile for your architecture and send a pull request or issue
  217. message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
  218. if (NOT MSVC)
  219. if (LLAMA_STATIC)
  220. add_link_options(-static)
  221. if (MINGW)
  222. add_link_options(-static-libgcc -static-libstdc++)
  223. endif()
  224. endif()
  225. if (LLAMA_GPROF)
  226. add_compile_options(-pg)
  227. endif()
  228. if (LLAMA_NATIVE)
  229. add_compile_options(-march=native)
  230. endif()
  231. endif()
  232. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  233. message(STATUS "ARM detected")
  234. if (MSVC)
  235. # TODO: arm msvc?
  236. else()
  237. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  238. # Apple M1, M2, etc.
  239. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  240. add_compile_options(-mcpu=native)
  241. endif()
  242. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv6")
  243. # Raspberry Pi 1, Zero
  244. add_compile_options(-mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access)
  245. endif()
  246. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7")
  247. # Raspberry Pi 2
  248. add_compile_options(-mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations)
  249. endif()
  250. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv8")
  251. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  252. add_compile_options(-mfp16-format=ieee -mno-unaligned-access)
  253. endif()
  254. endif()
  255. elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
  256. message(STATUS "x86 detected")
  257. if (MSVC)
  258. if (LLAMA_AVX512)
  259. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX512>)
  260. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX512>)
  261. # MSVC has no compile-time flags enabling specific
  262. # AVX512 extensions, neither it defines the
  263. # macros corresponding to the extensions.
  264. # Do it manually.
  265. if (LLAMA_AVX512_VBMI)
  266. add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VBMI__>)
  267. add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VBMI__>)
  268. endif()
  269. if (LLAMA_AVX512_VNNI)
  270. add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VNNI__>)
  271. add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VNNI__>)
  272. endif()
  273. elseif (LLAMA_AVX2)
  274. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX2>)
  275. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX2>)
  276. elseif (LLAMA_AVX)
  277. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX>)
  278. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX>)
  279. endif()
  280. else()
  281. if (LLAMA_F16C)
  282. add_compile_options(-mf16c)
  283. endif()
  284. if (LLAMA_FMA)
  285. add_compile_options(-mfma)
  286. endif()
  287. if (LLAMA_AVX)
  288. add_compile_options(-mavx)
  289. endif()
  290. if (LLAMA_AVX2)
  291. add_compile_options(-mavx2)
  292. endif()
  293. if (LLAMA_AVX512)
  294. add_compile_options(-mavx512f)
  295. add_compile_options(-mavx512bw)
  296. endif()
  297. if (LLAMA_AVX512_VBMI)
  298. add_compile_options(-mavx512vbmi)
  299. endif()
  300. if (LLAMA_AVX512_VNNI)
  301. add_compile_options(-mavx512vnni)
  302. endif()
  303. endif()
  304. elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "ppc64")
  305. message(STATUS "PowerPC detected")
  306. add_compile_options(-mcpu=native -mtune=native)
  307. #TODO: Add targets for Power8/Power9 (Altivec/VSX) and Power10(MMA) and query for big endian systems (ppc64/le/be)
  308. else()
  309. message(STATUS "Unknown architecture")
  310. endif()
  311. #
  312. # Build libraries
  313. #
  314. add_library(ggml OBJECT
  315. ggml.c
  316. ggml.h
  317. ${GGML_CUDA_SOURCES}
  318. ${GGML_OPENCL_SOURCES})
  319. target_include_directories(ggml PUBLIC .)
  320. target_compile_features(ggml PUBLIC c_std_11) # don't bump
  321. target_link_libraries(ggml PUBLIC Threads::Threads ${LLAMA_EXTRA_LIBS})
  322. if (BUILD_SHARED_LIBS)
  323. set_target_properties(ggml PROPERTIES POSITION_INDEPENDENT_CODE ON)
  324. endif()
  325. add_library(llama
  326. llama.cpp
  327. llama.h
  328. llama-util.h)
  329. target_include_directories(llama PUBLIC .)
  330. target_compile_features(llama PUBLIC cxx_std_11) # don't bump
  331. target_link_libraries(llama PRIVATE ggml ${LLAMA_EXTRA_LIBS})
  332. if (BUILD_SHARED_LIBS)
  333. set_target_properties(llama PROPERTIES POSITION_INDEPENDENT_CODE ON)
  334. target_compile_definitions(llama PRIVATE LLAMA_SHARED LLAMA_BUILD)
  335. endif()
  336. if (GGML_CUDA_SOURCES)
  337. message(STATUS "GGML CUDA sources found, configuring CUDA architecture")
  338. set_property(TARGET ggml PROPERTY CUDA_ARCHITECTURES OFF)
  339. set_property(TARGET ggml PROPERTY CUDA_SELECT_NVCC_ARCH_FLAGS "Auto")
  340. set_property(TARGET llama PROPERTY CUDA_ARCHITECTURES OFF)
  341. endif()
  342. #
  343. # programs, examples and tests
  344. #
  345. if (LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)
  346. include(CTest)
  347. add_subdirectory(tests)
  348. endif ()
  349. if (LLAMA_BUILD_EXAMPLES)
  350. add_subdirectory(examples)
  351. add_subdirectory(pocs)
  352. endif()