Makefile 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 tests/test-model-load-cancel tests/test-autorelease
  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. MK_NVCCFLAGS = -std=c++11
  88. # -Ofast tends to produce faster code, but may not be available for some compilers.
  89. ifdef LLAMA_FAST
  90. MK_CFLAGS += -Ofast
  91. HOST_CXXFLAGS += -Ofast
  92. MK_NVCCFLAGS += -O3
  93. else
  94. MK_CFLAGS += -O3
  95. MK_CXXFLAGS += -O3
  96. MK_NVCCFLAGS += -O3
  97. endif
  98. ifndef LLAMA_NO_CCACHE
  99. CCACHE := $(shell which ccache)
  100. ifdef CCACHE
  101. export CCACHE_SLOPPINESS = time_macros
  102. $(info I ccache found, compilation results will be cached. Disable with LLAMA_NO_CCACHE.)
  103. CC := $(CCACHE) $(CC)
  104. CXX := $(CCACHE) $(CXX)
  105. else
  106. $(info I ccache not found. Consider installing it for faster compilation.)
  107. endif # CCACHE
  108. endif # LLAMA_NO_CCACHE
  109. # clock_gettime came in POSIX.1b (1993)
  110. # CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
  111. # posix_memalign came in POSIX.1-2001 / SUSv3
  112. # M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
  113. MK_CPPFLAGS += -D_XOPEN_SOURCE=600
  114. # Somehow in OpenBSD whenever POSIX conformance is specified
  115. # some string functions rely on locale_t availability,
  116. # which was introduced in POSIX.1-2008, forcing us to go higher
  117. ifeq ($(UNAME_S),OpenBSD)
  118. MK_CPPFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
  119. endif
  120. # Data types, macros and functions related to controlling CPU affinity and
  121. # some memory allocation are available on Linux through GNU extensions in libc
  122. ifeq ($(UNAME_S),Linux)
  123. MK_CPPFLAGS += -D_GNU_SOURCE
  124. endif
  125. # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
  126. # and on macOS its availability depends on enabling Darwin extensions
  127. # similarly on DragonFly, enabling BSD extensions is necessary
  128. ifeq ($(UNAME_S),Darwin)
  129. MK_CPPFLAGS += -D_DARWIN_C_SOURCE
  130. endif
  131. ifeq ($(UNAME_S),DragonFly)
  132. MK_CPPFLAGS += -D__BSD_VISIBLE
  133. endif
  134. # alloca is a non-standard interface that is not visible on BSDs when
  135. # POSIX conformance is specified, but not all of them provide a clean way
  136. # to enable it in such cases
  137. ifeq ($(UNAME_S),FreeBSD)
  138. MK_CPPFLAGS += -D__BSD_VISIBLE
  139. endif
  140. ifeq ($(UNAME_S),NetBSD)
  141. MK_CPPFLAGS += -D_NETBSD_SOURCE
  142. endif
  143. ifeq ($(UNAME_S),OpenBSD)
  144. MK_CPPFLAGS += -D_BSD_SOURCE
  145. endif
  146. ifdef LLAMA_DEBUG
  147. MK_CFLAGS += -O0 -g
  148. MK_CXXFLAGS += -O0 -g
  149. MK_LDFLAGS += -g
  150. ifeq ($(UNAME_S),Linux)
  151. MK_CPPFLAGS += -D_GLIBCXX_ASSERTIONS
  152. endif
  153. else
  154. MK_CPPFLAGS += -DNDEBUG
  155. endif
  156. ifdef LLAMA_SANITIZE_THREAD
  157. MK_CFLAGS += -fsanitize=thread -g
  158. MK_CXXFLAGS += -fsanitize=thread -g
  159. MK_LDFLAGS += -fsanitize=thread -g
  160. endif
  161. ifdef LLAMA_SANITIZE_ADDRESS
  162. MK_CFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  163. MK_CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  164. MK_LDFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  165. endif
  166. ifdef LLAMA_SANITIZE_UNDEFINED
  167. MK_CFLAGS += -fsanitize=undefined -g
  168. MK_CXXFLAGS += -fsanitize=undefined -g
  169. MK_LDFLAGS += -fsanitize=undefined -g
  170. endif
  171. ifdef LLAMA_SERVER_VERBOSE
  172. MK_CPPFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
  173. endif
  174. ifdef LLAMA_SERVER_SSL
  175. MK_CPPFLAGS += -DCPPHTTPLIB_OPENSSL_SUPPORT
  176. MK_LDFLAGS += -lssl -lcrypto
  177. endif
  178. ifdef LLAMA_CODE_COVERAGE
  179. MK_CXXFLAGS += -fprofile-arcs -ftest-coverage -dumpbase ''
  180. endif
  181. ifdef LLAMA_DISABLE_LOGS
  182. MK_CPPFLAGS += -DLOG_DISABLE_LOGS
  183. endif # LLAMA_DISABLE_LOGS
  184. # warnings
  185. WARN_FLAGS = -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
  186. MK_CFLAGS += $(WARN_FLAGS) -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int \
  187. -Werror=implicit-function-declaration
  188. MK_CXXFLAGS += $(WARN_FLAGS) -Wmissing-declarations -Wmissing-noreturn
  189. ifeq ($(LLAMA_FATAL_WARNINGS),1)
  190. MK_CFLAGS += -Werror
  191. MK_CXXFLAGS += -Werror
  192. endif
  193. # this version of Apple ld64 is buggy
  194. ifneq '' '$(findstring dyld-1015.7,$(shell $(CC) $(LDFLAGS) -Wl,-v 2>&1))'
  195. MK_CPPFLAGS += -DHAVE_BUGGY_APPLE_LINKER
  196. endif
  197. # OS specific
  198. # TODO: support Windows
  199. ifneq '' '$(filter $(UNAME_S),Linux Darwin FreeBSD NetBSD OpenBSD Haiku)'
  200. MK_CFLAGS += -pthread
  201. MK_CXXFLAGS += -pthread
  202. endif
  203. # detect Windows
  204. ifneq ($(findstring _NT,$(UNAME_S)),)
  205. _WIN32 := 1
  206. endif
  207. # library name prefix
  208. ifneq ($(_WIN32),1)
  209. LIB_PRE := lib
  210. endif
  211. # Dynamic Shared Object extension
  212. ifneq ($(_WIN32),1)
  213. DSO_EXT := .so
  214. else
  215. DSO_EXT := .dll
  216. endif
  217. # Windows Sockets 2 (Winsock) for network-capable apps
  218. ifeq ($(_WIN32),1)
  219. LWINSOCK2 := -lws2_32
  220. endif
  221. ifdef LLAMA_GPROF
  222. MK_CFLAGS += -pg
  223. MK_CXXFLAGS += -pg
  224. endif
  225. ifdef LLAMA_PERF
  226. MK_CPPFLAGS += -DGGML_PERF
  227. endif
  228. # Architecture specific
  229. # TODO: probably these flags need to be tweaked on some architectures
  230. # feel free to update the Makefile for your architecture and send a pull request or issue
  231. ifndef RISCV
  232. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
  233. # Use all CPU extensions that are available:
  234. MK_CFLAGS += -march=native -mtune=native
  235. HOST_CXXFLAGS += -march=native -mtune=native
  236. # Usage AVX-only
  237. #MK_CFLAGS += -mfma -mf16c -mavx
  238. #MK_CXXFLAGS += -mfma -mf16c -mavx
  239. # Usage SSSE3-only (Not is SSE3!)
  240. #MK_CFLAGS += -mssse3
  241. #MK_CXXFLAGS += -mssse3
  242. endif
  243. ifneq '' '$(findstring mingw,$(shell $(CC) -dumpmachine))'
  244. # The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
  245. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
  246. # https://github.com/ggerganov/llama.cpp/issues/2922
  247. MK_CFLAGS += -Xassembler -muse-unaligned-vector-move
  248. MK_CXXFLAGS += -Xassembler -muse-unaligned-vector-move
  249. # Target Windows 8 for PrefetchVirtualMemory
  250. MK_CPPFLAGS += -D_WIN32_WINNT=0x602
  251. endif
  252. ifneq ($(filter aarch64%,$(UNAME_M)),)
  253. # Apple M1, M2, etc.
  254. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  255. # Nvidia Jetson
  256. MK_CFLAGS += -mcpu=native
  257. MK_CXXFLAGS += -mcpu=native
  258. JETSON_RELEASE_INFO = $(shell jetson_release)
  259. ifdef JETSON_RELEASE_INFO
  260. ifneq ($(filter TX2%,$(JETSON_RELEASE_INFO)),)
  261. JETSON_EOL_MODULE_DETECT = 1
  262. CC = aarch64-unknown-linux-gnu-gcc
  263. cxx = aarch64-unknown-linux-gnu-g++
  264. endif
  265. endif
  266. endif
  267. ifneq ($(filter armv6%,$(UNAME_M)),)
  268. # Raspberry Pi 1, Zero
  269. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  270. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  271. endif
  272. ifneq ($(filter armv7%,$(UNAME_M)),)
  273. # Raspberry Pi 2
  274. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  275. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  276. endif
  277. ifneq ($(filter armv8%,$(UNAME_M)),)
  278. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  279. MK_CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  280. MK_CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
  281. endif
  282. ifneq ($(filter ppc64%,$(UNAME_M)),)
  283. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  284. ifneq (,$(findstring POWER9,$(POWER9_M)))
  285. MK_CFLAGS += -mcpu=power9
  286. MK_CXXFLAGS += -mcpu=power9
  287. endif
  288. endif
  289. ifneq ($(filter ppc64le%,$(UNAME_M)),)
  290. MK_CFLAGS += -mcpu=powerpc64le
  291. MK_CXXFLAGS += -mcpu=powerpc64le
  292. CUDA_POWER_ARCH = 1
  293. endif
  294. else
  295. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  296. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  297. endif
  298. ifdef LLAMA_QKK_64
  299. MK_CPPFLAGS += -DGGML_QKK_64
  300. endif
  301. ifndef LLAMA_NO_ACCELERATE
  302. # Mac OS - include Accelerate framework.
  303. # `-framework Accelerate` works both with Apple Silicon and Mac Intel
  304. ifeq ($(UNAME_S),Darwin)
  305. MK_CPPFLAGS += -DGGML_USE_ACCELERATE
  306. MK_CPPFLAGS += -DACCELERATE_NEW_LAPACK
  307. MK_CPPFLAGS += -DACCELERATE_LAPACK_ILP64
  308. MK_LDFLAGS += -framework Accelerate
  309. endif
  310. endif # LLAMA_NO_ACCELERATE
  311. ifdef LLAMA_MPI
  312. MK_CPPFLAGS += -DGGML_USE_MPI
  313. MK_CFLAGS += -Wno-cast-qual
  314. MK_CXXFLAGS += -Wno-cast-qual
  315. OBJS += ggml-mpi.o
  316. endif # LLAMA_MPI
  317. ifdef LLAMA_OPENBLAS
  318. MK_CPPFLAGS += -DGGML_USE_OPENBLAS $(shell pkg-config --cflags-only-I openblas)
  319. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
  320. MK_LDFLAGS += $(shell pkg-config --libs openblas)
  321. endif # LLAMA_OPENBLAS
  322. ifdef LLAMA_BLIS
  323. MK_CPPFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
  324. MK_LDFLAGS += -lblis -L/usr/local/lib
  325. endif # LLAMA_BLIS
  326. ifdef LLAMA_CUBLAS
  327. ifneq ('', '$(wildcard /opt/cuda)')
  328. CUDA_PATH ?= /opt/cuda
  329. else
  330. CUDA_PATH ?= /usr/local/cuda
  331. endif
  332. MK_CPPFLAGS += -DGGML_USE_CUBLAS -I$(CUDA_PATH)/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include
  333. MK_LDFLAGS += -lcuda -lcublas -lculibos -lcudart -lcublasLt -lpthread -ldl -lrt -L$(CUDA_PATH)/lib64 -L/usr/lib64 -L$(CUDA_PATH)/targets/$(UNAME_M)-linux/lib -L/usr/lib/wsl/lib
  334. OBJS += ggml-cuda.o
  335. MK_NVCCFLAGS += -use_fast_math
  336. ifdef LLAMA_FATAL_WARNINGS
  337. MK_NVCCFLAGS += -Werror all-warnings
  338. endif # LLAMA_FATAL_WARNINGS
  339. ifndef JETSON_EOL_MODULE_DETECT
  340. MK_NVCCFLAGS += --forward-unknown-to-host-compiler
  341. endif # JETSON_EOL_MODULE_DETECT
  342. ifdef LLAMA_DEBUG
  343. MK_NVCCFLAGS += -lineinfo
  344. endif # LLAMA_DEBUG
  345. ifdef LLAMA_CUDA_NVCC
  346. NVCC = $(CCACHE) $(LLAMA_CUDA_NVCC)
  347. else
  348. NVCC = $(CCACHE) nvcc
  349. endif #LLAMA_CUDA_NVCC
  350. ifdef CUDA_DOCKER_ARCH
  351. MK_NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  352. else ifndef CUDA_POWER_ARCH
  353. MK_NVCCFLAGS += -arch=native
  354. endif # CUDA_DOCKER_ARCH
  355. ifdef LLAMA_CUDA_FORCE_DMMV
  356. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  357. endif # LLAMA_CUDA_FORCE_DMMV
  358. ifdef LLAMA_CUDA_FORCE_MMQ
  359. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  360. endif # LLAMA_CUDA_FORCE_MMQ
  361. ifdef LLAMA_CUDA_DMMV_X
  362. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  363. else
  364. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  365. endif # LLAMA_CUDA_DMMV_X
  366. ifdef LLAMA_CUDA_MMV_Y
  367. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  368. else ifdef LLAMA_CUDA_DMMV_Y
  369. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_DMMV_Y) # for backwards compatibility
  370. else
  371. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  372. endif # LLAMA_CUDA_MMV_Y
  373. ifdef LLAMA_CUDA_F16
  374. MK_NVCCFLAGS += -DGGML_CUDA_F16
  375. endif # LLAMA_CUDA_F16
  376. ifdef LLAMA_CUDA_DMMV_F16
  377. MK_NVCCFLAGS += -DGGML_CUDA_F16
  378. endif # LLAMA_CUDA_DMMV_F16
  379. ifdef LLAMA_CUDA_KQUANTS_ITER
  380. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  381. else
  382. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  383. endif
  384. ifdef LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  385. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(LLAMA_CUDA_PEER_MAX_BATCH_SIZE)
  386. else
  387. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  388. endif # LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  389. #ifdef LLAMA_CUDA_CUBLAS
  390. # MK_NVCCFLAGS += -DGGML_CUDA_CUBLAS
  391. #endif # LLAMA_CUDA_CUBLAS
  392. ifdef LLAMA_CUDA_CCBIN
  393. MK_NVCCFLAGS += -ccbin $(LLAMA_CUDA_CCBIN)
  394. endif
  395. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  396. ifdef JETSON_EOL_MODULE_DETECT
  397. $(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) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  398. else
  399. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  400. endif # JETSON_EOL_MODULE_DETECT
  401. endif # LLAMA_CUBLAS
  402. ifdef LLAMA_CLBLAST
  403. MK_CPPFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags-only-I clblast OpenCL)
  404. MK_CFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  405. MK_CXXFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  406. # Mac provides OpenCL as a framework
  407. ifeq ($(UNAME_S),Darwin)
  408. MK_LDFLAGS += -lclblast -framework OpenCL
  409. else
  410. MK_LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
  411. endif
  412. OBJS += ggml-opencl.o
  413. ggml-opencl.o: ggml-opencl.cpp ggml-opencl.h
  414. $(CXX) $(CXXFLAGS) -c $< -o $@
  415. endif # LLAMA_CLBLAST
  416. ifdef LLAMA_VULKAN
  417. MK_CPPFLAGS += -DGGML_USE_VULKAN
  418. MK_LDFLAGS += -lvulkan
  419. OBJS += ggml-vulkan.o
  420. ifdef LLAMA_VULKAN_CHECK_RESULTS
  421. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  422. endif
  423. ifdef LLAMA_VULKAN_DEBUG
  424. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  425. endif
  426. ifdef LLAMA_VULKAN_VALIDATE
  427. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  428. endif
  429. ifdef LLAMA_VULKAN_RUN_TESTS
  430. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  431. endif
  432. ggml-vulkan.o: ggml-vulkan.cpp ggml-vulkan.h
  433. $(CXX) $(CXXFLAGS) -c $< -o $@
  434. endif # LLAMA_VULKAN
  435. ifdef LLAMA_HIPBLAS
  436. ifeq ($(wildcard /opt/rocm),)
  437. ROCM_PATH ?= /usr
  438. GPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  439. else
  440. ROCM_PATH ?= /opt/rocm
  441. GPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  442. endif
  443. HIPCC ?= $(CCACHE) $(ROCM_PATH)/bin/hipcc
  444. LLAMA_CUDA_DMMV_X ?= 32
  445. LLAMA_CUDA_MMV_Y ?= 1
  446. LLAMA_CUDA_KQUANTS_ITER ?= 2
  447. MK_CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUBLAS
  448. ifdef LLAMA_HIP_UMA
  449. MK_CPPFLAGS += -DGGML_HIP_UMA
  450. endif # LLAMA_HIP_UMA
  451. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  452. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  453. HIPFLAGS += $(addprefix --offload-arch=,$(GPU_TARGETS))
  454. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  455. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  456. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  457. ifdef LLAMA_CUDA_FORCE_DMMV
  458. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  459. endif # LLAMA_CUDA_FORCE_DMMV
  460. OBJS += ggml-cuda.o
  461. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  462. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  463. endif # LLAMA_HIPBLAS
  464. ifdef LLAMA_METAL
  465. MK_CPPFLAGS += -DGGML_USE_METAL
  466. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  467. OBJS += ggml-metal.o
  468. ifdef LLAMA_METAL_NDEBUG
  469. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  470. endif
  471. ifdef LLAMA_METAL_EMBED_LIBRARY
  472. MK_CPPFLAGS += -DGGML_METAL_EMBED_LIBRARY
  473. OBJS += ggml-metal-embed.o
  474. endif
  475. endif # LLAMA_METAL
  476. ifdef LLAMA_METAL
  477. ggml-metal.o: ggml-metal.m ggml-metal.h
  478. $(CC) $(CFLAGS) -c $< -o $@
  479. ifdef LLAMA_METAL_EMBED_LIBRARY
  480. ggml-metal-embed.o: ggml-metal.metal
  481. @echo "Embedding Metal library"
  482. $(eval TEMP_ASSEMBLY=$(shell mktemp))
  483. @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)
  484. @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)
  485. @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)
  486. @echo ".incbin \"$<\"" >> $(TEMP_ASSEMBLY)
  487. @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)
  488. @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)
  489. @$(AS) $(TEMP_ASSEMBLY) -o $@
  490. @rm -f ${TEMP_ASSEMBLY}
  491. endif
  492. endif # LLAMA_METAL
  493. ifdef LLAMA_MPI
  494. ggml-mpi.o: ggml-mpi.c ggml-mpi.h
  495. $(CC) $(CFLAGS) -c $< -o $@
  496. endif # LLAMA_MPI
  497. GF_CC := $(CC)
  498. include scripts/get-flags.mk
  499. # combine build flags with cmdline overrides
  500. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  501. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  502. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  503. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  504. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  505. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  506. # identify CUDA host compiler
  507. ifdef LLAMA_CUBLAS
  508. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  509. include scripts/get-flags.mk
  510. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  511. endif
  512. #
  513. # Print build information
  514. #
  515. $(info I llama.cpp build info: )
  516. $(info I UNAME_S: $(UNAME_S))
  517. $(info I UNAME_P: $(UNAME_P))
  518. $(info I UNAME_M: $(UNAME_M))
  519. $(info I CFLAGS: $(CFLAGS))
  520. $(info I CXXFLAGS: $(CXXFLAGS))
  521. $(info I NVCCFLAGS: $(NVCCFLAGS))
  522. $(info I LDFLAGS: $(LDFLAGS))
  523. $(info I CC: $(shell $(CC) --version | head -n 1))
  524. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  525. ifdef LLAMA_CUBLAS
  526. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  527. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  528. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  529. ifndef CUDA_DOCKER_ARCH
  530. ifndef CUDA_POWER_ARCH
  531. $(error I ERROR: For CUDA versions < 11.7 a target CUDA architecture must be explicitly provided via CUDA_DOCKER_ARCH)
  532. endif # CUDA_POWER_ARCH
  533. endif # CUDA_DOCKER_ARCH
  534. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  535. endif # LLAMA_CUBLAS
  536. $(info )
  537. #
  538. # Build library
  539. #
  540. ggml.o: ggml.c ggml.h ggml-cuda.h
  541. $(CC) $(CFLAGS) -c $< -o $@
  542. ggml-alloc.o: ggml-alloc.c ggml.h ggml-alloc.h
  543. $(CC) $(CFLAGS) -c $< -o $@
  544. ggml-backend.o: ggml-backend.c ggml.h ggml-backend.h
  545. $(CC) $(CFLAGS) -c $< -o $@
  546. ggml-quants.o: ggml-quants.c ggml.h ggml-quants.h
  547. $(CC) $(CFLAGS) -c $< -o $@
  548. OBJS += ggml-alloc.o ggml-backend.o ggml-quants.o
  549. llama.o: llama.cpp ggml.h ggml-alloc.h ggml-backend.h ggml-cuda.h ggml-metal.h llama.h
  550. $(CXX) $(CXXFLAGS) -c $< -o $@
  551. COMMON_H_DEPS = common/common.h common/sampling.h common/log.h
  552. COMMON_DEPS = common.o sampling.o grammar-parser.o build-info.o
  553. common.o: common/common.cpp $(COMMON_H_DEPS)
  554. $(CXX) $(CXXFLAGS) -c $< -o $@
  555. sampling.o: common/sampling.cpp $(COMMON_H_DEPS)
  556. $(CXX) $(CXXFLAGS) -c $< -o $@
  557. console.o: common/console.cpp common/console.h
  558. $(CXX) $(CXXFLAGS) -c $< -o $@
  559. grammar-parser.o: common/grammar-parser.cpp common/grammar-parser.h
  560. $(CXX) $(CXXFLAGS) -c $< -o $@
  561. train.o: common/train.cpp common/train.h
  562. $(CXX) $(CXXFLAGS) -c $< -o $@
  563. libllama.so: llama.o ggml.o $(OBJS)
  564. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  565. libllama.a: llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  566. ar rcs libllama.a llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  567. clean:
  568. rm -vrf *.o tests/*.o *.so *.a *.dll benchmark-matmult common/build-info.cpp *.dot $(COV_TARGETS) $(BUILD_TARGETS) $(TEST_TARGETS)
  569. find examples pocs -type f -name "*.o" -delete
  570. #
  571. # Examples
  572. #
  573. # $< is the first prerequisite, i.e. the source file.
  574. # Explicitly compile this to an object file so that it can be cached with ccache.
  575. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  576. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  577. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  578. main: examples/main/main.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  579. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  580. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  581. @echo
  582. @echo '==== Run ./main -h for help. ===='
  583. @echo
  584. infill: examples/infill/infill.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  585. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  586. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  587. simple: examples/simple/simple.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  588. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  589. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  590. tokenize: examples/tokenize/tokenize.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  591. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  592. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  593. batched: examples/batched/batched.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  594. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  595. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  596. batched-bench: examples/batched-bench/batched-bench.cpp build-info.o ggml.o llama.o common.o $(OBJS)
  597. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  598. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  599. quantize: examples/quantize/quantize.cpp build-info.o ggml.o llama.o $(OBJS)
  600. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  601. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  602. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.o ggml.o llama.o $(OBJS)
  603. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  604. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  605. perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  606. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  607. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  608. imatrix: examples/imatrix/imatrix.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  609. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  610. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  611. embedding: examples/embedding/embedding.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  612. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  613. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  614. save-load-state: examples/save-load-state/save-load-state.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  615. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  616. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  617. server: examples/server/server.cpp examples/server/utils.hpp examples/server/httplib.h examples/server/json.hpp examples/server/index.html.hpp examples/server/index.js.hpp examples/server/completion.js.hpp common/stb_image.h ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  618. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  619. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  620. gguf: examples/gguf/gguf.cpp ggml.o $(OBJS)
  621. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  622. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  623. train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  624. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  625. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  626. convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp ggml.o llama.o $(OBJS)
  627. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  628. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  629. llama-bench: examples/llama-bench/llama-bench.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  630. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  631. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  632. 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)
  633. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  634. 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)
  635. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  636. $(CXX) $(CXXFLAGS) -c examples/llava/clip.cpp -o $(call GET_OBJ_FILE, examples/llava/clip.cpp) -Wno-cast-qual
  637. $(CXX) $(CXXFLAGS) -c examples/llava/llava.cpp -o $(call GET_OBJ_FILE, examples/llava/llava.cpp)
  638. $(CXX) $(CXXFLAGS) $(filter-out %.h $< examples/llava/clip.cpp examples/llava/llava.cpp,$^) $(call GET_OBJ_FILE, $<) $(call GET_OBJ_FILE, examples/llava/clip.cpp) $(call GET_OBJ_FILE, examples/llava/llava.cpp) -o $@ $(LDFLAGS)
  639. baby-llama: examples/baby-llama/baby-llama.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  640. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  641. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  642. beam-search: examples/beam-search/beam-search.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  643. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  644. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  645. finetune: examples/finetune/finetune.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  646. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  647. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  648. export-lora: examples/export-lora/export-lora.cpp ggml.o common/common.h $(OBJS)
  649. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  650. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  651. speculative: examples/speculative/speculative.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  652. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  653. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  654. parallel: examples/parallel/parallel.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  655. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  656. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  657. lookahead: examples/lookahead/lookahead.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  658. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  659. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  660. lookup: examples/lookup/lookup.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  661. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  662. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  663. passkey: examples/passkey/passkey.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  664. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  665. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  666. ifeq ($(UNAME_S),Darwin)
  667. swift: examples/batched.swift
  668. (cd examples/batched.swift; make build)
  669. endif
  670. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  671. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  672. @if ! cmp -s $@.tmp $@; then \
  673. mv $@.tmp $@; \
  674. else \
  675. rm $@.tmp; \
  676. fi
  677. build-info.o: common/build-info.cpp
  678. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  679. #
  680. # Tests
  681. #
  682. tests: $(TEST_TARGETS)
  683. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.o ggml.o $(OBJS)
  684. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  685. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  686. run-benchmark-matmult: benchmark-matmult
  687. ./$@
  688. .PHONY: run-benchmark-matmult swift
  689. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  690. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  691. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  692. q8dot: pocs/vdot/q8dot.cpp ggml.o $(OBJS)
  693. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  694. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  695. tests/test-llama-grammar: tests/test-llama-grammar.cpp ggml.o grammar-parser.o $(OBJS)
  696. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  697. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  698. tests/test-grammar-parser: tests/test-grammar-parser.cpp ggml.o llama.o grammar-parser.o $(OBJS)
  699. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  700. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  701. tests/test-double-float: tests/test-double-float.cpp ggml.o $(OBJS)
  702. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  703. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  704. tests/test-grad0: tests/test-grad0.cpp ggml.o $(OBJS)
  705. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  706. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  707. tests/test-opt: tests/test-opt.cpp ggml.o $(OBJS)
  708. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  709. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  710. tests/test-quantize-fns: tests/test-quantize-fns.cpp ggml.o $(OBJS)
  711. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  712. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  713. tests/test-quantize-perf: tests/test-quantize-perf.cpp ggml.o $(OBJS)
  714. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  715. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  716. tests/test-sampling: tests/test-sampling.cpp ggml.o llama.o $(OBJS)
  717. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  718. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  719. tests/test-tokenizer-0-falcon: tests/test-tokenizer-0-falcon.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  720. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  721. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  722. tests/test-tokenizer-0-llama: tests/test-tokenizer-0-llama.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  723. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  724. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  725. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  726. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  727. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  728. tests/test-tokenizer-1-llama: tests/test-tokenizer-1-llama.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  729. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  730. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  731. tests/test-rope: tests/test-rope.cpp ggml.o $(OBJS)
  732. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  733. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  734. tests/test-c.o: tests/test-c.c llama.h
  735. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  736. tests/test-backend-ops: tests/test-backend-ops.cpp ggml.o $(OBJS)
  737. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  738. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  739. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp ggml.o llama.o tests/get-model.cpp $(COMMON_DEPS) $(OBJS)
  740. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  741. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  742. tests/test-autorelease: tests/test-autorelease.cpp ggml.o llama.o tests/get-model.cpp $(COMMON_DEPS) $(OBJS)
  743. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  744. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  745. tests/test-chat-template: tests/test-chat-template.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  746. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  747. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)