CMakeLists.txt 14 KB

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