CMakeLists.txt 13 KB

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