Makefile 48 KB

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