1
0

Makefile 49 KB

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