Makefile 38 KB

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