Makefile 25 KB

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