Makefile 32 KB

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