CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. # Write header template to binary dir to keep source directory clean
  63. file(WRITE "${CMAKE_BINARY_DIR}/BUILD_INFO.h.in" "\
  64. #ifndef BUILD_INFO_H\n\
  65. #define BUILD_INFO_H\n\
  66. \n\
  67. #define BUILD_NUMBER @BUILD_NUMBER@\n\
  68. #define BUILD_COMMIT \"@BUILD_COMMIT@\"\n\
  69. \n\
  70. #endif // BUILD_INFO_H\n\
  71. ")
  72. # Generate initial build-info.h
  73. include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake)
  74. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
  75. # Add a custom target for build-info.h
  76. add_custom_target(BUILD_INFO ALL DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h")
  77. # Add a custom command to rebuild build-info.h when .git/index changes
  78. add_custom_command(
  79. OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h"
  80. COMMENT "Generating build details from Git"
  81. COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake"
  82. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  83. DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/.git/index"
  84. VERBATIM
  85. )
  86. else()
  87. 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.")
  88. endif()
  89. #
  90. # Compile flags
  91. #
  92. set(CMAKE_CXX_STANDARD 11)
  93. set(CMAKE_CXX_STANDARD_REQUIRED true)
  94. set(CMAKE_C_STANDARD 11)
  95. set(CMAKE_C_STANDARD_REQUIRED true)
  96. set(THREADS_PREFER_PTHREAD_FLAG ON)
  97. find_package(Threads REQUIRED)
  98. if (NOT MSVC)
  99. if (LLAMA_SANITIZE_THREAD)
  100. add_compile_options(-fsanitize=thread)
  101. link_libraries(-fsanitize=thread)
  102. endif()
  103. if (LLAMA_SANITIZE_ADDRESS)
  104. add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
  105. link_libraries(-fsanitize=address)
  106. endif()
  107. if (LLAMA_SANITIZE_UNDEFINED)
  108. add_compile_options(-fsanitize=undefined)
  109. link_libraries(-fsanitize=undefined)
  110. endif()
  111. endif()
  112. if (APPLE AND LLAMA_ACCELERATE)
  113. find_library(ACCELERATE_FRAMEWORK Accelerate)
  114. if (ACCELERATE_FRAMEWORK)
  115. message(STATUS "Accelerate framework found")
  116. add_compile_definitions(GGML_USE_ACCELERATE)
  117. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK})
  118. else()
  119. message(WARNING "Accelerate framework not found")
  120. endif()
  121. endif()
  122. if (LLAMA_OPENBLAS)
  123. if (LLAMA_STATIC)
  124. set(BLA_STATIC ON)
  125. endif()
  126. set(BLA_VENDOR OpenBLAS)
  127. find_package(BLAS)
  128. if (BLAS_FOUND)
  129. message(STATUS "OpenBLAS found")
  130. add_compile_definitions(GGML_USE_OPENBLAS)
  131. add_link_options(${BLAS_LIBRARIES})
  132. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} openblas)
  133. # find header file
  134. set(OPENBLAS_INCLUDE_SEARCH_PATHS
  135. /usr/include
  136. /usr/include/openblas
  137. /usr/include/openblas-base
  138. /usr/local/include
  139. /usr/local/include/openblas
  140. /usr/local/include/openblas-base
  141. /opt/OpenBLAS/include
  142. $ENV{OpenBLAS_HOME}
  143. $ENV{OpenBLAS_HOME}/include
  144. )
  145. find_path(OPENBLAS_INC NAMES cblas.h PATHS ${OPENBLAS_INCLUDE_SEARCH_PATHS})
  146. add_compile_options(-I${OPENBLAS_INC})
  147. else()
  148. message(WARNING "OpenBLAS not found")
  149. endif()
  150. endif()
  151. if (LLAMA_CUBLAS)
  152. cmake_minimum_required(VERSION 3.17)
  153. find_package(CUDAToolkit)
  154. if (CUDAToolkit_FOUND)
  155. message(STATUS "cuBLAS found")
  156. enable_language(CUDA)
  157. set(GGML_CUDA_SOURCES ggml-cuda.cu ggml-cuda.h)
  158. add_compile_definitions(GGML_USE_CUBLAS)
  159. if (LLAMA_STATIC)
  160. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} CUDA::cudart_static CUDA::cublas_static CUDA::cublasLt_static)
  161. else()
  162. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} CUDA::cudart CUDA::cublas CUDA::cublasLt)
  163. endif()
  164. else()
  165. message(WARNING "cuBLAS not found")
  166. endif()
  167. endif()
  168. if (LLAMA_CLBLAST)
  169. find_package(CLBlast)
  170. if (CLBlast_FOUND)
  171. message(STATUS "CLBlast found")
  172. set(GGML_OPENCL_SOURCES ggml-opencl.c ggml-opencl.h)
  173. add_compile_definitions(GGML_USE_CLBLAST)
  174. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} clblast)
  175. else()
  176. message(WARNING "CLBlast not found")
  177. endif()
  178. endif()
  179. if (LLAMA_ALL_WARNINGS)
  180. if (NOT MSVC)
  181. set(c_flags
  182. -Wall
  183. -Wextra
  184. -Wpedantic
  185. -Wcast-qual
  186. -Wdouble-promotion
  187. -Wshadow
  188. -Wstrict-prototypes
  189. -Wpointer-arith
  190. )
  191. set(cxx_flags
  192. -Wall
  193. -Wextra
  194. -Wpedantic
  195. -Wcast-qual
  196. -Wno-unused-function
  197. -Wno-multichar
  198. )
  199. else()
  200. # todo : msvc
  201. endif()
  202. add_compile_options(
  203. "$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
  204. "$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
  205. )
  206. endif()
  207. if (MSVC)
  208. add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
  209. if (BUILD_SHARED_LIBS)
  210. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  211. endif()
  212. endif()
  213. if (LLAMA_LTO)
  214. include(CheckIPOSupported)
  215. check_ipo_supported(RESULT result OUTPUT output)
  216. if (result)
  217. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
  218. else()
  219. message(WARNING "IPO is not supported: ${output}")
  220. endif()
  221. endif()
  222. # Architecture specific
  223. # TODO: probably these flags need to be tweaked on some architectures
  224. # feel free to update the Makefile for your architecture and send a pull request or issue
  225. message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
  226. if (NOT MSVC)
  227. if (LLAMA_STATIC)
  228. add_link_options(-static)
  229. if (MINGW)
  230. add_link_options(-static-libgcc -static-libstdc++)
  231. endif()
  232. endif()
  233. if (LLAMA_GPROF)
  234. add_compile_options(-pg)
  235. endif()
  236. if (LLAMA_NATIVE)
  237. add_compile_options(-march=native)
  238. endif()
  239. endif()
  240. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  241. message(STATUS "ARM detected")
  242. if (MSVC)
  243. # TODO: arm msvc?
  244. else()
  245. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  246. # Apple M1, M2, etc.
  247. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  248. add_compile_options(-mcpu=native)
  249. endif()
  250. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv6")
  251. # Raspberry Pi 1, Zero
  252. add_compile_options(-mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access)
  253. endif()
  254. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv7")
  255. # Raspberry Pi 2
  256. add_compile_options(-mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations)
  257. endif()
  258. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "armv8")
  259. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  260. add_compile_options(-mfp16-format=ieee -mno-unaligned-access)
  261. endif()
  262. endif()
  263. elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
  264. message(STATUS "x86 detected")
  265. if (MSVC)
  266. if (LLAMA_AVX512)
  267. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX512>)
  268. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX512>)
  269. # MSVC has no compile-time flags enabling specific
  270. # AVX512 extensions, neither it defines the
  271. # macros corresponding to the extensions.
  272. # Do it manually.
  273. if (LLAMA_AVX512_VBMI)
  274. add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VBMI__>)
  275. add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VBMI__>)
  276. endif()
  277. if (LLAMA_AVX512_VNNI)
  278. add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VNNI__>)
  279. add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VNNI__>)
  280. endif()
  281. elseif (LLAMA_AVX2)
  282. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX2>)
  283. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX2>)
  284. elseif (LLAMA_AVX)
  285. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX>)
  286. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX>)
  287. endif()
  288. else()
  289. if (LLAMA_F16C)
  290. add_compile_options(-mf16c)
  291. endif()
  292. if (LLAMA_FMA)
  293. add_compile_options(-mfma)
  294. endif()
  295. if (LLAMA_AVX)
  296. add_compile_options(-mavx)
  297. endif()
  298. if (LLAMA_AVX2)
  299. add_compile_options(-mavx2)
  300. endif()
  301. if (LLAMA_AVX512)
  302. add_compile_options(-mavx512f)
  303. add_compile_options(-mavx512bw)
  304. endif()
  305. if (LLAMA_AVX512_VBMI)
  306. add_compile_options(-mavx512vbmi)
  307. endif()
  308. if (LLAMA_AVX512_VNNI)
  309. add_compile_options(-mavx512vnni)
  310. endif()
  311. endif()
  312. else()
  313. # TODO: support PowerPC
  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()