Makefile 22 KB

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