Makefile 39 KB

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