Makefile 24 KB

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