Makefile 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629
  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-grad0 \
  51. tests/test-grammar-integration \
  52. tests/test-grammar-parser \
  53. tests/test-json-schema-to-grammar \
  54. tests/test-llama-grammar \
  55. tests/test-log \
  56. tests/test-model-load-cancel \
  57. tests/test-quantize-fns \
  58. tests/test-quantize-perf \
  59. tests/test-rope \
  60. tests/test-sampling \
  61. tests/test-tokenizer-0 \
  62. tests/test-tokenizer-1-bpe \
  63. tests/test-tokenizer-1-spm
  64. # tests/test-opt \
  65. # Legacy build targets that were renamed in #7809, but should still be removed when the project is cleaned
  66. LEGACY_TARGETS_CLEAN = main quantize quantize-stats perplexity imatrix embedding vdot q8dot convert-llama2c-to-ggml \
  67. simple batched batched-bench save-load-state server gguf gguf-split eval-callback llama-bench libllava.a llava-cli baby-llama \
  68. retrieval speculative infill tokenize parallel export-lora lookahead lookup passkey gritlm
  69. # Legacy build targets that were renamed in #7809, but we want to build binaries that for them that output a deprecation warning if people try to use them.
  70. # We don't want to clutter things too much, so we only build replacements for the most commonly used binaries.
  71. LEGACY_TARGETS_BUILD = main quantize perplexity embedding server
  72. # Deprecation aliases
  73. ifdef LLAMA_CUBLAS
  74. $(error LLAMA_CUBLAS is removed. Use GGML_CUDA instead.)
  75. endif
  76. ifdef LLAMA_CUDA
  77. GGML_CUDA := 1
  78. DEPRECATE_WARNING := 1
  79. endif
  80. ifdef LLAMA_KOMPUTE
  81. GGML_KOMPUTE := 1
  82. DEPRECATE_WARNING := 1
  83. endif
  84. ifdef LLAMA_METAL
  85. GGML_METAL := 1
  86. DEPRECATE_WARNING := 1
  87. endif
  88. ifdef LLAMA_RPC
  89. GGML_RPC := 1
  90. DEPRECATE_WARNING := 1
  91. endif
  92. ifdef LLAMA_SYCL
  93. GGML_SYCL := 1
  94. DEPRECATE_WARNING := 1
  95. endif
  96. ifdef LLAMA_SYCL_F16
  97. GGML_SYCL_F16 := 1
  98. DEPRECATE_WARNING := 1
  99. endif
  100. ifdef LLAMA_OPENBLAS
  101. GGML_OPENBLAS := 1
  102. DEPRECATE_WARNING := 1
  103. endif
  104. ifdef LLAMA_OPENBLAS64
  105. GGML_OPENBLAS64 := 1
  106. DEPRECATE_WARNING := 1
  107. endif
  108. ifdef LLAMA_BLIS
  109. GGML_BLIS := 1
  110. DEPRECATE_WARNING := 1
  111. endif
  112. ifdef LLAMA_NO_LLAMAFILE
  113. GGML_NO_LLAMAFILE := 1
  114. DEPRECATE_WARNING := 1
  115. endif
  116. ifdef LLAMA_NO_ACCELERATE
  117. GGML_NO_ACCELERATE := 1
  118. DEPRECATE_WARNING := 1
  119. endif
  120. ifdef LLAMA_NO_OPENMP
  121. GGML_NO_OPENMP := 1
  122. DEPRECATE_WARNING := 1
  123. endif
  124. ifdef LLAMA_NO_METAL
  125. GGML_NO_METAL := 1
  126. DEPRECATE_WARNING := 1
  127. endif
  128. ifdef LLAMA_DISABLE_LOGS
  129. REMOVE_WARNING := 1
  130. endif
  131. ifdef LLAMA_SERVER_VERBOSE
  132. REMOVE_WARNING := 1
  133. endif
  134. ifndef UNAME_S
  135. UNAME_S := $(shell uname -s)
  136. endif
  137. ifndef UNAME_P
  138. UNAME_P := $(shell uname -p)
  139. endif
  140. ifndef UNAME_M
  141. UNAME_M := $(shell uname -m)
  142. endif
  143. # In GNU make default CXX is g++ instead of c++. Let's fix that so that users
  144. # of non-gcc compilers don't have to provide g++ alias or wrapper.
  145. DEFCC := cc
  146. DEFCXX := c++
  147. ifeq ($(origin CC),default)
  148. CC := $(DEFCC)
  149. endif
  150. ifeq ($(origin CXX),default)
  151. CXX := $(DEFCXX)
  152. endif
  153. # Mac OS + Arm can report x86_64
  154. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  155. ifeq ($(UNAME_S),Darwin)
  156. ifndef GGML_NO_METAL
  157. GGML_METAL := 1
  158. endif
  159. GGML_NO_OPENMP := 1
  160. ifneq ($(UNAME_P),arm)
  161. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  162. ifeq ($(SYSCTL_M),1)
  163. # UNAME_P := arm
  164. # UNAME_M := arm64
  165. warn := $(warning Your arch is announced as x86_64, but it seems to actually be ARM64. Not fixing that can lead to bad performance. For more info see: https://github.com/ggerganov/whisper.cpp/issues/66\#issuecomment-1282546789)
  166. endif
  167. endif
  168. endif
  169. ifdef GGML_METAL
  170. GGML_METAL_EMBED_LIBRARY := 1
  171. endif
  172. ifdef GGML_RPC
  173. BUILD_TARGETS += rpc-server
  174. endif
  175. ifdef GGML_VULKAN
  176. BUILD_TARGETS += vulkan-shaders-gen
  177. endif
  178. default: $(BUILD_TARGETS) $(LEGACY_TARGETS_BUILD)
  179. test: $(TEST_TARGETS)
  180. @failures=0; \
  181. for test_target in $(TEST_TARGETS); do \
  182. if [ "$$test_target" = "tests/test-tokenizer-0" ]; then \
  183. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-spm.gguf; \
  184. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-bpe.gguf; \
  185. ./$$test_target $(CURDIR)/models/ggml-vocab-phi-3.gguf; \
  186. ./$$test_target $(CURDIR)/models/ggml-vocab-falcon.gguf; \
  187. ./$$test_target $(CURDIR)/models/ggml-vocab-bert-bge.gguf; \
  188. ./$$test_target $(CURDIR)/models/ggml-vocab-starcoder.gguf; \
  189. ./$$test_target $(CURDIR)/models/ggml-vocab-gpt-2.gguf; \
  190. ./$$test_target $(CURDIR)/models/ggml-vocab-refact.gguf; \
  191. elif [ "$$test_target" = "tests/test-tokenizer-1-spm" ]; then \
  192. continue; \
  193. elif [ "$$test_target" = "tests/test-tokenizer-1-bpe" ]; then \
  194. continue; \
  195. else \
  196. echo "Running test $$test_target..."; \
  197. ./$$test_target; \
  198. fi; \
  199. if [ $$? -ne 0 ]; then \
  200. printf 'Test %s FAILED!\n\n' $$test_target; \
  201. failures=$$(( failures + 1 )); \
  202. else \
  203. printf 'Test %s passed.\n\n' $$test_target; \
  204. fi; \
  205. done; \
  206. if [ $$failures -gt 0 ]; then \
  207. printf '\n%s tests failed.\n' $$failures; \
  208. exit 1; \
  209. fi
  210. @echo 'All tests passed.'
  211. all: $(BUILD_TARGETS) $(TEST_TARGETS) $(LEGACY_TARGETS_BUILD)
  212. ifdef RISCV_CROSS_COMPILE
  213. CC := riscv64-unknown-linux-gnu-gcc
  214. CXX := riscv64-unknown-linux-gnu-g++
  215. endif
  216. #
  217. # Compile flags
  218. #
  219. # keep standard at C11 and C++11
  220. MK_CPPFLAGS = -Iggml/include -Iggml/src -Iinclude -Isrc -Icommon
  221. MK_CFLAGS = -std=c11 -fPIC
  222. MK_CXXFLAGS = -std=c++11 -fPIC
  223. MK_NVCCFLAGS = -std=c++11
  224. ifdef LLAMA_NO_CCACHE
  225. GGML_NO_CCACHE := 1
  226. DEPRECATE_WARNING := 1
  227. endif
  228. ifndef GGML_NO_CCACHE
  229. CCACHE := $(shell which ccache)
  230. ifdef CCACHE
  231. export CCACHE_SLOPPINESS = time_macros
  232. $(info I ccache found, compilation results will be cached. Disable with GGML_NO_CCACHE.)
  233. CC := $(CCACHE) $(CC)
  234. CXX := $(CCACHE) $(CXX)
  235. else
  236. $(info I ccache not found. Consider installing it for faster compilation.)
  237. endif # CCACHE
  238. endif # GGML_NO_CCACHE
  239. # clock_gettime came in POSIX.1b (1993)
  240. # CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
  241. # posix_memalign came in POSIX.1-2001 / SUSv3
  242. # M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
  243. MK_CPPFLAGS += -D_XOPEN_SOURCE=600
  244. # Somehow in OpenBSD whenever POSIX conformance is specified
  245. # some string functions rely on locale_t availability,
  246. # which was introduced in POSIX.1-2008, forcing us to go higher
  247. ifeq ($(UNAME_S),OpenBSD)
  248. MK_CPPFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
  249. endif
  250. # Data types, macros and functions related to controlling CPU affinity and
  251. # some memory allocation are available on Linux through GNU extensions in libc
  252. ifeq ($(UNAME_S),Linux)
  253. MK_CPPFLAGS += -D_GNU_SOURCE
  254. 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_DMMV
  542. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  543. endif # GGML_CUDA_FORCE_DMMV
  544. ifdef GGML_CUDA_FORCE_MMQ
  545. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  546. endif # GGML_CUDA_FORCE_MMQ
  547. ifdef GGML_CUDA_FORCE_CUBLAS
  548. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  549. endif # GGML_CUDA_FORCE_CUBLAS
  550. ifdef GGML_CUDA_DMMV_X
  551. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  552. else
  553. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  554. endif # GGML_CUDA_DMMV_X
  555. ifdef GGML_CUDA_MMV_Y
  556. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  557. else ifdef GGML_CUDA_DMMV_Y
  558. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_DMMV_Y) # for backwards compatibility
  559. else
  560. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  561. endif # GGML_CUDA_MMV_Y
  562. ifdef GGML_CUDA_F16
  563. MK_NVCCFLAGS += -DGGML_CUDA_F16
  564. endif # GGML_CUDA_F16
  565. ifdef GGML_CUDA_DMMV_F16
  566. MK_NVCCFLAGS += -DGGML_CUDA_F16
  567. endif # GGML_CUDA_DMMV_F16
  568. ifdef GGML_CUDA_KQUANTS_ITER
  569. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  570. else
  571. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  572. endif
  573. ifdef GGML_CUDA_PEER_MAX_BATCH_SIZE
  574. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(GGML_CUDA_PEER_MAX_BATCH_SIZE)
  575. else
  576. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  577. endif # GGML_CUDA_PEER_MAX_BATCH_SIZE
  578. ifdef GGML_CUDA_NO_PEER_COPY
  579. MK_NVCCFLAGS += -DGGML_CUDA_NO_PEER_COPY
  580. endif # GGML_CUDA_NO_PEER_COPY
  581. ifdef GGML_CUDA_CCBIN
  582. MK_NVCCFLAGS += -ccbin $(GGML_CUDA_CCBIN)
  583. endif # GGML_CUDA_CCBIN
  584. ifdef GGML_CUDA_FA_ALL_QUANTS
  585. MK_NVCCFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  586. endif # GGML_CUDA_FA_ALL_QUANTS
  587. ifdef JETSON_EOL_MODULE_DETECT
  588. define NVCC_COMPILE
  589. $(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 $@
  590. endef # NVCC_COMPILE
  591. else
  592. define NVCC_COMPILE
  593. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  594. endef # NVCC_COMPILE
  595. endif # JETSON_EOL_MODULE_DETECT
  596. ggml/src/ggml-cuda/%.o: \
  597. ggml/src/ggml-cuda/%.cu \
  598. ggml/include/ggml.h \
  599. ggml/src/ggml-common.h \
  600. ggml/src/ggml-cuda/common.cuh
  601. $(NVCC_COMPILE)
  602. ggml/src/ggml-cuda/ggml-cuda.o: \
  603. ggml/src/ggml-cuda/ggml-cuda.cu \
  604. ggml/include/ggml-cuda.h \
  605. ggml/include/ggml.h \
  606. ggml/include/ggml-backend.h \
  607. ggml/src/ggml-backend-impl.h \
  608. ggml/src/ggml-common.h \
  609. $(wildcard ggml/src/ggml-cuda/*.cuh)
  610. $(NVCC_COMPILE)
  611. endif # GGML_CUDA
  612. ifdef GGML_VULKAN
  613. MK_CPPFLAGS += -DGGML_USE_VULKAN
  614. MK_LDFLAGS += $(shell pkg-config --libs vulkan)
  615. OBJ_GGML_EXT += ggml/src/ggml-vulkan.o ggml/src/ggml-vulkan-shaders.o
  616. ifdef GGML_VULKAN_CHECK_RESULTS
  617. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  618. endif
  619. ifdef GGML_VULKAN_DEBUG
  620. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  621. endif
  622. ifdef GGML_VULKAN_MEMORY_DEBUG
  623. MK_CPPFLAGS += -DGGML_VULKAN_MEMORY_DEBUG
  624. endif
  625. ifdef GGML_VULKAN_PERF
  626. MK_CPPFLAGS += -DGGML_VULKAN_PERF
  627. endif
  628. ifdef GGML_VULKAN_VALIDATE
  629. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  630. endif
  631. ifdef GGML_VULKAN_RUN_TESTS
  632. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  633. endif
  634. GLSLC_CMD = glslc
  635. _ggml_vk_genshaders_cmd = $(shell pwd)/vulkan-shaders-gen
  636. _ggml_vk_header = ggml/src/ggml-vulkan-shaders.hpp
  637. _ggml_vk_source = ggml/src/ggml-vulkan-shaders.cpp
  638. _ggml_vk_input_dir = ggml/src/vulkan-shaders
  639. _ggml_vk_shader_deps = $(echo $(_ggml_vk_input_dir)/*.comp)
  640. ggml/src/ggml-vulkan.o: ggml/src/ggml-vulkan.cpp ggml/include/ggml-vulkan.h $(_ggml_vk_header) $(_ggml_vk_source)
  641. $(CXX) $(CXXFLAGS) $(shell pkg-config --cflags vulkan) -c $< -o $@
  642. $(_ggml_vk_header): $(_ggml_vk_source)
  643. $(_ggml_vk_source): $(_ggml_vk_shader_deps) vulkan-shaders-gen
  644. $(_ggml_vk_genshaders_cmd) \
  645. --glslc $(GLSLC_CMD) \
  646. --input-dir $(_ggml_vk_input_dir) \
  647. --target-hpp $(_ggml_vk_header) \
  648. --target-cpp $(_ggml_vk_source)
  649. vulkan-shaders-gen: ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
  650. $(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
  651. endif # GGML_VULKAN
  652. ifdef GGML_HIPBLAS
  653. ifeq ($(wildcard /opt/rocm),)
  654. ROCM_PATH ?= /usr
  655. AMDGPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  656. else
  657. ROCM_PATH ?= /opt/rocm
  658. AMDGPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  659. endif
  660. GGML_CUDA_DMMV_X ?= 32
  661. GGML_CUDA_MMV_Y ?= 1
  662. GGML_CUDA_KQUANTS_ITER ?= 2
  663. MK_CPPFLAGS += -DGGML_USE_HIP -DGGML_USE_CUDA
  664. ifdef GGML_HIP_UMA
  665. MK_CPPFLAGS += -DGGML_HIP_UMA
  666. endif # GGML_HIP_UMA
  667. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  668. MK_LDFLAGS += -L$(ROCM_PATH)/lib64 -Wl,-rpath=$(ROCM_PATH)/lib64
  669. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  670. HIPCC ?= $(CCACHE) $(ROCM_PATH)/bin/hipcc
  671. HIPFLAGS += $(addprefix --offload-arch=,$(AMDGPU_TARGETS))
  672. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  673. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  674. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  675. ifdef GGML_CUDA_FORCE_DMMV
  676. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  677. endif # GGML_CUDA_FORCE_DMMV
  678. ifdef GGML_CUDA_FORCE_MMQ
  679. HIPFLAGS += -DGGML_CUDA_FORCE_MMQ
  680. endif # GGML_CUDA_FORCE_MMQ
  681. ifdef GGML_CUDA_FORCE_CUBLAS
  682. HIPFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  683. endif # GGML_CUDA_FORCE_CUBLAS
  684. ifdef GGML_CUDA_NO_PEER_COPY
  685. HIPFLAGS += -DGGML_CUDA_NO_PEER_COPY
  686. endif # GGML_CUDA_NO_PEER_COPY
  687. OBJ_GGML_EXT += ggml/src/ggml-cuda/ggml-cuda.o
  688. OBJ_GGML_EXT += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  689. OBJ_GGML_EXT += $(OBJ_CUDA_TMPL)
  690. ggml/src/ggml-cuda/ggml-cuda.o: \
  691. ggml/src/ggml-cuda/ggml-cuda.cu \
  692. ggml/include/ggml-cuda.h \
  693. ggml/include/ggml.h \
  694. ggml/include/ggml-backend.h \
  695. ggml/src/ggml-backend-impl.h \
  696. ggml/src/ggml-common.h \
  697. $(wildcard ggml/src/ggml-cuda/*.cuh)
  698. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  699. ggml/src/ggml-cuda/%.o: \
  700. ggml/src/ggml-cuda/%.cu \
  701. ggml/include/ggml.h \
  702. ggml/src/ggml-common.h \
  703. ggml/src/ggml-cuda/common.cuh
  704. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  705. endif # GGML_HIPBLAS
  706. ifdef GGML_MUSA
  707. ifeq ($(wildcard /opt/musa),)
  708. MUSA_PATH ?= /usr/local/musa
  709. else
  710. MUSA_PATH ?= /opt/musa
  711. endif
  712. MTGPU_TARGETS ?= mp_21 mp_22
  713. MK_CPPFLAGS += -DGGML_USE_MUSA -DGGML_USE_CUDA
  714. MK_LDFLAGS += -L$(MUSA_PATH)/lib -Wl,-rpath=$(MUSA_PATH)/lib
  715. MK_LDFLAGS += -lmusa -lmusart -lmublas
  716. ifndef GGML_NO_OPENMP
  717. # For Ubuntu Focal
  718. MK_CPPFLAGS += -I/usr/lib/llvm-10/include/openmp
  719. MK_LDFLAGS += -L/usr/lib/llvm-10/lib
  720. # For Ubuntu Jammy
  721. MK_CPPFLAGS += -I/usr/lib/llvm-14/lib/clang/14.0.0/include
  722. MK_LDFLAGS += -L/usr/lib/llvm-14/lib
  723. endif # GGML_NO_OPENMP
  724. CC := $(MUSA_PATH)/bin/clang
  725. CXX := $(MUSA_PATH)/bin/clang++
  726. MCC := $(CCACHE) $(MUSA_PATH)/bin/mcc
  727. MUSAFLAGS += $(addprefix --cuda-gpu-arch=, $(MTGPU_TARGETS))
  728. ifdef GGML_CUDA_FORCE_DMMV
  729. MUSAFLAGS += -DGGML_CUDA_FORCE_DMMV
  730. endif # GGML_CUDA_FORCE_DMMV
  731. ifdef GGML_CUDA_FORCE_MMQ
  732. MUSAFLAGS += -DGGML_CUDA_FORCE_MMQ
  733. endif # GGML_CUDA_FORCE_MMQ
  734. ifdef GGML_CUDA_FORCE_CUBLAS
  735. MUSAFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  736. endif # GGML_CUDA_FORCE_CUBLAS
  737. ifdef GGML_CUDA_DMMV_X
  738. MUSAFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  739. else
  740. MUSAFLAGS += -DGGML_CUDA_DMMV_X=32
  741. endif # GGML_CUDA_DMMV_X
  742. ifdef GGML_CUDA_MMV_Y
  743. MUSAFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  744. else
  745. MUSAFLAGS += -DGGML_CUDA_MMV_Y=1
  746. endif # GGML_CUDA_MMV_Y
  747. ifdef GGML_CUDA_F16
  748. MUSAFLAGS += -DGGML_CUDA_F16
  749. endif # GGML_CUDA_F16
  750. ifdef GGML_CUDA_DMMV_F16
  751. MUSAFLAGS += -DGGML_CUDA_F16
  752. endif # GGML_CUDA_DMMV_F16
  753. ifdef GGML_CUDA_KQUANTS_ITER
  754. MUSAFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  755. else
  756. MUSAFLAGS += -DK_QUANTS_PER_ITERATION=2
  757. endif
  758. ifdef GGML_CUDA_PEER_MAX_BATCH_SIZE
  759. MUSAFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(GGML_CUDA_PEER_MAX_BATCH_SIZE)
  760. else
  761. MUSAFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  762. endif # GGML_CUDA_PEER_MAX_BATCH_SIZE
  763. ifdef GGML_CUDA_NO_PEER_COPY
  764. MUSAFLAGS += -DGGML_CUDA_NO_PEER_COPY
  765. endif # GGML_CUDA_NO_PEER_COPY
  766. ifdef GGML_CUDA_FA_ALL_QUANTS
  767. MUSAFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  768. endif # GGML_CUDA_FA_ALL_QUANTS
  769. OBJ_GGML_EXT += ggml/src/ggml-cuda/ggml-cuda.o
  770. OBJ_GGML_EXT += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  771. OBJ_GGML_EXT += $(OBJ_CUDA_TMPL)
  772. ggml/src/ggml-cuda/ggml-cuda.o: \
  773. ggml/src/ggml-cuda/ggml-cuda.cu \
  774. ggml/include/ggml-cuda.h \
  775. ggml/include/ggml.h \
  776. ggml/include/ggml-backend.h \
  777. ggml/src/ggml-backend-impl.h \
  778. ggml/src/ggml-common.h \
  779. $(wildcard ggml/src/ggml-cuda/*.cuh)
  780. $(MCC) $(CXXFLAGS) $(MUSAFLAGS) -x musa -mtgpu -c -o $@ $<
  781. ggml/src/ggml-cuda/%.o: \
  782. ggml/src/ggml-cuda/%.cu \
  783. ggml/include/ggml.h \
  784. ggml/src/ggml-common.h \
  785. ggml/src/ggml-cuda/common.cuh
  786. $(MCC) $(CXXFLAGS) $(MUSAFLAGS) -x musa -mtgpu -c -o $@ $<
  787. endif # GGML_MUSA
  788. ifdef GGML_METAL
  789. MK_CPPFLAGS += -DGGML_USE_METAL
  790. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  791. OBJ_GGML_EXT += ggml/src/ggml-metal/ggml-metal.o
  792. ifdef GGML_METAL_USE_BF16
  793. MK_CPPFLAGS += -DGGML_METAL_USE_BF16
  794. endif # GGML_METAL_USE_BF16
  795. ifdef GGML_METAL_NDEBUG
  796. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  797. endif
  798. ifdef GGML_METAL_EMBED_LIBRARY
  799. MK_CPPFLAGS += -DGGML_METAL_EMBED_LIBRARY
  800. OBJ_GGML_EXT += ggml/src/ggml-metal-embed.o
  801. endif
  802. endif # GGML_METAL
  803. ifdef GGML_METAL
  804. ggml/src/ggml-metal/ggml-metal.o: \
  805. ggml/src/ggml-metal/ggml-metal.m \
  806. ggml/include/ggml-metal.h \
  807. ggml/include/ggml.h
  808. $(CC) $(CFLAGS) -c $< -o $@
  809. ifdef GGML_METAL_EMBED_LIBRARY
  810. ggml/src/ggml-metal-embed.o: \
  811. ggml/src/ggml-metal/ggml-metal.metal \
  812. ggml/src/ggml-common.h
  813. @echo "Embedding Metal library"
  814. @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
  815. $(eval TEMP_ASSEMBLY=$(shell mktemp -d))
  816. @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  817. @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  818. @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  819. @echo ".incbin \"ggml/src/ggml-metal/ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  820. @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  821. @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  822. $(CC) $(CFLAGS) -c $(TEMP_ASSEMBLY)/ggml-metal-embed.s -o $@
  823. @rm -f ${TEMP_ASSEMBLY}/ggml-metal-embed.s
  824. @rmdir ${TEMP_ASSEMBLY}
  825. endif
  826. endif # GGML_METAL
  827. DIR_GGML = ggml
  828. DIR_LLAMA = src
  829. DIR_COMMON = common
  830. OBJ_GGML = \
  831. $(DIR_GGML)/src/ggml.o \
  832. $(DIR_GGML)/src/ggml-aarch64.o \
  833. $(DIR_GGML)/src/ggml-alloc.o \
  834. $(DIR_GGML)/src/ggml-backend.o \
  835. $(DIR_GGML)/src/ggml-backend-reg.o \
  836. $(DIR_GGML)/src/ggml-quants.o \
  837. $(DIR_GGML)/src/ggml-threading.o \
  838. $(DIR_GGML)/src/ggml-cpu/ggml-cpu.o \
  839. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-cpp.o \
  840. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-aarch64.o \
  841. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-quants.o \
  842. $(OBJ_GGML_EXT)
  843. OBJ_LLAMA = \
  844. $(DIR_LLAMA)/llama.o \
  845. $(DIR_LLAMA)/llama-vocab.o \
  846. $(DIR_LLAMA)/llama-grammar.o \
  847. $(DIR_LLAMA)/llama-sampling.o \
  848. $(DIR_LLAMA)/unicode.o \
  849. $(DIR_LLAMA)/unicode-data.o
  850. OBJ_COMMON = \
  851. $(DIR_COMMON)/common.o \
  852. $(DIR_COMMON)/arg.o \
  853. $(DIR_COMMON)/log.o \
  854. $(DIR_COMMON)/console.o \
  855. $(DIR_COMMON)/ngram-cache.o \
  856. $(DIR_COMMON)/sampling.o \
  857. $(DIR_COMMON)/build-info.o \
  858. $(DIR_COMMON)/json-schema-to-grammar.o
  859. OBJ_ALL = $(OBJ_GGML) $(OBJ_LLAMA) $(OBJ_COMMON)
  860. LIB_GGML = $(LIB_PRE)ggml$(DSO_EXT)
  861. LIB_GGML_S = $(LIB_PRE)ggml.a
  862. LIB_LLAMA = $(LIB_PRE)llama$(DSO_EXT)
  863. LIB_LLAMA_S = $(LIB_PRE)llama.a
  864. LIB_COMMON = $(LIB_PRE)common$(DSO_EXT)
  865. LIB_COMMON_S = $(LIB_PRE)common.a
  866. LIB_ALL = $(LIB_GGML) $(LIB_LLAMA) $(LIB_COMMON)
  867. LIB_ALL_S = $(LIB_GGML_S) $(LIB_LLAMA_S) $(LIB_COMMON_S)
  868. GF_CC := $(CC)
  869. include scripts/get-flags.mk
  870. # combine build flags with cmdline overrides
  871. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  872. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  873. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  874. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  875. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  876. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  877. # identify CUDA host compiler
  878. ifdef GGML_CUDA
  879. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  880. include scripts/get-flags.mk
  881. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  882. endif
  883. ifdef LLAMA_CURL
  884. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  885. override LDFLAGS := $(LDFLAGS) -lcurl
  886. endif
  887. #
  888. # Print build information
  889. #
  890. $(info I llama.cpp build info: )
  891. $(info I UNAME_S: $(UNAME_S))
  892. $(info I UNAME_P: $(UNAME_P))
  893. $(info I UNAME_M: $(UNAME_M))
  894. $(info I CFLAGS: $(CFLAGS))
  895. $(info I CXXFLAGS: $(CXXFLAGS))
  896. $(info I NVCCFLAGS: $(NVCCFLAGS))
  897. $(info I LDFLAGS: $(LDFLAGS))
  898. $(info I CC: $(shell $(CC) --version | head -n 1))
  899. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  900. ifdef GGML_CUDA
  901. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  902. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  903. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  904. ifndef CUDA_DOCKER_ARCH
  905. ifndef CUDA_POWER_ARCH
  906. $(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 )
  907. endif # CUDA_POWER_ARCH
  908. endif # CUDA_DOCKER_ARCH
  909. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  910. endif # GGML_CUDA
  911. $(info )
  912. ifdef DEPRECATE_WARNING
  913. $(info !!! DEPRECATION WARNING !!!)
  914. $(info The following LLAMA_ options are deprecated and will be removed in the future. Use the GGML_ prefix instead)
  915. $(info - LLAMA_CUDA)
  916. $(info - LLAMA_METAL)
  917. $(info - LLAMA_METAL_EMBED_LIBRARY)
  918. $(info - LLAMA_OPENMP)
  919. $(info - LLAMA_RPC)
  920. $(info - LLAMA_SYCL)
  921. $(info - LLAMA_SYCL_F16)
  922. $(info - LLAMA_OPENBLAS)
  923. $(info - LLAMA_OPENBLAS64)
  924. $(info - LLAMA_BLIS)
  925. $(info - LLAMA_NO_LLAMAFILE)
  926. $(info - LLAMA_NO_ACCELERATE)
  927. $(info - LLAMA_NO_OPENMP)
  928. $(info - LLAMA_NO_METAL)
  929. $(info - LLAMA_NO_CCACHE)
  930. $(info )
  931. endif
  932. ifdef REMOVE_WARNING
  933. $(info !!! REMOVAL WARNING !!!)
  934. $(info The following LLAMA_ options have been removed and are no longer supported)
  935. $(info - LLAMA_DISABLE_LOGS (https://github.com/ggerganov/llama.cpp/pull/9418))
  936. $(info - LLAMA_SERVER_VERBOSE (https://github.com/ggerganov/llama.cpp/pull/9418))
  937. $(info )
  938. endif
  939. #
  940. # Build libraries
  941. #
  942. # Libraries
  943. LIB_GGML = libggml.so
  944. LIB_GGML_S = libggml.a
  945. LIB_LLAMA = libllama.so
  946. LIB_LLAMA_S = libllama.a
  947. LIB_COMMON = libcommon.so
  948. LIB_COMMON_S = libcommon.a
  949. # Targets
  950. BUILD_TARGETS += $(LIB_GGML) $(LIB_GGML_S) $(LIB_LLAMA) $(LIB_LLAMA_S) $(LIB_COMMON) $(LIB_COMMON_S)
  951. # Dependency files
  952. DEP_FILES = $(OBJ_GGML:.o=.d) $(OBJ_LLAMA:.o=.d) $(OBJ_COMMON:.o=.d)
  953. # Default target
  954. all: $(BUILD_TARGETS)
  955. # Note: need this exception because `ggml-cpu.c` and `ggml-cpu.cpp` both produce the same obj/dep files
  956. # g++ -M -I ./ggml/include/ -I ./ggml/src ggml/src/ggml-cpu/ggml-cpu.cpp | grep ggml
  957. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-cpp.o: \
  958. ggml/src/ggml-cpu/ggml-cpu.cpp \
  959. ggml/include/ggml-backend.h \
  960. ggml/include/ggml.h \
  961. ggml/include/ggml-alloc.h \
  962. ggml/src/ggml-backend-impl.h \
  963. ggml/include/ggml-cpu.h \
  964. ggml/src/ggml-impl.h
  965. $(CXX) $(CXXFLAGS) -c $< -o $@
  966. # Rules for building object files
  967. $(DIR_GGML)/%.o: $(DIR_GGML)/%.c
  968. $(CC) $(CFLAGS) -MMD -c $< -o $@
  969. $(DIR_GGML)/%.o: $(DIR_GGML)/%.cpp
  970. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  971. $(DIR_LLAMA)/%.o: $(DIR_LLAMA)/%.cpp
  972. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  973. $(DIR_COMMON)/%.o: $(DIR_COMMON)/%.cpp
  974. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  975. # Rules for building libraries
  976. $(LIB_GGML): $(OBJ_GGML)
  977. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  978. $(LIB_GGML_S): $(OBJ_GGML)
  979. ar rcs $(LIB_GGML_S) $^
  980. $(LIB_LLAMA): $(OBJ_LLAMA) $(LIB_GGML)
  981. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  982. $(LIB_LLAMA_S): $(OBJ_LLAMA)
  983. ar rcs $(LIB_LLAMA_S) $^
  984. $(LIB_COMMON): $(OBJ_COMMON) $(LIB_LLAMA) $(LIB_GGML)
  985. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  986. $(LIB_COMMON_S): $(OBJ_COMMON)
  987. ar rcs $(LIB_COMMON_S) $^
  988. # Include dependency files
  989. -include $(DEP_FILES)
  990. # Clean rule
  991. clean:
  992. rm -vrf $(BUILD_TARGETS) $(TEST_TARGETS)
  993. rm -rvf *.a *.dll *.so *.dot
  994. find ggml src common tests examples pocs -type f -name "*.o" -delete
  995. find ggml src common tests examples pocs -type f -name "*.d" -delete
  996. #
  997. # Examples
  998. #
  999. # $< is the first prerequisite, i.e. the source file.
  1000. # Explicitly compile this to an object file so that it can be cached with ccache.
  1001. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  1002. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  1003. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  1004. llama-cli: examples/main/main.cpp \
  1005. $(OBJ_ALL)
  1006. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1007. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1008. @echo
  1009. @echo '==== Run ./llama-cli -h for help. ===='
  1010. @echo
  1011. llama-infill: examples/infill/infill.cpp \
  1012. $(OBJ_ALL)
  1013. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1014. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1015. llama-simple: examples/simple/simple.cpp \
  1016. $(OBJ_ALL)
  1017. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1018. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1019. llama-simple-chat: examples/simple-chat/simple-chat.cpp \
  1020. $(OBJ_ALL)
  1021. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1022. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1023. llama-tokenize: examples/tokenize/tokenize.cpp \
  1024. $(OBJ_ALL)
  1025. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1026. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1027. llama-batched: examples/batched/batched.cpp \
  1028. $(OBJ_ALL)
  1029. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1030. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1031. llama-batched-bench: examples/batched-bench/batched-bench.cpp \
  1032. $(OBJ_ALL)
  1033. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1034. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1035. llama-quantize: examples/quantize/quantize.cpp \
  1036. $(OBJ_ALL)
  1037. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1038. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1039. llama-quantize-stats: examples/quantize-stats/quantize-stats.cpp \
  1040. $(OBJ_ALL)
  1041. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1042. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1043. llama-perplexity: examples/perplexity/perplexity.cpp \
  1044. $(OBJ_ALL)
  1045. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1046. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1047. llama-imatrix: examples/imatrix/imatrix.cpp \
  1048. $(OBJ_ALL)
  1049. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1050. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1051. llama-embedding: examples/embedding/embedding.cpp \
  1052. $(OBJ_ALL)
  1053. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1054. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1055. llama-gritlm: examples/gritlm/gritlm.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-save-load-state: examples/save-load-state/save-load-state.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-gguf: examples/gguf/gguf.cpp \
  1064. $(OBJ_GGML)
  1065. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1066. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1067. examples/gguf-hash/deps/sha1/sha1.o: \
  1068. examples/gguf-hash/deps/sha1/sha1.c
  1069. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1070. examples/gguf-hash/deps/xxhash/xxhash.o: \
  1071. examples/gguf-hash/deps/xxhash/xxhash.c
  1072. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1073. examples/gguf-hash/deps/sha256/sha256.o: \
  1074. examples/gguf-hash/deps/sha256/sha256.c
  1075. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1076. 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\
  1077. $(OBJ_ALL)
  1078. $(CXX) $(CXXFLAGS) -Iexamples/gguf-hash/deps -c $< -o $(call GET_OBJ_FILE, $<)
  1079. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1080. llama-gguf-split: examples/gguf-split/gguf-split.cpp \
  1081. $(OBJ_ALL)
  1082. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1083. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1084. llama-eval-callback: examples/eval-callback/eval-callback.cpp \
  1085. $(OBJ_ALL)
  1086. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1087. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1088. llama-cvector-generator: examples/cvector-generator/cvector-generator.cpp \
  1089. $(OBJ_ALL)
  1090. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1091. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1092. llama-convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp \
  1093. $(OBJ_ALL)
  1094. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1095. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1096. llama-bench: examples/llama-bench/llama-bench.cpp \
  1097. $(OBJ_ALL)
  1098. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1099. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1100. llama-export-lora: examples/export-lora/export-lora.cpp \
  1101. $(OBJ_ALL)
  1102. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1103. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1104. llama-retrieval: examples/retrieval/retrieval.cpp \
  1105. $(OBJ_ALL)
  1106. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1107. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1108. llama-speculative: examples/speculative/speculative.cpp \
  1109. $(OBJ_ALL)
  1110. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1111. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1112. llama-parallel: examples/parallel/parallel.cpp \
  1113. $(OBJ_ALL)
  1114. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1115. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1116. llama-lookahead: examples/lookahead/lookahead.cpp \
  1117. $(OBJ_ALL)
  1118. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1119. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1120. llama-lookup: examples/lookup/lookup.cpp \
  1121. $(OBJ_ALL)
  1122. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1123. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1124. llama-lookup-create: examples/lookup/lookup-create.cpp \
  1125. $(OBJ_ALL)
  1126. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1127. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1128. llama-lookup-merge: examples/lookup/lookup-merge.cpp \
  1129. $(OBJ_ALL)
  1130. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1131. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1132. llama-lookup-stats: examples/lookup/lookup-stats.cpp \
  1133. $(OBJ_ALL)
  1134. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1135. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1136. llama-passkey: examples/passkey/passkey.cpp \
  1137. $(OBJ_ALL)
  1138. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1139. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1140. llama-gbnf-validator: examples/gbnf-validator/gbnf-validator.cpp \
  1141. $(OBJ_ALL)
  1142. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1143. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1144. ifdef GGML_RPC
  1145. rpc-server: examples/rpc/rpc-server.cpp \
  1146. $(OBJ_GGML)
  1147. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  1148. endif # GGML_RPC
  1149. llama-server: \
  1150. examples/server/server.cpp \
  1151. examples/server/utils.hpp \
  1152. examples/server/httplib.h \
  1153. examples/server/index.html.hpp \
  1154. examples/server/completion.js.hpp \
  1155. examples/server/loading.html.hpp \
  1156. examples/server/deps_daisyui.min.css.hpp \
  1157. examples/server/deps_markdown-it.js.hpp \
  1158. examples/server/deps_tailwindcss.js.hpp \
  1159. examples/server/deps_vue.esm-browser.js.hpp \
  1160. common/json.hpp \
  1161. common/stb_image.h \
  1162. $(OBJ_ALL)
  1163. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1164. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  1165. # Portable equivalent of `cd examples/server/public && xxd -i $(notdir $<) ../$(notdir $<).hpp`:
  1166. examples/server/%.hpp: examples/server/public/% Makefile
  1167. @( export NAME=$(subst .,_,$(subst -,_,$(notdir $<))) && \
  1168. echo "unsigned char $${NAME}[] = {" && \
  1169. cat $< | od -v -t x1 -An | sed -E 's/([0-9a-fA-F]+)/0x\1, /g' && \
  1170. echo "};" && \
  1171. echo "unsigned int $${NAME}_len = $(shell cat $< | wc -c );" \
  1172. ) > $@
  1173. llama-gen-docs: examples/gen-docs/gen-docs.cpp \
  1174. $(OBJ_ALL)
  1175. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1176. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1177. libllava.a: examples/llava/llava.cpp \
  1178. examples/llava/llava.h \
  1179. examples/llava/clip.cpp \
  1180. examples/llava/clip.h \
  1181. common/stb_image.h \
  1182. common/base64.hpp \
  1183. $(OBJ_ALL)
  1184. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  1185. llama-llava-cli: examples/llava/llava-cli.cpp \
  1186. examples/llava/llava.cpp \
  1187. examples/llava/llava.h \
  1188. examples/llava/clip.cpp \
  1189. examples/llava/clip.h \
  1190. $(OBJ_ALL)
  1191. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1192. llama-minicpmv-cli: examples/llava/minicpmv-cli.cpp \
  1193. examples/llava/llava.cpp \
  1194. examples/llava/llava.h \
  1195. examples/llava/clip.cpp \
  1196. examples/llava/clip.h \
  1197. $(OBJ_ALL)
  1198. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1199. ifeq ($(UNAME_S),Darwin)
  1200. swift: examples/batched.swift
  1201. (cd examples/batched.swift; make build)
  1202. endif
  1203. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  1204. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  1205. @if ! cmp -s $@.tmp $@; then \
  1206. mv $@.tmp $@; \
  1207. else \
  1208. rm $@.tmp; \
  1209. fi
  1210. common/build-info.o: common/build-info.cpp
  1211. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  1212. #
  1213. # Tests
  1214. #
  1215. tests: $(TEST_TARGETS)
  1216. tests/test-arg-parser: tests/test-arg-parser.cpp \
  1217. $(OBJ_ALL)
  1218. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1219. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1220. tests/test-llama-grammar: tests/test-llama-grammar.cpp \
  1221. $(OBJ_ALL)
  1222. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1223. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1224. tests/test-log: tests/test-log.cpp \
  1225. $(OBJ_ALL)
  1226. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1227. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1228. tests/test-grammar-parser: tests/test-grammar-parser.cpp \
  1229. $(OBJ_ALL)
  1230. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1231. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1232. tests/test-grammar-integration: tests/test-grammar-integration.cpp \
  1233. $(OBJ_ALL)
  1234. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1235. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1236. tests/test-double-float: tests/test-double-float.cpp
  1237. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1238. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1239. tests/test-json-schema-to-grammar: tests/test-json-schema-to-grammar.cpp \
  1240. $(OBJ_ALL)
  1241. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  1242. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1243. tests/test-grad0: tests/test-grad0.cpp \
  1244. $(OBJ_GGML)
  1245. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1246. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1247. tests/test-opt: tests/test-opt.cpp \
  1248. $(OBJ_GGML)
  1249. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1250. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1251. tests/test-quantize-fns: tests/test-quantize-fns.cpp \
  1252. $(OBJ_GGML)
  1253. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1254. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1255. tests/test-quantize-perf: tests/test-quantize-perf.cpp \
  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-sampling: tests/test-sampling.cpp \
  1260. $(OBJ_ALL)
  1261. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1262. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1263. tests/test-tokenizer-0: tests/test-tokenizer-0.cpp \
  1264. $(OBJ_ALL)
  1265. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1266. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1267. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp \
  1268. $(OBJ_ALL)
  1269. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1270. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1271. tests/test-tokenizer-1-spm: tests/test-tokenizer-1-spm.cpp \
  1272. $(OBJ_ALL)
  1273. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1274. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1275. tests/test-rope: tests/test-rope.cpp ggml/src/ggml.o \
  1276. $(OBJ_GGML)
  1277. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1278. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1279. tests/test-c.o: tests/test-c.c include/llama.h
  1280. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  1281. tests/test-backend-ops: tests/test-backend-ops.cpp \
  1282. $(OBJ_GGML)
  1283. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1284. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1285. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp tests/get-model.cpp \
  1286. $(OBJ_ALL)
  1287. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1288. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1289. tests/test-autorelease: tests/test-autorelease.cpp tests/get-model.cpp \
  1290. $(OBJ_ALL)
  1291. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1292. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1293. tests/test-chat-template: tests/test-chat-template.cpp \
  1294. $(OBJ_ALL)
  1295. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1296. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1297. #
  1298. # PoCs
  1299. #
  1300. llama-vdot: pocs/vdot/vdot.cpp ggml/src/ggml.o \
  1301. $(OBJ_GGML)
  1302. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1303. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1304. llama-q8dot: pocs/vdot/q8dot.cpp ggml/src/ggml.o \
  1305. $(OBJ_GGML)
  1306. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1307. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1308. #
  1309. # Deprecated binaries that we want to keep around long enough for people to migrate to the new filenames, then these can be removed.
  1310. #
  1311. # Mark legacy binary targets as .PHONY so that they are always checked.
  1312. .PHONY: main quantize perplexity embedding server
  1313. # Define the object file target
  1314. examples/deprecation-warning/deprecation-warning.o: examples/deprecation-warning/deprecation-warning.cpp
  1315. $(CXX) $(CXXFLAGS) -c $< -o $@
  1316. # NOTE: We currently will always build the deprecation-warning `main` and `server` binaries to help users migrate.
  1317. # Eventually we will want to remove these target from building all the time.
  1318. main: examples/deprecation-warning/deprecation-warning.o
  1319. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1320. @echo "NOTICE: The 'main' binary is deprecated. Please use 'llama-cli' instead."
  1321. server: examples/deprecation-warning/deprecation-warning.o
  1322. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1323. @echo "NOTICE: The 'server' binary is deprecated. Please use 'llama-server' instead."
  1324. quantize: examples/deprecation-warning/deprecation-warning.o
  1325. ifneq (,$(wildcard quantize))
  1326. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1327. @echo "#########"
  1328. @echo "WARNING: The 'quantize' binary is deprecated. Please use 'llama-quantize' instead."
  1329. @echo " Remove the 'quantize' binary to remove this warning."
  1330. @echo "#########"
  1331. endif
  1332. perplexity: examples/deprecation-warning/deprecation-warning.o
  1333. ifneq (,$(wildcard perplexity))
  1334. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1335. @echo "#########"
  1336. @echo "WARNING: The 'perplexity' binary is deprecated. Please use 'llama-perplexity' instead."
  1337. @echo " Remove the 'perplexity' binary to remove this warning."
  1338. @echo "#########"
  1339. endif
  1340. embedding: examples/deprecation-warning/deprecation-warning.o
  1341. ifneq (,$(wildcard embedding))
  1342. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1343. @echo "#########"
  1344. @echo "WARNING: The 'embedding' binary is deprecated. Please use 'llama-embedding' instead."
  1345. @echo " Remove the 'embedding' binary to remove this warning."
  1346. @echo "#########"
  1347. endif