CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. enable_language(CUDA)
  126. set(GGML_CUDA_SOURCES ggml-cuda.cu ggml-cuda.h)
  127. add_compile_definitions(GGML_USE_CUBLAS)
  128. if (LLAMA_STATIC)
  129. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} CUDA::cudart_static CUDA::cublas_static CUDA::cublasLt_static)
  130. else()
  131. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} CUDA::cudart CUDA::cublas CUDA::cublasLt)
  132. endif()
  133. else()
  134. message(WARNING "cuBLAS not found")
  135. endif()
  136. endif()
  137. if (LLAMA_ALL_WARNINGS)
  138. if (NOT MSVC)
  139. set(c_flags
  140. -Wall
  141. -Wextra
  142. -Wpedantic
  143. -Wcast-qual
  144. -Wdouble-promotion
  145. -Wshadow
  146. -Wstrict-prototypes
  147. -Wpointer-arith
  148. )
  149. set(cxx_flags
  150. -Wall
  151. -Wextra
  152. -Wpedantic
  153. -Wcast-qual
  154. -Wno-unused-function
  155. -Wno-multichar
  156. )
  157. else()
  158. # todo : msvc
  159. endif()
  160. add_compile_options(
  161. "$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
  162. "$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
  163. )
  164. endif()
  165. if (MSVC)
  166. add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
  167. if (BUILD_SHARED_LIBS)
  168. set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
  169. endif()
  170. endif()
  171. if (LLAMA_LTO)
  172. include(CheckIPOSupported)
  173. check_ipo_supported(RESULT result OUTPUT output)
  174. if (result)
  175. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
  176. else()
  177. message(WARNING "IPO is not supported: ${output}")
  178. endif()
  179. endif()
  180. # Architecture specific
  181. # TODO: probably these flags need to be tweaked on some architectures
  182. # feel free to update the Makefile for your architecture and send a pull request or issue
  183. message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
  184. if (NOT MSVC)
  185. if (LLAMA_STATIC)
  186. add_link_options(-static)
  187. if (MINGW)
  188. add_link_options(-static-libgcc -static-libstdc++)
  189. endif()
  190. endif()
  191. if (LLAMA_GPROF)
  192. add_compile_options(-pg)
  193. endif()
  194. if (LLAMA_NATIVE)
  195. add_compile_options(-march=native)
  196. endif()
  197. endif()
  198. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  199. message(STATUS "ARM detected")
  200. if (MSVC)
  201. # TODO: arm msvc?
  202. else()
  203. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  204. add_compile_options(-mcpu=native)
  205. endif()
  206. # TODO: armv6,7,8 version specific flags
  207. endif()
  208. elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
  209. message(STATUS "x86 detected")
  210. if (MSVC)
  211. if (LLAMA_AVX512)
  212. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX512>)
  213. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX512>)
  214. # MSVC has no compile-time flags enabling specific
  215. # AVX512 extensions, neither it defines the
  216. # macros corresponding to the extensions.
  217. # Do it manually.
  218. if (LLAMA_AVX512_VBMI)
  219. add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VBMI__>)
  220. add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VBMI__>)
  221. endif()
  222. if (LLAMA_AVX512_VNNI)
  223. add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VNNI__>)
  224. add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VNNI__>)
  225. endif()
  226. elseif (LLAMA_AVX2)
  227. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX2>)
  228. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX2>)
  229. elseif (LLAMA_AVX)
  230. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX>)
  231. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX>)
  232. endif()
  233. else()
  234. if (LLAMA_F16C)
  235. add_compile_options(-mf16c)
  236. endif()
  237. if (LLAMA_FMA)
  238. add_compile_options(-mfma)
  239. endif()
  240. if (LLAMA_AVX)
  241. add_compile_options(-mavx)
  242. endif()
  243. if (LLAMA_AVX2)
  244. add_compile_options(-mavx2)
  245. endif()
  246. if (LLAMA_AVX512)
  247. add_compile_options(-mavx512f)
  248. add_compile_options(-mavx512bw)
  249. endif()
  250. if (LLAMA_AVX512_VBMI)
  251. add_compile_options(-mavx512vbmi)
  252. endif()
  253. if (LLAMA_AVX512_VNNI)
  254. add_compile_options(-mavx512vnni)
  255. endif()
  256. endif()
  257. else()
  258. # TODO: support PowerPC
  259. message(STATUS "Unknown architecture")
  260. endif()
  261. #
  262. # Build libraries
  263. #
  264. add_library(ggml OBJECT
  265. ggml.c
  266. ggml.h
  267. ${GGML_CUDA_SOURCES})
  268. target_include_directories(ggml PUBLIC .)
  269. target_compile_features(ggml PUBLIC c_std_11) # don't bump
  270. target_link_libraries(ggml PUBLIC Threads::Threads ${LLAMA_EXTRA_LIBS})
  271. if (BUILD_SHARED_LIBS)
  272. set_target_properties(ggml PROPERTIES POSITION_INDEPENDENT_CODE ON)
  273. endif()
  274. add_library(llama
  275. llama.cpp
  276. llama.h
  277. llama_util.h)
  278. target_include_directories(llama PUBLIC .)
  279. target_compile_features(llama PUBLIC cxx_std_11) # don't bump
  280. target_link_libraries(llama PRIVATE ggml ${LLAMA_EXTRA_LIBS})
  281. if (BUILD_SHARED_LIBS)
  282. set_target_properties(llama PROPERTIES POSITION_INDEPENDENT_CODE ON)
  283. target_compile_definitions(llama PRIVATE LLAMA_SHARED LLAMA_BUILD)
  284. endif()
  285. if (GGML_CUDA_SOURCES)
  286. message(STATUS "GGML CUDA sources found, configuring CUDA architecture")
  287. set_property(TARGET ggml PROPERTY CUDA_ARCHITECTURES OFF)
  288. set_property(TARGET ggml PROPERTY CUDA_SELECT_NVCC_ARCH_FLAGS "Auto")
  289. set_property(TARGET llama PROPERTY CUDA_ARCHITECTURES OFF)
  290. endif()
  291. #
  292. # programs, examples and tests
  293. #
  294. if (LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)
  295. include(CTest)
  296. add_subdirectory(tests)
  297. endif ()
  298. if (LLAMA_BUILD_EXAMPLES)
  299. add_subdirectory(examples)
  300. add_subdirectory(pocs)
  301. endif()