Makefile 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  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_AARCH64
  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_FA_ALL_QUANTS
  576. MK_NVCCFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  577. endif # GGML_CUDA_FA_ALL_QUANTS
  578. ifdef JETSON_EOL_MODULE_DETECT
  579. define NVCC_COMPILE
  580. $(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 $@
  581. endef # NVCC_COMPILE
  582. else
  583. define NVCC_COMPILE
  584. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  585. endef # NVCC_COMPILE
  586. endif # JETSON_EOL_MODULE_DETECT
  587. ggml/src/ggml-cuda/%.o: \
  588. ggml/src/ggml-cuda/%.cu \
  589. ggml/include/ggml.h \
  590. ggml/src/ggml-common.h \
  591. ggml/src/ggml-cuda/common.cuh
  592. $(NVCC_COMPILE)
  593. ggml/src/ggml-cuda/ggml-cuda.o: \
  594. ggml/src/ggml-cuda/ggml-cuda.cu \
  595. ggml/include/ggml-cuda.h \
  596. ggml/include/ggml.h \
  597. ggml/include/ggml-backend.h \
  598. ggml/src/ggml-backend-impl.h \
  599. ggml/src/ggml-common.h \
  600. $(wildcard ggml/src/ggml-cuda/*.cuh)
  601. $(NVCC_COMPILE)
  602. endif # GGML_CUDA
  603. ifdef GGML_VULKAN
  604. MK_CPPFLAGS += -DGGML_USE_VULKAN
  605. MK_LDFLAGS += $(shell pkg-config --libs vulkan)
  606. OBJ_GGML_EXT += ggml/src/ggml-vulkan.o ggml/src/ggml-vulkan-shaders.o
  607. ifdef GGML_VULKAN_CHECK_RESULTS
  608. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  609. endif
  610. ifdef GGML_VULKAN_DEBUG
  611. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  612. endif
  613. ifdef GGML_VULKAN_MEMORY_DEBUG
  614. MK_CPPFLAGS += -DGGML_VULKAN_MEMORY_DEBUG
  615. endif
  616. ifdef GGML_VULKAN_PERF
  617. MK_CPPFLAGS += -DGGML_VULKAN_PERF
  618. endif
  619. ifdef GGML_VULKAN_VALIDATE
  620. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  621. endif
  622. ifdef GGML_VULKAN_RUN_TESTS
  623. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  624. endif
  625. GLSLC_CMD = glslc
  626. _ggml_vk_genshaders_cmd = $(shell pwd)/vulkan-shaders-gen
  627. _ggml_vk_header = ggml/src/ggml-vulkan-shaders.hpp
  628. _ggml_vk_source = ggml/src/ggml-vulkan-shaders.cpp
  629. _ggml_vk_input_dir = ggml/src/ggml-vulkan/vulkan-shaders
  630. _ggml_vk_shader_deps = $(echo $(_ggml_vk_input_dir)/*.comp)
  631. ggml/src/ggml-vulkan.o: ggml/src/ggml-vulkan/ggml-vulkan.cpp ggml/include/ggml-vulkan.h $(_ggml_vk_header) $(_ggml_vk_source)
  632. $(CXX) $(CXXFLAGS) $(shell pkg-config --cflags vulkan) -c $< -o $@
  633. $(_ggml_vk_header): $(_ggml_vk_source)
  634. $(_ggml_vk_source): $(_ggml_vk_shader_deps) vulkan-shaders-gen
  635. $(_ggml_vk_genshaders_cmd) \
  636. --glslc $(GLSLC_CMD) \
  637. --input-dir $(_ggml_vk_input_dir) \
  638. --target-hpp $(_ggml_vk_header) \
  639. --target-cpp $(_ggml_vk_source)
  640. vulkan-shaders-gen: ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
  641. $(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp
  642. endif # GGML_VULKAN
  643. ifdef GGML_HIP
  644. ifeq ($(wildcard /opt/rocm),)
  645. ROCM_PATH ?= /usr
  646. AMDGPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  647. else
  648. ROCM_PATH ?= /opt/rocm
  649. AMDGPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  650. endif
  651. MK_CPPFLAGS += -DGGML_USE_HIP -DGGML_USE_CUDA
  652. ifdef GGML_HIP_UMA
  653. MK_CPPFLAGS += -DGGML_HIP_UMA
  654. endif # GGML_HIP_UMA
  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. OBJ_GGML_EXT += ggml/src/ggml-cuda/ggml-cuda.o
  670. OBJ_GGML_EXT += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  671. OBJ_GGML_EXT += $(OBJ_CUDA_TMPL)
  672. ggml/src/ggml-cuda/ggml-cuda.o: \
  673. ggml/src/ggml-cuda/ggml-cuda.cu \
  674. ggml/include/ggml-cuda.h \
  675. ggml/include/ggml.h \
  676. ggml/include/ggml-backend.h \
  677. ggml/src/ggml-backend-impl.h \
  678. ggml/src/ggml-common.h \
  679. $(wildcard ggml/src/ggml-cuda/*.cuh)
  680. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  681. ggml/src/ggml-cuda/%.o: \
  682. ggml/src/ggml-cuda/%.cu \
  683. ggml/include/ggml.h \
  684. ggml/src/ggml-common.h \
  685. ggml/src/ggml-cuda/common.cuh
  686. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  687. endif # GGML_HIP
  688. ifdef GGML_MUSA
  689. ifeq ($(wildcard /opt/musa),)
  690. MUSA_PATH ?= /usr/local/musa
  691. else
  692. MUSA_PATH ?= /opt/musa
  693. endif
  694. MUSA_ARCHITECTURES ?= 21;22
  695. MK_CPPFLAGS += -DGGML_USE_MUSA -DGGML_USE_CUDA
  696. MK_LDFLAGS += -L$(MUSA_PATH)/lib -Wl,-rpath=$(MUSA_PATH)/lib
  697. MK_LDFLAGS += -lmusa -lmusart -lmublas
  698. ifndef GGML_NO_OPENMP
  699. # For Ubuntu Focal
  700. MK_CPPFLAGS += -I/usr/lib/llvm-10/include/openmp
  701. MK_LDFLAGS += -L/usr/lib/llvm-10/lib
  702. # For Ubuntu Jammy
  703. MK_CPPFLAGS += -I/usr/lib/llvm-14/lib/clang/14.0.0/include
  704. MK_LDFLAGS += -L/usr/lib/llvm-14/lib
  705. endif # GGML_NO_OPENMP
  706. CC := $(MUSA_PATH)/bin/clang
  707. CXX := $(MUSA_PATH)/bin/clang++
  708. MCC := $(CCACHE) $(MUSA_PATH)/bin/mcc
  709. MUSAFLAGS = -x musa -mtgpu
  710. MUSAFLAGS += $(foreach arch,$(subst ;, ,$(MUSA_ARCHITECTURES)),--cuda-gpu-arch=mp_$(arch))
  711. ifdef GGML_CUDA_FORCE_MMQ
  712. MUSAFLAGS += -DGGML_CUDA_FORCE_MMQ
  713. endif # GGML_CUDA_FORCE_MMQ
  714. ifdef GGML_CUDA_FORCE_CUBLAS
  715. MUSAFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  716. endif # GGML_CUDA_FORCE_CUBLAS
  717. ifdef GGML_CUDA_F16
  718. MUSAFLAGS += -DGGML_CUDA_F16
  719. endif # GGML_CUDA_F16
  720. ifdef GGML_CUDA_DMMV_F16
  721. MUSAFLAGS += -DGGML_CUDA_F16
  722. endif # GGML_CUDA_DMMV_F16
  723. ifdef GGML_CUDA_PEER_MAX_BATCH_SIZE
  724. MUSAFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(GGML_CUDA_PEER_MAX_BATCH_SIZE)
  725. else
  726. MUSAFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  727. endif # GGML_CUDA_PEER_MAX_BATCH_SIZE
  728. ifdef GGML_CUDA_NO_PEER_COPY
  729. MUSAFLAGS += -DGGML_CUDA_NO_PEER_COPY
  730. endif # GGML_CUDA_NO_PEER_COPY
  731. ifdef GGML_CUDA_FA_ALL_QUANTS
  732. MUSAFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  733. endif # GGML_CUDA_FA_ALL_QUANTS
  734. OBJ_GGML_EXT += ggml/src/ggml-cuda/ggml-cuda.o
  735. OBJ_GGML_EXT += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  736. OBJ_GGML_EXT += $(OBJ_CUDA_TMPL)
  737. ggml/src/ggml-cuda/ggml-cuda.o: \
  738. ggml/src/ggml-cuda/ggml-cuda.cu \
  739. ggml/include/ggml-cuda.h \
  740. ggml/include/ggml.h \
  741. ggml/include/ggml-backend.h \
  742. ggml/src/ggml-backend-impl.h \
  743. ggml/src/ggml-common.h \
  744. $(wildcard ggml/src/ggml-cuda/*.cuh)
  745. $(MCC) $(CXXFLAGS) $(MUSAFLAGS) -c -o $@ $<
  746. ggml/src/ggml-cuda/%.o: \
  747. ggml/src/ggml-cuda/%.cu \
  748. ggml/include/ggml.h \
  749. ggml/src/ggml-common.h \
  750. ggml/src/ggml-cuda/common.cuh
  751. $(MCC) $(CXXFLAGS) $(MUSAFLAGS) -c -o $@ $<
  752. endif # GGML_MUSA
  753. ifdef GGML_METAL
  754. MK_CPPFLAGS += -DGGML_USE_METAL
  755. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  756. OBJ_GGML_EXT += ggml/src/ggml-metal/ggml-metal.o
  757. ifdef GGML_METAL_USE_BF16
  758. MK_CPPFLAGS += -DGGML_METAL_USE_BF16
  759. endif # GGML_METAL_USE_BF16
  760. ifdef GGML_METAL_NDEBUG
  761. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  762. endif
  763. ifdef GGML_METAL_EMBED_LIBRARY
  764. MK_CPPFLAGS += -DGGML_METAL_EMBED_LIBRARY
  765. OBJ_GGML_EXT += ggml/src/ggml-metal-embed.o
  766. endif
  767. endif # GGML_METAL
  768. ifdef GGML_METAL
  769. ggml/src/ggml-metal/ggml-metal.o: \
  770. ggml/src/ggml-metal/ggml-metal.m \
  771. ggml/src/ggml-metal/ggml-metal-impl.h \
  772. ggml/include/ggml-metal.h \
  773. ggml/include/ggml.h
  774. $(CC) $(CFLAGS) -c $< -o $@
  775. ifdef GGML_METAL_EMBED_LIBRARY
  776. ggml/src/ggml-metal-embed.o: \
  777. ggml/src/ggml-metal/ggml-metal.metal \
  778. ggml/src/ggml-metal/ggml-metal-impl.h \
  779. ggml/src/ggml-common.h
  780. @echo "Embedding Metal library"
  781. @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
  782. @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
  783. $(eval TEMP_ASSEMBLY=$(shell mktemp -d))
  784. @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  785. @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  786. @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  787. @echo ".incbin \"ggml/src/ggml-metal/ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  788. @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  789. @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  790. $(CC) $(CFLAGS) -c $(TEMP_ASSEMBLY)/ggml-metal-embed.s -o $@
  791. @rm -f ${TEMP_ASSEMBLY}/ggml-metal-embed.s
  792. @rmdir ${TEMP_ASSEMBLY}
  793. endif
  794. endif # GGML_METAL
  795. DIR_GGML = ggml
  796. DIR_LLAMA = src
  797. DIR_COMMON = common
  798. OBJ_GGML = \
  799. $(DIR_GGML)/src/ggml.o \
  800. $(DIR_GGML)/src/ggml-alloc.o \
  801. $(DIR_GGML)/src/ggml-backend.o \
  802. $(DIR_GGML)/src/ggml-backend-reg.o \
  803. $(DIR_GGML)/src/ggml-opt.o \
  804. $(DIR_GGML)/src/ggml-quants.o \
  805. $(DIR_GGML)/src/ggml-threading.o \
  806. $(DIR_GGML)/src/ggml-cpu/ggml-cpu.o \
  807. $(DIR_GGML)/src/ggml-cpu/ggml-cpu_cpp.o \
  808. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-aarch64.o \
  809. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-hbm.o \
  810. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-quants.o \
  811. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-traits.o \
  812. $(OBJ_GGML_EXT)
  813. OBJ_LLAMA = \
  814. $(DIR_LLAMA)/llama.o \
  815. $(DIR_LLAMA)/llama-vocab.o \
  816. $(DIR_LLAMA)/llama-grammar.o \
  817. $(DIR_LLAMA)/llama-sampling.o \
  818. $(DIR_LLAMA)/unicode.o \
  819. $(DIR_LLAMA)/unicode-data.o
  820. OBJ_COMMON = \
  821. $(DIR_COMMON)/common.o \
  822. $(DIR_COMMON)/arg.o \
  823. $(DIR_COMMON)/log.o \
  824. $(DIR_COMMON)/console.o \
  825. $(DIR_COMMON)/ngram-cache.o \
  826. $(DIR_COMMON)/sampling.o \
  827. $(DIR_COMMON)/speculative.o \
  828. $(DIR_COMMON)/chat.o \
  829. $(DIR_COMMON)/build-info.o \
  830. $(DIR_COMMON)/json-schema-to-grammar.o
  831. OBJ_ALL = $(OBJ_GGML) $(OBJ_LLAMA) $(OBJ_COMMON)
  832. LIB_GGML = $(LIB_PRE)ggml$(DSO_EXT)
  833. LIB_GGML_S = $(LIB_PRE)ggml.a
  834. LIB_LLAMA = $(LIB_PRE)llama$(DSO_EXT)
  835. LIB_LLAMA_S = $(LIB_PRE)llama.a
  836. LIB_COMMON = $(LIB_PRE)common$(DSO_EXT)
  837. LIB_COMMON_S = $(LIB_PRE)common.a
  838. LIB_ALL = $(LIB_GGML) $(LIB_LLAMA) $(LIB_COMMON)
  839. LIB_ALL_S = $(LIB_GGML_S) $(LIB_LLAMA_S) $(LIB_COMMON_S)
  840. GF_CC := $(CC)
  841. include scripts/get-flags.mk
  842. # combine build flags with cmdline overrides
  843. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  844. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  845. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  846. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  847. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  848. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  849. # identify CUDA host compiler
  850. ifdef GGML_CUDA
  851. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  852. include scripts/get-flags.mk
  853. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  854. endif
  855. ifdef LLAMA_CURL
  856. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  857. override LDFLAGS := $(LDFLAGS) -lcurl
  858. endif
  859. #
  860. # Print build information
  861. #
  862. $(info I llama.cpp build info: )
  863. $(info I UNAME_S: $(UNAME_S))
  864. $(info I UNAME_P: $(UNAME_P))
  865. $(info I UNAME_M: $(UNAME_M))
  866. $(info I CFLAGS: $(CFLAGS))
  867. $(info I CXXFLAGS: $(CXXFLAGS))
  868. $(info I NVCCFLAGS: $(NVCCFLAGS))
  869. $(info I LDFLAGS: $(LDFLAGS))
  870. $(info I CC: $(shell $(CC) --version | head -n 1))
  871. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  872. ifdef GGML_CUDA
  873. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  874. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  875. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  876. ifndef CUDA_DOCKER_ARCH
  877. ifndef CUDA_POWER_ARCH
  878. $(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 )
  879. endif # CUDA_POWER_ARCH
  880. endif # CUDA_DOCKER_ARCH
  881. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  882. endif # GGML_CUDA
  883. $(info )
  884. ifdef DEPRECATE_WARNING
  885. $(info !!! DEPRECATION WARNING !!!)
  886. $(info The following LLAMA_ options are deprecated and will be removed in the future. Use the GGML_ prefix instead)
  887. $(info - LLAMA_CUDA)
  888. $(info - LLAMA_METAL)
  889. $(info - LLAMA_METAL_EMBED_LIBRARY)
  890. $(info - LLAMA_OPENMP)
  891. $(info - LLAMA_RPC)
  892. $(info - LLAMA_SYCL)
  893. $(info - LLAMA_SYCL_F16)
  894. $(info - LLAMA_OPENBLAS)
  895. $(info - LLAMA_OPENBLAS64)
  896. $(info - LLAMA_BLIS)
  897. $(info - LLAMA_NO_LLAMAFILE)
  898. $(info - LLAMA_NO_ACCELERATE)
  899. $(info - LLAMA_NO_OPENMP)
  900. $(info - LLAMA_NO_METAL)
  901. $(info - LLAMA_NO_CCACHE)
  902. $(info )
  903. endif
  904. ifdef REMOVE_WARNING
  905. $(info !!! REMOVAL WARNING !!!)
  906. $(info The following LLAMA_ options have been removed and are no longer supported)
  907. $(info - LLAMA_DISABLE_LOGS (https://github.com/ggml-org/llama.cpp/pull/9418))
  908. $(info - LLAMA_SERVER_VERBOSE (https://github.com/ggml-org/llama.cpp/pull/9418))
  909. $(info )
  910. endif
  911. #
  912. # Build libraries
  913. #
  914. # Libraries
  915. LIB_GGML = libggml.so
  916. LIB_GGML_S = libggml.a
  917. LIB_LLAMA = libllama.so
  918. LIB_LLAMA_S = libllama.a
  919. LIB_COMMON = libcommon.so
  920. LIB_COMMON_S = libcommon.a
  921. # Targets
  922. BUILD_TARGETS += $(LIB_GGML) $(LIB_GGML_S) $(LIB_LLAMA) $(LIB_LLAMA_S) $(LIB_COMMON) $(LIB_COMMON_S)
  923. # Dependency files
  924. DEP_FILES = $(OBJ_GGML:.o=.d) $(OBJ_LLAMA:.o=.d) $(OBJ_COMMON:.o=.d)
  925. # Default target
  926. all: $(BUILD_TARGETS)
  927. # force c++ build for source file that have same name as c file
  928. # Note: need this exception because `ggml-cpu.c` and `ggml-cpu.cpp` both produce the same obj/dep files
  929. $(DIR_GGML)/%_cpp.o: $(DIR_GGML)/%.cpp
  930. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  931. # Rules for building object files
  932. $(DIR_GGML)/%.o: $(DIR_GGML)/%.c
  933. $(CC) $(CFLAGS) -MMD -c $< -o $@
  934. $(DIR_GGML)/%.o: $(DIR_GGML)/%.cpp
  935. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  936. $(DIR_LLAMA)/%.o: $(DIR_LLAMA)/%.cpp
  937. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  938. $(DIR_COMMON)/%.o: $(DIR_COMMON)/%.cpp
  939. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  940. # Rules for building libraries
  941. $(LIB_GGML): $(OBJ_GGML)
  942. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  943. $(LIB_GGML_S): $(OBJ_GGML)
  944. ar rcs $(LIB_GGML_S) $^
  945. $(LIB_LLAMA): $(OBJ_LLAMA) $(LIB_GGML)
  946. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  947. $(LIB_LLAMA_S): $(OBJ_LLAMA)
  948. ar rcs $(LIB_LLAMA_S) $^
  949. $(LIB_COMMON): $(OBJ_COMMON) $(LIB_LLAMA) $(LIB_GGML)
  950. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  951. $(LIB_COMMON_S): $(OBJ_COMMON)
  952. ar rcs $(LIB_COMMON_S) $^
  953. # Include dependency files
  954. -include $(DEP_FILES)
  955. # Clean generated server assets
  956. clean-server-assets:
  957. find examples/server -type f -name "*.js.hpp" -delete
  958. find examples/server -type f -name "*.mjs.hpp" -delete
  959. find examples/server -type f -name "*.css.hpp" -delete
  960. find examples/server -type f -name "*.html.hpp" -delete
  961. # Clean rule
  962. clean: clean-server-assets
  963. rm -vrf $(BUILD_TARGETS) $(TEST_TARGETS)
  964. rm -rvf *.a *.dll *.so *.dot
  965. find ggml src common tests examples pocs -type f -name "*.o" -delete
  966. find ggml src common tests examples pocs -type f -name "*.d" -delete
  967. #
  968. # Examples
  969. #
  970. # $< is the first prerequisite, i.e. the source file.
  971. # Explicitly compile this to an object file so that it can be cached with ccache.
  972. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  973. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  974. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  975. llama-cli: examples/main/main.cpp \
  976. $(OBJ_ALL)
  977. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  978. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  979. @echo
  980. @echo '==== Run ./llama-cli -h for help. ===='
  981. @echo
  982. llama-infill: examples/infill/infill.cpp \
  983. $(OBJ_ALL)
  984. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  985. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  986. llama-run: examples/run/run.cpp \
  987. $(OBJ_ALL)
  988. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  989. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  990. llama-simple: examples/simple/simple.cpp \
  991. $(OBJ_ALL)
  992. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  993. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  994. llama-simple-chat: examples/simple-chat/simple-chat.cpp \
  995. $(OBJ_ALL)
  996. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  997. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  998. llama-tokenize: examples/tokenize/tokenize.cpp \
  999. $(OBJ_ALL)
  1000. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1001. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1002. llama-batched: examples/batched/batched.cpp \
  1003. $(OBJ_ALL)
  1004. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1005. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1006. llama-batched-bench: examples/batched-bench/batched-bench.cpp \
  1007. $(OBJ_ALL)
  1008. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1009. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1010. llama-quantize: examples/quantize/quantize.cpp \
  1011. $(OBJ_ALL)
  1012. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1013. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1014. llama-quantize-stats: examples/quantize-stats/quantize-stats.cpp \
  1015. $(OBJ_ALL)
  1016. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1017. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1018. llama-perplexity: examples/perplexity/perplexity.cpp \
  1019. $(OBJ_ALL)
  1020. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1021. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1022. llama-imatrix: examples/imatrix/imatrix.cpp \
  1023. $(OBJ_ALL)
  1024. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1025. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1026. llama-embedding: examples/embedding/embedding.cpp \
  1027. $(OBJ_ALL)
  1028. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1029. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1030. llama-gritlm: examples/gritlm/gritlm.cpp \
  1031. $(OBJ_ALL)
  1032. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1033. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1034. llama-save-load-state: examples/save-load-state/save-load-state.cpp \
  1035. $(OBJ_ALL)
  1036. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1037. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1038. llama-gguf: examples/gguf/gguf.cpp \
  1039. $(OBJ_GGML)
  1040. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1041. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1042. examples/gguf-hash/deps/sha1/sha1.o: \
  1043. examples/gguf-hash/deps/sha1/sha1.c
  1044. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1045. examples/gguf-hash/deps/xxhash/xxhash.o: \
  1046. examples/gguf-hash/deps/xxhash/xxhash.c
  1047. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1048. examples/gguf-hash/deps/sha256/sha256.o: \
  1049. examples/gguf-hash/deps/sha256/sha256.c
  1050. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1051. 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\
  1052. $(OBJ_ALL)
  1053. $(CXX) $(CXXFLAGS) -Iexamples/gguf-hash/deps -c $< -o $(call GET_OBJ_FILE, $<)
  1054. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1055. llama-gguf-split: examples/gguf-split/gguf-split.cpp \
  1056. $(OBJ_ALL)
  1057. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1058. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1059. llama-eval-callback: examples/eval-callback/eval-callback.cpp \
  1060. $(OBJ_ALL)
  1061. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1062. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1063. llama-cvector-generator: examples/cvector-generator/cvector-generator.cpp \
  1064. $(OBJ_ALL)
  1065. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1066. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1067. llama-convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp \
  1068. $(OBJ_ALL)
  1069. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1070. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1071. llama-bench: examples/llama-bench/llama-bench.cpp \
  1072. $(OBJ_ALL)
  1073. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1074. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1075. llama-export-lora: examples/export-lora/export-lora.cpp \
  1076. $(OBJ_ALL)
  1077. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1078. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1079. llama-retrieval: examples/retrieval/retrieval.cpp \
  1080. $(OBJ_ALL)
  1081. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1082. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1083. llama-speculative: examples/speculative/speculative.cpp \
  1084. $(OBJ_ALL)
  1085. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1086. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1087. llama-parallel: examples/parallel/parallel.cpp \
  1088. $(OBJ_ALL)
  1089. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1090. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1091. llama-lookahead: examples/lookahead/lookahead.cpp \
  1092. $(OBJ_ALL)
  1093. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1094. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1095. llama-lookup: examples/lookup/lookup.cpp \
  1096. $(OBJ_ALL)
  1097. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1098. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1099. llama-lookup-create: examples/lookup/lookup-create.cpp \
  1100. $(OBJ_ALL)
  1101. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1102. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1103. llama-lookup-merge: examples/lookup/lookup-merge.cpp \
  1104. $(OBJ_ALL)
  1105. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1106. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1107. llama-lookup-stats: examples/lookup/lookup-stats.cpp \
  1108. $(OBJ_ALL)
  1109. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1110. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1111. llama-passkey: examples/passkey/passkey.cpp \
  1112. $(OBJ_ALL)
  1113. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1114. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1115. llama-gbnf-validator: examples/gbnf-validator/gbnf-validator.cpp \
  1116. $(OBJ_ALL)
  1117. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1118. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1119. ifdef GGML_RPC
  1120. rpc-server: examples/rpc/rpc-server.cpp \
  1121. $(OBJ_GGML)
  1122. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  1123. endif # GGML_RPC
  1124. llama-server: \
  1125. examples/server/server.cpp \
  1126. examples/server/utils.hpp \
  1127. examples/server/httplib.h \
  1128. examples/server/index.html.hpp \
  1129. examples/server/loading.html.hpp \
  1130. common/chat.cpp \
  1131. common/chat.h \
  1132. common/chat-template.hpp \
  1133. common/json.hpp \
  1134. common/minja.hpp \
  1135. $(OBJ_ALL)
  1136. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1137. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  1138. # Portable equivalent of `cd examples/server/public && xxd -i $(notdir $<) ../$(notdir $<).hpp`:
  1139. examples/server/%.hpp: examples/server/public/% FORCE Makefile
  1140. @( export NAME=$(subst .,_,$(subst -,_,$(notdir $<))) && \
  1141. echo "unsigned char $${NAME}[] = {" && \
  1142. cat $< | od -v -t x1 -An | sed -E 's/([0-9a-fA-F]+)/0x\1, /g' && \
  1143. echo "};" && \
  1144. echo "unsigned int $${NAME}_len = $(shell cat $< | wc -c );" \
  1145. ) > $@
  1146. llama-gen-docs: examples/gen-docs/gen-docs.cpp \
  1147. $(OBJ_ALL)
  1148. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1149. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1150. libllava.a: examples/llava/llava.cpp \
  1151. examples/llava/llava.h \
  1152. examples/llava/clip.cpp \
  1153. examples/llava/clip.h \
  1154. common/stb_image.h \
  1155. common/base64.hpp \
  1156. $(OBJ_ALL)
  1157. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  1158. llama-llava-cli: examples/llava/llava-cli.cpp \
  1159. examples/llava/llava.cpp \
  1160. examples/llava/llava.h \
  1161. examples/llava/clip.cpp \
  1162. examples/llava/clip.h \
  1163. $(OBJ_ALL)
  1164. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1165. llama-minicpmv-cli: examples/llava/minicpmv-cli.cpp \
  1166. examples/llava/llava.cpp \
  1167. examples/llava/llava.h \
  1168. examples/llava/clip.cpp \
  1169. examples/llava/clip.h \
  1170. $(OBJ_ALL)
  1171. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1172. llama-qwen2vl-cli: examples/llava/qwen2vl-cli.cpp \
  1173. examples/llava/llava.cpp \
  1174. examples/llava/llava.h \
  1175. examples/llava/clip.cpp \
  1176. examples/llava/clip.h \
  1177. $(OBJ_ALL)
  1178. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1179. ifeq ($(UNAME_S),Darwin)
  1180. swift: examples/batched.swift
  1181. (cd examples/batched.swift; make build)
  1182. endif
  1183. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  1184. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  1185. @if ! cmp -s $@.tmp $@; then \
  1186. mv $@.tmp $@; \
  1187. else \
  1188. rm $@.tmp; \
  1189. fi
  1190. common/build-info.o: common/build-info.cpp
  1191. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  1192. #
  1193. # Tests
  1194. #
  1195. tests: $(TEST_TARGETS)
  1196. tests/test-arg-parser: tests/test-arg-parser.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-llama-grammar: tests/test-llama-grammar.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-log: tests/test-log.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-parser: tests/test-grammar-parser.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-grammar-integration: tests/test-grammar-integration.cpp \
  1213. $(OBJ_ALL)
  1214. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1215. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1216. tests/test-double-float: tests/test-double-float.cpp
  1217. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1218. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1219. tests/test-json-schema-to-grammar: tests/test-json-schema-to-grammar.cpp \
  1220. $(OBJ_ALL)
  1221. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  1222. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1223. tests/test-chat: tests/test-chat.cpp \
  1224. $(OBJ_ALL)
  1225. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  1226. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1227. tests/test-opt: tests/test-opt.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-quantize-fns: tests/test-quantize-fns.cpp \
  1232. $(OBJ_GGML)
  1233. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1234. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1235. tests/test-quantize-perf: tests/test-quantize-perf.cpp \
  1236. $(OBJ_GGML)
  1237. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1238. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1239. tests/test-sampling: tests/test-sampling.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-0: tests/test-tokenizer-0.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-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp \
  1248. $(OBJ_ALL)
  1249. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1250. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1251. tests/test-tokenizer-1-spm: tests/test-tokenizer-1-spm.cpp \
  1252. $(OBJ_ALL)
  1253. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1254. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1255. tests/test-rope: tests/test-rope.cpp ggml/src/ggml.o \
  1256. $(OBJ_GGML)
  1257. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1258. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1259. tests/test-c.o: tests/test-c.c include/llama.h
  1260. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  1261. tests/test-backend-ops: tests/test-backend-ops.cpp \
  1262. $(OBJ_GGML)
  1263. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1264. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1265. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp tests/get-model.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. tests/test-autorelease: tests/test-autorelease.cpp tests/get-model.cpp \
  1270. $(OBJ_ALL)
  1271. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1272. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1273. tests/test-chat-template: tests/test-chat-template.cpp \
  1274. $(OBJ_ALL)
  1275. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1276. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1277. #
  1278. # PoCs
  1279. #
  1280. llama-vdot: pocs/vdot/vdot.cpp ggml/src/ggml.o \
  1281. $(OBJ_GGML)
  1282. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1283. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1284. llama-q8dot: pocs/vdot/q8dot.cpp ggml/src/ggml.o \
  1285. $(OBJ_GGML)
  1286. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1287. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1288. #
  1289. # Deprecated binaries that we want to keep around long enough for people to migrate to the new filenames, then these can be removed.
  1290. #
  1291. # Mark legacy binary targets as .PHONY so that they are always checked.
  1292. .PHONY: FORCE main quantize perplexity embedding server
  1293. # Define the object file target
  1294. examples/deprecation-warning/deprecation-warning.o: examples/deprecation-warning/deprecation-warning.cpp
  1295. $(CXX) $(CXXFLAGS) -c $< -o $@
  1296. # NOTE: We currently will always build the deprecation-warning `main` and `server` binaries to help users migrate.
  1297. # Eventually we will want to remove these target from building all the time.
  1298. main: examples/deprecation-warning/deprecation-warning.o
  1299. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1300. @echo "NOTICE: The 'main' binary is deprecated. Please use 'llama-cli' instead."
  1301. server: examples/deprecation-warning/deprecation-warning.o
  1302. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1303. @echo "NOTICE: The 'server' binary is deprecated. Please use 'llama-server' instead."
  1304. quantize: examples/deprecation-warning/deprecation-warning.o
  1305. ifneq (,$(wildcard quantize))
  1306. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1307. @echo "#########"
  1308. @echo "WARNING: The 'quantize' binary is deprecated. Please use 'llama-quantize' instead."
  1309. @echo " Remove the 'quantize' binary to remove this warning."
  1310. @echo "#########"
  1311. endif
  1312. perplexity: examples/deprecation-warning/deprecation-warning.o
  1313. ifneq (,$(wildcard perplexity))
  1314. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1315. @echo "#########"
  1316. @echo "WARNING: The 'perplexity' binary is deprecated. Please use 'llama-perplexity' instead."
  1317. @echo " Remove the 'perplexity' binary to remove this warning."
  1318. @echo "#########"
  1319. endif
  1320. embedding: examples/deprecation-warning/deprecation-warning.o
  1321. ifneq (,$(wildcard embedding))
  1322. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1323. @echo "#########"
  1324. @echo "WARNING: The 'embedding' binary is deprecated. Please use 'llama-embedding' instead."
  1325. @echo " Remove the 'embedding' binary to remove this warning."
  1326. @echo "#########"
  1327. endif