Makefile 38 KB

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