Makefile 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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 embd-input-test gguf llama-bench llava baby-llama beam-search \
  5. speculative infill benchmark-matmult parallel finetune export-lora 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
  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 LLVM,$(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. else
  153. MK_CPPFLAGS += -DNDEBUG
  154. endif
  155. ifdef LLAMA_SANITIZE_THREAD
  156. MK_CFLAGS += -fsanitize=thread -g
  157. MK_CXXFLAGS += -fsanitize=thread -g
  158. MK_LDFLAGS += -fsanitize=thread -g
  159. endif
  160. ifdef LLAMA_SANITIZE_ADDRESS
  161. MK_CFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  162. MK_CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  163. MK_LDFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  164. endif
  165. ifdef LLAMA_SANITIZE_UNDEFINED
  166. MK_CFLAGS += -fsanitize=undefined -g
  167. MK_CXXFLAGS += -fsanitize=undefined -g
  168. MK_LDFLAGS += -fsanitize=undefined -g
  169. endif
  170. ifdef LLAMA_SERVER_VERBOSE
  171. MK_CPPFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
  172. endif
  173. ifdef LLAMA_CODE_COVERAGE
  174. MK_CXXFLAGS += -fprofile-arcs -ftest-coverage -dumpbase ''
  175. endif
  176. ifdef LLAMA_DISABLE_LOGS
  177. MK_CPPFLAGS += -DLOG_DISABLE_LOGS
  178. endif # LLAMA_DISABLE_LOGS
  179. # warnings
  180. WARN_FLAGS = -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
  181. MK_CFLAGS += $(WARN_FLAGS) -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int \
  182. -Werror=implicit-function-declaration
  183. MK_CXXFLAGS += $(WARN_FLAGS) -Wmissing-declarations -Wmissing-noreturn
  184. ifeq ($(CC_IS_CLANG), 1)
  185. # clang options
  186. MK_CFLAGS += -Wunreachable-code-break -Wunreachable-code-return
  187. MK_HOST_CXXFLAGS += -Wunreachable-code-break -Wunreachable-code-return -Wmissing-prototypes -Wextra-semi
  188. ifneq '' '$(and $(CC_IS_LLVM_CLANG),$(filter 1,$(shell expr $(CC_VER) \>= 030800)))'
  189. MK_CFLAGS += -Wdouble-promotion
  190. endif
  191. ifneq '' '$(and $(CC_IS_APPLE_CLANG),$(filter 1,$(shell expr $(CC_VER) \>= 070300)))'
  192. MK_CFLAGS += -Wdouble-promotion
  193. endif
  194. else
  195. # gcc options
  196. MK_CFLAGS += -Wdouble-promotion
  197. MK_HOST_CXXFLAGS += -Wno-array-bounds
  198. ifeq ($(shell expr $(CC_VER) \>= 070100), 1)
  199. MK_HOST_CXXFLAGS += -Wno-format-truncation
  200. endif
  201. ifeq ($(shell expr $(CC_VER) \>= 080100), 1)
  202. MK_HOST_CXXFLAGS += -Wextra-semi
  203. endif
  204. endif
  205. # OS specific
  206. # TODO: support Windows
  207. ifneq '' '$(filter $(UNAME_S),Linux Darwin FreeBSD NetBSD OpenBSD Haiku)'
  208. MK_CFLAGS += -pthread
  209. MK_CXXFLAGS += -pthread
  210. endif
  211. # detect Windows
  212. ifneq ($(findstring _NT,$(UNAME_S)),)
  213. _WIN32 := 1
  214. endif
  215. # library name prefix
  216. ifneq ($(_WIN32),1)
  217. LIB_PRE := lib
  218. endif
  219. # Dynamic Shared Object extension
  220. ifneq ($(_WIN32),1)
  221. DSO_EXT := .so
  222. else
  223. DSO_EXT := .dll
  224. endif
  225. # Windows Sockets 2 (Winsock) for network-capable apps
  226. ifeq ($(_WIN32),1)
  227. LWINSOCK2 := -lws2_32
  228. endif
  229. ifdef LLAMA_GPROF
  230. MK_CFLAGS += -pg
  231. MK_CXXFLAGS += -pg
  232. endif
  233. ifdef LLAMA_PERF
  234. MK_CPPFLAGS += -DGGML_PERF
  235. endif
  236. # Architecture specific
  237. # TODO: probably these flags need to be tweaked on some architectures
  238. # feel free to update the Makefile for your architecture and send a pull request or issue
  239. ifndef RISCV
  240. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
  241. # Use all CPU extensions that are available:
  242. MK_CFLAGS += -march=native -mtune=native
  243. MK_HOST_CXXFLAGS += -march=native -mtune=native
  244. # Usage AVX-only
  245. #MK_CFLAGS += -mfma -mf16c -mavx
  246. #MK_CXXFLAGS += -mfma -mf16c -mavx
  247. # Usage SSSE3-only (Not is SSE3!)
  248. #MK_CFLAGS += -mssse3
  249. #MK_CXXFLAGS += -mssse3
  250. endif
  251. # The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
  252. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
  253. # https://github.com/ggerganov/llama.cpp/issues/2922
  254. ifneq '' '$(findstring mingw,$(shell $(CC) -dumpmachine))'
  255. MK_CFLAGS += -Xassembler -muse-unaligned-vector-move
  256. MK_CXXFLAGS += -Xassembler -muse-unaligned-vector-move
  257. endif
  258. ifneq ($(filter aarch64%,$(UNAME_M)),)
  259. # Apple M1, M2, etc.
  260. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  261. MK_CFLAGS += -mcpu=native
  262. MK_CXXFLAGS += -mcpu=native
  263. endif
  264. ifneq ($(filter armv6%,$(UNAME_M)),)
  265. # Raspberry Pi 1, Zero
  266. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  267. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  268. endif
  269. ifneq ($(filter armv7%,$(UNAME_M)),)
  270. # Raspberry Pi 2
  271. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  272. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  273. endif
  274. ifneq ($(filter armv8%,$(UNAME_M)),)
  275. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  276. MK_CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  277. MK_CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
  278. endif
  279. ifneq ($(filter ppc64%,$(UNAME_M)),)
  280. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  281. ifneq (,$(findstring POWER9,$(POWER9_M)))
  282. MK_CFLAGS += -mcpu=power9
  283. MK_CXXFLAGS += -mcpu=power9
  284. endif
  285. endif
  286. else
  287. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  288. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  289. endif
  290. ifndef LLAMA_NO_K_QUANTS
  291. MK_CPPFLAGS += -DGGML_USE_K_QUANTS
  292. OBJS += k_quants.o
  293. ifdef LLAMA_QKK_64
  294. MK_CPPFLAGS += -DGGML_QKK_64
  295. endif
  296. endif
  297. ifndef LLAMA_NO_ACCELERATE
  298. # Mac OS - include Accelerate framework.
  299. # `-framework Accelerate` works both with Apple Silicon and Mac Intel
  300. ifeq ($(UNAME_S),Darwin)
  301. MK_CPPFLAGS += -DGGML_USE_ACCELERATE
  302. MK_CPPFLAGS += -DACCELERATE_NEW_LAPACK
  303. MK_CPPFLAGS += -DACCELERATE_LAPACK_ILP64
  304. MK_LDFLAGS += -framework Accelerate
  305. endif
  306. endif # LLAMA_NO_ACCELERATE
  307. ifdef LLAMA_MPI
  308. MK_CPPFLAGS += -DGGML_USE_MPI
  309. MK_CFLAGS += -Wno-cast-qual
  310. MK_CXXFLAGS += -Wno-cast-qual
  311. OBJS += ggml-mpi.o
  312. endif # LLAMA_MPI
  313. ifdef LLAMA_OPENBLAS
  314. MK_CPPFLAGS += -DGGML_USE_OPENBLAS $(shell pkg-config --cflags-only-I openblas)
  315. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
  316. MK_LDFLAGS += $(shell pkg-config --libs openblas)
  317. endif # LLAMA_OPENBLAS
  318. ifdef LLAMA_BLIS
  319. MK_CPPFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
  320. MK_LDFLAGS += -lblis -L/usr/local/lib
  321. endif # LLAMA_BLIS
  322. ifdef LLAMA_CUBLAS
  323. MK_CPPFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  324. 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
  325. OBJS += ggml-cuda.o
  326. NVCCFLAGS = --forward-unknown-to-host-compiler -use_fast_math
  327. ifdef LLAMA_CUDA_NVCC
  328. NVCC = $(LLAMA_CUDA_NVCC)
  329. else
  330. NVCC = nvcc
  331. endif #LLAMA_CUDA_NVCC
  332. ifdef CUDA_DOCKER_ARCH
  333. NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  334. else
  335. NVCCFLAGS += -arch=native
  336. endif # CUDA_DOCKER_ARCH
  337. ifdef LLAMA_CUDA_FORCE_DMMV
  338. NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  339. endif # LLAMA_CUDA_FORCE_DMMV
  340. ifdef LLAMA_CUDA_DMMV_X
  341. NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  342. else
  343. NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  344. endif # LLAMA_CUDA_DMMV_X
  345. ifdef LLAMA_CUDA_MMV_Y
  346. NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  347. else ifdef LLAMA_CUDA_DMMV_Y
  348. NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_DMMV_Y) # for backwards compatibility
  349. else
  350. NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  351. endif # LLAMA_CUDA_MMV_Y
  352. ifdef LLAMA_CUDA_F16
  353. NVCCFLAGS += -DGGML_CUDA_F16
  354. endif # LLAMA_CUDA_F16
  355. ifdef LLAMA_CUDA_DMMV_F16
  356. NVCCFLAGS += -DGGML_CUDA_F16
  357. endif # LLAMA_CUDA_DMMV_F16
  358. ifdef LLAMA_CUDA_KQUANTS_ITER
  359. NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  360. else
  361. NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  362. endif
  363. ifdef LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  364. NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(LLAMA_CUDA_PEER_MAX_BATCH_SIZE)
  365. else
  366. NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  367. endif # LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  368. #ifdef LLAMA_CUDA_CUBLAS
  369. # NVCCFLAGS += -DGGML_CUDA_CUBLAS
  370. #endif # LLAMA_CUDA_CUBLAS
  371. ifdef LLAMA_CUDA_CCBIN
  372. NVCCFLAGS += -ccbin $(LLAMA_CUDA_CCBIN)
  373. endif
  374. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  375. $(NVCC) $(NVCCFLAGS) -c $< -o $@
  376. endif # LLAMA_CUBLAS
  377. ifdef LLAMA_CLBLAST
  378. MK_CPPFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags-only-I clblast OpenCL)
  379. MK_CFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  380. MK_CXXFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  381. # Mac provides OpenCL as a framework
  382. ifeq ($(UNAME_S),Darwin)
  383. MK_LDFLAGS += -lclblast -framework OpenCL
  384. else
  385. MK_LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
  386. endif
  387. OBJS += ggml-opencl.o
  388. ggml-opencl.o: ggml-opencl.cpp ggml-opencl.h
  389. $(CXX) $(CXXFLAGS) -c $< -o $@
  390. endif # LLAMA_CLBLAST
  391. ifdef LLAMA_HIPBLAS
  392. ROCM_PATH ?= /opt/rocm
  393. HIPCC ?= $(ROCM_PATH)/bin/hipcc
  394. GPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  395. LLAMA_CUDA_DMMV_X ?= 32
  396. LLAMA_CUDA_MMV_Y ?= 1
  397. LLAMA_CUDA_KQUANTS_ITER ?= 2
  398. MK_CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUBLAS
  399. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  400. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  401. HIPFLAGS += $(addprefix --offload-arch=,$(GPU_TARGETS))
  402. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  403. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  404. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  405. ifdef LLAMA_CUDA_FORCE_DMMV
  406. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  407. endif # LLAMA_CUDA_FORCE_DMMV
  408. OBJS += ggml-cuda.o
  409. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  410. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  411. endif # LLAMA_HIPBLAS
  412. ifdef LLAMA_METAL
  413. MK_CPPFLAGS += -DGGML_USE_METAL
  414. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  415. OBJS += ggml-metal.o
  416. ifdef LLAMA_METAL_NDEBUG
  417. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  418. endif
  419. endif # LLAMA_METAL
  420. ifdef LLAMA_METAL
  421. ggml-metal.o: ggml-metal.m ggml-metal.h
  422. $(CC) $(CFLAGS) -c $< -o $@
  423. endif # LLAMA_METAL
  424. ifdef LLAMA_MPI
  425. ggml-mpi.o: ggml-mpi.c ggml-mpi.h
  426. $(CC) $(CFLAGS) -c $< -o $@
  427. endif # LLAMA_MPI
  428. ifndef LLAMA_NO_K_QUANTS
  429. k_quants.o: k_quants.c k_quants.h
  430. $(CC) $(CFLAGS) -c $< -o $@
  431. endif # LLAMA_NO_K_QUANTS
  432. # combine build flags with cmdline overrides
  433. override CFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS) $(MK_CFLAGS) $(CFLAGS)
  434. override CXXFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS) $(MK_CXXFLAGS) $(CXXFLAGS)
  435. override CUDA_CXXFLAGS := $(MK_CUDA_CXXFLAGS) $(CUDA_CXXFLAGS)
  436. override HOST_CXXFLAGS := $(MK_HOST_CXXFLAGS) $(HOST_CXXFLAGS)
  437. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  438. # save CXXFLAGS before we add host-only options
  439. NVCCFLAGS := $(NVCCFLAGS) $(CXXFLAGS) $(CUDA_CXXFLAGS) -Wno-pedantic -Xcompiler "$(HOST_CXXFLAGS)"
  440. override CXXFLAGS += $(HOST_CXXFLAGS)
  441. #
  442. # Print build information
  443. #
  444. $(info I llama.cpp build info: )
  445. $(info I UNAME_S: $(UNAME_S))
  446. $(info I UNAME_P: $(UNAME_P))
  447. $(info I UNAME_M: $(UNAME_M))
  448. $(info I CFLAGS: $(CFLAGS))
  449. $(info I CXXFLAGS: $(CXXFLAGS))
  450. $(info I NVCCFLAGS: $(NVCCFLAGS))
  451. $(info I LDFLAGS: $(LDFLAGS))
  452. $(info I CC: $(shell $(CC) --version | head -n 1))
  453. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  454. $(info )
  455. #
  456. # Build library
  457. #
  458. ggml.o: ggml.c ggml.h ggml-cuda.h
  459. $(CC) $(CFLAGS) -c $< -o $@
  460. ggml-alloc.o: ggml-alloc.c ggml.h ggml-alloc.h
  461. $(CC) $(CFLAGS) -c $< -o $@
  462. ggml-backend.o: ggml-backend.c ggml.h ggml-backend.h
  463. $(CC) $(CFLAGS) -c $< -o $@
  464. OBJS += ggml-alloc.o ggml-backend.o
  465. llama.o: llama.cpp ggml.h ggml-alloc.h ggml-backend.h ggml-cuda.h ggml-metal.h llama.h
  466. $(CXX) $(CXXFLAGS) -c $< -o $@
  467. COMMON_H_DEPS = common/common.h common/sampling.h build-info.h common/log.h
  468. COMMON_DEPS = $(COMMON_H_DEPS) common.o sampling.o
  469. common.o: common/common.cpp $(COMMON_H_DEPS)
  470. $(CXX) $(CXXFLAGS) -c $< -o $@
  471. sampling.o: common/sampling.cpp $(COMMON_H_DEPS)
  472. $(CXX) $(CXXFLAGS) -c $< -o $@
  473. console.o: common/console.cpp common/console.h
  474. $(CXX) $(CXXFLAGS) -c $< -o $@
  475. grammar-parser.o: common/grammar-parser.cpp common/grammar-parser.h
  476. $(CXX) $(CXXFLAGS) -c $< -o $@
  477. train.o: common/train.cpp common/train.h
  478. $(CXX) $(CXXFLAGS) -c $< -o $@
  479. libllama.so: llama.o ggml.o $(OBJS)
  480. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  481. clean:
  482. rm -vrf *.o tests/*.o *.so *.dll benchmark-matmult build-info.h *.dot $(COV_TARGETS) $(BUILD_TARGETS) $(TEST_TARGETS)
  483. #
  484. # Examples
  485. #
  486. main: examples/main/main.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  487. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  488. @echo
  489. @echo '==== Run ./main -h for help. ===='
  490. @echo
  491. infill: examples/infill/infill.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  492. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  493. simple: examples/simple/simple.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  494. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  495. batched: examples/batched/batched.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  496. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  497. batched-bench: examples/batched-bench/batched-bench.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  498. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  499. quantize: examples/quantize/quantize.cpp build-info.h ggml.o llama.o $(OBJS)
  500. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  501. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.h ggml.o llama.o $(OBJS)
  502. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  503. perplexity: examples/perplexity/perplexity.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  504. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  505. embedding: examples/embedding/embedding.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  506. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  507. save-load-state: examples/save-load-state/save-load-state.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  508. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  509. 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 build-info.h ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  510. $(CXX) $(CXXFLAGS) -Iexamples/server $(filter-out %.h,$(filter-out %.hpp,$^)) -o $@ $(LDFLAGS) $(LWINSOCK2)
  511. $(LIB_PRE)embdinput$(DSO_EXT): examples/embd-input/embd-input.h examples/embd-input/embd-input-lib.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  512. $(CXX) --shared $(CXXFLAGS) $(filter-out %.h,$(filter-out %.hpp,$^)) -o $@ $(LDFLAGS)
  513. embd-input-test: $(LIB_PRE)embdinput$(DSO_EXT) examples/embd-input/embd-input-test.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  514. $(CXX) $(CXXFLAGS) $(filter-out %$(DSO_EXT),$(filter-out %.h,$(filter-out %.hpp,$^))) -o $@ $(LDFLAGS) -L. -lembdinput
  515. gguf: examples/gguf/gguf.cpp ggml.o llama.o $(OBJS)
  516. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  517. train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  518. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  519. convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp ggml.o llama.o $(OBJS)
  520. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  521. llama-bench: examples/llama-bench/llama-bench.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  522. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  523. llava: examples/llava/llava.cpp examples/llava/llava-utils.h examples/llava/clip.cpp examples/llava/clip.h common/stb_image.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  524. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  525. baby-llama: examples/baby-llama/baby-llama.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  526. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  527. beam-search: examples/beam-search/beam-search.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  528. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  529. finetune: examples/finetune/finetune.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  530. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  531. export-lora: examples/export-lora/export-lora.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  532. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  533. speculative: examples/speculative/speculative.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  534. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  535. parallel: examples/parallel/parallel.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  536. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  537. ifdef LLAMA_METAL
  538. metal: examples/metal/metal.cpp ggml.o $(OBJS)
  539. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  540. endif
  541. ifeq ($(UNAME_S),Darwin)
  542. swift: examples/batched.swift
  543. (cd examples/batched.swift; make build)
  544. endif
  545. build-info.h: $(wildcard .git/index) scripts/build-info.sh
  546. @sh scripts/build-info.sh $(CC) > $@.tmp
  547. @if ! cmp -s $@.tmp $@; then \
  548. mv $@.tmp $@; \
  549. else \
  550. rm $@.tmp; \
  551. fi
  552. #
  553. # Tests
  554. #
  555. tests: $(TEST_TARGETS)
  556. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.h ggml.o $(OBJS)
  557. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  558. run-benchmark-matmult: benchmark-matmult
  559. ./$@
  560. .PHONY: run-benchmark-matmult swift
  561. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  562. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  563. q8dot: pocs/vdot/q8dot.cpp ggml.o $(OBJS)
  564. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  565. tests/test-llama-grammar: tests/test-llama-grammar.cpp build-info.h ggml.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  566. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  567. tests/test-grammar-parser: tests/test-grammar-parser.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  568. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  569. tests/test-double-float: tests/test-double-float.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  570. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  571. tests/test-grad0: tests/test-grad0.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  572. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  573. tests/test-opt: tests/test-opt.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  574. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  575. tests/test-quantize-fns: tests/test-quantize-fns.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  576. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  577. tests/test-quantize-perf: tests/test-quantize-perf.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  578. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  579. tests/test-sampling: tests/test-sampling.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  580. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  581. tests/test-tokenizer-0-falcon: tests/test-tokenizer-0-falcon.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  582. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  583. tests/test-tokenizer-0-llama: tests/test-tokenizer-0-llama.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  584. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  585. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  586. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  587. tests/test-tokenizer-1-llama: tests/test-tokenizer-1-llama.cpp build-info.h ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  588. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  589. tests/test-c.o: tests/test-c.c llama.h
  590. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@