Makefile 38 KB

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