Makefile 49 KB

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