Makefile 40 KB

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