Makefile 24 KB

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