Makefile 25 KB

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