Makefile 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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-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 tests/test-grammar-integration
  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. # disable ggml.c's use of sgemm.cpp
  189. ifdef LLAMA_NO_LLAMAFILE
  190. MK_CPPFLAGS += -DGGML_USE_LLAMAFILE=0
  191. else
  192. MK_CPPFLAGS += -DGGML_USE_LLAMAFILE=1
  193. endif
  194. # warnings
  195. WARN_FLAGS = -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
  196. MK_CFLAGS += $(WARN_FLAGS) -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int \
  197. -Werror=implicit-function-declaration
  198. MK_CXXFLAGS += $(WARN_FLAGS) -Wmissing-declarations -Wmissing-noreturn
  199. ifeq ($(LLAMA_FATAL_WARNINGS),1)
  200. MK_CFLAGS += -Werror
  201. MK_CXXFLAGS += -Werror
  202. endif
  203. # this version of Apple ld64 is buggy
  204. ifneq '' '$(findstring dyld-1015.7,$(shell $(CC) $(LDFLAGS) -Wl,-v 2>&1))'
  205. MK_CPPFLAGS += -DHAVE_BUGGY_APPLE_LINKER
  206. endif
  207. # OS specific
  208. # TODO: support Windows
  209. ifneq '' '$(filter $(UNAME_S),Linux Darwin FreeBSD NetBSD OpenBSD Haiku)'
  210. MK_CFLAGS += -pthread
  211. MK_CXXFLAGS += -pthread
  212. endif
  213. # detect Windows
  214. ifneq ($(findstring _NT,$(UNAME_S)),)
  215. _WIN32 := 1
  216. endif
  217. # library name prefix
  218. ifneq ($(_WIN32),1)
  219. LIB_PRE := lib
  220. endif
  221. # Dynamic Shared Object extension
  222. ifneq ($(_WIN32),1)
  223. DSO_EXT := .so
  224. else
  225. DSO_EXT := .dll
  226. endif
  227. # Windows Sockets 2 (Winsock) for network-capable apps
  228. ifeq ($(_WIN32),1)
  229. LWINSOCK2 := -lws2_32
  230. endif
  231. ifdef LLAMA_GPROF
  232. MK_CFLAGS += -pg
  233. MK_CXXFLAGS += -pg
  234. endif
  235. ifdef LLAMA_PERF
  236. MK_CPPFLAGS += -DGGML_PERF
  237. endif
  238. # Architecture specific
  239. # TODO: probably these flags need to be tweaked on some architectures
  240. # feel free to update the Makefile for your architecture and send a pull request or issue
  241. ifndef RISCV
  242. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
  243. # Use all CPU extensions that are available:
  244. MK_CFLAGS += -march=native -mtune=native
  245. HOST_CXXFLAGS += -march=native -mtune=native
  246. # Usage AVX-only
  247. #MK_CFLAGS += -mfma -mf16c -mavx
  248. #MK_CXXFLAGS += -mfma -mf16c -mavx
  249. # Usage SSSE3-only (Not is SSE3!)
  250. #MK_CFLAGS += -mssse3
  251. #MK_CXXFLAGS += -mssse3
  252. endif
  253. ifneq '' '$(findstring mingw,$(shell $(CC) -dumpmachine))'
  254. # The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
  255. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
  256. # https://github.com/ggerganov/llama.cpp/issues/2922
  257. MK_CFLAGS += -Xassembler -muse-unaligned-vector-move
  258. MK_CXXFLAGS += -Xassembler -muse-unaligned-vector-move
  259. # Target Windows 8 for PrefetchVirtualMemory
  260. MK_CPPFLAGS += -D_WIN32_WINNT=0x602
  261. endif
  262. ifneq ($(filter aarch64%,$(UNAME_M)),)
  263. # Apple M1, M2, etc.
  264. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  265. # Nvidia Jetson
  266. MK_CFLAGS += -mcpu=native
  267. MK_CXXFLAGS += -mcpu=native
  268. JETSON_RELEASE_INFO = $(shell jetson_release)
  269. ifdef JETSON_RELEASE_INFO
  270. ifneq ($(filter TX2%,$(JETSON_RELEASE_INFO)),)
  271. JETSON_EOL_MODULE_DETECT = 1
  272. CC = aarch64-unknown-linux-gnu-gcc
  273. cxx = aarch64-unknown-linux-gnu-g++
  274. endif
  275. endif
  276. endif
  277. ifneq ($(filter armv6%,$(UNAME_M)),)
  278. # Raspberry Pi 1, Zero
  279. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  280. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  281. endif
  282. ifneq ($(filter armv7%,$(UNAME_M)),)
  283. # Raspberry Pi 2
  284. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  285. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  286. endif
  287. ifneq ($(filter armv8%,$(UNAME_M)),)
  288. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  289. MK_CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  290. MK_CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
  291. endif
  292. ifneq ($(filter ppc64%,$(UNAME_M)),)
  293. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  294. ifneq (,$(findstring POWER9,$(POWER9_M)))
  295. MK_CFLAGS += -mcpu=power9
  296. MK_CXXFLAGS += -mcpu=power9
  297. endif
  298. endif
  299. ifneq ($(filter ppc64le%,$(UNAME_M)),)
  300. MK_CFLAGS += -mcpu=powerpc64le
  301. MK_CXXFLAGS += -mcpu=powerpc64le
  302. CUDA_POWER_ARCH = 1
  303. endif
  304. else
  305. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  306. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  307. endif
  308. ifdef LLAMA_QKK_64
  309. MK_CPPFLAGS += -DGGML_QKK_64
  310. endif
  311. ifndef LLAMA_NO_ACCELERATE
  312. # Mac OS - include Accelerate framework.
  313. # `-framework Accelerate` works both with Apple Silicon and Mac Intel
  314. ifeq ($(UNAME_S),Darwin)
  315. MK_CPPFLAGS += -DGGML_USE_ACCELERATE
  316. MK_CPPFLAGS += -DACCELERATE_NEW_LAPACK
  317. MK_CPPFLAGS += -DACCELERATE_LAPACK_ILP64
  318. MK_LDFLAGS += -framework Accelerate
  319. endif
  320. endif # LLAMA_NO_ACCELERATE
  321. ifdef LLAMA_MPI
  322. MK_CPPFLAGS += -DGGML_USE_MPI
  323. MK_CFLAGS += -Wno-cast-qual
  324. MK_CXXFLAGS += -Wno-cast-qual
  325. OBJS += ggml-mpi.o
  326. endif # LLAMA_MPI
  327. ifdef LLAMA_OPENBLAS
  328. MK_CPPFLAGS += -DGGML_USE_OPENBLAS $(shell pkg-config --cflags-only-I openblas)
  329. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
  330. MK_LDFLAGS += $(shell pkg-config --libs openblas)
  331. endif # LLAMA_OPENBLAS
  332. ifdef LLAMA_BLIS
  333. MK_CPPFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
  334. MK_LDFLAGS += -lblis -L/usr/local/lib
  335. endif # LLAMA_BLIS
  336. ifdef LLAMA_CUBLAS
  337. # LLAMA_CUBLAS is deprecated and will be removed in the future
  338. LLAMA_CUDA := 1
  339. endif
  340. ifdef LLAMA_CUDA
  341. ifneq ('', '$(wildcard /opt/cuda)')
  342. CUDA_PATH ?= /opt/cuda
  343. else
  344. CUDA_PATH ?= /usr/local/cuda
  345. endif
  346. MK_CPPFLAGS += -DGGML_USE_CUDA -I$(CUDA_PATH)/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include
  347. 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
  348. OBJS += ggml-cuda.o
  349. OBJS += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/*.cu))
  350. MK_NVCCFLAGS += -use_fast_math
  351. ifdef LLAMA_FATAL_WARNINGS
  352. MK_NVCCFLAGS += -Werror all-warnings
  353. endif # LLAMA_FATAL_WARNINGS
  354. ifndef JETSON_EOL_MODULE_DETECT
  355. MK_NVCCFLAGS += --forward-unknown-to-host-compiler
  356. endif # JETSON_EOL_MODULE_DETECT
  357. ifdef LLAMA_DEBUG
  358. MK_NVCCFLAGS += -lineinfo
  359. endif # LLAMA_DEBUG
  360. ifdef LLAMA_CUDA_NVCC
  361. NVCC = $(CCACHE) $(LLAMA_CUDA_NVCC)
  362. else
  363. NVCC = $(CCACHE) nvcc
  364. endif #LLAMA_CUDA_NVCC
  365. ifdef CUDA_DOCKER_ARCH
  366. MK_NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  367. else ifndef CUDA_POWER_ARCH
  368. MK_NVCCFLAGS += -arch=native
  369. endif # CUDA_DOCKER_ARCH
  370. ifdef LLAMA_CUDA_FORCE_DMMV
  371. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  372. endif # LLAMA_CUDA_FORCE_DMMV
  373. ifdef LLAMA_CUDA_FORCE_MMQ
  374. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  375. endif # LLAMA_CUDA_FORCE_MMQ
  376. ifdef LLAMA_CUDA_DMMV_X
  377. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  378. else
  379. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  380. endif # LLAMA_CUDA_DMMV_X
  381. ifdef LLAMA_CUDA_MMV_Y
  382. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  383. else ifdef LLAMA_CUDA_DMMV_Y
  384. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_DMMV_Y) # for backwards compatibility
  385. else
  386. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  387. endif # LLAMA_CUDA_MMV_Y
  388. ifdef LLAMA_CUDA_F16
  389. MK_NVCCFLAGS += -DGGML_CUDA_F16
  390. endif # LLAMA_CUDA_F16
  391. ifdef LLAMA_CUDA_DMMV_F16
  392. MK_NVCCFLAGS += -DGGML_CUDA_F16
  393. endif # LLAMA_CUDA_DMMV_F16
  394. ifdef LLAMA_CUDA_KQUANTS_ITER
  395. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  396. else
  397. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  398. endif
  399. ifdef LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  400. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(LLAMA_CUDA_PEER_MAX_BATCH_SIZE)
  401. else
  402. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  403. endif # LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  404. ifdef LLAMA_CUDA_NO_PEER_COPY
  405. MK_NVCCFLAGS += -DGGML_CUDA_NO_PEER_COPY
  406. endif # LLAMA_CUDA_NO_PEER_COPY
  407. ifdef LLAMA_CUDA_CCBIN
  408. MK_NVCCFLAGS += -ccbin $(LLAMA_CUDA_CCBIN)
  409. endif
  410. ifdef JETSON_EOL_MODULE_DETECT
  411. define NVCC_COMPILE
  412. $(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 $@
  413. endef # NVCC_COMPILE
  414. else
  415. define NVCC_COMPILE
  416. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  417. endef # NVCC_COMPILE
  418. endif # JETSON_EOL_MODULE_DETECT
  419. ggml-cuda/%.o: ggml-cuda/%.cu ggml-cuda/%.cuh ggml.h ggml-common.h ggml-cuda/common.cuh
  420. $(NVCC_COMPILE)
  421. 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)
  422. $(NVCC_COMPILE)
  423. endif # LLAMA_CUDA
  424. ifdef LLAMA_CLBLAST
  425. MK_CPPFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags-only-I clblast OpenCL)
  426. MK_CFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  427. MK_CXXFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  428. # Mac provides OpenCL as a framework
  429. ifeq ($(UNAME_S),Darwin)
  430. MK_LDFLAGS += -lclblast -framework OpenCL
  431. else
  432. MK_LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
  433. endif
  434. OBJS += ggml-opencl.o
  435. ggml-opencl.o: ggml-opencl.cpp ggml-opencl.h
  436. $(CXX) $(CXXFLAGS) -c $< -o $@
  437. endif # LLAMA_CLBLAST
  438. ifdef LLAMA_VULKAN
  439. MK_CPPFLAGS += -DGGML_USE_VULKAN
  440. MK_LDFLAGS += -lvulkan
  441. OBJS += ggml-vulkan.o
  442. ifdef LLAMA_VULKAN_CHECK_RESULTS
  443. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  444. endif
  445. ifdef LLAMA_VULKAN_DEBUG
  446. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  447. endif
  448. ifdef LLAMA_VULKAN_VALIDATE
  449. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  450. endif
  451. ifdef LLAMA_VULKAN_RUN_TESTS
  452. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  453. endif
  454. ggml-vulkan.o: ggml-vulkan.cpp ggml-vulkan.h
  455. $(CXX) $(CXXFLAGS) -c $< -o $@
  456. endif # LLAMA_VULKAN
  457. ifdef LLAMA_HIPBLAS
  458. ifeq ($(wildcard /opt/rocm),)
  459. ROCM_PATH ?= /usr
  460. GPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  461. else
  462. ROCM_PATH ?= /opt/rocm
  463. GPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  464. endif
  465. HIPCC ?= $(CCACHE) $(ROCM_PATH)/bin/hipcc
  466. LLAMA_CUDA_DMMV_X ?= 32
  467. LLAMA_CUDA_MMV_Y ?= 1
  468. LLAMA_CUDA_KQUANTS_ITER ?= 2
  469. MK_CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUDA
  470. ifdef LLAMA_HIP_UMA
  471. MK_CPPFLAGS += -DGGML_HIP_UMA
  472. endif # LLAMA_HIP_UMA
  473. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  474. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  475. HIPFLAGS += $(addprefix --offload-arch=,$(GPU_TARGETS))
  476. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  477. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  478. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  479. ifdef LLAMA_CUDA_FORCE_DMMV
  480. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  481. endif # LLAMA_CUDA_FORCE_DMMV
  482. ifdef LLAMA_CUDA_NO_PEER_COPY
  483. HIPFLAGS += -DGGML_CUDA_NO_PEER_COPY
  484. endif # LLAMA_CUDA_NO_PEER_COPY
  485. OBJS += ggml-cuda.o
  486. OBJS += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/*.cu))
  487. 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)
  488. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  489. ggml-cuda/%.o: ggml-cuda/%.cu ggml-cuda/%.cuh ggml.h ggml-common.h ggml-cuda/common.cuh
  490. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  491. endif # LLAMA_HIPBLAS
  492. ifdef LLAMA_METAL
  493. MK_CPPFLAGS += -DGGML_USE_METAL
  494. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  495. OBJS += ggml-metal.o
  496. ifdef LLAMA_METAL_NDEBUG
  497. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  498. endif
  499. ifdef LLAMA_METAL_EMBED_LIBRARY
  500. MK_CPPFLAGS += -DGGML_METAL_EMBED_LIBRARY
  501. OBJS += ggml-metal-embed.o
  502. endif
  503. endif # LLAMA_METAL
  504. ifdef LLAMA_METAL
  505. ggml-metal.o: ggml-metal.m ggml-metal.h ggml.h
  506. $(CC) $(CFLAGS) -c $< -o $@
  507. ifdef LLAMA_METAL_EMBED_LIBRARY
  508. ggml-metal-embed.o: ggml-metal.metal ggml-common.h
  509. @echo "Embedding Metal library"
  510. @sed -e '/#include "ggml-common.h"/r ggml-common.h' -e '/#include "ggml-common.h"/d' < ggml-metal.metal > ggml-metal-embed.metal
  511. $(eval TEMP_ASSEMBLY=$(shell mktemp))
  512. @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)
  513. @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)
  514. @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)
  515. @echo ".incbin \"ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY)
  516. @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)
  517. @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)
  518. @$(AS) $(TEMP_ASSEMBLY) -o $@
  519. @rm -f ${TEMP_ASSEMBLY}
  520. endif
  521. endif # LLAMA_METAL
  522. ifdef LLAMA_MPI
  523. ggml-mpi.o: ggml-mpi.c ggml-mpi.h
  524. $(CC) $(CFLAGS) -c $< -o $@
  525. endif # LLAMA_MPI
  526. GF_CC := $(CC)
  527. include scripts/get-flags.mk
  528. # combine build flags with cmdline overrides
  529. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  530. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  531. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  532. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  533. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  534. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  535. # identify CUDA host compiler
  536. ifdef LLAMA_CUDA
  537. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  538. include scripts/get-flags.mk
  539. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  540. endif
  541. ifdef LLAMA_CURL
  542. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  543. override LDFLAGS := $(LDFLAGS) -lcurl
  544. endif
  545. #
  546. # Print build information
  547. #
  548. $(info I llama.cpp build info: )
  549. $(info I UNAME_S: $(UNAME_S))
  550. $(info I UNAME_P: $(UNAME_P))
  551. $(info I UNAME_M: $(UNAME_M))
  552. $(info I CFLAGS: $(CFLAGS))
  553. $(info I CXXFLAGS: $(CXXFLAGS))
  554. $(info I NVCCFLAGS: $(NVCCFLAGS))
  555. $(info I LDFLAGS: $(LDFLAGS))
  556. $(info I CC: $(shell $(CC) --version | head -n 1))
  557. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  558. ifdef LLAMA_CUDA
  559. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  560. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  561. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  562. ifndef CUDA_DOCKER_ARCH
  563. ifndef CUDA_POWER_ARCH
  564. $(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 )
  565. endif # CUDA_POWER_ARCH
  566. endif # CUDA_DOCKER_ARCH
  567. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  568. endif # LLAMA_CUDA
  569. $(info )
  570. ifdef LLAMA_CUBLAS
  571. $(info !!!!)
  572. $(info LLAMA_CUBLAS is deprecated and will be removed in the future. Use LLAMA_CUDA instead.)
  573. $(info !!!!)
  574. $(info )
  575. endif
  576. #
  577. # Build library
  578. #
  579. ggml.o: ggml.c ggml.h ggml-cuda.h
  580. $(CC) $(CFLAGS) -c $< -o $@
  581. ggml-alloc.o: ggml-alloc.c ggml.h ggml-alloc.h
  582. $(CC) $(CFLAGS) -c $< -o $@
  583. ggml-backend.o: ggml-backend.c ggml.h ggml-backend.h
  584. $(CC) $(CFLAGS) -c $< -o $@
  585. ggml-quants.o: ggml-quants.c ggml.h ggml-quants.h ggml-common.h
  586. $(CC) $(CFLAGS) -c $< -o $@
  587. sgemm.o: sgemm.cpp sgemm.h ggml.h
  588. $(CXX) $(CXXFLAGS) -c $< -o $@
  589. unicode.o: unicode.cpp unicode.h
  590. $(CXX) $(CXXFLAGS) -c $< -o $@
  591. unicode-data.o: unicode-data.cpp unicode-data.h
  592. $(CXX) $(CXXFLAGS) -c $< -o $@
  593. OBJS += ggml-alloc.o ggml-backend.o ggml-quants.o unicode.o unicode-data.o sgemm.o
  594. llama.o: llama.cpp unicode.h ggml.h ggml-alloc.h ggml-backend.h ggml-cuda.h ggml-metal.h llama.h
  595. $(CXX) $(CXXFLAGS) -c $< -o $@
  596. COMMON_H_DEPS = common/common.h common/sampling.h common/log.h
  597. COMMON_DEPS = common.o sampling.o grammar-parser.o build-info.o json-schema-to-grammar.o
  598. common.o: common/common.cpp $(COMMON_H_DEPS)
  599. $(CXX) $(CXXFLAGS) -c $< -o $@
  600. sampling.o: common/sampling.cpp $(COMMON_H_DEPS)
  601. $(CXX) $(CXXFLAGS) -c $< -o $@
  602. console.o: common/console.cpp common/console.h
  603. $(CXX) $(CXXFLAGS) -c $< -o $@
  604. grammar-parser.o: common/grammar-parser.cpp common/grammar-parser.h
  605. $(CXX) $(CXXFLAGS) -c $< -o $@
  606. json-schema-to-grammar.o: common/json-schema-to-grammar.cpp common/json-schema-to-grammar.h
  607. $(CXX) $(CXXFLAGS) -c $< -o $@
  608. train.o: common/train.cpp common/train.h
  609. $(CXX) $(CXXFLAGS) -c $< -o $@
  610. ngram-cache.o: common/ngram-cache.cpp common/ngram-cache.h
  611. $(CXX) $(CXXFLAGS) -c $< -o $@
  612. libllama.so: llama.o ggml.o $(OBJS)
  613. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  614. libllama.a: llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  615. ar rcs libllama.a llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  616. clean:
  617. 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)
  618. rm -vrf ggml-cuda/*.o
  619. find examples pocs -type f -name "*.o" -delete
  620. #
  621. # Examples
  622. #
  623. # $< is the first prerequisite, i.e. the source file.
  624. # Explicitly compile this to an object file so that it can be cached with ccache.
  625. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  626. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  627. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  628. main: examples/main/main.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  629. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  630. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  631. @echo
  632. @echo '==== Run ./main -h for help. ===='
  633. @echo
  634. infill: examples/infill/infill.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  635. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  636. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  637. simple: examples/simple/simple.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  638. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  639. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  640. tokenize: examples/tokenize/tokenize.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  641. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  642. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  643. batched: examples/batched/batched.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  644. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  645. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  646. batched-bench: examples/batched-bench/batched-bench.cpp build-info.o ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  647. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  648. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  649. quantize: examples/quantize/quantize.cpp build-info.o ggml.o llama.o $(OBJS)
  650. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  651. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  652. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.o ggml.o llama.o $(OBJS)
  653. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  654. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  655. perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  656. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  657. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  658. imatrix: examples/imatrix/imatrix.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  659. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  660. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  661. embedding: examples/embedding/embedding.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  662. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  663. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  664. gritlm: examples/gritlm/gritlm.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  665. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  666. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  667. save-load-state: examples/save-load-state/save-load-state.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  668. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  669. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  670. 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 common/stb_image.h ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  671. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  672. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  673. gguf: examples/gguf/gguf.cpp ggml.o $(OBJS)
  674. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  675. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  676. gguf-split: examples/gguf-split/gguf-split.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  677. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  678. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  679. eval-callback: examples/eval-callback/eval-callback.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  680. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  681. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  682. train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  683. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  684. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  685. convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp ggml.o llama.o $(OBJS)
  686. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  687. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  688. llama-bench: examples/llama-bench/llama-bench.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  689. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  690. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  691. 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)
  692. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  693. 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)
  694. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  695. $(CXX) $(CXXFLAGS) -c examples/llava/clip.cpp -o $(call GET_OBJ_FILE, examples/llava/clip.cpp) -Wno-cast-qual
  696. $(CXX) $(CXXFLAGS) -c examples/llava/llava.cpp -o $(call GET_OBJ_FILE, examples/llava/llava.cpp)
  697. $(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)
  698. baby-llama: examples/baby-llama/baby-llama.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  699. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  700. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  701. beam-search: examples/beam-search/beam-search.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. finetune: examples/finetune/finetune.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  705. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  706. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  707. export-lora: examples/export-lora/export-lora.cpp ggml.o common/common.h $(OBJS)
  708. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  709. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  710. retrieval: examples/retrieval/retrieval.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  711. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  712. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  713. speculative: examples/speculative/speculative.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  714. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  715. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  716. parallel: examples/parallel/parallel.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  717. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  718. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  719. lookahead: examples/lookahead/lookahead.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  720. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  721. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  722. lookup: examples/lookup/lookup.cpp ggml.o llama.o ngram-cache.o $(COMMON_DEPS) $(OBJS)
  723. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  724. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  725. $(CXX) $(CXXFLAGS) -c examples/lookup/lookup-create.cpp -o $(call GET_OBJ_FILE, examples/lookup/lookup-create.cpp)
  726. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, examples/lookup/lookup-create.cpp) -o lookup-create $(LDFLAGS)
  727. $(CXX) $(CXXFLAGS) -c examples/lookup/lookup-merge.cpp -o $(call GET_OBJ_FILE, examples/lookup/lookup-merge.cpp)
  728. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, examples/lookup/lookup-merge.cpp) -o lookup-merge $(LDFLAGS)
  729. $(CXX) $(CXXFLAGS) -c examples/lookup/lookup-stats.cpp -o $(call GET_OBJ_FILE, examples/lookup/lookup-stats.cpp)
  730. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, examples/lookup/lookup-stats.cpp) -o lookup-stats $(LDFLAGS)
  731. passkey: examples/passkey/passkey.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  732. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  733. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  734. gbnf-validator: examples/gbnf-validator/gbnf-validator.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  735. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  736. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  737. ifeq ($(UNAME_S),Darwin)
  738. swift: examples/batched.swift
  739. (cd examples/batched.swift; make build)
  740. endif
  741. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  742. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  743. @if ! cmp -s $@.tmp $@; then \
  744. mv $@.tmp $@; \
  745. else \
  746. rm $@.tmp; \
  747. fi
  748. build-info.o: common/build-info.cpp
  749. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  750. #
  751. # Tests
  752. #
  753. tests: $(TEST_TARGETS)
  754. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.o ggml.o $(OBJS)
  755. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  756. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  757. run-benchmark-matmult: benchmark-matmult
  758. ./$@
  759. .PHONY: run-benchmark-matmult swift
  760. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  761. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  762. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  763. q8dot: pocs/vdot/q8dot.cpp ggml.o $(OBJS)
  764. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  765. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  766. tests/test-llama-grammar: tests/test-llama-grammar.cpp ggml.o grammar-parser.o $(OBJS)
  767. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  768. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  769. tests/test-grammar-parser: tests/test-grammar-parser.cpp ggml.o llama.o grammar-parser.o $(OBJS)
  770. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  771. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  772. tests/test-grammar-integration: tests/test-grammar-integration.cpp ggml.o llama.o grammar-parser.o $(OBJS)
  773. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  774. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  775. tests/test-double-float: tests/test-double-float.cpp ggml.o $(OBJS)
  776. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  777. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  778. 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)
  779. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  780. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  781. tests/test-grad0: tests/test-grad0.cpp ggml.o $(OBJS)
  782. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  783. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  784. tests/test-opt: tests/test-opt.cpp ggml.o $(OBJS)
  785. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  786. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  787. tests/test-quantize-fns: tests/test-quantize-fns.cpp ggml.o $(OBJS)
  788. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  789. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  790. tests/test-quantize-perf: tests/test-quantize-perf.cpp ggml.o $(OBJS)
  791. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  792. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  793. tests/test-sampling: tests/test-sampling.cpp ggml.o llama.o $(OBJS)
  794. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  795. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  796. tests/test-tokenizer-0-falcon: tests/test-tokenizer-0-falcon.cpp ggml.o llama.o $(COMMON_DEPS) console.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-tokenizer-0-llama: tests/test-tokenizer-0-llama.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(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-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(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-tokenizer-1-llama: tests/test-tokenizer-1-llama.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  806. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  807. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  808. tests/test-rope: tests/test-rope.cpp ggml.o $(OBJS)
  809. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  810. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  811. tests/test-c.o: tests/test-c.c llama.h
  812. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  813. tests/test-backend-ops: tests/test-backend-ops.cpp ggml.o $(OBJS)
  814. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  815. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  816. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp ggml.o llama.o tests/get-model.cpp $(COMMON_DEPS) $(OBJS)
  817. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  818. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  819. tests/test-autorelease: tests/test-autorelease.cpp ggml.o llama.o tests/get-model.cpp $(COMMON_DEPS) $(OBJS)
  820. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  821. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  822. tests/test-chat-template: tests/test-chat-template.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  823. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  824. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)