CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. add_compile_options(-mcpu=native)
  217. endif()
  218. # TODO: armv6,7,8 version specific flags
  219. endif()
  220. elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
  221. message(STATUS "x86 detected")
  222. if (MSVC)
  223. if (LLAMA_AVX512)
  224. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX512>)
  225. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX512>)
  226. # MSVC has no compile-time flags enabling specific
  227. # AVX512 extensions, neither it defines the
  228. # macros corresponding to the extensions.
  229. # Do it manually.
  230. if (LLAMA_AVX512_VBMI)
  231. add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VBMI__>)
  232. add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VBMI__>)
  233. endif()
  234. if (LLAMA_AVX512_VNNI)
  235. add_compile_definitions($<$<COMPILE_LANGUAGE:C>:__AVX512VNNI__>)
  236. add_compile_definitions($<$<COMPILE_LANGUAGE:CXX>:__AVX512VNNI__>)
  237. endif()
  238. elseif (LLAMA_AVX2)
  239. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX2>)
  240. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX2>)
  241. elseif (LLAMA_AVX)
  242. add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX>)
  243. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX>)
  244. endif()
  245. else()
  246. if (LLAMA_F16C)
  247. add_compile_options(-mf16c)
  248. endif()
  249. if (LLAMA_FMA)
  250. add_compile_options(-mfma)
  251. endif()
  252. if (LLAMA_AVX)
  253. add_compile_options(-mavx)
  254. endif()
  255. if (LLAMA_AVX2)
  256. add_compile_options(-mavx2)
  257. endif()
  258. if (LLAMA_AVX512)
  259. add_compile_options(-mavx512f)
  260. add_compile_options(-mavx512bw)
  261. endif()
  262. if (LLAMA_AVX512_VBMI)
  263. add_compile_options(-mavx512vbmi)
  264. endif()
  265. if (LLAMA_AVX512_VNNI)
  266. add_compile_options(-mavx512vnni)
  267. endif()
  268. endif()
  269. else()
  270. # TODO: support PowerPC
  271. message(STATUS "Unknown architecture")
  272. endif()
  273. #
  274. # Build libraries
  275. #
  276. add_library(ggml OBJECT
  277. ggml.c
  278. ggml.h
  279. ${GGML_CUDA_SOURCES}
  280. ${GGML_OPENCL_SOURCES})
  281. target_include_directories(ggml PUBLIC .)
  282. target_compile_features(ggml PUBLIC c_std_11) # don't bump
  283. target_link_libraries(ggml PUBLIC Threads::Threads ${LLAMA_EXTRA_LIBS})
  284. if (BUILD_SHARED_LIBS)
  285. set_target_properties(ggml PROPERTIES POSITION_INDEPENDENT_CODE ON)
  286. endif()
  287. add_library(llama
  288. llama.cpp
  289. llama.h
  290. llama_util.h)
  291. target_include_directories(llama PUBLIC .)
  292. target_compile_features(llama PUBLIC cxx_std_11) # don't bump
  293. target_link_libraries(llama PRIVATE ggml ${LLAMA_EXTRA_LIBS})
  294. if (BUILD_SHARED_LIBS)
  295. set_target_properties(llama PROPERTIES POSITION_INDEPENDENT_CODE ON)
  296. target_compile_definitions(llama PRIVATE LLAMA_SHARED LLAMA_BUILD)
  297. endif()
  298. if (GGML_CUDA_SOURCES)
  299. message(STATUS "GGML CUDA sources found, configuring CUDA architecture")
  300. set_property(TARGET ggml PROPERTY CUDA_ARCHITECTURES OFF)
  301. set_property(TARGET ggml PROPERTY CUDA_SELECT_NVCC_ARCH_FLAGS "Auto")
  302. set_property(TARGET llama PROPERTY CUDA_ARCHITECTURES OFF)
  303. endif()
  304. #
  305. # programs, examples and tests
  306. #
  307. if (LLAMA_BUILD_TESTS AND NOT CMAKE_JS_VERSION)
  308. include(CTest)
  309. add_subdirectory(tests)
  310. endif ()
  311. if (LLAMA_BUILD_EXAMPLES)
  312. add_subdirectory(examples)
  313. add_subdirectory(pocs)
  314. endif()