Makefile 36 KB

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