Makefile 40 KB

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