Makefile 20 KB

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