Makefile 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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. ifndef LLAMA_DEBUG
  121. MK_NVCCFLAGS += -O3
  122. endif # LLAMA_DEBUG
  123. else
  124. MK_CFLAGS += -O3
  125. MK_CXXFLAGS += -O3
  126. ifndef LLAMA_DEBUG
  127. MK_NVCCFLAGS += -O3
  128. endif # LLAMA_DEBUG
  129. endif # LLAMA_FAST
  130. ifndef LLAMA_NO_CCACHE
  131. CCACHE := $(shell which ccache)
  132. ifdef CCACHE
  133. export CCACHE_SLOPPINESS = time_macros
  134. $(info I ccache found, compilation results will be cached. Disable with LLAMA_NO_CCACHE.)
  135. CC := $(CCACHE) $(CC)
  136. CXX := $(CCACHE) $(CXX)
  137. else
  138. $(info I ccache not found. Consider installing it for faster compilation.)
  139. endif # CCACHE
  140. endif # LLAMA_NO_CCACHE
  141. # clock_gettime came in POSIX.1b (1993)
  142. # CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
  143. # posix_memalign came in POSIX.1-2001 / SUSv3
  144. # M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
  145. MK_CPPFLAGS += -D_XOPEN_SOURCE=600
  146. # Somehow in OpenBSD whenever POSIX conformance is specified
  147. # some string functions rely on locale_t availability,
  148. # which was introduced in POSIX.1-2008, forcing us to go higher
  149. ifeq ($(UNAME_S),OpenBSD)
  150. MK_CPPFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
  151. endif
  152. # Data types, macros and functions related to controlling CPU affinity and
  153. # some memory allocation are available on Linux through GNU extensions in libc
  154. ifeq ($(UNAME_S),Linux)
  155. MK_CPPFLAGS += -D_GNU_SOURCE
  156. endif
  157. # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
  158. # and on macOS its availability depends on enabling Darwin extensions
  159. # similarly on DragonFly, enabling BSD extensions is necessary
  160. ifeq ($(UNAME_S),Darwin)
  161. MK_CPPFLAGS += -D_DARWIN_C_SOURCE
  162. endif
  163. ifeq ($(UNAME_S),DragonFly)
  164. MK_CPPFLAGS += -D__BSD_VISIBLE
  165. endif
  166. # alloca is a non-standard interface that is not visible on BSDs when
  167. # POSIX conformance is specified, but not all of them provide a clean way
  168. # to enable it in such cases
  169. ifeq ($(UNAME_S),FreeBSD)
  170. MK_CPPFLAGS += -D__BSD_VISIBLE
  171. endif
  172. ifeq ($(UNAME_S),NetBSD)
  173. MK_CPPFLAGS += -D_NETBSD_SOURCE
  174. endif
  175. ifeq ($(UNAME_S),OpenBSD)
  176. MK_CPPFLAGS += -D_BSD_SOURCE
  177. endif
  178. ifdef LLAMA_SCHED_MAX_COPIES
  179. MK_CPPFLAGS += -DGGML_SCHED_MAX_COPIES=$(LLAMA_SCHED_MAX_COPIES)
  180. endif
  181. ifdef LLAMA_DEBUG
  182. MK_CFLAGS += -O0 -g
  183. MK_CXXFLAGS += -O0 -g
  184. MK_LDFLAGS += -g
  185. MK_NVCCFLAGS += -O0 -g
  186. ifeq ($(UNAME_S),Linux)
  187. MK_CPPFLAGS += -D_GLIBCXX_ASSERTIONS
  188. endif
  189. else
  190. MK_CPPFLAGS += -DNDEBUG
  191. endif
  192. ifdef LLAMA_SANITIZE_THREAD
  193. MK_CFLAGS += -fsanitize=thread -g
  194. MK_CXXFLAGS += -fsanitize=thread -g
  195. MK_LDFLAGS += -fsanitize=thread -g
  196. endif
  197. ifdef LLAMA_SANITIZE_ADDRESS
  198. MK_CFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  199. MK_CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  200. MK_LDFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  201. endif
  202. ifdef LLAMA_SANITIZE_UNDEFINED
  203. MK_CFLAGS += -fsanitize=undefined -g
  204. MK_CXXFLAGS += -fsanitize=undefined -g
  205. MK_LDFLAGS += -fsanitize=undefined -g
  206. endif
  207. ifdef LLAMA_SERVER_VERBOSE
  208. MK_CPPFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
  209. endif
  210. ifdef LLAMA_SERVER_SSL
  211. MK_CPPFLAGS += -DCPPHTTPLIB_OPENSSL_SUPPORT
  212. MK_LDFLAGS += -lssl -lcrypto
  213. endif
  214. ifdef LLAMA_CODE_COVERAGE
  215. MK_CXXFLAGS += -fprofile-arcs -ftest-coverage -dumpbase ''
  216. endif
  217. ifdef LLAMA_DISABLE_LOGS
  218. MK_CPPFLAGS += -DLOG_DISABLE_LOGS
  219. endif # LLAMA_DISABLE_LOGS
  220. # warnings
  221. WARN_FLAGS = -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
  222. MK_CFLAGS += $(WARN_FLAGS) -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int \
  223. -Werror=implicit-function-declaration
  224. MK_CXXFLAGS += $(WARN_FLAGS) -Wmissing-declarations -Wmissing-noreturn
  225. ifeq ($(LLAMA_FATAL_WARNINGS),1)
  226. MK_CFLAGS += -Werror
  227. MK_CXXFLAGS += -Werror
  228. endif
  229. # this version of Apple ld64 is buggy
  230. ifneq '' '$(findstring dyld-1015.7,$(shell $(CC) $(LDFLAGS) -Wl,-v 2>&1))'
  231. MK_CPPFLAGS += -DHAVE_BUGGY_APPLE_LINKER
  232. endif
  233. # OS specific
  234. # TODO: support Windows
  235. ifneq '' '$(filter $(UNAME_S),Linux Darwin FreeBSD NetBSD OpenBSD Haiku)'
  236. MK_CFLAGS += -pthread
  237. MK_CXXFLAGS += -pthread
  238. endif
  239. # detect Windows
  240. ifneq ($(findstring _NT,$(UNAME_S)),)
  241. _WIN32 := 1
  242. endif
  243. # library name prefix
  244. ifneq ($(_WIN32),1)
  245. LIB_PRE := lib
  246. endif
  247. # Dynamic Shared Object extension
  248. ifneq ($(_WIN32),1)
  249. DSO_EXT := .so
  250. else
  251. DSO_EXT := .dll
  252. endif
  253. # Windows Sockets 2 (Winsock) for network-capable apps
  254. ifeq ($(_WIN32),1)
  255. LWINSOCK2 := -lws2_32
  256. endif
  257. ifdef LLAMA_GPROF
  258. MK_CFLAGS += -pg
  259. MK_CXXFLAGS += -pg
  260. endif
  261. ifdef LLAMA_PERF
  262. MK_CPPFLAGS += -DGGML_PERF
  263. endif
  264. # Architecture specific
  265. # TODO: probably these flags need to be tweaked on some architectures
  266. # feel free to update the Makefile for your architecture and send a pull request or issue
  267. ifndef RISCV
  268. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
  269. # Use all CPU extensions that are available:
  270. MK_CFLAGS += -march=native -mtune=native
  271. HOST_CXXFLAGS += -march=native -mtune=native
  272. # Usage AVX-only
  273. #MK_CFLAGS += -mfma -mf16c -mavx
  274. #MK_CXXFLAGS += -mfma -mf16c -mavx
  275. # Usage SSSE3-only (Not is SSE3!)
  276. #MK_CFLAGS += -mssse3
  277. #MK_CXXFLAGS += -mssse3
  278. endif
  279. ifneq '' '$(findstring mingw,$(shell $(CC) -dumpmachine))'
  280. # The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
  281. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
  282. # https://github.com/ggerganov/llama.cpp/issues/2922
  283. MK_CFLAGS += -Xassembler -muse-unaligned-vector-move
  284. MK_CXXFLAGS += -Xassembler -muse-unaligned-vector-move
  285. # Target Windows 8 for PrefetchVirtualMemory
  286. MK_CPPFLAGS += -D_WIN32_WINNT=0x602
  287. endif
  288. ifneq ($(filter aarch64%,$(UNAME_M)),)
  289. # Apple M1, M2, etc.
  290. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  291. # Nvidia Jetson
  292. MK_CFLAGS += -mcpu=native
  293. MK_CXXFLAGS += -mcpu=native
  294. JETSON_RELEASE_INFO = $(shell jetson_release)
  295. ifdef JETSON_RELEASE_INFO
  296. ifneq ($(filter TX2%,$(JETSON_RELEASE_INFO)),)
  297. JETSON_EOL_MODULE_DETECT = 1
  298. CC = aarch64-unknown-linux-gnu-gcc
  299. cxx = aarch64-unknown-linux-gnu-g++
  300. endif
  301. endif
  302. endif
  303. ifneq ($(filter armv6%,$(UNAME_M)),)
  304. # Raspberry Pi 1, Zero
  305. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  306. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  307. endif
  308. ifneq ($(filter armv7%,$(UNAME_M)),)
  309. # Raspberry Pi 2
  310. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  311. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  312. endif
  313. ifneq ($(filter armv8%,$(UNAME_M)),)
  314. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  315. MK_CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  316. MK_CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
  317. endif
  318. ifneq ($(filter ppc64%,$(UNAME_M)),)
  319. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  320. ifneq (,$(findstring POWER9,$(POWER9_M)))
  321. MK_CFLAGS += -mcpu=power9
  322. MK_CXXFLAGS += -mcpu=power9
  323. endif
  324. endif
  325. ifneq ($(filter ppc64le%,$(UNAME_M)),)
  326. MK_CFLAGS += -mcpu=powerpc64le
  327. MK_CXXFLAGS += -mcpu=powerpc64le
  328. CUDA_POWER_ARCH = 1
  329. endif
  330. ifneq ($(filter loongarch64%,$(UNAME_M)),)
  331. MK_CFLAGS += -mlasx
  332. MK_CXXFLAGS += -mlasx
  333. endif
  334. else
  335. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  336. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  337. endif
  338. ifndef LLAMA_NO_ACCELERATE
  339. # Mac OS - include Accelerate framework.
  340. # `-framework Accelerate` works both with Apple Silicon and Mac Intel
  341. ifeq ($(UNAME_S),Darwin)
  342. MK_CPPFLAGS += -DGGML_USE_ACCELERATE
  343. MK_CPPFLAGS += -DACCELERATE_NEW_LAPACK
  344. MK_CPPFLAGS += -DACCELERATE_LAPACK_ILP64
  345. MK_LDFLAGS += -framework Accelerate
  346. endif
  347. endif # LLAMA_NO_ACCELERATE
  348. ifdef LLAMA_OPENBLAS
  349. MK_CPPFLAGS += -DGGML_USE_OPENBLAS $(shell pkg-config --cflags-only-I openblas)
  350. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
  351. MK_LDFLAGS += $(shell pkg-config --libs openblas)
  352. endif # LLAMA_OPENBLAS
  353. ifndef LLAMA_NO_LLAMAFILE
  354. MK_CPPFLAGS += -DGGML_USE_LLAMAFILE
  355. OBJS += sgemm.o
  356. endif
  357. ifdef LLAMA_BLIS
  358. MK_CPPFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
  359. MK_LDFLAGS += -lblis -L/usr/local/lib
  360. endif # LLAMA_BLIS
  361. ifdef LLAMA_CUBLAS
  362. # LLAMA_CUBLAS is deprecated and will be removed in the future
  363. LLAMA_CUDA := 1
  364. endif
  365. OBJS_CUDA_TEMP_INST = $(patsubst %.cu,%.o,$(wildcard ggml-cuda/template-instances/fattn-wmma*.cu))
  366. ifdef LLAMA_CUDA_FA_ALL_QUANTS
  367. OBJS_CUDA_TEMP_INST += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/template-instances/fattn-vec*.cu))
  368. else
  369. OBJS_CUDA_TEMP_INST += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/template-instances/fattn-vec*q4_0-q4_0.cu))
  370. OBJS_CUDA_TEMP_INST += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/template-instances/fattn-vec*q8_0-q8_0.cu))
  371. OBJS_CUDA_TEMP_INST += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/template-instances/fattn-vec*f16-f16.cu))
  372. endif # LLAMA_CUDA_FA_ALL_QUANTS
  373. ifdef LLAMA_CUDA
  374. ifneq ('', '$(wildcard /opt/cuda)')
  375. CUDA_PATH ?= /opt/cuda
  376. else
  377. CUDA_PATH ?= /usr/local/cuda
  378. endif
  379. MK_CPPFLAGS += -DGGML_USE_CUDA -I$(CUDA_PATH)/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include -DGGML_CUDA_USE_GRAPHS
  380. 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
  381. OBJS += ggml-cuda.o
  382. OBJS += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/*.cu))
  383. OBJS += $(OBJS_CUDA_TEMP_INST)
  384. MK_NVCCFLAGS += -use_fast_math
  385. ifdef LLAMA_FATAL_WARNINGS
  386. MK_NVCCFLAGS += -Werror all-warnings
  387. endif # LLAMA_FATAL_WARNINGS
  388. ifndef JETSON_EOL_MODULE_DETECT
  389. MK_NVCCFLAGS += --forward-unknown-to-host-compiler
  390. endif # JETSON_EOL_MODULE_DETECT
  391. ifdef LLAMA_DEBUG
  392. MK_NVCCFLAGS += -lineinfo
  393. endif # LLAMA_DEBUG
  394. ifdef LLAMA_CUDA_DEBUG
  395. MK_NVCCFLAGS += --device-debug
  396. endif # LLAMA_CUDA_DEBUG
  397. ifdef LLAMA_CUDA_NVCC
  398. NVCC = $(CCACHE) $(LLAMA_CUDA_NVCC)
  399. else
  400. NVCC = $(CCACHE) nvcc
  401. endif #LLAMA_CUDA_NVCC
  402. ifdef CUDA_DOCKER_ARCH
  403. MK_NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  404. else ifndef CUDA_POWER_ARCH
  405. MK_NVCCFLAGS += -arch=native
  406. endif # CUDA_DOCKER_ARCH
  407. ifdef LLAMA_CUDA_FORCE_DMMV
  408. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  409. endif # LLAMA_CUDA_FORCE_DMMV
  410. ifdef LLAMA_CUDA_FORCE_MMQ
  411. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  412. endif # LLAMA_CUDA_FORCE_MMQ
  413. ifdef LLAMA_CUDA_DMMV_X
  414. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  415. else
  416. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  417. endif # LLAMA_CUDA_DMMV_X
  418. ifdef LLAMA_CUDA_MMV_Y
  419. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  420. else ifdef LLAMA_CUDA_DMMV_Y
  421. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_DMMV_Y) # for backwards compatibility
  422. else
  423. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  424. endif # LLAMA_CUDA_MMV_Y
  425. ifdef LLAMA_CUDA_F16
  426. MK_NVCCFLAGS += -DGGML_CUDA_F16
  427. endif # LLAMA_CUDA_F16
  428. ifdef LLAMA_CUDA_DMMV_F16
  429. MK_NVCCFLAGS += -DGGML_CUDA_F16
  430. endif # LLAMA_CUDA_DMMV_F16
  431. ifdef LLAMA_CUDA_KQUANTS_ITER
  432. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  433. else
  434. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  435. endif
  436. ifdef LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  437. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(LLAMA_CUDA_PEER_MAX_BATCH_SIZE)
  438. else
  439. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  440. endif # LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  441. ifdef LLAMA_CUDA_NO_PEER_COPY
  442. MK_NVCCFLAGS += -DGGML_CUDA_NO_PEER_COPY
  443. endif # LLAMA_CUDA_NO_PEER_COPY
  444. ifdef LLAMA_CUDA_CCBIN
  445. MK_NVCCFLAGS += -ccbin $(LLAMA_CUDA_CCBIN)
  446. endif # LLAMA_CUDA_CCBIN
  447. ifdef LLAMA_CUDA_FA_ALL_QUANTS
  448. MK_NVCCFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  449. endif # LLAMA_CUDA_FA_ALL_QUANTS
  450. ifdef JETSON_EOL_MODULE_DETECT
  451. define NVCC_COMPILE
  452. $(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 $@
  453. endef # NVCC_COMPILE
  454. else
  455. define NVCC_COMPILE
  456. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  457. endef # NVCC_COMPILE
  458. endif # JETSON_EOL_MODULE_DETECT
  459. ggml-cuda/%.o: ggml-cuda/%.cu ggml.h ggml-common.h ggml-cuda/common.cuh
  460. $(NVCC_COMPILE)
  461. 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)
  462. $(NVCC_COMPILE)
  463. endif # LLAMA_CUDA
  464. ifdef LLAMA_CLBLAST
  465. MK_CPPFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags-only-I clblast OpenCL)
  466. MK_CFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  467. MK_CXXFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  468. # Mac provides OpenCL as a framework
  469. ifeq ($(UNAME_S),Darwin)
  470. MK_LDFLAGS += -lclblast -framework OpenCL
  471. else
  472. MK_LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
  473. endif
  474. OBJS += ggml-opencl.o
  475. ggml-opencl.o: ggml-opencl.cpp ggml-opencl.h
  476. $(CXX) $(CXXFLAGS) -c $< -o $@
  477. endif # LLAMA_CLBLAST
  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. ifndef LLAMA_NO_LLAMAFILE
  565. sgemm.o: sgemm.cpp sgemm.h ggml.h
  566. $(CXX) $(CXXFLAGS) -c $< -o $@
  567. endif
  568. GF_CC := $(CC)
  569. include scripts/get-flags.mk
  570. # combine build flags with cmdline overrides
  571. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  572. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  573. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  574. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  575. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  576. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  577. # identify CUDA host compiler
  578. ifdef LLAMA_CUDA
  579. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  580. include scripts/get-flags.mk
  581. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  582. endif
  583. ifdef LLAMA_CURL
  584. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  585. override LDFLAGS := $(LDFLAGS) -lcurl
  586. endif
  587. #
  588. # Print build information
  589. #
  590. $(info I llama.cpp build info: )
  591. $(info I UNAME_S: $(UNAME_S))
  592. $(info I UNAME_P: $(UNAME_P))
  593. $(info I UNAME_M: $(UNAME_M))
  594. $(info I CFLAGS: $(CFLAGS))
  595. $(info I CXXFLAGS: $(CXXFLAGS))
  596. $(info I NVCCFLAGS: $(NVCCFLAGS))
  597. $(info I LDFLAGS: $(LDFLAGS))
  598. $(info I CC: $(shell $(CC) --version | head -n 1))
  599. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  600. ifdef LLAMA_CUDA
  601. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  602. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  603. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  604. ifndef CUDA_DOCKER_ARCH
  605. ifndef CUDA_POWER_ARCH
  606. $(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 )
  607. endif # CUDA_POWER_ARCH
  608. endif # CUDA_DOCKER_ARCH
  609. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  610. endif # LLAMA_CUDA
  611. $(info )
  612. ifdef LLAMA_CUBLAS
  613. $(info !!!!)
  614. $(info LLAMA_CUBLAS is deprecated and will be removed in the future. Use LLAMA_CUDA instead.)
  615. $(info !!!!)
  616. $(info )
  617. endif
  618. #
  619. # Build library
  620. #
  621. ggml.o: ggml.c ggml.h ggml-cuda.h
  622. $(CC) $(CFLAGS) -c $< -o $@
  623. ggml-alloc.o: ggml-alloc.c ggml.h ggml-alloc.h
  624. $(CC) $(CFLAGS) -c $< -o $@
  625. ggml-backend.o: ggml-backend.c ggml.h ggml-backend.h
  626. $(CC) $(CFLAGS) -c $< -o $@
  627. ggml-quants.o: ggml-quants.c ggml.h ggml-quants.h ggml-common.h
  628. $(CC) $(CFLAGS) -c $< -o $@
  629. unicode.o: unicode.cpp unicode.h
  630. $(CXX) $(CXXFLAGS) -c $< -o $@
  631. unicode-data.o: unicode-data.cpp unicode-data.h
  632. $(CXX) $(CXXFLAGS) -c $< -o $@
  633. OBJS += ggml-alloc.o ggml-backend.o ggml-quants.o unicode.o unicode-data.o
  634. llama.o: llama.cpp unicode.h ggml.h ggml-alloc.h ggml-backend.h ggml-cuda.h ggml-metal.h llama.h
  635. $(CXX) $(CXXFLAGS) -c $< -o $@
  636. COMMON_H_DEPS = common/common.h common/sampling.h common/log.h llama.h
  637. COMMON_DEPS = common.o sampling.o grammar-parser.o build-info.o json-schema-to-grammar.o
  638. common.o: common/common.cpp $(COMMON_H_DEPS)
  639. $(CXX) $(CXXFLAGS) -c $< -o $@
  640. sampling.o: common/sampling.cpp $(COMMON_H_DEPS)
  641. $(CXX) $(CXXFLAGS) -c $< -o $@
  642. console.o: common/console.cpp common/console.h
  643. $(CXX) $(CXXFLAGS) -c $< -o $@
  644. grammar-parser.o: common/grammar-parser.cpp common/grammar-parser.h
  645. $(CXX) $(CXXFLAGS) -c $< -o $@
  646. json-schema-to-grammar.o: common/json-schema-to-grammar.cpp common/json-schema-to-grammar.h
  647. $(CXX) $(CXXFLAGS) -c $< -o $@
  648. train.o: common/train.cpp common/train.h
  649. $(CXX) $(CXXFLAGS) -c $< -o $@
  650. ngram-cache.o: common/ngram-cache.cpp common/ngram-cache.h
  651. $(CXX) $(CXXFLAGS) -c $< -o $@
  652. libllama.so: llama.o ggml.o $(OBJS)
  653. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  654. libllama.a: llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  655. ar rcs libllama.a llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  656. clean:
  657. 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)
  658. rm -vrf ggml-cuda/*.o
  659. rm -vrf ggml-cuda/template-instances/*.o
  660. find examples pocs -type f -name "*.o" -delete
  661. #
  662. # Examples
  663. #
  664. # $< is the first prerequisite, i.e. the source file.
  665. # Explicitly compile this to an object file so that it can be cached with ccache.
  666. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  667. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  668. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  669. main: examples/main/main.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  670. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  671. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  672. @echo
  673. @echo '==== Run ./main -h for help. ===='
  674. @echo
  675. infill: examples/infill/infill.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  676. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  677. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  678. simple: examples/simple/simple.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  679. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  680. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  681. tokenize: examples/tokenize/tokenize.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  682. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  683. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  684. batched: examples/batched/batched.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  685. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  686. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  687. batched-bench: examples/batched-bench/batched-bench.cpp build-info.o ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  688. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  689. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  690. quantize: examples/quantize/quantize.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  691. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  692. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  693. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.o ggml.o llama.o $(OBJS)
  694. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  695. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  696. perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  697. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  698. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  699. imatrix: examples/imatrix/imatrix.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  700. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  701. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  702. embedding: examples/embedding/embedding.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  703. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  704. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  705. gritlm: examples/gritlm/gritlm.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. save-load-state: examples/save-load-state/save-load-state.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. 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)
  712. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  713. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  714. # Portable equivalent of `cd examples/server/public && xxd -i $(notdir $<) ../$(notdir $<).hpp`:
  715. examples/server/%.hpp: examples/server/public/% Makefile
  716. @( export NAME=$(subst .,_,$(subst -,_,$(notdir $<))) && \
  717. echo "unsigned char $${NAME}[] = {" && \
  718. cat $< | od -v -t x1 -An | sed -E 's/([0-9a-fA-F]+)/0x\1, /g' && \
  719. echo "};" && \
  720. echo "unsigned int $${NAME}_len = $(shell cat $< | wc -c );" \
  721. ) > $@
  722. gguf: examples/gguf/gguf.cpp ggml.o $(OBJS)
  723. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  724. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  725. gguf-split: examples/gguf-split/gguf-split.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  726. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  727. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  728. eval-callback: examples/eval-callback/eval-callback.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  729. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  730. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  731. train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  732. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  733. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  734. convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp ggml.o llama.o $(OBJS)
  735. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  736. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  737. llama-bench: examples/llama-bench/llama-bench.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  738. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  739. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  740. 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)
  741. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  742. 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)
  743. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  744. $(CXX) $(CXXFLAGS) -c examples/llava/clip.cpp -o $(call GET_OBJ_FILE, examples/llava/clip.cpp) -Wno-cast-qual
  745. $(CXX) $(CXXFLAGS) -c examples/llava/llava.cpp -o $(call GET_OBJ_FILE, examples/llava/llava.cpp)
  746. $(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)
  747. baby-llama: examples/baby-llama/baby-llama.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  748. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  749. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  750. beam-search: examples/beam-search/beam-search.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  751. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  752. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  753. finetune: examples/finetune/finetune.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  754. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  755. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  756. export-lora: examples/export-lora/export-lora.cpp ggml.o common/common.h $(OBJS)
  757. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  758. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  759. retrieval: examples/retrieval/retrieval.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  760. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  761. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  762. speculative: examples/speculative/speculative.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  763. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  764. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  765. parallel: examples/parallel/parallel.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  766. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  767. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  768. lookahead: examples/lookahead/lookahead.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  769. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  770. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  771. lookup: examples/lookup/lookup.cpp ggml.o llama.o ngram-cache.o $(COMMON_DEPS) $(OBJS)
  772. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  773. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  774. $(CXX) $(CXXFLAGS) -c examples/lookup/lookup-create.cpp -o $(call GET_OBJ_FILE, examples/lookup/lookup-create.cpp)
  775. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, examples/lookup/lookup-create.cpp) -o lookup-create $(LDFLAGS)
  776. $(CXX) $(CXXFLAGS) -c examples/lookup/lookup-merge.cpp -o $(call GET_OBJ_FILE, examples/lookup/lookup-merge.cpp)
  777. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, examples/lookup/lookup-merge.cpp) -o lookup-merge $(LDFLAGS)
  778. $(CXX) $(CXXFLAGS) -c examples/lookup/lookup-stats.cpp -o $(call GET_OBJ_FILE, examples/lookup/lookup-stats.cpp)
  779. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, examples/lookup/lookup-stats.cpp) -o lookup-stats $(LDFLAGS)
  780. passkey: examples/passkey/passkey.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  781. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  782. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  783. gbnf-validator: examples/gbnf-validator/gbnf-validator.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  784. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  785. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  786. ifeq ($(UNAME_S),Darwin)
  787. swift: examples/batched.swift
  788. (cd examples/batched.swift; make build)
  789. endif
  790. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  791. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  792. @if ! cmp -s $@.tmp $@; then \
  793. mv $@.tmp $@; \
  794. else \
  795. rm $@.tmp; \
  796. fi
  797. build-info.o: common/build-info.cpp
  798. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  799. #
  800. # Tests
  801. #
  802. tests: $(TEST_TARGETS)
  803. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.o ggml.o $(OBJS)
  804. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  805. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  806. run-benchmark-matmult: benchmark-matmult
  807. ./$@
  808. .PHONY: run-benchmark-matmult swift
  809. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  810. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  811. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  812. q8dot: pocs/vdot/q8dot.cpp ggml.o $(OBJS)
  813. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  814. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  815. tests/test-llama-grammar: tests/test-llama-grammar.cpp ggml.o grammar-parser.o $(OBJS)
  816. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  817. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  818. tests/test-grammar-parser: tests/test-grammar-parser.cpp ggml.o llama.o grammar-parser.o $(OBJS)
  819. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  820. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  821. tests/test-grammar-integration: tests/test-grammar-integration.cpp ggml.o llama.o grammar-parser.o $(OBJS)
  822. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  823. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  824. tests/test-double-float: tests/test-double-float.cpp ggml.o $(OBJS)
  825. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  826. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  827. 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)
  828. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  829. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  830. tests/test-grad0: tests/test-grad0.cpp ggml.o $(OBJS)
  831. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  832. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  833. tests/test-opt: tests/test-opt.cpp ggml.o $(OBJS)
  834. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  835. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  836. tests/test-quantize-fns: tests/test-quantize-fns.cpp ggml.o $(OBJS)
  837. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  838. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  839. tests/test-quantize-perf: tests/test-quantize-perf.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-sampling: tests/test-sampling.cpp ggml.o llama.o $(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-tokenizer-0: tests/test-tokenizer-0.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(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-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  849. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  850. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  851. tests/test-tokenizer-1-spm: tests/test-tokenizer-1-spm.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  852. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  853. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  854. tests/test-rope: tests/test-rope.cpp ggml.o $(OBJS)
  855. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  856. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  857. tests/test-c.o: tests/test-c.c llama.h
  858. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  859. tests/test-backend-ops: tests/test-backend-ops.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-model-load-cancel: tests/test-model-load-cancel.cpp ggml.o llama.o tests/get-model.cpp $(COMMON_DEPS) $(OBJS)
  863. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  864. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  865. tests/test-autorelease: tests/test-autorelease.cpp ggml.o llama.o tests/get-model.cpp $(COMMON_DEPS) $(OBJS)
  866. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  867. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  868. tests/test-chat-template: tests/test-chat-template.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  869. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  870. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)