Makefile 39 KB

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