1
0

CMakeLists.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. cmake_minimum_required(VERSION 3.12)
  2. project("llama.cpp" C CXX)
  3. if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE)
  4. set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
  5. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
  6. endif()
  7. #
  8. # Option list
  9. #
  10. # general
  11. option(LLAMA_STATIC "llama: static link libraries" OFF)
  12. option(LLAMA_NATIVE "llama: enable -march=native flag" OFF)
  13. option(LLAMA_LTO "llama: enable link time optimization" OFF)
  14. # debug
  15. option(LLAMA_ALL_WARNINGS "llama: enable all compiler warnings" ON)
  16. option(LLAMA_ALL_WARNINGS_3RD_PARTY "llama: enable all compiler warnings in 3rd party libs" OFF)
  17. option(LLAMA_GPROF "llama: enable gprof" OFF)
  18. # sanitizers
  19. option(LLAMA_SANITIZE_THREAD "llama: enable thread sanitizer" OFF)
  20. option(LLAMA_SANITIZE_ADDRESS "llama: enable address sanitizer" OFF)
  21. option(LLAMA_SANITIZE_UNDEFINED "llama: enable undefined sanitizer" OFF)
  22. # instruction set specific
  23. option(LLAMA_AVX "llama: enable AVX" ON)
  24. option(LLAMA_AVX2 "llama: enable AVX2" ON)
  25. option(LLAMA_FMA "llama: enable FMA" ON)
  26. # 3rd party libs
  27. option(LLAMA_ACCELERATE "llama: enable Accelerate framework" ON)
  28. option(LLAMA_OPENBLAS "llama: use OpenBLAS" OFF)
  29. #
  30. # Compile flags
  31. #
  32. set(CMAKE_CXX_STANDARD_REQUIRED true)
  33. set(CMAKE_C_STANDARD_REQUIRED true)
  34. set(THREADS_PREFER_PTHREAD_FLAG ON)
  35. find_package(Threads REQUIRED)
  36. if (NOT MSVC)
  37. if (LLAMA_SANITIZE_THREAD)
  38. add_compile_options(-fsanitize=thread)
  39. endif()
  40. if (LLAMA_SANITIZE_ADDRESS)
  41. add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
  42. endif()
  43. if (LLAMA_SANITIZE_UNDEFINED)
  44. add_compile_options(-fsanitize=undefined)
  45. endif()
  46. endif()
  47. if (APPLE AND LLAMA_ACCELERATE)
  48. find_library(ACCELERATE_FRAMEWORK Accelerate)
  49. if (ACCELERATE_FRAMEWORK)
  50. message(STATUS "Accelerate framework found")
  51. add_compile_definitions(GGML_USE_ACCELERATE)
  52. set(LLAMA_EXTRA_LIBS ${LLAMA_EXTRA_LIBS} ${ACCELERATE_FRAMEWORK})
  53. else()
  54. message(WARNING "Accelerate framework not found")
  55. endif()
  56. endif()
  57. if (LLAMA_OPENBLAS)
  58. if (LLAMA_STATIC)
  59. set(BLA_STATIC ON)
  60. endif()
  61. set(BLA_VENDOR OpenBLAS)
  62. find_package(BLAS)
  63. if (BLAS_FOUND)
  64. message(STATUS "OpenBLAS found")
  65. add_compile_definitions(GGML_USE_OPENBLAS)
  66. add_link_options(${BLAS_LIBRARIES})
  67. else()
  68. message(WARNING "OpenBLAS not found")
  69. endif()
  70. endif()
  71. if (LLAMA_ALL_WARNINGS)
  72. if (NOT MSVC)
  73. set(c_flags
  74. -Wall
  75. -Wextra
  76. -Wpedantic
  77. -Wshadow
  78. -Wcast-qual
  79. -Wstrict-prototypes
  80. -Wpointer-arith
  81. -Wno-unused-function
  82. )
  83. set(cxx_flags
  84. -Wall
  85. -Wextra
  86. -Wpedantic
  87. -Wcast-qual
  88. )
  89. else()
  90. # todo : msvc
  91. endif()
  92. add_compile_options(
  93. "$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
  94. "$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
  95. )
  96. endif()
  97. if (LLAMA_LTO)
  98. include(CheckIPOSupported)
  99. check_ipo_supported(RESULT result OUTPUT output)
  100. if (result)
  101. set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
  102. else()
  103. message(WARNING "IPO is not supported: ${output}")
  104. endif()
  105. endif()
  106. # Architecture specific
  107. # TODO: probably these flags need to be tweaked on some architectures
  108. # feel free to update the Makefile for your architecture and send a pull request or issue
  109. message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")
  110. if (NOT MSVC)
  111. if (LLAMA_STATIC)
  112. add_link_options(-static)
  113. if (MINGW)
  114. add_link_options(-static-libgcc -static-libstdc++)
  115. endif()
  116. endif()
  117. if (LLAMA_GPROF)
  118. add_compile_options(-pg)
  119. endif()
  120. if (LLAMA_NATIVE)
  121. add_compile_options(-march=native)
  122. endif()
  123. endif()
  124. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  125. message(STATUS "ARM detected")
  126. if (MSVC)
  127. # TODO: arm msvc?
  128. else()
  129. if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
  130. add_compile_options(-mcpu=native)
  131. endif()
  132. # TODO: armv6,7,8 version specific flags
  133. endif()
  134. elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
  135. message(STATUS "x86 detected")
  136. if (MSVC)
  137. if (LLAMA_AVX2)
  138. add_compile_options(/arch:AVX2)
  139. elseif (LLAMA_AVX)
  140. add_compile_options(/arch:AVX)
  141. endif()
  142. else()
  143. add_compile_options(-mf16c)
  144. if (LLAMA_FMA)
  145. add_compile_options(-mfma)
  146. endif()
  147. if (LLAMA_AVX)
  148. add_compile_options(-mavx)
  149. endif()
  150. if (LLAMA_AVX2)
  151. add_compile_options(-mavx2)
  152. endif()
  153. endif()
  154. else()
  155. # TODO: support PowerPC
  156. message(STATUS "Unknown architecture")
  157. endif()
  158. #
  159. # Build library
  160. #
  161. add_executable(llama main.cpp)
  162. add_executable(quantize quantize.cpp)
  163. add_library(ggml OBJECT
  164. ggml.c
  165. ggml.h)
  166. add_library(utils OBJECT
  167. utils.cpp
  168. utils.h)
  169. target_include_directories(ggml PUBLIC .)
  170. target_compile_features(ggml PUBLIC c_std_11)
  171. target_compile_features(utils PUBLIC cxx_std_17)
  172. #
  173. # Linking
  174. #
  175. target_link_libraries(ggml PRIVATE Threads::Threads ${LLAMA_EXTRA_LIBS})
  176. target_link_libraries(llama PRIVATE ggml utils)
  177. target_link_libraries(quantize PRIVATE ggml utils)