Makefile 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. # Define the default target now so that it is always the first target
  2. BUILD_TARGETS = \
  3. libllava.a \
  4. llama-batched \
  5. llama-batched-bench \
  6. llama-bench \
  7. llama-cli \
  8. llama-convert-llama2c-to-ggml \
  9. llama-embedding \
  10. llama-eval-callback \
  11. llama-export-lora \
  12. llama-gbnf-validator \
  13. llama-gguf \
  14. llama-gguf-hash \
  15. llama-gguf-split \
  16. llama-gritlm \
  17. llama-imatrix \
  18. llama-infill \
  19. llama-llava-cli \
  20. llama-minicpmv-cli\
  21. llama-lookahead \
  22. llama-lookup \
  23. llama-lookup-create \
  24. llama-lookup-merge \
  25. llama-lookup-stats \
  26. llama-parallel \
  27. llama-passkey \
  28. llama-perplexity \
  29. llama-q8dot \
  30. llama-quantize \
  31. llama-quantize-stats \
  32. llama-retrieval \
  33. llama-save-load-state \
  34. llama-server \
  35. llama-simple \
  36. llama-simple-chat \
  37. llama-run \
  38. llama-speculative \
  39. llama-tokenize \
  40. llama-vdot \
  41. llama-cvector-generator \
  42. llama-gen-docs \
  43. tests/test-c.o
  44. # Binaries only useful for tests
  45. TEST_TARGETS = \
  46. tests/test-arg-parser \
  47. tests/test-autorelease \
  48. tests/test-backend-ops \
  49. tests/test-chat-template \
  50. tests/test-double-float \
  51. tests/test-grammar-integration \
  52. tests/test-grammar-parser \
  53. tests/test-json-schema-to-grammar \
  54. tests/test-llama-grammar \
  55. tests/test-log \
  56. tests/test-model-load-cancel \
  57. tests/test-quantize-fns \
  58. tests/test-quantize-perf \
  59. tests/test-rope \
  60. tests/test-sampling \
  61. tests/test-tokenizer-0 \
  62. tests/test-tokenizer-1-bpe \
  63. tests/test-tokenizer-1-spm
  64. # tests/test-opt \
  65. # Legacy build targets that were renamed in #7809, but should still be removed when the project is cleaned
  66. LEGACY_TARGETS_CLEAN = main quantize quantize-stats perplexity imatrix embedding vdot q8dot convert-llama2c-to-ggml \
  67. simple batched batched-bench save-load-state server gguf gguf-split eval-callback llama-bench libllava.a llava-cli baby-llama \
  68. retrieval speculative infill tokenize parallel export-lora lookahead lookup passkey gritlm
  69. # Legacy build targets that were renamed in #7809, but we want to build binaries that for them that output a deprecation warning if people try to use them.
  70. # We don't want to clutter things too much, so we only build replacements for the most commonly used binaries.
  71. LEGACY_TARGETS_BUILD = main quantize perplexity embedding server
  72. # Deprecation aliases
  73. ifdef LLAMA_CUBLAS
  74. $(error LLAMA_CUBLAS is removed. Use GGML_CUDA instead.)
  75. endif
  76. ifdef LLAMA_CUDA
  77. GGML_CUDA := 1
  78. DEPRECATE_WARNING := 1
  79. endif
  80. ifdef LLAMA_KOMPUTE
  81. GGML_KOMPUTE := 1
  82. DEPRECATE_WARNING := 1
  83. endif
  84. ifdef LLAMA_METAL
  85. GGML_METAL := 1
  86. DEPRECATE_WARNING := 1
  87. endif
  88. ifdef LLAMA_RPC
  89. GGML_RPC := 1
  90. DEPRECATE_WARNING := 1
  91. endif
  92. ifdef LLAMA_SYCL
  93. GGML_SYCL := 1
  94. DEPRECATE_WARNING := 1
  95. endif
  96. ifdef LLAMA_SYCL_F16
  97. GGML_SYCL_F16 := 1
  98. DEPRECATE_WARNING := 1
  99. endif
  100. ifdef LLAMA_OPENBLAS
  101. GGML_OPENBLAS := 1
  102. DEPRECATE_WARNING := 1
  103. endif
  104. ifdef LLAMA_OPENBLAS64
  105. GGML_OPENBLAS64 := 1
  106. DEPRECATE_WARNING := 1
  107. endif
  108. ifdef LLAMA_BLIS
  109. GGML_BLIS := 1
  110. DEPRECATE_WARNING := 1
  111. endif
  112. ifdef LLAMA_NO_LLAMAFILE
  113. GGML_NO_LLAMAFILE := 1
  114. DEPRECATE_WARNING := 1
  115. endif
  116. ifdef LLAMA_NO_ACCELERATE
  117. GGML_NO_ACCELERATE := 1
  118. DEPRECATE_WARNING := 1
  119. endif
  120. ifdef LLAMA_NO_OPENMP
  121. GGML_NO_OPENMP := 1
  122. DEPRECATE_WARNING := 1
  123. endif
  124. ifdef LLAMA_NO_METAL
  125. GGML_NO_METAL := 1
  126. DEPRECATE_WARNING := 1
  127. endif
  128. ifdef LLAMA_DISABLE_LOGS
  129. REMOVE_WARNING := 1
  130. endif
  131. ifdef LLAMA_SERVER_VERBOSE
  132. REMOVE_WARNING := 1
  133. endif
  134. ifndef UNAME_S
  135. UNAME_S := $(shell uname -s)
  136. endif
  137. ifndef UNAME_P
  138. UNAME_P := $(shell uname -p)
  139. endif
  140. ifndef UNAME_M
  141. UNAME_M := $(shell uname -m)
  142. endif
  143. # In GNU make default CXX is g++ instead of c++. Let's fix that so that users
  144. # of non-gcc compilers don't have to provide g++ alias or wrapper.
  145. DEFCC := cc
  146. DEFCXX := c++
  147. ifeq ($(origin CC),default)
  148. CC := $(DEFCC)
  149. endif
  150. ifeq ($(origin CXX),default)
  151. CXX := $(DEFCXX)
  152. endif
  153. # Mac OS + Arm can report x86_64
  154. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  155. ifeq ($(UNAME_S),Darwin)
  156. ifndef GGML_NO_METAL
  157. GGML_METAL := 1
  158. endif
  159. GGML_NO_OPENMP := 1
  160. ifneq ($(UNAME_P),arm)
  161. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  162. ifeq ($(SYSCTL_M),1)
  163. # UNAME_P := arm
  164. # UNAME_M := arm64
  165. 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)
  166. endif
  167. endif
  168. endif
  169. ifdef GGML_METAL
  170. GGML_METAL_EMBED_LIBRARY := 1
  171. endif
  172. ifdef GGML_RPC
  173. BUILD_TARGETS += rpc-server
  174. endif
  175. ifdef GGML_VULKAN
  176. BUILD_TARGETS += vulkan-shaders-gen
  177. endif
  178. default: $(BUILD_TARGETS) $(LEGACY_TARGETS_BUILD)
  179. test: $(TEST_TARGETS)
  180. @failures=0; \
  181. for test_target in $(TEST_TARGETS); do \
  182. if [ "$$test_target" = "tests/test-tokenizer-0" ]; then \
  183. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-spm.gguf; \
  184. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-bpe.gguf; \
  185. ./$$test_target $(CURDIR)/models/ggml-vocab-phi-3.gguf; \
  186. ./$$test_target $(CURDIR)/models/ggml-vocab-falcon.gguf; \
  187. ./$$test_target $(CURDIR)/models/ggml-vocab-bert-bge.gguf; \
  188. ./$$test_target $(CURDIR)/models/ggml-vocab-starcoder.gguf; \
  189. ./$$test_target $(CURDIR)/models/ggml-vocab-gpt-2.gguf; \
  190. ./$$test_target $(CURDIR)/models/ggml-vocab-refact.gguf; \
  191. elif [ "$$test_target" = "tests/test-tokenizer-1-spm" ]; then \
  192. continue; \
  193. elif [ "$$test_target" = "tests/test-tokenizer-1-bpe" ]; then \
  194. continue; \
  195. else \
  196. echo "Running test $$test_target..."; \
  197. ./$$test_target; \
  198. fi; \
  199. if [ $$? -ne 0 ]; then \
  200. printf 'Test %s FAILED!\n\n' $$test_target; \
  201. failures=$$(( failures + 1 )); \
  202. else \
  203. printf 'Test %s passed.\n\n' $$test_target; \
  204. fi; \
  205. done; \
  206. if [ $$failures -gt 0 ]; then \
  207. printf '\n%s tests failed.\n' $$failures; \
  208. exit 1; \
  209. fi
  210. @echo 'All tests passed.'
  211. all: $(BUILD_TARGETS) $(TEST_TARGETS) $(LEGACY_TARGETS_BUILD)
  212. ifdef RISCV_CROSS_COMPILE
  213. CC := riscv64-unknown-linux-gnu-gcc
  214. CXX := riscv64-unknown-linux-gnu-g++
  215. endif
  216. #
  217. # Compile flags
  218. #
  219. # keep standard at C11 and C++11
  220. MK_CPPFLAGS = -Iggml/include -Iggml/src -Iinclude -Isrc -Icommon -DGGML_USE_CPU
  221. MK_CFLAGS = -std=c11 -fPIC
  222. MK_CXXFLAGS = -std=c++11 -fPIC
  223. MK_NVCCFLAGS = -std=c++11
  224. ifdef LLAMA_NO_CCACHE
  225. GGML_NO_CCACHE := 1
  226. DEPRECATE_WARNING := 1
  227. endif
  228. ifndef GGML_NO_CCACHE
  229. CCACHE := $(shell which ccache)
  230. ifdef CCACHE
  231. export CCACHE_SLOPPINESS = time_macros
  232. $(info I ccache found, compilation results will be cached. Disable with GGML_NO_CCACHE.)
  233. CC := $(CCACHE) $(CC)
  234. CXX := $(CCACHE) $(CXX)
  235. else
  236. $(info I ccache not found. Consider installing it for faster compilation.)
  237. endif # CCACHE
  238. endif # GGML_NO_CCACHE
  239. # clock_gettime came in POSIX.1b (1993)
  240. # CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
  241. # posix_memalign came in POSIX.1-2001 / SUSv3
  242. # M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
  243. MK_CPPFLAGS += -D_XOPEN_SOURCE=600
  244. # Somehow in OpenBSD whenever POSIX conformance is specified
  245. # some string functions rely on locale_t availability,
  246. # which was introduced in POSIX.1-2008, forcing us to go higher
  247. ifeq ($(UNAME_S),OpenBSD)
  248. MK_CPPFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
  249. endif
  250. # Data types, macros and functions related to controlling CPU affinity and
  251. # some memory allocation are available on Linux through GNU extensions in libc
  252. ifeq ($(UNAME_S),Linux)
  253. MK_CPPFLAGS += -D_GNU_SOURCE
  254. MK_LDFLAGS += -ldl
  255. endif
  256. # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
  257. # and on macOS its availability depends on enabling Darwin extensions
  258. # similarly on DragonFly, enabling BSD extensions is necessary
  259. ifeq ($(UNAME_S),Darwin)
  260. MK_CPPFLAGS += -D_DARWIN_C_SOURCE
  261. endif
  262. ifeq ($(UNAME_S),DragonFly)
  263. MK_CPPFLAGS += -D__BSD_VISIBLE
  264. endif
  265. # alloca is a non-standard interface that is not visible on BSDs when
  266. # POSIX conformance is specified, but not all of them provide a clean way
  267. # to enable it in such cases
  268. ifeq ($(UNAME_S),FreeBSD)
  269. MK_CPPFLAGS += -D__BSD_VISIBLE
  270. endif
  271. ifeq ($(UNAME_S),NetBSD)
  272. MK_CPPFLAGS += -D_NETBSD_SOURCE
  273. endif
  274. ifeq ($(UNAME_S),OpenBSD)
  275. MK_CPPFLAGS += -D_BSD_SOURCE
  276. endif
  277. ifdef GGML_SCHED_MAX_COPIES
  278. MK_CPPFLAGS += -DGGML_SCHED_MAX_COPIES=$(GGML_SCHED_MAX_COPIES)
  279. endif
  280. ifdef LLAMA_DEBUG
  281. MK_CFLAGS += -O0 -g
  282. MK_CXXFLAGS += -O0 -g
  283. MK_LDFLAGS += -g
  284. MK_NVCCFLAGS += -O0 -g
  285. ifeq ($(UNAME_S),Linux)
  286. MK_CPPFLAGS += -D_GLIBCXX_ASSERTIONS
  287. endif
  288. else
  289. MK_CPPFLAGS += -DNDEBUG
  290. MK_CFLAGS += -O3 -g
  291. MK_CXXFLAGS += -O3 -g
  292. MK_NVCCFLAGS += -O3 -g
  293. endif
  294. ifdef LLAMA_SANITIZE_THREAD
  295. MK_CFLAGS += -fsanitize=thread -g
  296. MK_CXXFLAGS += -fsanitize=thread -g
  297. MK_LDFLAGS += -fsanitize=thread -g
  298. endif
  299. ifdef LLAMA_SANITIZE_ADDRESS
  300. MK_CFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  301. MK_CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  302. MK_LDFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  303. endif
  304. ifdef LLAMA_SANITIZE_UNDEFINED
  305. MK_CFLAGS += -fsanitize=undefined -g
  306. MK_CXXFLAGS += -fsanitize=undefined -g
  307. MK_LDFLAGS += -fsanitize=undefined -g
  308. endif
  309. ifdef LLAMA_SERVER_SSL
  310. MK_CPPFLAGS += -DCPPHTTPLIB_OPENSSL_SUPPORT
  311. MK_LDFLAGS += -lssl -lcrypto
  312. endif
  313. ifndef GGML_NO_CPU_AARCH64
  314. MK_CPPFLAGS += -DGGML_USE_CPU_AARCH64
  315. endif
  316. # warnings
  317. WARN_FLAGS = \
  318. -Wall \
  319. -Wextra \
  320. -Wpedantic \
  321. -Wcast-qual \
  322. -Wno-unused-function
  323. MK_CFLAGS += \
  324. $(WARN_FLAGS) \
  325. -Wshadow \
  326. -Wstrict-prototypes \
  327. -Wpointer-arith \
  328. -Wmissing-prototypes \
  329. -Werror=implicit-int \
  330. -Werror=implicit-function-declaration
  331. MK_CXXFLAGS += \
  332. $(WARN_FLAGS) \
  333. -Wmissing-declarations \
  334. -Wmissing-noreturn
  335. ifeq ($(LLAMA_FATAL_WARNINGS),1)
  336. MK_CFLAGS += -Werror
  337. MK_CXXFLAGS += -Werror
  338. endif
  339. # this version of Apple ld64 is buggy
  340. ifneq '' '$(findstring dyld-1015.7,$(shell $(CC) $(LDFLAGS) -Wl,-v 2>&1))'
  341. MK_CPPFLAGS += -DHAVE_BUGGY_APPLE_LINKER
  342. endif
  343. # OS specific
  344. # TODO: support Windows
  345. ifneq '' '$(filter $(UNAME_S),Linux Darwin FreeBSD NetBSD OpenBSD Haiku)'
  346. MK_CFLAGS += -pthread
  347. MK_CXXFLAGS += -pthread
  348. endif
  349. # detect Windows
  350. ifneq ($(findstring _NT,$(UNAME_S)),)
  351. _WIN32 := 1
  352. endif
  353. # library name prefix
  354. ifneq ($(_WIN32),1)
  355. LIB_PRE := lib
  356. endif
  357. # Dynamic Shared Object extension
  358. ifneq ($(_WIN32),1)
  359. DSO_EXT := .so
  360. else
  361. DSO_EXT := .dll
  362. endif
  363. # Windows Sockets 2 (Winsock) for network-capable apps
  364. ifeq ($(_WIN32),1)
  365. LWINSOCK2 := -lws2_32
  366. endif
  367. ifdef LLAMA_GPROF
  368. MK_CFLAGS += -pg
  369. MK_CXXFLAGS += -pg
  370. endif
  371. # Architecture specific
  372. # TODO: probably these flags need to be tweaked on some architectures
  373. # feel free to update the Makefile for your architecture and send a pull request or issue
  374. ifndef RISCV_CROSS_COMPILE
  375. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
  376. # Use all CPU extensions that are available:
  377. MK_CFLAGS += -march=native -mtune=native
  378. HOST_CXXFLAGS += -march=native -mtune=native
  379. # Usage AVX-only
  380. #MK_CFLAGS += -mfma -mf16c -mavx
  381. #MK_CXXFLAGS += -mfma -mf16c -mavx
  382. # Usage SSSE3-only (Not is SSE3!)
  383. #MK_CFLAGS += -mssse3
  384. #MK_CXXFLAGS += -mssse3
  385. endif
  386. ifneq '' '$(findstring mingw,$(shell $(CC) -dumpmachine))'
  387. # The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
  388. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
  389. # https://github.com/ggerganov/llama.cpp/issues/2922
  390. MK_CFLAGS += -Xassembler -muse-unaligned-vector-move
  391. MK_CXXFLAGS += -Xassembler -muse-unaligned-vector-move
  392. # Target Windows 8 for PrefetchVirtualMemory
  393. MK_CPPFLAGS += -D_WIN32_WINNT=0x602
  394. endif
  395. ifneq ($(filter aarch64%,$(UNAME_M)),)
  396. # Apple M1, M2, etc.
  397. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  398. # Nvidia Jetson
  399. MK_CFLAGS += -mcpu=native
  400. MK_CXXFLAGS += -mcpu=native
  401. JETSON_RELEASE_INFO = $(shell jetson_release)
  402. ifdef JETSON_RELEASE_INFO
  403. ifneq ($(filter TX2%,$(JETSON_RELEASE_INFO)),)
  404. JETSON_EOL_MODULE_DETECT = 1
  405. CC = aarch64-unknown-linux-gnu-gcc
  406. cxx = aarch64-unknown-linux-gnu-g++
  407. endif
  408. endif
  409. endif
  410. ifneq ($(filter armv6%,$(UNAME_M)),)
  411. # Raspberry Pi 1, Zero
  412. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  413. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  414. endif
  415. ifneq ($(filter armv7%,$(UNAME_M)),)
  416. # Raspberry Pi 2
  417. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  418. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  419. endif
  420. ifneq ($(filter armv8%,$(UNAME_M)),)
  421. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  422. MK_CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  423. MK_CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
  424. endif
  425. ifneq ($(filter ppc64%,$(UNAME_M)),)
  426. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  427. ifneq (,$(findstring POWER9,$(POWER9_M)))
  428. MK_CFLAGS += -mcpu=power9
  429. MK_CXXFLAGS += -mcpu=power9
  430. endif
  431. endif
  432. ifneq ($(filter ppc64le%,$(UNAME_M)),)
  433. MK_CFLAGS += -mcpu=powerpc64le
  434. MK_CXXFLAGS += -mcpu=powerpc64le
  435. CUDA_POWER_ARCH = 1
  436. endif
  437. ifneq ($(filter loongarch64%,$(UNAME_M)),)
  438. MK_CFLAGS += -mlasx
  439. MK_CXXFLAGS += -mlasx
  440. endif
  441. ifneq ($(filter riscv64%,$(UNAME_M)),)
  442. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  443. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  444. endif
  445. else # RISC-V CROSS COMPILATION
  446. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  447. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  448. endif
  449. ifndef GGML_NO_ACCELERATE
  450. # Mac OS - include Accelerate framework.
  451. # `-framework Accelerate` works both with Apple Silicon and Mac Intel
  452. ifeq ($(UNAME_S),Darwin)
  453. MK_CPPFLAGS += -DGGML_USE_ACCELERATE -DGGML_USE_BLAS -DGGML_BLAS_USE_ACCELERATE
  454. MK_CPPFLAGS += -DACCELERATE_NEW_LAPACK
  455. MK_CPPFLAGS += -DACCELERATE_LAPACK_ILP64
  456. MK_LDFLAGS += -framework Accelerate
  457. OBJ_GGML_EXT += ggml/src/ggml-blas/ggml-blas.o
  458. endif
  459. endif # GGML_NO_ACCELERATE
  460. ifndef GGML_NO_OPENMP
  461. MK_CPPFLAGS += -DGGML_USE_OPENMP
  462. MK_CFLAGS += -fopenmp
  463. MK_CXXFLAGS += -fopenmp
  464. endif # GGML_NO_OPENMP
  465. ifdef GGML_OPENBLAS
  466. MK_CPPFLAGS += -DGGML_USE_BLAS $(shell pkg-config --cflags-only-I openblas)
  467. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
  468. MK_LDFLAGS += $(shell pkg-config --libs openblas)
  469. OBJ_GGML_EXT += ggml/src/ggml-blas/ggml-blas.o
  470. endif # GGML_OPENBLAS
  471. ifdef GGML_OPENBLAS64
  472. MK_CPPFLAGS += -DGGML_USE_BLAS $(shell pkg-config --cflags-only-I openblas64)
  473. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas64)
  474. MK_LDFLAGS += $(shell pkg-config --libs openblas64)
  475. OBJ_GGML_EXT += ggml/src/ggml-blas/ggml-blas.o
  476. endif # GGML_OPENBLAS64
  477. ifdef GGML_BLIS
  478. MK_CPPFLAGS += -DGGML_USE_BLAS -DGGML_BLAS_USE_BLIS -I/usr/local/include/blis -I/usr/include/blis
  479. MK_LDFLAGS += -lblis -L/usr/local/lib
  480. OBJ_GGML_EXT += ggml/src/ggml-blas/ggml-blas.o
  481. endif # GGML_BLIS
  482. ifdef GGML_NVPL
  483. MK_CPPFLAGS += -DGGML_USE_BLAS -DGGML_BLAS_USE_NVPL -DNVPL_ILP64 -I/usr/local/include/nvpl_blas -I/usr/include/nvpl_blas
  484. MK_LDFLAGS += -L/usr/local/lib -lnvpl_blas_core -lnvpl_blas_ilp64_gomp
  485. OBJ_GGML_EXT += ggml/src/ggml-blas/ggml-blas.o
  486. endif # GGML_NVPL
  487. ifndef GGML_NO_LLAMAFILE
  488. MK_CPPFLAGS += -DGGML_USE_LLAMAFILE
  489. OBJ_GGML_EXT += ggml/src/ggml-cpu/llamafile/sgemm.o
  490. endif
  491. ifndef GGML_NO_AMX
  492. MK_CPPFLAGS += -DGGML_USE_AMX
  493. OBJ_GGML_EXT += ggml/src/ggml-amx/ggml-amx.o ggml/src/ggml-amx/mmq.o
  494. endif
  495. ifdef GGML_RPC
  496. MK_CPPFLAGS += -DGGML_USE_RPC
  497. OBJ_GGML_EXT += ggml/src/ggml-rpc.o
  498. endif # GGML_RPC
  499. OBJ_CUDA_TMPL = $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-wmma*.cu))
  500. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/mmq*.cu))
  501. ifdef GGML_CUDA_FA_ALL_QUANTS
  502. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*.cu))
  503. else
  504. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*q4_0-q4_0.cu))
  505. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*q8_0-q8_0.cu))
  506. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*f16-f16.cu))
  507. endif # GGML_CUDA_FA_ALL_QUANTS
  508. ifdef GGML_CUDA
  509. ifneq ('', '$(wildcard /opt/cuda)')
  510. CUDA_PATH ?= /opt/cuda
  511. else
  512. CUDA_PATH ?= /usr/local/cuda
  513. endif
  514. MK_CPPFLAGS += -DGGML_USE_CUDA -DGGML_CUDA_USE_GRAPHS -I$(CUDA_PATH)/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include
  515. 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
  516. MK_NVCCFLAGS += -use_fast_math
  517. OBJ_GGML_EXT += ggml/src/ggml-cuda/ggml-cuda.o
  518. OBJ_GGML_EXT += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  519. OBJ_GGML_EXT += $(OBJ_CUDA_TMPL)
  520. ifdef LLAMA_FATAL_WARNINGS
  521. MK_NVCCFLAGS += -Werror all-warnings
  522. endif # LLAMA_FATAL_WARNINGS
  523. ifndef JETSON_EOL_MODULE_DETECT
  524. MK_NVCCFLAGS += --forward-unknown-to-host-compiler
  525. endif # JETSON_EOL_MODULE_DETECT
  526. ifdef LLAMA_DEBUG
  527. MK_NVCCFLAGS += -lineinfo
  528. endif # LLAMA_DEBUG
  529. ifdef GGML_CUDA_DEBUG
  530. MK_NVCCFLAGS += --device-debug
  531. endif # GGML_CUDA_DEBUG
  532. ifdef GGML_CUDA_NVCC
  533. NVCC = $(CCACHE) $(GGML_CUDA_NVCC)
  534. else
  535. NVCC = $(CCACHE) nvcc
  536. endif # GGML_CUDA_NVCC
  537. ifdef CUDA_DOCKER_ARCH
  538. MK_NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  539. else ifndef CUDA_POWER_ARCH
  540. MK_NVCCFLAGS += -arch=native
  541. endif # CUDA_DOCKER_ARCH
  542. ifdef GGML_CUDA_FORCE_MMQ
  543. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  544. endif # GGML_CUDA_FORCE_MMQ
  545. ifdef GGML_CUDA_FORCE_CUBLAS
  546. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  547. endif # GGML_CUDA_FORCE_CUBLAS
  548. ifdef GGML_CUDA_F16
  549. MK_NVCCFLAGS += -DGGML_CUDA_F16
  550. endif # GGML_CUDA_F16
  551. ifdef GGML_CUDA_DMMV_F16
  552. MK_NVCCFLAGS += -DGGML_CUDA_F16
  553. endif # GGML_CUDA_DMMV_F16
  554. ifdef GGML_CUDA_PEER_MAX_BATCH_SIZE
  555. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(GGML_CUDA_PEER_MAX_BATCH_SIZE)
  556. else
  557. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  558. endif # GGML_CUDA_PEER_MAX_BATCH_SIZE
  559. ifdef GGML_CUDA_NO_PEER_COPY
  560. MK_NVCCFLAGS += -DGGML_CUDA_NO_PEER_COPY
  561. endif # GGML_CUDA_NO_PEER_COPY
  562. ifdef GGML_CUDA_CCBIN
  563. MK_NVCCFLAGS += -ccbin $(GGML_CUDA_CCBIN)
  564. endif # GGML_CUDA_CCBIN
  565. ifdef GGML_CUDA_FA_ALL_QUANTS
  566. MK_NVCCFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  567. endif # GGML_CUDA_FA_ALL_QUANTS
  568. ifdef JETSON_EOL_MODULE_DETECT
  569. define NVCC_COMPILE
  570. $(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 $@
  571. endef # NVCC_COMPILE
  572. else
  573. define NVCC_COMPILE
  574. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  575. endef # NVCC_COMPILE
  576. endif # JETSON_EOL_MODULE_DETECT
  577. ggml/src/ggml-cuda/%.o: \
  578. ggml/src/ggml-cuda/%.cu \
  579. ggml/include/ggml.h \
  580. ggml/src/ggml-common.h \
  581. ggml/src/ggml-cuda/common.cuh
  582. $(NVCC_COMPILE)
  583. ggml/src/ggml-cuda/ggml-cuda.o: \
  584. ggml/src/ggml-cuda/ggml-cuda.cu \
  585. ggml/include/ggml-cuda.h \
  586. ggml/include/ggml.h \
  587. ggml/include/ggml-backend.h \
  588. ggml/src/ggml-backend-impl.h \
  589. ggml/src/ggml-common.h \
  590. $(wildcard ggml/src/ggml-cuda/*.cuh)
  591. $(NVCC_COMPILE)
  592. endif # GGML_CUDA
  593. ifdef GGML_VULKAN
  594. MK_CPPFLAGS += -DGGML_USE_VULKAN
  595. MK_LDFLAGS += $(shell pkg-config --libs vulkan)
  596. OBJ_GGML_EXT += ggml/src/ggml-vulkan.o ggml/src/ggml-vulkan-shaders.o
  597. ifdef GGML_VULKAN_CHECK_RESULTS
  598. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  599. endif
  600. ifdef GGML_VULKAN_DEBUG
  601. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  602. endif
  603. ifdef GGML_VULKAN_MEMORY_DEBUG
  604. MK_CPPFLAGS += -DGGML_VULKAN_MEMORY_DEBUG
  605. endif
  606. ifdef GGML_VULKAN_PERF
  607. MK_CPPFLAGS += -DGGML_VULKAN_PERF
  608. endif
  609. ifdef GGML_VULKAN_VALIDATE
  610. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  611. endif
  612. ifdef GGML_VULKAN_RUN_TESTS
  613. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  614. endif
  615. GLSLC_CMD = glslc
  616. _ggml_vk_genshaders_cmd = $(shell pwd)/vulkan-shaders-gen
  617. _ggml_vk_header = ggml/src/ggml-vulkan-shaders.hpp
  618. _ggml_vk_source = ggml/src/ggml-vulkan-shaders.cpp
  619. _ggml_vk_input_dir = ggml/src/ggml-vulkan/vulkan-shaders
  620. _ggml_vk_shader_deps = $(echo $(_ggml_vk_input_dir)/*.comp)
  621. ggml/src/ggml-vulkan.o: ggml/src/ggml-vulkan/ggml-vulkan.cpp ggml/include/ggml-vulkan.h $(_ggml_vk_header) $(_ggml_vk_source)
  622. $(CXX) $(CXXFLAGS) $(shell pkg-config --cflags vulkan) -c $< -o $@
  623. $(_ggml_vk_header): $(_ggml_vk_source)
  624. $(_ggml_vk_source): $(_ggml_vk_shader_deps) vulkan-shaders-gen
  625. $(_ggml_vk_genshaders_cmd) \
  626. --glslc $(GLSLC_CMD) \
  627. --input-dir $(_ggml_vk_input_dir) \
  628. --target-hpp $(_ggml_vk_header) \
  629. --target-cpp $(_ggml_vk_source)
  630. vulkan-shaders-gen: ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
  631. $(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
  632. endif # GGML_VULKAN
  633. ifdef GGML_HIPBLAS
  634. ifeq ($(wildcard /opt/rocm),)
  635. ROCM_PATH ?= /usr
  636. AMDGPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  637. else
  638. ROCM_PATH ?= /opt/rocm
  639. AMDGPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  640. endif
  641. MK_CPPFLAGS += -DGGML_USE_HIP -DGGML_USE_CUDA
  642. ifdef GGML_HIP_UMA
  643. MK_CPPFLAGS += -DGGML_HIP_UMA
  644. endif # GGML_HIP_UMA
  645. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  646. MK_LDFLAGS += -L$(ROCM_PATH)/lib64 -Wl,-rpath=$(ROCM_PATH)/lib64
  647. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  648. HIPCC ?= $(CCACHE) $(ROCM_PATH)/bin/hipcc
  649. HIPFLAGS += $(addprefix --offload-arch=,$(AMDGPU_TARGETS))
  650. ifdef GGML_CUDA_FORCE_MMQ
  651. HIPFLAGS += -DGGML_CUDA_FORCE_MMQ
  652. endif # GGML_CUDA_FORCE_MMQ
  653. ifdef GGML_CUDA_FORCE_CUBLAS
  654. HIPFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  655. endif # GGML_CUDA_FORCE_CUBLAS
  656. ifdef GGML_CUDA_NO_PEER_COPY
  657. HIPFLAGS += -DGGML_CUDA_NO_PEER_COPY
  658. endif # GGML_CUDA_NO_PEER_COPY
  659. OBJ_GGML_EXT += ggml/src/ggml-cuda/ggml-cuda.o
  660. OBJ_GGML_EXT += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  661. OBJ_GGML_EXT += $(OBJ_CUDA_TMPL)
  662. ggml/src/ggml-cuda/ggml-cuda.o: \
  663. ggml/src/ggml-cuda/ggml-cuda.cu \
  664. ggml/include/ggml-cuda.h \
  665. ggml/include/ggml.h \
  666. ggml/include/ggml-backend.h \
  667. ggml/src/ggml-backend-impl.h \
  668. ggml/src/ggml-common.h \
  669. $(wildcard ggml/src/ggml-cuda/*.cuh)
  670. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  671. ggml/src/ggml-cuda/%.o: \
  672. ggml/src/ggml-cuda/%.cu \
  673. ggml/include/ggml.h \
  674. ggml/src/ggml-common.h \
  675. ggml/src/ggml-cuda/common.cuh
  676. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  677. endif # GGML_HIPBLAS
  678. ifdef GGML_MUSA
  679. ifeq ($(wildcard /opt/musa),)
  680. MUSA_PATH ?= /usr/local/musa
  681. else
  682. MUSA_PATH ?= /opt/musa
  683. endif
  684. MTGPU_TARGETS ?= mp_21 mp_22
  685. MK_CPPFLAGS += -DGGML_USE_MUSA -DGGML_USE_CUDA
  686. MK_LDFLAGS += -L$(MUSA_PATH)/lib -Wl,-rpath=$(MUSA_PATH)/lib
  687. MK_LDFLAGS += -lmusa -lmusart -lmublas
  688. ifndef GGML_NO_OPENMP
  689. # For Ubuntu Focal
  690. MK_CPPFLAGS += -I/usr/lib/llvm-10/include/openmp
  691. MK_LDFLAGS += -L/usr/lib/llvm-10/lib
  692. # For Ubuntu Jammy
  693. MK_CPPFLAGS += -I/usr/lib/llvm-14/lib/clang/14.0.0/include
  694. MK_LDFLAGS += -L/usr/lib/llvm-14/lib
  695. endif # GGML_NO_OPENMP
  696. CC := $(MUSA_PATH)/bin/clang
  697. CXX := $(MUSA_PATH)/bin/clang++
  698. MCC := $(CCACHE) $(MUSA_PATH)/bin/mcc
  699. MUSAFLAGS += $(addprefix --cuda-gpu-arch=, $(MTGPU_TARGETS))
  700. ifdef GGML_CUDA_FORCE_MMQ
  701. MUSAFLAGS += -DGGML_CUDA_FORCE_MMQ
  702. endif # GGML_CUDA_FORCE_MMQ
  703. ifdef GGML_CUDA_FORCE_CUBLAS
  704. MUSAFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  705. endif # GGML_CUDA_FORCE_CUBLAS
  706. ifdef GGML_CUDA_F16
  707. MUSAFLAGS += -DGGML_CUDA_F16
  708. endif # GGML_CUDA_F16
  709. ifdef GGML_CUDA_DMMV_F16
  710. MUSAFLAGS += -DGGML_CUDA_F16
  711. endif # GGML_CUDA_DMMV_F16
  712. ifdef GGML_CUDA_PEER_MAX_BATCH_SIZE
  713. MUSAFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(GGML_CUDA_PEER_MAX_BATCH_SIZE)
  714. else
  715. MUSAFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  716. endif # GGML_CUDA_PEER_MAX_BATCH_SIZE
  717. ifdef GGML_CUDA_NO_PEER_COPY
  718. MUSAFLAGS += -DGGML_CUDA_NO_PEER_COPY
  719. endif # GGML_CUDA_NO_PEER_COPY
  720. ifdef GGML_CUDA_FA_ALL_QUANTS
  721. MUSAFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  722. endif # GGML_CUDA_FA_ALL_QUANTS
  723. OBJ_GGML_EXT += ggml/src/ggml-cuda/ggml-cuda.o
  724. OBJ_GGML_EXT += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  725. OBJ_GGML_EXT += $(OBJ_CUDA_TMPL)
  726. ggml/src/ggml-cuda/ggml-cuda.o: \
  727. ggml/src/ggml-cuda/ggml-cuda.cu \
  728. ggml/include/ggml-cuda.h \
  729. ggml/include/ggml.h \
  730. ggml/include/ggml-backend.h \
  731. ggml/src/ggml-backend-impl.h \
  732. ggml/src/ggml-common.h \
  733. $(wildcard ggml/src/ggml-cuda/*.cuh)
  734. $(MCC) $(CXXFLAGS) $(MUSAFLAGS) -x musa -mtgpu -c -o $@ $<
  735. ggml/src/ggml-cuda/%.o: \
  736. ggml/src/ggml-cuda/%.cu \
  737. ggml/include/ggml.h \
  738. ggml/src/ggml-common.h \
  739. ggml/src/ggml-cuda/common.cuh
  740. $(MCC) $(CXXFLAGS) $(MUSAFLAGS) -x musa -mtgpu -c -o $@ $<
  741. endif # GGML_MUSA
  742. ifdef GGML_METAL
  743. MK_CPPFLAGS += -DGGML_USE_METAL
  744. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  745. OBJ_GGML_EXT += ggml/src/ggml-metal/ggml-metal.o
  746. ifdef GGML_METAL_USE_BF16
  747. MK_CPPFLAGS += -DGGML_METAL_USE_BF16
  748. endif # GGML_METAL_USE_BF16
  749. ifdef GGML_METAL_NDEBUG
  750. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  751. endif
  752. ifdef GGML_METAL_EMBED_LIBRARY
  753. MK_CPPFLAGS += -DGGML_METAL_EMBED_LIBRARY
  754. OBJ_GGML_EXT += ggml/src/ggml-metal-embed.o
  755. endif
  756. endif # GGML_METAL
  757. ifdef GGML_METAL
  758. ggml/src/ggml-metal/ggml-metal.o: \
  759. ggml/src/ggml-metal/ggml-metal.m \
  760. ggml/src/ggml-metal/ggml-metal-impl.h \
  761. ggml/include/ggml-metal.h \
  762. ggml/include/ggml.h
  763. $(CC) $(CFLAGS) -c $< -o $@
  764. ifdef GGML_METAL_EMBED_LIBRARY
  765. ggml/src/ggml-metal-embed.o: \
  766. ggml/src/ggml-metal/ggml-metal.metal \
  767. ggml/src/ggml-metal/ggml-metal-impl.h \
  768. ggml/src/ggml-common.h
  769. @echo "Embedding Metal library"
  770. @sed -e '/__embed_ggml-common.h__/r ggml/src/ggml-common.h' -e '/__embed_ggml-common.h__/d' < ggml/src/ggml-metal/ggml-metal.metal > ggml/src/ggml-metal/ggml-metal-embed.metal.tmp
  771. @sed -e '/#include "ggml-metal-impl.h"/r ggml/src/ggml-metal/ggml-metal-impl.h' -e '/#include "ggml-metal-impl.h"/d' < ggml/src/ggml-metal/ggml-metal-embed.metal.tmp > ggml/src/ggml-metal/ggml-metal-embed.metal
  772. $(eval TEMP_ASSEMBLY=$(shell mktemp -d))
  773. @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  774. @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  775. @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  776. @echo ".incbin \"ggml/src/ggml-metal/ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  777. @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  778. @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  779. $(CC) $(CFLAGS) -c $(TEMP_ASSEMBLY)/ggml-metal-embed.s -o $@
  780. @rm -f ${TEMP_ASSEMBLY}/ggml-metal-embed.s
  781. @rmdir ${TEMP_ASSEMBLY}
  782. endif
  783. endif # GGML_METAL
  784. DIR_GGML = ggml
  785. DIR_LLAMA = src
  786. DIR_COMMON = common
  787. OBJ_GGML = \
  788. $(DIR_GGML)/src/ggml.o \
  789. $(DIR_GGML)/src/ggml-aarch64.o \
  790. $(DIR_GGML)/src/ggml-alloc.o \
  791. $(DIR_GGML)/src/ggml-backend.o \
  792. $(DIR_GGML)/src/ggml-backend-reg.o \
  793. $(DIR_GGML)/src/ggml-opt.o \
  794. $(DIR_GGML)/src/ggml-quants.o \
  795. $(DIR_GGML)/src/ggml-threading.o \
  796. $(DIR_GGML)/src/ggml-cpu/ggml-cpu.o \
  797. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-cpp.o \
  798. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-aarch64.o \
  799. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-quants.o \
  800. $(OBJ_GGML_EXT)
  801. OBJ_LLAMA = \
  802. $(DIR_LLAMA)/llama.o \
  803. $(DIR_LLAMA)/llama-vocab.o \
  804. $(DIR_LLAMA)/llama-grammar.o \
  805. $(DIR_LLAMA)/llama-sampling.o \
  806. $(DIR_LLAMA)/unicode.o \
  807. $(DIR_LLAMA)/unicode-data.o
  808. OBJ_COMMON = \
  809. $(DIR_COMMON)/common.o \
  810. $(DIR_COMMON)/arg.o \
  811. $(DIR_COMMON)/log.o \
  812. $(DIR_COMMON)/console.o \
  813. $(DIR_COMMON)/ngram-cache.o \
  814. $(DIR_COMMON)/sampling.o \
  815. $(DIR_COMMON)/speculative.o \
  816. $(DIR_COMMON)/build-info.o \
  817. $(DIR_COMMON)/json-schema-to-grammar.o
  818. OBJ_ALL = $(OBJ_GGML) $(OBJ_LLAMA) $(OBJ_COMMON)
  819. LIB_GGML = $(LIB_PRE)ggml$(DSO_EXT)
  820. LIB_GGML_S = $(LIB_PRE)ggml.a
  821. LIB_LLAMA = $(LIB_PRE)llama$(DSO_EXT)
  822. LIB_LLAMA_S = $(LIB_PRE)llama.a
  823. LIB_COMMON = $(LIB_PRE)common$(DSO_EXT)
  824. LIB_COMMON_S = $(LIB_PRE)common.a
  825. LIB_ALL = $(LIB_GGML) $(LIB_LLAMA) $(LIB_COMMON)
  826. LIB_ALL_S = $(LIB_GGML_S) $(LIB_LLAMA_S) $(LIB_COMMON_S)
  827. GF_CC := $(CC)
  828. include scripts/get-flags.mk
  829. # combine build flags with cmdline overrides
  830. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  831. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  832. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  833. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  834. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  835. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  836. # identify CUDA host compiler
  837. ifdef GGML_CUDA
  838. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  839. include scripts/get-flags.mk
  840. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  841. endif
  842. ifdef LLAMA_CURL
  843. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  844. override LDFLAGS := $(LDFLAGS) -lcurl
  845. endif
  846. #
  847. # Print build information
  848. #
  849. $(info I llama.cpp build info: )
  850. $(info I UNAME_S: $(UNAME_S))
  851. $(info I UNAME_P: $(UNAME_P))
  852. $(info I UNAME_M: $(UNAME_M))
  853. $(info I CFLAGS: $(CFLAGS))
  854. $(info I CXXFLAGS: $(CXXFLAGS))
  855. $(info I NVCCFLAGS: $(NVCCFLAGS))
  856. $(info I LDFLAGS: $(LDFLAGS))
  857. $(info I CC: $(shell $(CC) --version | head -n 1))
  858. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  859. ifdef GGML_CUDA
  860. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  861. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  862. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  863. ifndef CUDA_DOCKER_ARCH
  864. ifndef CUDA_POWER_ARCH
  865. $(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 )
  866. endif # CUDA_POWER_ARCH
  867. endif # CUDA_DOCKER_ARCH
  868. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  869. endif # GGML_CUDA
  870. $(info )
  871. ifdef DEPRECATE_WARNING
  872. $(info !!! DEPRECATION WARNING !!!)
  873. $(info The following LLAMA_ options are deprecated and will be removed in the future. Use the GGML_ prefix instead)
  874. $(info - LLAMA_CUDA)
  875. $(info - LLAMA_METAL)
  876. $(info - LLAMA_METAL_EMBED_LIBRARY)
  877. $(info - LLAMA_OPENMP)
  878. $(info - LLAMA_RPC)
  879. $(info - LLAMA_SYCL)
  880. $(info - LLAMA_SYCL_F16)
  881. $(info - LLAMA_OPENBLAS)
  882. $(info - LLAMA_OPENBLAS64)
  883. $(info - LLAMA_BLIS)
  884. $(info - LLAMA_NO_LLAMAFILE)
  885. $(info - LLAMA_NO_ACCELERATE)
  886. $(info - LLAMA_NO_OPENMP)
  887. $(info - LLAMA_NO_METAL)
  888. $(info - LLAMA_NO_CCACHE)
  889. $(info )
  890. endif
  891. ifdef REMOVE_WARNING
  892. $(info !!! REMOVAL WARNING !!!)
  893. $(info The following LLAMA_ options have been removed and are no longer supported)
  894. $(info - LLAMA_DISABLE_LOGS (https://github.com/ggerganov/llama.cpp/pull/9418))
  895. $(info - LLAMA_SERVER_VERBOSE (https://github.com/ggerganov/llama.cpp/pull/9418))
  896. $(info )
  897. endif
  898. #
  899. # Build libraries
  900. #
  901. # Libraries
  902. LIB_GGML = libggml.so
  903. LIB_GGML_S = libggml.a
  904. LIB_LLAMA = libllama.so
  905. LIB_LLAMA_S = libllama.a
  906. LIB_COMMON = libcommon.so
  907. LIB_COMMON_S = libcommon.a
  908. # Targets
  909. BUILD_TARGETS += $(LIB_GGML) $(LIB_GGML_S) $(LIB_LLAMA) $(LIB_LLAMA_S) $(LIB_COMMON) $(LIB_COMMON_S)
  910. # Dependency files
  911. DEP_FILES = $(OBJ_GGML:.o=.d) $(OBJ_LLAMA:.o=.d) $(OBJ_COMMON:.o=.d)
  912. # Default target
  913. all: $(BUILD_TARGETS)
  914. # Note: need this exception because `ggml-cpu.c` and `ggml-cpu.cpp` both produce the same obj/dep files
  915. # g++ -M -I ./ggml/include/ -I ./ggml/src ggml/src/ggml-cpu/ggml-cpu.cpp | grep ggml
  916. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-cpp.o: \
  917. ggml/src/ggml-cpu/ggml-cpu.cpp \
  918. ggml/include/ggml-backend.h \
  919. ggml/include/ggml.h \
  920. ggml/include/ggml-alloc.h \
  921. ggml/src/ggml-backend-impl.h \
  922. ggml/include/ggml-cpu.h \
  923. ggml/src/ggml-impl.h
  924. $(CXX) $(CXXFLAGS) -c $< -o $@
  925. # Rules for building object files
  926. $(DIR_GGML)/%.o: $(DIR_GGML)/%.c
  927. $(CC) $(CFLAGS) -MMD -c $< -o $@
  928. $(DIR_GGML)/%.o: $(DIR_GGML)/%.cpp
  929. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  930. $(DIR_LLAMA)/%.o: $(DIR_LLAMA)/%.cpp
  931. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  932. $(DIR_COMMON)/%.o: $(DIR_COMMON)/%.cpp
  933. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  934. # Rules for building libraries
  935. $(LIB_GGML): $(OBJ_GGML)
  936. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  937. $(LIB_GGML_S): $(OBJ_GGML)
  938. ar rcs $(LIB_GGML_S) $^
  939. $(LIB_LLAMA): $(OBJ_LLAMA) $(LIB_GGML)
  940. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  941. $(LIB_LLAMA_S): $(OBJ_LLAMA)
  942. ar rcs $(LIB_LLAMA_S) $^
  943. $(LIB_COMMON): $(OBJ_COMMON) $(LIB_LLAMA) $(LIB_GGML)
  944. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  945. $(LIB_COMMON_S): $(OBJ_COMMON)
  946. ar rcs $(LIB_COMMON_S) $^
  947. # Include dependency files
  948. -include $(DEP_FILES)
  949. # Clean rule
  950. clean:
  951. rm -vrf $(BUILD_TARGETS) $(TEST_TARGETS)
  952. rm -rvf *.a *.dll *.so *.dot
  953. find ggml src common tests examples pocs -type f -name "*.o" -delete
  954. find ggml src common tests examples pocs -type f -name "*.d" -delete
  955. #
  956. # Examples
  957. #
  958. # $< is the first prerequisite, i.e. the source file.
  959. # Explicitly compile this to an object file so that it can be cached with ccache.
  960. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  961. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  962. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  963. llama-cli: examples/main/main.cpp \
  964. $(OBJ_ALL)
  965. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  966. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  967. @echo
  968. @echo '==== Run ./llama-cli -h for help. ===='
  969. @echo
  970. llama-infill: examples/infill/infill.cpp \
  971. $(OBJ_ALL)
  972. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  973. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  974. llama-run: examples/run/run.cpp \
  975. $(OBJ_ALL)
  976. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  977. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  978. llama-simple: examples/simple/simple.cpp \
  979. $(OBJ_ALL)
  980. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  981. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  982. llama-simple-chat: examples/simple-chat/simple-chat.cpp \
  983. $(OBJ_ALL)
  984. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  985. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  986. llama-tokenize: examples/tokenize/tokenize.cpp \
  987. $(OBJ_ALL)
  988. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  989. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  990. llama-batched: examples/batched/batched.cpp \
  991. $(OBJ_ALL)
  992. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  993. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  994. llama-batched-bench: examples/batched-bench/batched-bench.cpp \
  995. $(OBJ_ALL)
  996. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  997. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  998. llama-quantize: examples/quantize/quantize.cpp \
  999. $(OBJ_ALL)
  1000. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1001. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1002. llama-quantize-stats: examples/quantize-stats/quantize-stats.cpp \
  1003. $(OBJ_ALL)
  1004. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1005. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1006. llama-perplexity: examples/perplexity/perplexity.cpp \
  1007. $(OBJ_ALL)
  1008. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1009. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1010. llama-imatrix: examples/imatrix/imatrix.cpp \
  1011. $(OBJ_ALL)
  1012. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1013. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1014. llama-embedding: examples/embedding/embedding.cpp \
  1015. $(OBJ_ALL)
  1016. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1017. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1018. llama-gritlm: examples/gritlm/gritlm.cpp \
  1019. $(OBJ_ALL)
  1020. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1021. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1022. llama-save-load-state: examples/save-load-state/save-load-state.cpp \
  1023. $(OBJ_ALL)
  1024. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1025. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1026. llama-gguf: examples/gguf/gguf.cpp \
  1027. $(OBJ_GGML)
  1028. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1029. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1030. examples/gguf-hash/deps/sha1/sha1.o: \
  1031. examples/gguf-hash/deps/sha1/sha1.c
  1032. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1033. examples/gguf-hash/deps/xxhash/xxhash.o: \
  1034. examples/gguf-hash/deps/xxhash/xxhash.c
  1035. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1036. examples/gguf-hash/deps/sha256/sha256.o: \
  1037. examples/gguf-hash/deps/sha256/sha256.c
  1038. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1039. llama-gguf-hash: examples/gguf-hash/gguf-hash.cpp examples/gguf-hash/deps/sha1/sha1.o examples/gguf-hash/deps/xxhash/xxhash.o examples/gguf-hash/deps/sha256/sha256.o\
  1040. $(OBJ_ALL)
  1041. $(CXX) $(CXXFLAGS) -Iexamples/gguf-hash/deps -c $< -o $(call GET_OBJ_FILE, $<)
  1042. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1043. llama-gguf-split: examples/gguf-split/gguf-split.cpp \
  1044. $(OBJ_ALL)
  1045. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1046. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1047. llama-eval-callback: examples/eval-callback/eval-callback.cpp \
  1048. $(OBJ_ALL)
  1049. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1050. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1051. llama-cvector-generator: examples/cvector-generator/cvector-generator.cpp \
  1052. $(OBJ_ALL)
  1053. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1054. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1055. llama-convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp \
  1056. $(OBJ_ALL)
  1057. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1058. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1059. llama-bench: examples/llama-bench/llama-bench.cpp \
  1060. $(OBJ_ALL)
  1061. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1062. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1063. llama-export-lora: examples/export-lora/export-lora.cpp \
  1064. $(OBJ_ALL)
  1065. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1066. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1067. llama-retrieval: examples/retrieval/retrieval.cpp \
  1068. $(OBJ_ALL)
  1069. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1070. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1071. llama-speculative: examples/speculative/speculative.cpp \
  1072. $(OBJ_ALL)
  1073. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1074. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1075. llama-parallel: examples/parallel/parallel.cpp \
  1076. $(OBJ_ALL)
  1077. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1078. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1079. llama-lookahead: examples/lookahead/lookahead.cpp \
  1080. $(OBJ_ALL)
  1081. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1082. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1083. llama-lookup: examples/lookup/lookup.cpp \
  1084. $(OBJ_ALL)
  1085. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1086. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1087. llama-lookup-create: examples/lookup/lookup-create.cpp \
  1088. $(OBJ_ALL)
  1089. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1090. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1091. llama-lookup-merge: examples/lookup/lookup-merge.cpp \
  1092. $(OBJ_ALL)
  1093. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1094. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1095. llama-lookup-stats: examples/lookup/lookup-stats.cpp \
  1096. $(OBJ_ALL)
  1097. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1098. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1099. llama-passkey: examples/passkey/passkey.cpp \
  1100. $(OBJ_ALL)
  1101. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1102. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1103. llama-gbnf-validator: examples/gbnf-validator/gbnf-validator.cpp \
  1104. $(OBJ_ALL)
  1105. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1106. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1107. ifdef GGML_RPC
  1108. rpc-server: examples/rpc/rpc-server.cpp \
  1109. $(OBJ_GGML)
  1110. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  1111. endif # GGML_RPC
  1112. llama-server: \
  1113. examples/server/server.cpp \
  1114. examples/server/utils.hpp \
  1115. examples/server/httplib.h \
  1116. examples/server/index.html.hpp \
  1117. examples/server/completion.js.hpp \
  1118. examples/server/loading.html.hpp \
  1119. examples/server/deps_daisyui.min.css.hpp \
  1120. examples/server/deps_markdown-it.js.hpp \
  1121. examples/server/deps_tailwindcss.js.hpp \
  1122. examples/server/deps_vue.esm-browser.js.hpp \
  1123. common/json.hpp \
  1124. common/stb_image.h \
  1125. $(OBJ_ALL)
  1126. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1127. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  1128. # Portable equivalent of `cd examples/server/public && xxd -i $(notdir $<) ../$(notdir $<).hpp`:
  1129. examples/server/%.hpp: examples/server/public/% Makefile
  1130. @( export NAME=$(subst .,_,$(subst -,_,$(notdir $<))) && \
  1131. echo "unsigned char $${NAME}[] = {" && \
  1132. cat $< | od -v -t x1 -An | sed -E 's/([0-9a-fA-F]+)/0x\1, /g' && \
  1133. echo "};" && \
  1134. echo "unsigned int $${NAME}_len = $(shell cat $< | wc -c );" \
  1135. ) > $@
  1136. llama-gen-docs: examples/gen-docs/gen-docs.cpp \
  1137. $(OBJ_ALL)
  1138. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1139. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1140. libllava.a: examples/llava/llava.cpp \
  1141. examples/llava/llava.h \
  1142. examples/llava/clip.cpp \
  1143. examples/llava/clip.h \
  1144. common/stb_image.h \
  1145. common/base64.hpp \
  1146. $(OBJ_ALL)
  1147. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  1148. llama-llava-cli: examples/llava/llava-cli.cpp \
  1149. examples/llava/llava.cpp \
  1150. examples/llava/llava.h \
  1151. examples/llava/clip.cpp \
  1152. examples/llava/clip.h \
  1153. $(OBJ_ALL)
  1154. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1155. llama-minicpmv-cli: examples/llava/minicpmv-cli.cpp \
  1156. examples/llava/llava.cpp \
  1157. examples/llava/llava.h \
  1158. examples/llava/clip.cpp \
  1159. examples/llava/clip.h \
  1160. $(OBJ_ALL)
  1161. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1162. ifeq ($(UNAME_S),Darwin)
  1163. swift: examples/batched.swift
  1164. (cd examples/batched.swift; make build)
  1165. endif
  1166. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  1167. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  1168. @if ! cmp -s $@.tmp $@; then \
  1169. mv $@.tmp $@; \
  1170. else \
  1171. rm $@.tmp; \
  1172. fi
  1173. common/build-info.o: common/build-info.cpp
  1174. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  1175. #
  1176. # Tests
  1177. #
  1178. tests: $(TEST_TARGETS)
  1179. tests/test-arg-parser: tests/test-arg-parser.cpp \
  1180. $(OBJ_ALL)
  1181. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1182. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1183. tests/test-llama-grammar: tests/test-llama-grammar.cpp \
  1184. $(OBJ_ALL)
  1185. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1186. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1187. tests/test-log: tests/test-log.cpp \
  1188. $(OBJ_ALL)
  1189. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1190. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1191. tests/test-grammar-parser: tests/test-grammar-parser.cpp \
  1192. $(OBJ_ALL)
  1193. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1194. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1195. tests/test-grammar-integration: tests/test-grammar-integration.cpp \
  1196. $(OBJ_ALL)
  1197. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1198. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1199. tests/test-double-float: tests/test-double-float.cpp
  1200. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1201. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1202. tests/test-json-schema-to-grammar: tests/test-json-schema-to-grammar.cpp \
  1203. $(OBJ_ALL)
  1204. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  1205. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1206. tests/test-opt: tests/test-opt.cpp \
  1207. $(OBJ_GGML)
  1208. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1209. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1210. tests/test-quantize-fns: tests/test-quantize-fns.cpp \
  1211. $(OBJ_GGML)
  1212. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1213. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1214. tests/test-quantize-perf: tests/test-quantize-perf.cpp \
  1215. $(OBJ_GGML)
  1216. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1217. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1218. tests/test-sampling: tests/test-sampling.cpp \
  1219. $(OBJ_ALL)
  1220. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1221. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1222. tests/test-tokenizer-0: tests/test-tokenizer-0.cpp \
  1223. $(OBJ_ALL)
  1224. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1225. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1226. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp \
  1227. $(OBJ_ALL)
  1228. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1229. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1230. tests/test-tokenizer-1-spm: tests/test-tokenizer-1-spm.cpp \
  1231. $(OBJ_ALL)
  1232. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1233. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1234. tests/test-rope: tests/test-rope.cpp ggml/src/ggml.o \
  1235. $(OBJ_GGML)
  1236. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1237. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1238. tests/test-c.o: tests/test-c.c include/llama.h
  1239. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  1240. tests/test-backend-ops: tests/test-backend-ops.cpp \
  1241. $(OBJ_GGML)
  1242. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1243. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1244. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp tests/get-model.cpp \
  1245. $(OBJ_ALL)
  1246. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1247. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1248. tests/test-autorelease: tests/test-autorelease.cpp tests/get-model.cpp \
  1249. $(OBJ_ALL)
  1250. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1251. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1252. tests/test-chat-template: tests/test-chat-template.cpp \
  1253. $(OBJ_ALL)
  1254. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1255. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1256. #
  1257. # PoCs
  1258. #
  1259. llama-vdot: pocs/vdot/vdot.cpp ggml/src/ggml.o \
  1260. $(OBJ_GGML)
  1261. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1262. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1263. llama-q8dot: pocs/vdot/q8dot.cpp ggml/src/ggml.o \
  1264. $(OBJ_GGML)
  1265. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1266. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1267. #
  1268. # Deprecated binaries that we want to keep around long enough for people to migrate to the new filenames, then these can be removed.
  1269. #
  1270. # Mark legacy binary targets as .PHONY so that they are always checked.
  1271. .PHONY: main quantize perplexity embedding server
  1272. # Define the object file target
  1273. examples/deprecation-warning/deprecation-warning.o: examples/deprecation-warning/deprecation-warning.cpp
  1274. $(CXX) $(CXXFLAGS) -c $< -o $@
  1275. # NOTE: We currently will always build the deprecation-warning `main` and `server` binaries to help users migrate.
  1276. # Eventually we will want to remove these target from building all the time.
  1277. main: examples/deprecation-warning/deprecation-warning.o
  1278. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1279. @echo "NOTICE: The 'main' binary is deprecated. Please use 'llama-cli' instead."
  1280. server: examples/deprecation-warning/deprecation-warning.o
  1281. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1282. @echo "NOTICE: The 'server' binary is deprecated. Please use 'llama-server' instead."
  1283. quantize: examples/deprecation-warning/deprecation-warning.o
  1284. ifneq (,$(wildcard quantize))
  1285. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1286. @echo "#########"
  1287. @echo "WARNING: The 'quantize' binary is deprecated. Please use 'llama-quantize' instead."
  1288. @echo " Remove the 'quantize' binary to remove this warning."
  1289. @echo "#########"
  1290. endif
  1291. perplexity: examples/deprecation-warning/deprecation-warning.o
  1292. ifneq (,$(wildcard perplexity))
  1293. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1294. @echo "#########"
  1295. @echo "WARNING: The 'perplexity' binary is deprecated. Please use 'llama-perplexity' instead."
  1296. @echo " Remove the 'perplexity' binary to remove this warning."
  1297. @echo "#########"
  1298. endif
  1299. embedding: examples/deprecation-warning/deprecation-warning.o
  1300. ifneq (,$(wildcard embedding))
  1301. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1302. @echo "#########"
  1303. @echo "WARNING: The 'embedding' binary is deprecated. Please use 'llama-embedding' instead."
  1304. @echo " Remove the 'embedding' binary to remove this warning."
  1305. @echo "#########"
  1306. endif