Makefile 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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_FORCE_CUBLAS
  471. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  472. endif # LLAMA_CUDA_FORCE_CUBLAS
  473. ifdef LLAMA_CUDA_DMMV_X
  474. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  475. else
  476. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  477. endif # LLAMA_CUDA_DMMV_X
  478. ifdef LLAMA_CUDA_MMV_Y
  479. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  480. else ifdef LLAMA_CUDA_DMMV_Y
  481. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_DMMV_Y) # for backwards compatibility
  482. else
  483. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  484. endif # LLAMA_CUDA_MMV_Y
  485. ifdef LLAMA_CUDA_F16
  486. MK_NVCCFLAGS += -DGGML_CUDA_F16
  487. endif # LLAMA_CUDA_F16
  488. ifdef LLAMA_CUDA_DMMV_F16
  489. MK_NVCCFLAGS += -DGGML_CUDA_F16
  490. endif # LLAMA_CUDA_DMMV_F16
  491. ifdef LLAMA_CUDA_KQUANTS_ITER
  492. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  493. else
  494. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  495. endif
  496. ifdef LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  497. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(LLAMA_CUDA_PEER_MAX_BATCH_SIZE)
  498. else
  499. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  500. endif # LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  501. ifdef LLAMA_CUDA_NO_PEER_COPY
  502. MK_NVCCFLAGS += -DGGML_CUDA_NO_PEER_COPY
  503. endif # LLAMA_CUDA_NO_PEER_COPY
  504. ifdef LLAMA_CUDA_CCBIN
  505. MK_NVCCFLAGS += -ccbin $(LLAMA_CUDA_CCBIN)
  506. endif # LLAMA_CUDA_CCBIN
  507. ifdef LLAMA_CUDA_FA_ALL_QUANTS
  508. MK_NVCCFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  509. endif # LLAMA_CUDA_FA_ALL_QUANTS
  510. ifdef JETSON_EOL_MODULE_DETECT
  511. define NVCC_COMPILE
  512. $(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 $@
  513. endef # NVCC_COMPILE
  514. else
  515. define NVCC_COMPILE
  516. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  517. endef # NVCC_COMPILE
  518. endif # JETSON_EOL_MODULE_DETECT
  519. ggml-cuda/%.o: ggml-cuda/%.cu ggml.h ggml-common.h ggml-cuda/common.cuh
  520. $(NVCC_COMPILE)
  521. 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)
  522. $(NVCC_COMPILE)
  523. endif # LLAMA_CUDA
  524. ifdef LLAMA_VULKAN
  525. MK_CPPFLAGS += -DGGML_USE_VULKAN
  526. MK_LDFLAGS += -lvulkan
  527. OBJS += ggml-vulkan.o
  528. ifdef LLAMA_VULKAN_CHECK_RESULTS
  529. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  530. endif
  531. ifdef LLAMA_VULKAN_DEBUG
  532. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  533. endif
  534. ifdef LLAMA_VULKAN_MEMORY_DEBUG
  535. MK_CPPFLAGS += -DGGML_VULKAN_MEMORY_DEBUG
  536. endif
  537. ifdef LLAMA_VULKAN_VALIDATE
  538. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  539. endif
  540. ifdef LLAMA_VULKAN_RUN_TESTS
  541. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  542. endif
  543. ggml-vulkan.o: ggml-vulkan.cpp ggml-vulkan.h
  544. $(CXX) $(CXXFLAGS) -c $< -o $@
  545. endif # LLAMA_VULKAN
  546. ifdef LLAMA_HIPBLAS
  547. ifeq ($(wildcard /opt/rocm),)
  548. ROCM_PATH ?= /usr
  549. AMDGPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  550. else
  551. ROCM_PATH ?= /opt/rocm
  552. AMDGPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  553. endif
  554. HIPCC ?= $(CCACHE) $(ROCM_PATH)/bin/hipcc
  555. LLAMA_CUDA_DMMV_X ?= 32
  556. LLAMA_CUDA_MMV_Y ?= 1
  557. LLAMA_CUDA_KQUANTS_ITER ?= 2
  558. MK_CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUDA
  559. ifdef LLAMA_HIP_UMA
  560. MK_CPPFLAGS += -DGGML_HIP_UMA
  561. endif # LLAMA_HIP_UMA
  562. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  563. MK_LDFLAGS += -L$(ROCM_PATH)/lib64 -Wl,-rpath=$(ROCM_PATH)/lib64
  564. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  565. HIPFLAGS += $(addprefix --offload-arch=,$(AMDGPU_TARGETS))
  566. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  567. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  568. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  569. ifdef LLAMA_CUDA_FORCE_DMMV
  570. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  571. endif # LLAMA_CUDA_FORCE_DMMV
  572. ifdef LLAMA_CUDA_NO_PEER_COPY
  573. HIPFLAGS += -DGGML_CUDA_NO_PEER_COPY
  574. endif # LLAMA_CUDA_NO_PEER_COPY
  575. OBJS += ggml-cuda.o
  576. OBJS += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/*.cu))
  577. OBJS += $(OBJS_CUDA_TEMP_INST)
  578. 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)
  579. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  580. ggml-cuda/%.o: ggml-cuda/%.cu ggml.h ggml-common.h ggml-cuda/common.cuh
  581. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  582. endif # LLAMA_HIPBLAS
  583. ifdef LLAMA_METAL
  584. MK_CPPFLAGS += -DGGML_USE_METAL
  585. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  586. OBJS += ggml-metal.o
  587. ifdef LLAMA_METAL_NDEBUG
  588. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  589. endif
  590. ifdef LLAMA_METAL_EMBED_LIBRARY
  591. MK_CPPFLAGS += -DGGML_METAL_EMBED_LIBRARY
  592. OBJS += ggml-metal-embed.o
  593. endif
  594. endif # LLAMA_METAL
  595. ifdef LLAMA_METAL
  596. ggml-metal.o: ggml-metal.m ggml-metal.h ggml.h
  597. $(CC) $(CFLAGS) -c $< -o $@
  598. ifdef LLAMA_METAL_EMBED_LIBRARY
  599. ggml-metal-embed.o: ggml-metal.metal ggml-common.h
  600. @echo "Embedding Metal library"
  601. @sed -e '/#include "ggml-common.h"/r ggml-common.h' -e '/#include "ggml-common.h"/d' < ggml-metal.metal > ggml-metal-embed.metal
  602. $(eval TEMP_ASSEMBLY=$(shell mktemp))
  603. @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)
  604. @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)
  605. @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)
  606. @echo ".incbin \"ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY)
  607. @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)
  608. @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)
  609. @$(AS) $(TEMP_ASSEMBLY) -o $@
  610. @rm -f ${TEMP_ASSEMBLY}
  611. endif
  612. endif # LLAMA_METAL
  613. OBJS += ggml-alloc.o ggml-backend.o ggml-quants.o unicode.o unicode-data.o
  614. COMMON_H_DEPS = common/common.h common/sampling.h common/log.h llama.h
  615. COMMON_DEPS = common.o sampling.o grammar-parser.o build-info.o json-schema-to-grammar.o
  616. ifndef LLAMA_NO_LLAMAFILE
  617. sgemm.o: sgemm.cpp sgemm.h ggml.h
  618. $(CXX) $(CXXFLAGS) -c $< -o $@
  619. endif
  620. ifdef LLAMA_RPC
  621. ggml-rpc.o: ggml-rpc.cpp ggml-rpc.h
  622. $(CXX) $(CXXFLAGS) -c $< -o $@
  623. rpc-server.o: examples/rpc/rpc-server.cpp ggml-rpc.h
  624. $(CXX) $(CXXFLAGS) -c $< -o $@
  625. rpc-server: rpc-server.o ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  626. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  627. endif # LLAMA_RPC
  628. GF_CC := $(CC)
  629. include scripts/get-flags.mk
  630. # combine build flags with cmdline overrides
  631. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  632. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  633. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  634. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  635. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  636. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  637. # identify CUDA host compiler
  638. ifdef LLAMA_CUDA
  639. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  640. include scripts/get-flags.mk
  641. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  642. endif
  643. ifdef LLAMA_CURL
  644. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  645. override LDFLAGS := $(LDFLAGS) -lcurl
  646. endif
  647. #
  648. # Print build information
  649. #
  650. $(info I llama.cpp build info: )
  651. $(info I UNAME_S: $(UNAME_S))
  652. $(info I UNAME_P: $(UNAME_P))
  653. $(info I UNAME_M: $(UNAME_M))
  654. $(info I CFLAGS: $(CFLAGS))
  655. $(info I CXXFLAGS: $(CXXFLAGS))
  656. $(info I NVCCFLAGS: $(NVCCFLAGS))
  657. $(info I LDFLAGS: $(LDFLAGS))
  658. $(info I CC: $(shell $(CC) --version | head -n 1))
  659. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  660. ifdef LLAMA_CUDA
  661. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  662. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  663. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  664. ifndef CUDA_DOCKER_ARCH
  665. ifndef CUDA_POWER_ARCH
  666. $(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 )
  667. endif # CUDA_POWER_ARCH
  668. endif # CUDA_DOCKER_ARCH
  669. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  670. endif # LLAMA_CUDA
  671. $(info )
  672. ifdef LLAMA_CUBLAS
  673. $(info !!!!)
  674. $(info LLAMA_CUBLAS is deprecated and will be removed in the future. Use LLAMA_CUDA instead.)
  675. $(info !!!!)
  676. $(info )
  677. endif
  678. #
  679. # Build library
  680. #
  681. ggml.o: ggml.c ggml.h ggml-cuda.h
  682. $(CC) $(CFLAGS) -c $< -o $@
  683. ggml-alloc.o: ggml-alloc.c ggml.h ggml-alloc.h
  684. $(CC) $(CFLAGS) -c $< -o $@
  685. ggml-backend.o: ggml-backend.c ggml.h ggml-backend.h
  686. $(CC) $(CFLAGS) -c $< -o $@
  687. ggml-quants.o: ggml-quants.c ggml.h ggml-quants.h ggml-common.h
  688. $(CC) $(CFLAGS) -c $< -o $@
  689. ggml-blas.o: ggml-blas.cpp ggml-blas.h
  690. $(CXX) $(CXXFLAGS) -c $< -o $@
  691. unicode.o: unicode.cpp unicode.h
  692. $(CXX) $(CXXFLAGS) -c $< -o $@
  693. unicode-data.o: unicode-data.cpp unicode-data.h
  694. $(CXX) $(CXXFLAGS) -c $< -o $@
  695. llama.o: llama.cpp unicode.h ggml.h ggml-alloc.h ggml-backend.h ggml-cuda.h ggml-metal.h llama.h
  696. $(CXX) $(CXXFLAGS) -c $< -o $@
  697. common.o: common/common.cpp $(COMMON_H_DEPS)
  698. $(CXX) $(CXXFLAGS) -c $< -o $@
  699. sampling.o: common/sampling.cpp $(COMMON_H_DEPS)
  700. $(CXX) $(CXXFLAGS) -c $< -o $@
  701. console.o: common/console.cpp common/console.h
  702. $(CXX) $(CXXFLAGS) -c $< -o $@
  703. grammar-parser.o: common/grammar-parser.cpp common/grammar-parser.h
  704. $(CXX) $(CXXFLAGS) -c $< -o $@
  705. json-schema-to-grammar.o: common/json-schema-to-grammar.cpp common/json-schema-to-grammar.h
  706. $(CXX) $(CXXFLAGS) -c $< -o $@
  707. train.o: common/train.cpp common/train.h
  708. $(CXX) $(CXXFLAGS) -c $< -o $@
  709. ngram-cache.o: common/ngram-cache.cpp common/ngram-cache.h
  710. $(CXX) $(CXXFLAGS) -c $< -o $@
  711. libllama.so: llama.o ggml.o $(OBJS)
  712. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  713. libllama.a: llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  714. ar rcs libllama.a llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  715. clean:
  716. rm -vrf *.o tests/*.o *.so *.a *.dll common/build-info.cpp *.dot $(COV_TARGETS) $(BUILD_TARGETS) $(TEST_TARGETS)
  717. rm -vrf ggml-cuda/*.o
  718. rm -vrf ggml-cuda/template-instances/*.o
  719. find examples pocs -type f -name "*.o" -delete
  720. #
  721. # Examples
  722. #
  723. # $< is the first prerequisite, i.e. the source file.
  724. # Explicitly compile this to an object file so that it can be cached with ccache.
  725. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  726. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  727. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  728. llama-cli: examples/main/main.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  729. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  730. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  731. @echo
  732. @echo '==== Run ./llama-cli -h for help. ===='
  733. @echo
  734. llama-infill: examples/infill/infill.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  735. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  736. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  737. llama-simple: examples/simple/simple.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-tokenize: examples/tokenize/tokenize.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: examples/batched/batched.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  744. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  745. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  746. llama-batched-bench: examples/batched-bench/batched-bench.cpp build-info.o 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: examples/quantize/quantize.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  750. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  751. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  752. llama-quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.o ggml.o llama.o $(OBJS)
  753. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  754. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  755. llama-perplexity: examples/perplexity/perplexity.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-imatrix: examples/imatrix/imatrix.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-embedding: examples/embedding/embedding.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-gritlm: examples/gritlm/gritlm.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-save-load-state: examples/save-load-state/save-load-state.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  768. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  769. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  770. 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)
  771. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  772. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  773. # Portable equivalent of `cd examples/server/public && xxd -i $(notdir $<) ../$(notdir $<).hpp`:
  774. examples/server/%.hpp: examples/server/public/% Makefile
  775. @( export NAME=$(subst .,_,$(subst -,_,$(notdir $<))) && \
  776. echo "unsigned char $${NAME}[] = {" && \
  777. cat $< | od -v -t x1 -An | sed -E 's/([0-9a-fA-F]+)/0x\1, /g' && \
  778. echo "};" && \
  779. echo "unsigned int $${NAME}_len = $(shell cat $< | wc -c );" \
  780. ) > $@
  781. llama-gguf: examples/gguf/gguf.cpp ggml.o $(OBJS)
  782. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  783. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  784. llama-gguf-split: examples/gguf-split/gguf-split.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-eval-callback: examples/eval-callback/eval-callback.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-cvector-generator: examples/cvector-generator/cvector-generator.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  791. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  792. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  793. llama-train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp ggml.o llama.o $(COMMON_DEPS) train.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-convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp ggml.o llama.o $(OBJS)
  797. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  798. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  799. llama-bench: examples/llama-bench/llama-bench.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  800. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  801. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  802. 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)
  803. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  804. 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)
  805. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  806. $(CXX) $(CXXFLAGS) -c examples/llava/clip.cpp -o $(call GET_OBJ_FILE, examples/llava/clip.cpp) -Wno-cast-qual
  807. $(CXX) $(CXXFLAGS) -c examples/llava/llava.cpp -o $(call GET_OBJ_FILE, examples/llava/llava.cpp)
  808. $(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)
  809. llama-baby-llama: examples/baby-llama/baby-llama.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-finetune: examples/finetune/finetune.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  813. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  814. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  815. llama-export-lora: examples/export-lora/export-lora.cpp ggml.o common/common.h $(OBJS)
  816. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  817. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  818. llama-retrieval: examples/retrieval/retrieval.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  819. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  820. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  821. llama-speculative: examples/speculative/speculative.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  822. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  823. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  824. llama-parallel: examples/parallel/parallel.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-lookahead: examples/lookahead/lookahead.cpp ggml.o llama.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: examples/lookup/lookup.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-create: examples/lookup/lookup-create.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-merge: examples/lookup/lookup-merge.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-lookup-stats: examples/lookup/lookup-stats.cpp ggml.o llama.o ngram-cache.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-passkey: examples/passkey/passkey.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  843. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  844. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  845. llama-gbnf-validator: examples/gbnf-validator/gbnf-validator.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  846. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  847. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  848. ifeq ($(UNAME_S),Darwin)
  849. swift: examples/batched.swift
  850. (cd examples/batched.swift; make build)
  851. endif
  852. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  853. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  854. @if ! cmp -s $@.tmp $@; then \
  855. mv $@.tmp $@; \
  856. else \
  857. rm $@.tmp; \
  858. fi
  859. build-info.o: common/build-info.cpp
  860. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  861. #
  862. # Tests
  863. #
  864. tests: $(TEST_TARGETS)
  865. llama-benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.o ggml.o $(OBJS)
  866. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  867. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  868. run-benchmark-matmult: llama-benchmark-matmult
  869. ./$@
  870. .PHONY: run-benchmark-matmult swift
  871. llama-vdot: pocs/vdot/vdot.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. llama-q8dot: pocs/vdot/q8dot.cpp ggml.o $(OBJS)
  875. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  876. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  877. tests/test-llama-grammar: tests/test-llama-grammar.cpp ggml.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-parser: tests/test-grammar-parser.cpp 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-grammar-integration: tests/test-grammar-integration.cpp json-schema-to-grammar.o ggml.o llama.o grammar-parser.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-double-float: tests/test-double-float.cpp ggml.o $(OBJS)
  887. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  888. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  889. 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)
  890. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  891. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  892. tests/test-grad0: tests/test-grad0.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-opt: tests/test-opt.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-fns: tests/test-quantize-fns.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-quantize-perf: tests/test-quantize-perf.cpp ggml.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-sampling: tests/test-sampling.cpp ggml.o llama.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-0: tests/test-tokenizer-0.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-bpe: tests/test-tokenizer-1-bpe.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-tokenizer-1-spm: tests/test-tokenizer-1-spm.cpp ggml.o llama.o $(COMMON_DEPS) console.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-rope: tests/test-rope.cpp ggml.o $(OBJS)
  917. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  918. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  919. tests/test-c.o: tests/test-c.c llama.h
  920. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  921. tests/test-backend-ops: tests/test-backend-ops.cpp ggml.o $(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-model-load-cancel: tests/test-model-load-cancel.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-autorelease: tests/test-autorelease.cpp ggml.o llama.o tests/get-model.cpp $(COMMON_DEPS) $(OBJS)
  928. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  929. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  930. tests/test-chat-template: tests/test-chat-template.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  931. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  932. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)