Makefile 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. # Define the default target now so that it is always the first target
  2. BUILD_TARGETS = \
  3. main quantize quantize-stats perplexity imatrix embedding vdot q8dot train-text-from-scratch convert-llama2c-to-ggml \
  4. simple batched batched-bench save-load-state server gguf llama-bench libllava.a llava-cli baby-llama beam-search \
  5. speculative infill tokenize benchmark-matmult parallel finetune export-lora lookahead lookup passkey tests/test-c.o
  6. # Binaries only useful for tests
  7. TEST_TARGETS = \
  8. tests/test-llama-grammar tests/test-grammar-parser tests/test-double-float tests/test-grad0 tests/test-opt \
  9. tests/test-quantize-fns tests/test-quantize-perf tests/test-sampling tests/test-tokenizer-0-llama \
  10. tests/test-tokenizer-0-falcon tests/test-tokenizer-1-llama tests/test-tokenizer-1-bpe tests/test-rope \
  11. tests/test-backend-ops tests/test-model-load-cancel tests/test-autorelease
  12. # Code coverage output files
  13. COV_TARGETS = *.gcno tests/*.gcno *.gcda tests/*.gcda *.gcov tests/*.gcov lcov-report gcovr-report
  14. ifndef UNAME_S
  15. UNAME_S := $(shell uname -s)
  16. endif
  17. ifndef UNAME_P
  18. UNAME_P := $(shell uname -p)
  19. endif
  20. ifndef UNAME_M
  21. UNAME_M := $(shell uname -m)
  22. endif
  23. # Mac OS + Arm can report x86_64
  24. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  25. ifeq ($(UNAME_S),Darwin)
  26. ifndef LLAMA_NO_METAL
  27. LLAMA_METAL := 1
  28. endif
  29. ifneq ($(UNAME_P),arm)
  30. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  31. ifeq ($(SYSCTL_M),1)
  32. # UNAME_P := arm
  33. # UNAME_M := arm64
  34. warn := $(warning Your arch is announced as x86_64, but it seems to actually be ARM64. Not fixing that can lead to bad performance. For more info see: https://github.com/ggerganov/whisper.cpp/issues/66\#issuecomment-1282546789)
  35. endif
  36. endif
  37. endif
  38. default: $(BUILD_TARGETS)
  39. test: $(TEST_TARGETS)
  40. @failures=0; \
  41. for test_target in $(TEST_TARGETS); do \
  42. if [ "$$test_target" = "tests/test-tokenizer-0-llama" ]; then \
  43. ./$$test_target $(CURDIR)/models/ggml-vocab-llama.gguf; \
  44. elif [ "$$test_target" = "tests/test-tokenizer-0-falcon" ]; then \
  45. ./$$test_target $(CURDIR)/models/ggml-vocab-falcon.gguf; \
  46. elif [ "$$test_target" = "tests/test-tokenizer-1-llama" ]; then \
  47. continue; \
  48. elif [ "$$test_target" = "tests/test-tokenizer-1-bpe" ]; then \
  49. continue; \
  50. else \
  51. echo "Running test $$test_target..."; \
  52. ./$$test_target; \
  53. fi; \
  54. if [ $$? -ne 0 ]; then \
  55. printf 'Test %s FAILED!\n\n' $$test_target; \
  56. failures=$$(( failures + 1 )); \
  57. else \
  58. printf 'Test %s passed.\n\n' $$test_target; \
  59. fi; \
  60. done; \
  61. if [ $$failures -gt 0 ]; then \
  62. printf '\n%s tests failed.\n' $$failures; \
  63. exit 1; \
  64. fi
  65. @echo 'All tests passed.'
  66. all: $(BUILD_TARGETS) $(TEST_TARGETS)
  67. coverage: ## Run code coverage
  68. gcov -pb tests/*.cpp
  69. lcov-report: coverage ## Generate lcov report
  70. mkdir -p lcov-report
  71. lcov --capture --directory . --output-file lcov-report/coverage.info
  72. genhtml lcov-report/coverage.info --output-directory lcov-report
  73. gcovr-report: coverage ## Generate gcovr report
  74. mkdir -p gcovr-report
  75. gcovr --root . --html --html-details --output gcovr-report/coverage.html
  76. ifdef RISCV_CROSS_COMPILE
  77. CC := riscv64-unknown-linux-gnu-gcc
  78. CXX := riscv64-unknown-linux-gnu-g++
  79. endif
  80. #
  81. # Compile flags
  82. #
  83. # keep standard at C11 and C++11
  84. MK_CPPFLAGS = -I. -Icommon
  85. MK_CFLAGS = -std=c11 -fPIC
  86. MK_CXXFLAGS = -std=c++11 -fPIC
  87. # -Ofast tends to produce faster code, but may not be available for some compilers.
  88. ifdef LLAMA_FAST
  89. MK_CFLAGS += -Ofast
  90. HOST_CXXFLAGS += -Ofast
  91. MK_NVCCFLAGS += -O3
  92. else
  93. MK_CFLAGS += -O3
  94. MK_CXXFLAGS += -O3
  95. MK_NVCCFLAGS += -O3
  96. endif
  97. # clock_gettime came in POSIX.1b (1993)
  98. # CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
  99. # posix_memalign came in POSIX.1-2001 / SUSv3
  100. # M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
  101. MK_CPPFLAGS += -D_XOPEN_SOURCE=600
  102. # Somehow in OpenBSD whenever POSIX conformance is specified
  103. # some string functions rely on locale_t availability,
  104. # which was introduced in POSIX.1-2008, forcing us to go higher
  105. ifeq ($(UNAME_S),OpenBSD)
  106. MK_CPPFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
  107. endif
  108. # Data types, macros and functions related to controlling CPU affinity and
  109. # some memory allocation are available on Linux through GNU extensions in libc
  110. ifeq ($(UNAME_S),Linux)
  111. MK_CPPFLAGS += -D_GNU_SOURCE
  112. endif
  113. # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
  114. # and on macOS its availability depends on enabling Darwin extensions
  115. # similarly on DragonFly, enabling BSD extensions is necessary
  116. ifeq ($(UNAME_S),Darwin)
  117. MK_CPPFLAGS += -D_DARWIN_C_SOURCE
  118. endif
  119. ifeq ($(UNAME_S),DragonFly)
  120. MK_CPPFLAGS += -D__BSD_VISIBLE
  121. endif
  122. # alloca is a non-standard interface that is not visible on BSDs when
  123. # POSIX conformance is specified, but not all of them provide a clean way
  124. # to enable it in such cases
  125. ifeq ($(UNAME_S),FreeBSD)
  126. MK_CPPFLAGS += -D__BSD_VISIBLE
  127. endif
  128. ifeq ($(UNAME_S),NetBSD)
  129. MK_CPPFLAGS += -D_NETBSD_SOURCE
  130. endif
  131. ifeq ($(UNAME_S),OpenBSD)
  132. MK_CPPFLAGS += -D_BSD_SOURCE
  133. endif
  134. ifdef LLAMA_DEBUG
  135. MK_CFLAGS += -O0 -g
  136. MK_CXXFLAGS += -O0 -g
  137. MK_LDFLAGS += -g
  138. ifeq ($(UNAME_S),Linux)
  139. MK_CXXFLAGS += -Wp,-D_GLIBCXX_ASSERTIONS
  140. endif
  141. else
  142. MK_CPPFLAGS += -DNDEBUG
  143. endif
  144. ifdef LLAMA_SANITIZE_THREAD
  145. MK_CFLAGS += -fsanitize=thread -g
  146. MK_CXXFLAGS += -fsanitize=thread -g
  147. MK_LDFLAGS += -fsanitize=thread -g
  148. endif
  149. ifdef LLAMA_SANITIZE_ADDRESS
  150. MK_CFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  151. MK_CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  152. MK_LDFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  153. endif
  154. ifdef LLAMA_SANITIZE_UNDEFINED
  155. MK_CFLAGS += -fsanitize=undefined -g
  156. MK_CXXFLAGS += -fsanitize=undefined -g
  157. MK_LDFLAGS += -fsanitize=undefined -g
  158. endif
  159. ifdef LLAMA_SERVER_VERBOSE
  160. MK_CPPFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
  161. endif
  162. ifdef LLAMA_CODE_COVERAGE
  163. MK_CXXFLAGS += -fprofile-arcs -ftest-coverage -dumpbase ''
  164. endif
  165. ifdef LLAMA_DISABLE_LOGS
  166. MK_CPPFLAGS += -DLOG_DISABLE_LOGS
  167. endif # LLAMA_DISABLE_LOGS
  168. # warnings
  169. WARN_FLAGS = -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
  170. MK_CFLAGS += $(WARN_FLAGS) -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int \
  171. -Werror=implicit-function-declaration
  172. MK_CXXFLAGS += $(WARN_FLAGS) -Wmissing-declarations -Wmissing-noreturn
  173. # this version of Apple ld64 is buggy
  174. ifneq '' '$(findstring dyld-1015.7,$(shell $(CC) $(LDFLAGS) -Wl,-v 2>&1))'
  175. MK_CPPFLAGS += -DHAVE_BUGGY_APPLE_LINKER
  176. endif
  177. # OS specific
  178. # TODO: support Windows
  179. ifneq '' '$(filter $(UNAME_S),Linux Darwin FreeBSD NetBSD OpenBSD Haiku)'
  180. MK_CFLAGS += -pthread
  181. MK_CXXFLAGS += -pthread
  182. endif
  183. # detect Windows
  184. ifneq ($(findstring _NT,$(UNAME_S)),)
  185. _WIN32 := 1
  186. endif
  187. # library name prefix
  188. ifneq ($(_WIN32),1)
  189. LIB_PRE := lib
  190. endif
  191. # Dynamic Shared Object extension
  192. ifneq ($(_WIN32),1)
  193. DSO_EXT := .so
  194. else
  195. DSO_EXT := .dll
  196. endif
  197. # Windows Sockets 2 (Winsock) for network-capable apps
  198. ifeq ($(_WIN32),1)
  199. LWINSOCK2 := -lws2_32
  200. endif
  201. ifdef LLAMA_GPROF
  202. MK_CFLAGS += -pg
  203. MK_CXXFLAGS += -pg
  204. endif
  205. ifdef LLAMA_PERF
  206. MK_CPPFLAGS += -DGGML_PERF
  207. endif
  208. # Architecture specific
  209. # TODO: probably these flags need to be tweaked on some architectures
  210. # feel free to update the Makefile for your architecture and send a pull request or issue
  211. ifndef RISCV
  212. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
  213. # Use all CPU extensions that are available:
  214. MK_CFLAGS += -march=native -mtune=native
  215. HOST_CXXFLAGS += -march=native -mtune=native
  216. # Usage AVX-only
  217. #MK_CFLAGS += -mfma -mf16c -mavx
  218. #MK_CXXFLAGS += -mfma -mf16c -mavx
  219. # Usage SSSE3-only (Not is SSE3!)
  220. #MK_CFLAGS += -mssse3
  221. #MK_CXXFLAGS += -mssse3
  222. endif
  223. ifneq '' '$(findstring mingw,$(shell $(CC) -dumpmachine))'
  224. # The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
  225. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
  226. # https://github.com/ggerganov/llama.cpp/issues/2922
  227. MK_CFLAGS += -Xassembler -muse-unaligned-vector-move
  228. MK_CXXFLAGS += -Xassembler -muse-unaligned-vector-move
  229. # Target Windows 8 for PrefetchVirtualMemory
  230. MK_CPPFLAGS += -D_WIN32_WINNT=0x602
  231. endif
  232. ifneq ($(filter aarch64%,$(UNAME_M)),)
  233. # Apple M1, M2, etc.
  234. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  235. # Nvidia Jetson
  236. MK_CFLAGS += -mcpu=native
  237. MK_CXXFLAGS += -mcpu=native
  238. JETSON_RELEASE_INFO = $(shell jetson_release)
  239. ifdef JETSON_RELEASE_INFO
  240. ifneq ($(filter TX2%,$(JETSON_RELEASE_INFO)),)
  241. JETSON_EOL_MODULE_DETECT = 1
  242. CC = aarch64-unknown-linux-gnu-gcc
  243. cxx = aarch64-unknown-linux-gnu-g++
  244. endif
  245. endif
  246. endif
  247. ifneq ($(filter armv6%,$(UNAME_M)),)
  248. # Raspberry Pi 1, Zero
  249. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  250. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  251. endif
  252. ifneq ($(filter armv7%,$(UNAME_M)),)
  253. # Raspberry Pi 2
  254. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  255. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  256. endif
  257. ifneq ($(filter armv8%,$(UNAME_M)),)
  258. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  259. MK_CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  260. MK_CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
  261. endif
  262. ifneq ($(filter ppc64%,$(UNAME_M)),)
  263. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  264. ifneq (,$(findstring POWER9,$(POWER9_M)))
  265. MK_CFLAGS += -mcpu=power9
  266. MK_CXXFLAGS += -mcpu=power9
  267. endif
  268. endif
  269. ifneq ($(filter ppc64le%,$(UNAME_M)),)
  270. MK_CFLAGS += -mcpu=powerpc64le
  271. MK_CXXFLAGS += -mcpu=powerpc64le
  272. CUDA_POWER_ARCH = 1
  273. endif
  274. else
  275. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  276. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  277. endif
  278. ifdef LLAMA_QKK_64
  279. MK_CPPFLAGS += -DGGML_QKK_64
  280. endif
  281. ifndef LLAMA_NO_ACCELERATE
  282. # Mac OS - include Accelerate framework.
  283. # `-framework Accelerate` works both with Apple Silicon and Mac Intel
  284. ifeq ($(UNAME_S),Darwin)
  285. MK_CPPFLAGS += -DGGML_USE_ACCELERATE
  286. MK_CPPFLAGS += -DACCELERATE_NEW_LAPACK
  287. MK_CPPFLAGS += -DACCELERATE_LAPACK_ILP64
  288. MK_LDFLAGS += -framework Accelerate
  289. endif
  290. endif # LLAMA_NO_ACCELERATE
  291. ifdef LLAMA_MPI
  292. MK_CPPFLAGS += -DGGML_USE_MPI
  293. MK_CFLAGS += -Wno-cast-qual
  294. MK_CXXFLAGS += -Wno-cast-qual
  295. OBJS += ggml-mpi.o
  296. endif # LLAMA_MPI
  297. ifdef LLAMA_OPENBLAS
  298. MK_CPPFLAGS += -DGGML_USE_OPENBLAS $(shell pkg-config --cflags-only-I openblas)
  299. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
  300. MK_LDFLAGS += $(shell pkg-config --libs openblas)
  301. endif # LLAMA_OPENBLAS
  302. ifdef LLAMA_BLIS
  303. MK_CPPFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
  304. MK_LDFLAGS += -lblis -L/usr/local/lib
  305. endif # LLAMA_BLIS
  306. ifdef LLAMA_CUBLAS
  307. MK_CPPFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include -I/usr/local/cuda/targets/aarch64-linux/include
  308. MK_LDFLAGS += -lcuda -lcublas -lculibos -lcudart -lcublasLt -lpthread -ldl -lrt -L/usr/local/cuda/lib64 -L/opt/cuda/lib64 -L$(CUDA_PATH)/targets/x86_64-linux/lib -L/usr/local/cuda/targets/aarch64-linux/lib -L/usr/lib/wsl/lib
  309. OBJS += ggml-cuda.o
  310. MK_NVCCFLAGS += -use_fast_math
  311. ifndef JETSON_EOL_MODULE_DETECT
  312. MK_NVCCFLAGS += --forward-unknown-to-host-compiler
  313. endif # JETSON_EOL_MODULE_DETECT
  314. ifdef LLAMA_DEBUG
  315. MK_NVCCFLAGS += -lineinfo
  316. endif # LLAMA_DEBUG
  317. ifdef LLAMA_CUDA_NVCC
  318. NVCC = $(LLAMA_CUDA_NVCC)
  319. else
  320. NVCC = nvcc
  321. endif #LLAMA_CUDA_NVCC
  322. ifdef CUDA_DOCKER_ARCH
  323. MK_NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  324. else ifndef CUDA_POWER_ARCH
  325. MK_NVCCFLAGS += -arch=native
  326. endif # CUDA_DOCKER_ARCH
  327. ifdef LLAMA_CUDA_FORCE_DMMV
  328. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  329. endif # LLAMA_CUDA_FORCE_DMMV
  330. ifdef LLAMA_CUDA_FORCE_MMQ
  331. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  332. endif # LLAMA_CUDA_FORCE_MMQ
  333. ifdef LLAMA_CUDA_DMMV_X
  334. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  335. else
  336. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  337. endif # LLAMA_CUDA_DMMV_X
  338. ifdef LLAMA_CUDA_MMV_Y
  339. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  340. else ifdef LLAMA_CUDA_DMMV_Y
  341. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_DMMV_Y) # for backwards compatibility
  342. else
  343. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  344. endif # LLAMA_CUDA_MMV_Y
  345. ifdef LLAMA_CUDA_F16
  346. MK_NVCCFLAGS += -DGGML_CUDA_F16
  347. endif # LLAMA_CUDA_F16
  348. ifdef LLAMA_CUDA_DMMV_F16
  349. MK_NVCCFLAGS += -DGGML_CUDA_F16
  350. endif # LLAMA_CUDA_DMMV_F16
  351. ifdef LLAMA_CUDA_KQUANTS_ITER
  352. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  353. else
  354. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  355. endif
  356. ifdef LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  357. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(LLAMA_CUDA_PEER_MAX_BATCH_SIZE)
  358. else
  359. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  360. endif # LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  361. #ifdef LLAMA_CUDA_CUBLAS
  362. # MK_NVCCFLAGS += -DGGML_CUDA_CUBLAS
  363. #endif # LLAMA_CUDA_CUBLAS
  364. ifdef LLAMA_CUDA_CCBIN
  365. MK_NVCCFLAGS += -ccbin $(LLAMA_CUDA_CCBIN)
  366. endif
  367. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  368. ifdef JETSON_EOL_MODULE_DETECT
  369. $(NVCC) -I. -Icommon -D_XOPEN_SOURCE=600 -D_GNU_SOURCE -DNDEBUG -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I/usr/local/cuda/targets/aarch64-linux/include -std=c++11 -O3 $(NVCCFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  370. else
  371. $(NVCC) $(BASE_CXXFLAGS) $(NVCCFLAGS) -Wno-pedantic -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  372. endif # JETSON_EOL_MODULE_DETECT
  373. endif # LLAMA_CUBLAS
  374. ifdef LLAMA_CLBLAST
  375. MK_CPPFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags-only-I clblast OpenCL)
  376. MK_CFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  377. MK_CXXFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  378. # Mac provides OpenCL as a framework
  379. ifeq ($(UNAME_S),Darwin)
  380. MK_LDFLAGS += -lclblast -framework OpenCL
  381. else
  382. MK_LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
  383. endif
  384. OBJS += ggml-opencl.o
  385. ggml-opencl.o: ggml-opencl.cpp ggml-opencl.h
  386. $(CXX) $(CXXFLAGS) -c $< -o $@
  387. endif # LLAMA_CLBLAST
  388. ifdef LLAMA_VULKAN
  389. MK_CPPFLAGS += -DGGML_USE_VULKAN
  390. MK_LDFLAGS += -lvulkan
  391. OBJS += ggml-vulkan.o
  392. ifdef LLAMA_VULKAN_CHECK_RESULTS
  393. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  394. endif
  395. ifdef LLAMA_VULKAN_DEBUG
  396. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  397. endif
  398. ifdef LLAMA_VULKAN_VALIDATE
  399. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  400. endif
  401. ifdef LLAMA_VULKAN_RUN_TESTS
  402. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  403. endif
  404. ggml-vulkan.o: ggml-vulkan.cpp ggml-vulkan.h
  405. $(CXX) $(CXXFLAGS) -c $< -o $@
  406. endif # LLAMA_VULKAN
  407. ifdef LLAMA_HIPBLAS
  408. ifeq ($(wildcard /opt/rocm),)
  409. ROCM_PATH ?= /usr
  410. GPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  411. else
  412. ROCM_PATH ?= /opt/rocm
  413. GPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  414. endif
  415. HIPCC ?= $(ROCM_PATH)/bin/hipcc
  416. LLAMA_CUDA_DMMV_X ?= 32
  417. LLAMA_CUDA_MMV_Y ?= 1
  418. LLAMA_CUDA_KQUANTS_ITER ?= 2
  419. MK_CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUBLAS
  420. ifdef LLAMA_HIP_UMA
  421. MK_CPPFLAGS += -DGGML_HIP_UMA
  422. endif # LLAMA_HIP_UMA
  423. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  424. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  425. HIPFLAGS += $(addprefix --offload-arch=,$(GPU_TARGETS))
  426. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  427. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  428. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  429. ifdef LLAMA_CUDA_FORCE_DMMV
  430. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  431. endif # LLAMA_CUDA_FORCE_DMMV
  432. OBJS += ggml-cuda.o
  433. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  434. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  435. endif # LLAMA_HIPBLAS
  436. ifdef LLAMA_METAL
  437. MK_CPPFLAGS += -DGGML_USE_METAL
  438. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  439. OBJS += ggml-metal.o
  440. ifdef LLAMA_METAL_NDEBUG
  441. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  442. endif
  443. endif # LLAMA_METAL
  444. ifdef LLAMA_METAL
  445. ggml-metal.o: ggml-metal.m ggml-metal.h
  446. $(CC) $(CFLAGS) -c $< -o $@
  447. endif # LLAMA_METAL
  448. ifdef LLAMA_MPI
  449. ggml-mpi.o: ggml-mpi.c ggml-mpi.h
  450. $(CC) $(CFLAGS) -c $< -o $@
  451. endif # LLAMA_MPI
  452. GF_CC := $(CC)
  453. include scripts/get-flags.mk
  454. # combine build flags with cmdline overrides
  455. override CFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  456. BASE_CXXFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS) $(MK_CXXFLAGS) $(CXXFLAGS)
  457. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS)
  458. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  459. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  460. # identify CUDA host compiler
  461. ifdef LLAMA_CUBLAS
  462. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  463. include scripts/get-flags.mk
  464. CUDA_CXXFLAGS := $(GF_CXXFLAGS)
  465. endif
  466. #
  467. # Print build information
  468. #
  469. $(info I llama.cpp build info: )
  470. $(info I UNAME_S: $(UNAME_S))
  471. $(info I UNAME_P: $(UNAME_P))
  472. $(info I UNAME_M: $(UNAME_M))
  473. $(info I CFLAGS: $(CFLAGS))
  474. $(info I CXXFLAGS: $(CXXFLAGS))
  475. $(info I NVCCFLAGS: $(NVCCFLAGS))
  476. $(info I LDFLAGS: $(LDFLAGS))
  477. $(info I CC: $(shell $(CC) --version | head -n 1))
  478. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  479. ifdef LLAMA_CUBLAS
  480. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  481. endif # LLAMA_CUBLAS
  482. $(info )
  483. #
  484. # Build library
  485. #
  486. ggml.o: ggml.c ggml.h ggml-cuda.h
  487. $(CC) $(CFLAGS) -c $< -o $@
  488. ggml-alloc.o: ggml-alloc.c ggml.h ggml-alloc.h
  489. $(CC) $(CFLAGS) -c $< -o $@
  490. ggml-backend.o: ggml-backend.c ggml.h ggml-backend.h
  491. $(CC) $(CFLAGS) -c $< -o $@
  492. ggml-quants.o: ggml-quants.c ggml.h ggml-quants.h
  493. $(CC) $(CFLAGS) -c $< -o $@
  494. OBJS += ggml-alloc.o ggml-backend.o ggml-quants.o
  495. llama.o: llama.cpp ggml.h ggml-alloc.h ggml-backend.h ggml-cuda.h ggml-metal.h llama.h
  496. $(CXX) $(CXXFLAGS) -c $< -o $@
  497. COMMON_H_DEPS = common/common.h common/sampling.h common/log.h
  498. COMMON_DEPS = common.o sampling.o grammar-parser.o build-info.o
  499. common.o: common/common.cpp $(COMMON_H_DEPS)
  500. $(CXX) $(CXXFLAGS) -c $< -o $@
  501. sampling.o: common/sampling.cpp $(COMMON_H_DEPS)
  502. $(CXX) $(CXXFLAGS) -c $< -o $@
  503. console.o: common/console.cpp common/console.h
  504. $(CXX) $(CXXFLAGS) -c $< -o $@
  505. grammar-parser.o: common/grammar-parser.cpp common/grammar-parser.h
  506. $(CXX) $(CXXFLAGS) -c $< -o $@
  507. train.o: common/train.cpp common/train.h
  508. $(CXX) $(CXXFLAGS) -c $< -o $@
  509. libllama.so: llama.o ggml.o $(OBJS)
  510. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  511. libllama.a: llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  512. ar rcs libllama.a llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  513. clean:
  514. rm -vrf *.o tests/*.o *.so *.a *.dll benchmark-matmult common/build-info.cpp *.dot $(COV_TARGETS) $(BUILD_TARGETS) $(TEST_TARGETS)
  515. #
  516. # Examples
  517. #
  518. main: examples/main/main.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  519. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  520. @echo
  521. @echo '==== Run ./main -h for help. ===='
  522. @echo
  523. infill: examples/infill/infill.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  524. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  525. simple: examples/simple/simple.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  526. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  527. tokenize: examples/tokenize/tokenize.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  528. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  529. batched: examples/batched/batched.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  530. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  531. batched-bench: examples/batched-bench/batched-bench.cpp build-info.o ggml.o llama.o common.o $(OBJS)
  532. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  533. quantize: examples/quantize/quantize.cpp build-info.o ggml.o llama.o $(OBJS)
  534. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  535. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.o ggml.o llama.o $(OBJS)
  536. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  537. perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  538. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  539. imatrix: examples/imatrix/imatrix.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  540. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  541. embedding: examples/embedding/embedding.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  542. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  543. save-load-state: examples/save-load-state/save-load-state.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  544. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  545. server: examples/server/server.cpp examples/server/oai.hpp examples/server/utils.hpp examples/server/httplib.h examples/server/json.hpp examples/server/index.html.hpp examples/server/index.js.hpp examples/server/completion.js.hpp examples/llava/clip.cpp examples/llava/clip.h common/stb_image.h ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  546. $(CXX) $(CXXFLAGS) -Iexamples/server $(filter-out %.h,$(filter-out %.hpp,$^)) -o $@ $(LDFLAGS) $(LWINSOCK2) -Wno-cast-qual
  547. gguf: examples/gguf/gguf.cpp ggml.o $(OBJS)
  548. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  549. train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  550. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  551. convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp ggml.o llama.o $(OBJS)
  552. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  553. llama-bench: examples/llama-bench/llama-bench.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  554. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  555. libllava.a: examples/llava/llava.cpp examples/llava/llava.h examples/llava/clip.cpp examples/llava/clip.h common/stb_image.h common/base64.hpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  556. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  557. llava-cli: examples/llava/llava-cli.cpp examples/llava/clip.h examples/llava/clip.cpp examples/llava/llava.h examples/llava/llava.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  558. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  559. baby-llama: examples/baby-llama/baby-llama.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  560. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  561. beam-search: examples/beam-search/beam-search.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  562. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  563. finetune: examples/finetune/finetune.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  564. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  565. export-lora: examples/export-lora/export-lora.cpp ggml.o common/common.h $(OBJS)
  566. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  567. speculative: examples/speculative/speculative.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  568. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  569. parallel: examples/parallel/parallel.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  570. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  571. lookahead: examples/lookahead/lookahead.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  572. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  573. lookup: examples/lookup/lookup.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  574. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  575. passkey: examples/passkey/passkey.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  576. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  577. ifeq ($(UNAME_S),Darwin)
  578. swift: examples/batched.swift
  579. (cd examples/batched.swift; make build)
  580. endif
  581. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  582. @sh scripts/build-info.sh $(CC) > $@.tmp
  583. @if ! cmp -s $@.tmp $@; then \
  584. mv $@.tmp $@; \
  585. else \
  586. rm $@.tmp; \
  587. fi
  588. build-info.o: common/build-info.cpp
  589. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  590. #
  591. # Tests
  592. #
  593. tests: $(TEST_TARGETS)
  594. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.o ggml.o $(OBJS)
  595. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  596. run-benchmark-matmult: benchmark-matmult
  597. ./$@
  598. .PHONY: run-benchmark-matmult swift
  599. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  600. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  601. q8dot: pocs/vdot/q8dot.cpp ggml.o $(OBJS)
  602. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  603. tests/test-llama-grammar: tests/test-llama-grammar.cpp ggml.o grammar-parser.o $(OBJS)
  604. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  605. tests/test-grammar-parser: tests/test-grammar-parser.cpp ggml.o llama.o grammar-parser.o $(OBJS)
  606. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  607. tests/test-double-float: tests/test-double-float.cpp ggml.o $(OBJS)
  608. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  609. tests/test-grad0: tests/test-grad0.cpp ggml.o $(OBJS)
  610. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  611. tests/test-opt: tests/test-opt.cpp ggml.o $(OBJS)
  612. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  613. tests/test-quantize-fns: tests/test-quantize-fns.cpp ggml.o $(OBJS)
  614. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  615. tests/test-quantize-perf: tests/test-quantize-perf.cpp ggml.o $(OBJS)
  616. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  617. tests/test-sampling: tests/test-sampling.cpp ggml.o llama.o $(OBJS)
  618. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  619. tests/test-tokenizer-0-falcon: tests/test-tokenizer-0-falcon.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  620. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  621. tests/test-tokenizer-0-llama: tests/test-tokenizer-0-llama.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  622. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  623. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  624. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  625. tests/test-tokenizer-1-llama: tests/test-tokenizer-1-llama.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  626. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  627. tests/test-rope: tests/test-rope.cpp ggml.o $(OBJS)
  628. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  629. tests/test-c.o: tests/test-c.c llama.h
  630. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  631. tests/test-backend-ops: tests/test-backend-ops.cpp ggml.o $(OBJS)
  632. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  633. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp ggml.o llama.o tests/get-model.cpp $(COMMON_DEPS) $(OBJS)
  634. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  635. tests/test-autorelease: tests/test-autorelease.cpp ggml.o llama.o tests/get-model.cpp $(COMMON_DEPS) $(OBJS)
  636. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)