Makefile 38 KB

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