CMakeLists.txt 9.9 KB

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