CMakeLists.txt 12 KB

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