Makefile 40 KB

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