CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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_OPENBLAS "llama: use OpenBLAS" OFF)
  55. option(LLAMA_CUBLAS "llama: use cuBLAS" OFF)
  56. option(LLAMA_CLBLAST "llama: use CLBlast" OFF)
  57. option(LLAMA_BUILD_TESTS "llama: build tests" ${LLAMA_STANDALONE})
  58. option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE})
  59. #
  60. # Build info header
  61. #
  62. # Generate initial build-info.h
  63. include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake)
  64. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
  65. set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.git")
  66. # Is git submodule
  67. if(NOT IS_DIRECTORY "${GIT_DIR}")
  68. file(READ ${GIT_DIR} REAL_GIT_DIR_LINK)
  69. string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" REAL_GIT_DIR ${REAL_GIT_DIR_LINK})
  70. set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${REAL_GIT_DIR}")
  71. endif()
  72. # Add a custom target for build-info.h
  73. add_custom_target(BUILD_INFO ALL DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h")
  74. # Add a custom command to rebuild build-info.h when .git/index changes
  75. add_custom_command(
  76. OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h"
  77. COMMENT "Generating build details from Git"
  78. COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake"
  79. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  80. DEPENDS "${GIT_DIR}/index"
  81. VERBATIM
  82. )
  83. else()
  84. 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.")
  85. endif()
  86. #
  87. # Compile flags
  88. #
  89. set(CMAKE_CXX_STANDARD 11)
  90. set(CMAKE_CXX_STANDARD_REQUIRED true)
  91. set(CMAKE_C_STANDARD 11)
  92. set(CMAKE_C_STANDARD_REQUIRED true)
  93. set(THREADS_PREFER_PTHREAD_FLAG ON)
  94. find_package(Threads REQUIRED)
  95. if (NOT MSVC)
  96. if (LLAMA_SANITIZE_THREAD)
  97. add_compile_options(-fsanitize=thread)
  98. link_libraries(-fsanitize=thread)
  99. endif()
  100. if (LLAMA_SANITIZE_ADDRESS)
  101. add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
  102. link_libraries(-fsanitize=address)
  103. endif()
  104. if (LLAMA_SANITIZE_UNDEFINED)
  105. add_compile_options(-fsanitize=undefined)
  106. link_libraries(-fsanitize=undefined)
  107. endif()
  108. endif()
  109. if (APPLE AND LLAMA_ACCELERATE)
  110. find_library(ACCELERATE_FRAMEWORK Accelerate)
  111. if (ACCELERATE_FRAMEWORK)
  112. message(STATUS "Accelerate framework found")
  113. add_compile_definitions(GGML_USE_ACCELERATE)
  114. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK})
  115. else()
  116. message(WARNING "Accelerate framework not found")
  117. endif()
  118. endif()
  119. if (LLAMA_OPENBLAS)
  120. if (LLAMA_STATIC)
  121. set(BLA_STATIC ON)
  122. endif()
  123. set(BLA_VENDOR OpenBLAS)
  124. find_package(BLAS)
  125. if (BLAS_FOUND)
  126. message(STATUS "OpenBLAS found")
  127. add_compile_definitions(GGML_USE_OPENBLAS)
  128. add_link_options(${BLAS_LIBRARIES})
  129. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} openblas)
  130. # find header file
  131. set(OPENBLAS_INCLUDE_SEARCH_PATHS
  132. /usr/include
  133. /usr/include/openblas
  134. /usr/include/openblas-base
  135. /usr/local/include
  136. /usr/local/include/openblas
  137. /usr/local/include/openblas-base
  138. /opt/OpenBLAS/include
  139. $ENV{OpenBLAS_HOME}
  140. $ENV{OpenBLAS_HOME}/include
  141. )
  142. find_path(OPENBLAS_INC NAMES cblas.h PATHS ${OPENBLAS_INCLUDE_SEARCH_PATHS})
  143. add_compile_options(-I${OPENBLAS_INC})
  144. else()
  145. message(WARNING "OpenBLAS not found")
  146. endif()
  147. endif()
  148. if (LLAMA_CUBLAS)
  149. cmake_minimum_required(VERSION 3.17)
  150. find_package(CUDAToolkit)
  151. if (CUDAToolkit_FOUND)
  152. message(STATUS "cuBLAS found")
  153. enable_language(CUDA)
  154. set(GGML_CUDA_SOURCES ggml-cuda.cu ggml-cuda.h)
  155. add_compile_definitions(GGML_USE_CUBLAS)
  156. if (LLAMA_STATIC)
  157. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} CUDA::cudart_static CUDA::cublas_static CUDA::cublasLt_static)
  158. else()
  159. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} CUDA::cudart CUDA::cublas CUDA::cublasLt)
  160. endif()
  161. else()
  162. message(WARNING "cuBLAS not found")
  163. endif()
  164. endif()
  165. if (LLAMA_CLBLAST)
  166. find_package(CLBlast)
  167. if (CLBlast_FOUND)
  168. message(STATUS "CLBlast found")
  169. set(GGML_OPENCL_SOURCES ggml-opencl.c ggml-opencl.h)
  170. add_compile_definitions(GGML_USE_CLBLAST)
  171. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} clblast)
  172. else()
  173. message(WARNING "CLBlast not found")
  174. endif()
  175. endif()
  176. if (LLAMA_ALL_WARNINGS)
  177. if (NOT MSVC)
  178. set(c_flags
  179. -Wall
  180. -Wextra
  181. -Wpedantic
  182. -Wcast-qual
  183. -Wdouble-promotion
  184. -Wshadow
  185. -Wstrict-prototypes
  186. -Wpointer-arith
  187. )
  188. set(cxx_flags
  189. -Wall
  190. -Wextra
  191. -Wpedantic
  192. -Wcast-qual
  193. -Wno-unused-function
  194. -Wno-multichar
  195. )
  196. else()
  197. # todo : msvc
  198. endif()
  199. add_compile_options(
  200. "$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
  201. "$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
  202. )
  203. endif()
  204. if (MSVC)
  205. add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
  206. if (BUILD_SHARED_LIBS)
  207. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  208. endif()
  209. endif()
  210. if (LLAMA_LTO)
  211. include(CheckIPOSupported)
  212. check_ipo_supported(RESULT result OUTPUT output)
  213. if (result)
  214. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
  215. else()
  216. message(WARNING "IPO is not supported: ${output}")
  217. endif()
  218. endif()
  219. # Architecture specific
  220. # TODO: probably these flags need to be tweaked on some architectures
  221. # feel free to update the Makefile for your architecture and send a pull request or issue
  222. message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
  223. if (NOT MSVC)
  224. if (LLAMA_STATIC)
  225. add_link_options(-static)
  226. if (MINGW)
  227. add_link_options(-static-libgcc -static-libstdc++)
  228. endif()
  229. endif()
  230. if (LLAMA_GPROF)
  231. add_compile_options(-pg)
  232. endif()
  233. if (LLAMA_NATIVE)
  234. add_compile_options(-march=native)
  235. endif()
  236. endif()
  237. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  238. message(STATUS "ARM detected")
  239. if (MSVC)
  240. # TODO: arm msvc?
  241. else()
  242. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  243. # Apple M1, M2, etc.
  244. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  245. add_compile_options(-mcpu=native)
  246. endif()
  247. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv6")
  248. # Raspberry Pi 1, Zero
  249. add_compile_options(-mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access)
  250. endif()
  251. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7")
  252. # Raspberry Pi 2
  253. add_compile_options(-mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations)
  254. endif()
  255. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv8")
  256. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  257. add_compile_options(-mfp16-format=ieee -mno-unaligned-access)
  258. endif()
  259. endif()
  260. elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
  261. message(STATUS "x86 detected")
  262. if (MSVC)
  263. if (LLAMA_AVX512)
  264. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX512>)
  265. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX512>)
  266. # MSVC has no compile-time flags enabling specific
  267. # AVX512 extensions, neither it defines the
  268. # macros corresponding to the extensions.
  269. # Do it manually.
  270. if (LLAMA_AVX512_VBMI)
  271. add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VBMI__>)
  272. add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VBMI__>)
  273. endif()
  274. if (LLAMA_AVX512_VNNI)
  275. add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VNNI__>)
  276. add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VNNI__>)
  277. endif()
  278. elseif (LLAMA_AVX2)
  279. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX2>)
  280. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX2>)
  281. elseif (LLAMA_AVX)
  282. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX>)
  283. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX>)
  284. endif()
  285. else()
  286. if (LLAMA_F16C)
  287. add_compile_options(-mf16c)
  288. endif()
  289. if (LLAMA_FMA)
  290. add_compile_options(-mfma)
  291. endif()
  292. if (LLAMA_AVX)
  293. add_compile_options(-mavx)
  294. endif()
  295. if (LLAMA_AVX2)
  296. add_compile_options(-mavx2)
  297. endif()
  298. if (LLAMA_AVX512)
  299. add_compile_options(-mavx512f)
  300. add_compile_options(-mavx512bw)
  301. endif()
  302. if (LLAMA_AVX512_VBMI)
  303. add_compile_options(-mavx512vbmi)
  304. endif()
  305. if (LLAMA_AVX512_VNNI)
  306. add_compile_options(-mavx512vnni)
  307. endif()
  308. endif()
  309. elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "ppc64")
  310. message(STATUS "PowerPC detected")
  311. add_compile_options(-mcpu=native -mtune=native)
  312. #TODO: Add targets for Power8/Power9 (Altivec/VSX) and Power10(MMA) and query for big endian systems (ppc64/le/be)
  313. else()
  314. message(STATUS "Unknown architecture")
  315. endif()
  316. #
  317. # Build libraries
  318. #
  319. add_library(ggml OBJECT
  320. ggml.c
  321. ggml.h
  322. ${GGML_CUDA_SOURCES}
  323. ${GGML_OPENCL_SOURCES})
  324. target_include_directories(ggml PUBLIC .)
  325. target_compile_features(ggml PUBLIC c_std_11) # don't bump
  326. target_link_libraries(ggml PUBLIC Threads::Threads ${LLAMA_EXTRA_LIBS})
  327. if (BUILD_SHARED_LIBS)
  328. set_target_properties(ggml PROPERTIES POSITION_INDEPENDENT_CODE ON)
  329. endif()
  330. add_library(llama
  331. llama.cpp
  332. llama.h
  333. llama-util.h)
  334. target_include_directories(llama PUBLIC .)
  335. target_compile_features(llama PUBLIC cxx_std_11) # don't bump
  336. target_link_libraries(llama PRIVATE ggml ${LLAMA_EXTRA_LIBS})
  337. if (BUILD_SHARED_LIBS)
  338. set_target_properties(llama PROPERTIES POSITION_INDEPENDENT_CODE ON)
  339. target_compile_definitions(llama PRIVATE LLAMA_SHARED LLAMA_BUILD)
  340. endif()
  341. if (GGML_CUDA_SOURCES)
  342. message(STATUS "GGML CUDA sources found, configuring CUDA architecture")
  343. set_property(TARGET ggml PROPERTY CUDA_ARCHITECTURES OFF)
  344. set_property(TARGET ggml PROPERTY CUDA_SELECT_NVCC_ARCH_FLAGS "Auto")
  345. set_property(TARGET llama PROPERTY CUDA_ARCHITECTURES OFF)
  346. endif()
  347. #
  348. # programs, examples and tests
  349. #
  350. if (LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)
  351. include(CTest)
  352. add_subdirectory(tests)
  353. endif ()
  354. if (LLAMA_BUILD_EXAMPLES)
  355. add_subdirectory(examples)
  356. add_subdirectory(pocs)
  357. endif()