1
0

Makefile 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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 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. ifdef LLAMA_QKK_64
  291. MK_CPPFLAGS += -DGGML_QKK_64
  292. endif
  293. ifndef LLAMA_NO_ACCELERATE
  294. # Mac OS - include Accelerate framework.
  295. # `-framework Accelerate` works both with Apple Silicon and Mac Intel
  296. ifeq ($(UNAME_S),Darwin)
  297. MK_CPPFLAGS += -DGGML_USE_ACCELERATE
  298. MK_CPPFLAGS += -DACCELERATE_NEW_LAPACK
  299. MK_CPPFLAGS += -DACCELERATE_LAPACK_ILP64
  300. MK_LDFLAGS += -framework Accelerate
  301. endif
  302. endif # LLAMA_NO_ACCELERATE
  303. ifdef LLAMA_MPI
  304. MK_CPPFLAGS += -DGGML_USE_MPI
  305. MK_CFLAGS += -Wno-cast-qual
  306. MK_CXXFLAGS += -Wno-cast-qual
  307. OBJS += ggml-mpi.o
  308. endif # LLAMA_MPI
  309. ifdef LLAMA_OPENBLAS
  310. MK_CPPFLAGS += -DGGML_USE_OPENBLAS $(shell pkg-config --cflags-only-I openblas)
  311. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
  312. MK_LDFLAGS += $(shell pkg-config --libs openblas)
  313. endif # LLAMA_OPENBLAS
  314. ifdef LLAMA_BLIS
  315. MK_CPPFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
  316. MK_LDFLAGS += -lblis -L/usr/local/lib
  317. endif # LLAMA_BLIS
  318. ifdef LLAMA_CUBLAS
  319. MK_CPPFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  320. 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
  321. OBJS += ggml-cuda.o
  322. NVCCFLAGS = --forward-unknown-to-host-compiler -use_fast_math
  323. ifdef LLAMA_CUDA_NVCC
  324. NVCC = $(LLAMA_CUDA_NVCC)
  325. else
  326. NVCC = nvcc
  327. endif #LLAMA_CUDA_NVCC
  328. ifdef CUDA_DOCKER_ARCH
  329. NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  330. else
  331. NVCCFLAGS += -arch=native
  332. endif # CUDA_DOCKER_ARCH
  333. ifdef LLAMA_CUDA_FORCE_DMMV
  334. NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  335. endif # LLAMA_CUDA_FORCE_DMMV
  336. ifdef LLAMA_CUDA_FORCE_MMQ
  337. NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  338. endif # LLAMA_CUDA_FORCE_MMQ
  339. ifdef LLAMA_CUDA_DMMV_X
  340. NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  341. else
  342. NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  343. endif # LLAMA_CUDA_DMMV_X
  344. ifdef LLAMA_CUDA_MMV_Y
  345. NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  346. else ifdef LLAMA_CUDA_DMMV_Y
  347. NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_DMMV_Y) # for backwards compatibility
  348. else
  349. NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  350. endif # LLAMA_CUDA_MMV_Y
  351. ifdef LLAMA_CUDA_F16
  352. NVCCFLAGS += -DGGML_CUDA_F16
  353. endif # LLAMA_CUDA_F16
  354. ifdef LLAMA_CUDA_DMMV_F16
  355. NVCCFLAGS += -DGGML_CUDA_F16
  356. endif # LLAMA_CUDA_DMMV_F16
  357. ifdef LLAMA_CUDA_KQUANTS_ITER
  358. NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  359. else
  360. NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  361. endif
  362. ifdef LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  363. NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(LLAMA_CUDA_PEER_MAX_BATCH_SIZE)
  364. else
  365. NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  366. endif # LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  367. #ifdef LLAMA_CUDA_CUBLAS
  368. # NVCCFLAGS += -DGGML_CUDA_CUBLAS
  369. #endif # LLAMA_CUDA_CUBLAS
  370. ifdef LLAMA_CUDA_CCBIN
  371. NVCCFLAGS += -ccbin $(LLAMA_CUDA_CCBIN)
  372. endif
  373. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  374. $(NVCC) $(NVCCFLAGS) -c $< -o $@
  375. endif # LLAMA_CUBLAS
  376. ifdef LLAMA_CLBLAST
  377. MK_CPPFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags-only-I clblast OpenCL)
  378. MK_CFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  379. MK_CXXFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  380. # Mac provides OpenCL as a framework
  381. ifeq ($(UNAME_S),Darwin)
  382. MK_LDFLAGS += -lclblast -framework OpenCL
  383. else
  384. MK_LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
  385. endif
  386. OBJS += ggml-opencl.o
  387. ggml-opencl.o: ggml-opencl.cpp ggml-opencl.h
  388. $(CXX) $(CXXFLAGS) -c $< -o $@
  389. endif # LLAMA_CLBLAST
  390. ifdef LLAMA_HIPBLAS
  391. ROCM_PATH ?= /opt/rocm
  392. HIPCC ?= $(ROCM_PATH)/bin/hipcc
  393. GPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  394. LLAMA_CUDA_DMMV_X ?= 32
  395. LLAMA_CUDA_MMV_Y ?= 1
  396. LLAMA_CUDA_KQUANTS_ITER ?= 2
  397. MK_CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUBLAS
  398. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  399. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  400. HIPFLAGS += $(addprefix --offload-arch=,$(GPU_TARGETS))
  401. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  402. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  403. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  404. ifdef LLAMA_CUDA_FORCE_DMMV
  405. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  406. endif # LLAMA_CUDA_FORCE_DMMV
  407. OBJS += ggml-cuda.o
  408. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  409. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  410. endif # LLAMA_HIPBLAS
  411. ifdef LLAMA_METAL
  412. MK_CPPFLAGS += -DGGML_USE_METAL
  413. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  414. OBJS += ggml-metal.o
  415. ifdef LLAMA_METAL_NDEBUG
  416. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  417. endif
  418. endif # LLAMA_METAL
  419. ifdef LLAMA_METAL
  420. ggml-metal.o: ggml-metal.m ggml-metal.h
  421. $(CC) $(CFLAGS) -c $< -o $@
  422. endif # LLAMA_METAL
  423. ifdef LLAMA_MPI
  424. ggml-mpi.o: ggml-mpi.c ggml-mpi.h
  425. $(CC) $(CFLAGS) -c $< -o $@
  426. endif # LLAMA_MPI
  427. # combine build flags with cmdline overrides
  428. override CFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS) $(MK_CFLAGS) $(CFLAGS)
  429. override CXXFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS) $(MK_CXXFLAGS) $(CXXFLAGS)
  430. override CUDA_CXXFLAGS := $(MK_CUDA_CXXFLAGS) $(CUDA_CXXFLAGS)
  431. override HOST_CXXFLAGS := $(MK_HOST_CXXFLAGS) $(HOST_CXXFLAGS)
  432. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  433. # save CXXFLAGS before we add host-only options
  434. NVCCFLAGS := $(NVCCFLAGS) $(CXXFLAGS) $(CUDA_CXXFLAGS) -Wno-pedantic -Xcompiler "$(HOST_CXXFLAGS)"
  435. override CXXFLAGS += $(HOST_CXXFLAGS)
  436. #
  437. # Print build information
  438. #
  439. $(info I llama.cpp build info: )
  440. $(info I UNAME_S: $(UNAME_S))
  441. $(info I UNAME_P: $(UNAME_P))
  442. $(info I UNAME_M: $(UNAME_M))
  443. $(info I CFLAGS: $(CFLAGS))
  444. $(info I CXXFLAGS: $(CXXFLAGS))
  445. $(info I NVCCFLAGS: $(NVCCFLAGS))
  446. $(info I LDFLAGS: $(LDFLAGS))
  447. $(info I CC: $(shell $(CC) --version | head -n 1))
  448. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  449. $(info )
  450. #
  451. # Build library
  452. #
  453. ggml.o: ggml.c ggml.h ggml-cuda.h
  454. $(CC) $(CFLAGS) -c $< -o $@
  455. ggml-alloc.o: ggml-alloc.c ggml.h ggml-alloc.h
  456. $(CC) $(CFLAGS) -c $< -o $@
  457. ggml-backend.o: ggml-backend.c ggml.h ggml-backend.h
  458. $(CC) $(CFLAGS) -c $< -o $@
  459. ggml-quants.o: ggml-quants.c ggml.h ggml-quants.h
  460. $(CC) $(CFLAGS) -c $< -o $@
  461. OBJS += ggml-alloc.o ggml-backend.o ggml-quants.o
  462. llama.o: llama.cpp ggml.h ggml-alloc.h ggml-backend.h ggml-cuda.h ggml-metal.h llama.h
  463. $(CXX) $(CXXFLAGS) -c $< -o $@
  464. COMMON_H_DEPS = common/common.h common/sampling.h common/log.h
  465. COMMON_DEPS = common.o sampling.o grammar-parser.o build-info.o
  466. common.o: common/common.cpp $(COMMON_H_DEPS)
  467. $(CXX) $(CXXFLAGS) -c $< -o $@
  468. sampling.o: common/sampling.cpp $(COMMON_H_DEPS)
  469. $(CXX) $(CXXFLAGS) -c $< -o $@
  470. console.o: common/console.cpp common/console.h
  471. $(CXX) $(CXXFLAGS) -c $< -o $@
  472. grammar-parser.o: common/grammar-parser.cpp common/grammar-parser.h
  473. $(CXX) $(CXXFLAGS) -c $< -o $@
  474. train.o: common/train.cpp common/train.h
  475. $(CXX) $(CXXFLAGS) -c $< -o $@
  476. libllama.so: llama.o ggml.o $(OBJS)
  477. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  478. clean:
  479. rm -vrf *.o tests/*.o *.so *.dll benchmark-matmult common/build-info.cpp *.dot $(COV_TARGETS) $(BUILD_TARGETS) $(TEST_TARGETS)
  480. #
  481. # Examples
  482. #
  483. main: examples/main/main.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  484. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  485. @echo
  486. @echo '==== Run ./main -h for help. ===='
  487. @echo
  488. infill: examples/infill/infill.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  489. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  490. simple: examples/simple/simple.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  491. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  492. batched: examples/batched/batched.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  493. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  494. batched-bench: examples/batched-bench/batched-bench.cpp build-info.o ggml.o llama.o common.o $(OBJS)
  495. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  496. quantize: examples/quantize/quantize.cpp build-info.o ggml.o llama.o $(OBJS)
  497. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  498. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.o ggml.o llama.o $(OBJS)
  499. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  500. perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  501. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  502. embedding: examples/embedding/embedding.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  503. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  504. save-load-state: examples/save-load-state/save-load-state.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  505. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  506. 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)
  507. $(CXX) $(CXXFLAGS) -Iexamples/server $(filter-out %.h,$(filter-out %.hpp,$^)) -o $@ $(LDFLAGS) $(LWINSOCK2) -Wno-cast-qual
  508. gguf: examples/gguf/gguf.cpp ggml.o llama.o $(OBJS)
  509. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  510. train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  511. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  512. convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp ggml.o llama.o $(OBJS)
  513. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  514. llama-bench: examples/llama-bench/llama-bench.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  515. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  516. 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)
  517. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ $(LDFLAGS) -Wno-cast-qual
  518. 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)
  519. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  520. baby-llama: examples/baby-llama/baby-llama.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  521. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  522. beam-search: examples/beam-search/beam-search.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  523. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  524. finetune: examples/finetune/finetune.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  525. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  526. export-lora: examples/export-lora/export-lora.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  527. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  528. speculative: examples/speculative/speculative.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  529. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  530. parallel: examples/parallel/parallel.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  531. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  532. ifdef LLAMA_METAL
  533. metal: examples/metal/metal.cpp ggml.o $(OBJS)
  534. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  535. endif
  536. ifeq ($(UNAME_S),Darwin)
  537. swift: examples/batched.swift
  538. (cd examples/batched.swift; make build)
  539. endif
  540. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  541. @sh scripts/build-info.sh $(CC) > $@.tmp
  542. @if ! cmp -s $@.tmp $@; then \
  543. mv $@.tmp $@; \
  544. else \
  545. rm $@.tmp; \
  546. fi
  547. build-info.o: common/build-info.cpp
  548. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  549. #
  550. # Tests
  551. #
  552. tests: $(TEST_TARGETS)
  553. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.o ggml.o $(OBJS)
  554. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  555. run-benchmark-matmult: benchmark-matmult
  556. ./$@
  557. .PHONY: run-benchmark-matmult swift
  558. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  559. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  560. q8dot: pocs/vdot/q8dot.cpp ggml.o $(OBJS)
  561. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  562. tests/test-llama-grammar: tests/test-llama-grammar.cpp ggml.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  563. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  564. tests/test-grammar-parser: tests/test-grammar-parser.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  565. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  566. tests/test-double-float: tests/test-double-float.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  567. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  568. tests/test-grad0: tests/test-grad0.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  569. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  570. tests/test-opt: tests/test-opt.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  571. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  572. tests/test-quantize-fns: tests/test-quantize-fns.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  573. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  574. tests/test-quantize-perf: tests/test-quantize-perf.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  575. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  576. tests/test-sampling: tests/test-sampling.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  577. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  578. tests/test-tokenizer-0-falcon: tests/test-tokenizer-0-falcon.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  579. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  580. tests/test-tokenizer-0-llama: tests/test-tokenizer-0-llama.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  581. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  582. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  583. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  584. tests/test-tokenizer-1-llama: tests/test-tokenizer-1-llama.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  585. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  586. tests/test-c.o: tests/test-c.c llama.h
  587. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@