Makefile 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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
  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. endif
  254. # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
  255. # and on macOS its availability depends on enabling Darwin extensions
  256. # similarly on DragonFly, enabling BSD extensions is necessary
  257. ifeq ($(UNAME_S),Darwin)
  258. MK_CPPFLAGS += -D_DARWIN_C_SOURCE
  259. endif
  260. ifeq ($(UNAME_S),DragonFly)
  261. MK_CPPFLAGS += -D__BSD_VISIBLE
  262. endif
  263. # alloca is a non-standard interface that is not visible on BSDs when
  264. # POSIX conformance is specified, but not all of them provide a clean way
  265. # to enable it in such cases
  266. ifeq ($(UNAME_S),FreeBSD)
  267. MK_CPPFLAGS += -D__BSD_VISIBLE
  268. endif
  269. ifeq ($(UNAME_S),NetBSD)
  270. MK_CPPFLAGS += -D_NETBSD_SOURCE
  271. endif
  272. ifeq ($(UNAME_S),OpenBSD)
  273. MK_CPPFLAGS += -D_BSD_SOURCE
  274. endif
  275. ifdef GGML_SCHED_MAX_COPIES
  276. MK_CPPFLAGS += -DGGML_SCHED_MAX_COPIES=$(GGML_SCHED_MAX_COPIES)
  277. endif
  278. ifdef LLAMA_DEBUG
  279. MK_CFLAGS += -O0 -g
  280. MK_CXXFLAGS += -O0 -g
  281. MK_LDFLAGS += -g
  282. MK_NVCCFLAGS += -O0 -g
  283. ifeq ($(UNAME_S),Linux)
  284. MK_CPPFLAGS += -D_GLIBCXX_ASSERTIONS
  285. endif
  286. else
  287. MK_CPPFLAGS += -DNDEBUG
  288. MK_CFLAGS += -O3 -g
  289. MK_CXXFLAGS += -O3 -g
  290. MK_NVCCFLAGS += -O3 -g
  291. endif
  292. ifdef LLAMA_SANITIZE_THREAD
  293. MK_CFLAGS += -fsanitize=thread -g
  294. MK_CXXFLAGS += -fsanitize=thread -g
  295. MK_LDFLAGS += -fsanitize=thread -g
  296. endif
  297. ifdef LLAMA_SANITIZE_ADDRESS
  298. MK_CFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  299. MK_CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  300. MK_LDFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  301. endif
  302. ifdef LLAMA_SANITIZE_UNDEFINED
  303. MK_CFLAGS += -fsanitize=undefined -g
  304. MK_CXXFLAGS += -fsanitize=undefined -g
  305. MK_LDFLAGS += -fsanitize=undefined -g
  306. endif
  307. ifdef LLAMA_SERVER_SSL
  308. MK_CPPFLAGS += -DCPPHTTPLIB_OPENSSL_SUPPORT
  309. MK_LDFLAGS += -lssl -lcrypto
  310. endif
  311. ifndef GGML_NO_CPU_AARCH64
  312. MK_CPPFLAGS += -DGGML_USE_CPU_AARCH64
  313. endif
  314. # warnings
  315. WARN_FLAGS = \
  316. -Wall \
  317. -Wextra \
  318. -Wpedantic \
  319. -Wcast-qual \
  320. -Wno-unused-function
  321. MK_CFLAGS += \
  322. $(WARN_FLAGS) \
  323. -Wshadow \
  324. -Wstrict-prototypes \
  325. -Wpointer-arith \
  326. -Wmissing-prototypes \
  327. -Werror=implicit-int \
  328. -Werror=implicit-function-declaration
  329. MK_CXXFLAGS += \
  330. $(WARN_FLAGS) \
  331. -Wmissing-declarations \
  332. -Wmissing-noreturn
  333. ifeq ($(LLAMA_FATAL_WARNINGS),1)
  334. MK_CFLAGS += -Werror
  335. MK_CXXFLAGS += -Werror
  336. endif
  337. # this version of Apple ld64 is buggy
  338. ifneq '' '$(findstring dyld-1015.7,$(shell $(CC) $(LDFLAGS) -Wl,-v 2>&1))'
  339. MK_CPPFLAGS += -DHAVE_BUGGY_APPLE_LINKER
  340. endif
  341. # OS specific
  342. # TODO: support Windows
  343. ifneq '' '$(filter $(UNAME_S),Linux Darwin FreeBSD NetBSD OpenBSD Haiku)'
  344. MK_CFLAGS += -pthread
  345. MK_CXXFLAGS += -pthread
  346. endif
  347. # detect Windows
  348. ifneq ($(findstring _NT,$(UNAME_S)),)
  349. _WIN32 := 1
  350. endif
  351. # library name prefix
  352. ifneq ($(_WIN32),1)
  353. LIB_PRE := lib
  354. endif
  355. # Dynamic Shared Object extension
  356. ifneq ($(_WIN32),1)
  357. DSO_EXT := .so
  358. else
  359. DSO_EXT := .dll
  360. endif
  361. # Windows Sockets 2 (Winsock) for network-capable apps
  362. ifeq ($(_WIN32),1)
  363. LWINSOCK2 := -lws2_32
  364. endif
  365. ifdef LLAMA_GPROF
  366. MK_CFLAGS += -pg
  367. MK_CXXFLAGS += -pg
  368. endif
  369. # Architecture specific
  370. # TODO: probably these flags need to be tweaked on some architectures
  371. # feel free to update the Makefile for your architecture and send a pull request or issue
  372. ifndef RISCV_CROSS_COMPILE
  373. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
  374. # Use all CPU extensions that are available:
  375. MK_CFLAGS += -march=native -mtune=native
  376. HOST_CXXFLAGS += -march=native -mtune=native
  377. # Usage AVX-only
  378. #MK_CFLAGS += -mfma -mf16c -mavx
  379. #MK_CXXFLAGS += -mfma -mf16c -mavx
  380. # Usage SSSE3-only (Not is SSE3!)
  381. #MK_CFLAGS += -mssse3
  382. #MK_CXXFLAGS += -mssse3
  383. endif
  384. ifneq '' '$(findstring mingw,$(shell $(CC) -dumpmachine))'
  385. # The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
  386. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
  387. # https://github.com/ggerganov/llama.cpp/issues/2922
  388. MK_CFLAGS += -Xassembler -muse-unaligned-vector-move
  389. MK_CXXFLAGS += -Xassembler -muse-unaligned-vector-move
  390. # Target Windows 8 for PrefetchVirtualMemory
  391. MK_CPPFLAGS += -D_WIN32_WINNT=0x602
  392. endif
  393. ifneq ($(filter aarch64%,$(UNAME_M)),)
  394. # Apple M1, M2, etc.
  395. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  396. # Nvidia Jetson
  397. MK_CFLAGS += -mcpu=native
  398. MK_CXXFLAGS += -mcpu=native
  399. JETSON_RELEASE_INFO = $(shell jetson_release)
  400. ifdef JETSON_RELEASE_INFO
  401. ifneq ($(filter TX2%,$(JETSON_RELEASE_INFO)),)
  402. JETSON_EOL_MODULE_DETECT = 1
  403. CC = aarch64-unknown-linux-gnu-gcc
  404. cxx = aarch64-unknown-linux-gnu-g++
  405. endif
  406. endif
  407. endif
  408. ifneq ($(filter armv6%,$(UNAME_M)),)
  409. # Raspberry Pi 1, Zero
  410. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  411. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  412. endif
  413. ifneq ($(filter armv7%,$(UNAME_M)),)
  414. # Raspberry Pi 2
  415. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  416. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  417. endif
  418. ifneq ($(filter armv8%,$(UNAME_M)),)
  419. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  420. MK_CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  421. MK_CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
  422. endif
  423. ifneq ($(filter ppc64%,$(UNAME_M)),)
  424. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  425. ifneq (,$(findstring POWER9,$(POWER9_M)))
  426. MK_CFLAGS += -mcpu=power9
  427. MK_CXXFLAGS += -mcpu=power9
  428. endif
  429. endif
  430. ifneq ($(filter ppc64le%,$(UNAME_M)),)
  431. MK_CFLAGS += -mcpu=powerpc64le
  432. MK_CXXFLAGS += -mcpu=powerpc64le
  433. CUDA_POWER_ARCH = 1
  434. endif
  435. ifneq ($(filter loongarch64%,$(UNAME_M)),)
  436. MK_CFLAGS += -mlasx
  437. MK_CXXFLAGS += -mlasx
  438. endif
  439. ifneq ($(filter riscv64%,$(UNAME_M)),)
  440. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  441. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  442. endif
  443. else # RISC-V CROSS COMPILATION
  444. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  445. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  446. endif
  447. ifndef GGML_NO_ACCELERATE
  448. # Mac OS - include Accelerate framework.
  449. # `-framework Accelerate` works both with Apple Silicon and Mac Intel
  450. ifeq ($(UNAME_S),Darwin)
  451. MK_CPPFLAGS += -DGGML_USE_ACCELERATE -DGGML_USE_BLAS -DGGML_BLAS_USE_ACCELERATE
  452. MK_CPPFLAGS += -DACCELERATE_NEW_LAPACK
  453. MK_CPPFLAGS += -DACCELERATE_LAPACK_ILP64
  454. MK_LDFLAGS += -framework Accelerate
  455. OBJ_GGML_EXT += ggml/src/ggml-blas/ggml-blas.o
  456. endif
  457. endif # GGML_NO_ACCELERATE
  458. ifndef GGML_NO_OPENMP
  459. MK_CPPFLAGS += -DGGML_USE_OPENMP
  460. MK_CFLAGS += -fopenmp
  461. MK_CXXFLAGS += -fopenmp
  462. endif # GGML_NO_OPENMP
  463. ifdef GGML_OPENBLAS
  464. MK_CPPFLAGS += -DGGML_USE_BLAS $(shell pkg-config --cflags-only-I openblas)
  465. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
  466. MK_LDFLAGS += $(shell pkg-config --libs openblas)
  467. OBJ_GGML_EXT += ggml/src/ggml-blas/ggml-blas.o
  468. endif # GGML_OPENBLAS
  469. ifdef GGML_OPENBLAS64
  470. MK_CPPFLAGS += -DGGML_USE_BLAS $(shell pkg-config --cflags-only-I openblas64)
  471. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas64)
  472. MK_LDFLAGS += $(shell pkg-config --libs openblas64)
  473. OBJ_GGML_EXT += ggml/src/ggml-blas/ggml-blas.o
  474. endif # GGML_OPENBLAS64
  475. ifdef GGML_BLIS
  476. MK_CPPFLAGS += -DGGML_USE_BLAS -DGGML_BLAS_USE_BLIS -I/usr/local/include/blis -I/usr/include/blis
  477. MK_LDFLAGS += -lblis -L/usr/local/lib
  478. OBJ_GGML_EXT += ggml/src/ggml-blas/ggml-blas.o
  479. endif # GGML_BLIS
  480. ifdef GGML_NVPL
  481. MK_CPPFLAGS += -DGGML_USE_BLAS -DGGML_BLAS_USE_NVPL -DNVPL_ILP64 -I/usr/local/include/nvpl_blas -I/usr/include/nvpl_blas
  482. MK_LDFLAGS += -L/usr/local/lib -lnvpl_blas_core -lnvpl_blas_ilp64_gomp
  483. OBJ_GGML_EXT += ggml/src/ggml-blas/ggml-blas.o
  484. endif # GGML_NVPL
  485. ifndef GGML_NO_LLAMAFILE
  486. MK_CPPFLAGS += -DGGML_USE_LLAMAFILE
  487. OBJ_GGML_EXT += ggml/src/ggml-cpu/llamafile/sgemm.o
  488. endif
  489. ifndef GGML_NO_AMX
  490. MK_CPPFLAGS += -DGGML_USE_AMX
  491. OBJ_GGML_EXT += ggml/src/ggml-amx/ggml-amx.o ggml/src/ggml-amx/mmq.o
  492. endif
  493. ifdef GGML_RPC
  494. MK_CPPFLAGS += -DGGML_USE_RPC
  495. OBJ_GGML_EXT += ggml/src/ggml-rpc.o
  496. endif # GGML_RPC
  497. OBJ_CUDA_TMPL = $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-wmma*.cu))
  498. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/mmq*.cu))
  499. ifdef GGML_CUDA_FA_ALL_QUANTS
  500. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*.cu))
  501. else
  502. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*q4_0-q4_0.cu))
  503. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*q8_0-q8_0.cu))
  504. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*f16-f16.cu))
  505. endif # GGML_CUDA_FA_ALL_QUANTS
  506. ifdef GGML_CUDA
  507. ifneq ('', '$(wildcard /opt/cuda)')
  508. CUDA_PATH ?= /opt/cuda
  509. else
  510. CUDA_PATH ?= /usr/local/cuda
  511. endif
  512. MK_CPPFLAGS += -DGGML_USE_CUDA -DGGML_CUDA_USE_GRAPHS -I$(CUDA_PATH)/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include
  513. 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
  514. MK_NVCCFLAGS += -use_fast_math
  515. OBJ_GGML_EXT += ggml/src/ggml-cuda/ggml-cuda.o
  516. OBJ_GGML_EXT += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  517. OBJ_GGML_EXT += $(OBJ_CUDA_TMPL)
  518. ifdef LLAMA_FATAL_WARNINGS
  519. MK_NVCCFLAGS += -Werror all-warnings
  520. endif # LLAMA_FATAL_WARNINGS
  521. ifndef JETSON_EOL_MODULE_DETECT
  522. MK_NVCCFLAGS += --forward-unknown-to-host-compiler
  523. endif # JETSON_EOL_MODULE_DETECT
  524. ifdef LLAMA_DEBUG
  525. MK_NVCCFLAGS += -lineinfo
  526. endif # LLAMA_DEBUG
  527. ifdef GGML_CUDA_DEBUG
  528. MK_NVCCFLAGS += --device-debug
  529. endif # GGML_CUDA_DEBUG
  530. ifdef GGML_CUDA_NVCC
  531. NVCC = $(CCACHE) $(GGML_CUDA_NVCC)
  532. else
  533. NVCC = $(CCACHE) nvcc
  534. endif # GGML_CUDA_NVCC
  535. ifdef CUDA_DOCKER_ARCH
  536. MK_NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  537. else ifndef CUDA_POWER_ARCH
  538. MK_NVCCFLAGS += -arch=native
  539. endif # CUDA_DOCKER_ARCH
  540. ifdef GGML_CUDA_FORCE_DMMV
  541. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  542. endif # GGML_CUDA_FORCE_DMMV
  543. ifdef GGML_CUDA_FORCE_MMQ
  544. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  545. endif # GGML_CUDA_FORCE_MMQ
  546. ifdef GGML_CUDA_FORCE_CUBLAS
  547. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  548. endif # GGML_CUDA_FORCE_CUBLAS
  549. ifdef GGML_CUDA_DMMV_X
  550. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  551. else
  552. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  553. endif # GGML_CUDA_DMMV_X
  554. ifdef GGML_CUDA_MMV_Y
  555. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  556. else ifdef GGML_CUDA_DMMV_Y
  557. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_DMMV_Y) # for backwards compatibility
  558. else
  559. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  560. endif # GGML_CUDA_MMV_Y
  561. ifdef GGML_CUDA_F16
  562. MK_NVCCFLAGS += -DGGML_CUDA_F16
  563. endif # GGML_CUDA_F16
  564. ifdef GGML_CUDA_DMMV_F16
  565. MK_NVCCFLAGS += -DGGML_CUDA_F16
  566. endif # GGML_CUDA_DMMV_F16
  567. ifdef GGML_CUDA_KQUANTS_ITER
  568. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  569. else
  570. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  571. endif
  572. ifdef GGML_CUDA_PEER_MAX_BATCH_SIZE
  573. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(GGML_CUDA_PEER_MAX_BATCH_SIZE)
  574. else
  575. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  576. endif # GGML_CUDA_PEER_MAX_BATCH_SIZE
  577. ifdef GGML_CUDA_NO_PEER_COPY
  578. MK_NVCCFLAGS += -DGGML_CUDA_NO_PEER_COPY
  579. endif # GGML_CUDA_NO_PEER_COPY
  580. ifdef GGML_CUDA_CCBIN
  581. MK_NVCCFLAGS += -ccbin $(GGML_CUDA_CCBIN)
  582. endif # GGML_CUDA_CCBIN
  583. ifdef GGML_CUDA_FA_ALL_QUANTS
  584. MK_NVCCFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  585. endif # GGML_CUDA_FA_ALL_QUANTS
  586. ifdef JETSON_EOL_MODULE_DETECT
  587. define NVCC_COMPILE
  588. $(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 $@
  589. endef # NVCC_COMPILE
  590. else
  591. define NVCC_COMPILE
  592. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  593. endef # NVCC_COMPILE
  594. endif # JETSON_EOL_MODULE_DETECT
  595. ggml/src/ggml-cuda/%.o: \
  596. ggml/src/ggml-cuda/%.cu \
  597. ggml/include/ggml.h \
  598. ggml/src/ggml-common.h \
  599. ggml/src/ggml-cuda/common.cuh
  600. $(NVCC_COMPILE)
  601. ggml/src/ggml-cuda/ggml-cuda.o: \
  602. ggml/src/ggml-cuda/ggml-cuda.cu \
  603. ggml/include/ggml-cuda.h \
  604. ggml/include/ggml.h \
  605. ggml/include/ggml-backend.h \
  606. ggml/src/ggml-backend-impl.h \
  607. ggml/src/ggml-common.h \
  608. $(wildcard ggml/src/ggml-cuda/*.cuh)
  609. $(NVCC_COMPILE)
  610. endif # GGML_CUDA
  611. ifdef GGML_VULKAN
  612. MK_CPPFLAGS += -DGGML_USE_VULKAN
  613. MK_LDFLAGS += $(shell pkg-config --libs vulkan)
  614. OBJ_GGML_EXT += ggml/src/ggml-vulkan.o ggml/src/ggml-vulkan-shaders.o
  615. ifdef GGML_VULKAN_CHECK_RESULTS
  616. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  617. endif
  618. ifdef GGML_VULKAN_DEBUG
  619. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  620. endif
  621. ifdef GGML_VULKAN_MEMORY_DEBUG
  622. MK_CPPFLAGS += -DGGML_VULKAN_MEMORY_DEBUG
  623. endif
  624. ifdef GGML_VULKAN_PERF
  625. MK_CPPFLAGS += -DGGML_VULKAN_PERF
  626. endif
  627. ifdef GGML_VULKAN_VALIDATE
  628. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  629. endif
  630. ifdef GGML_VULKAN_RUN_TESTS
  631. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  632. endif
  633. GLSLC_CMD = glslc
  634. _ggml_vk_genshaders_cmd = $(shell pwd)/vulkan-shaders-gen
  635. _ggml_vk_header = ggml/src/ggml-vulkan-shaders.hpp
  636. _ggml_vk_source = ggml/src/ggml-vulkan-shaders.cpp
  637. _ggml_vk_input_dir = ggml/src/vulkan-shaders
  638. _ggml_vk_shader_deps = $(echo $(_ggml_vk_input_dir)/*.comp)
  639. ggml/src/ggml-vulkan.o: ggml/src/ggml-vulkan.cpp ggml/include/ggml-vulkan.h $(_ggml_vk_header) $(_ggml_vk_source)
  640. $(CXX) $(CXXFLAGS) $(shell pkg-config --cflags vulkan) -c $< -o $@
  641. $(_ggml_vk_header): $(_ggml_vk_source)
  642. $(_ggml_vk_source): $(_ggml_vk_shader_deps) vulkan-shaders-gen
  643. $(_ggml_vk_genshaders_cmd) \
  644. --glslc $(GLSLC_CMD) \
  645. --input-dir $(_ggml_vk_input_dir) \
  646. --target-hpp $(_ggml_vk_header) \
  647. --target-cpp $(_ggml_vk_source)
  648. vulkan-shaders-gen: ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
  649. $(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
  650. endif # GGML_VULKAN
  651. ifdef GGML_HIPBLAS
  652. ifeq ($(wildcard /opt/rocm),)
  653. ROCM_PATH ?= /usr
  654. AMDGPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  655. else
  656. ROCM_PATH ?= /opt/rocm
  657. AMDGPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  658. endif
  659. GGML_CUDA_DMMV_X ?= 32
  660. GGML_CUDA_MMV_Y ?= 1
  661. GGML_CUDA_KQUANTS_ITER ?= 2
  662. MK_CPPFLAGS += -DGGML_USE_HIP -DGGML_USE_CUDA
  663. ifdef GGML_HIP_UMA
  664. MK_CPPFLAGS += -DGGML_HIP_UMA
  665. endif # GGML_HIP_UMA
  666. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  667. MK_LDFLAGS += -L$(ROCM_PATH)/lib64 -Wl,-rpath=$(ROCM_PATH)/lib64
  668. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  669. HIPCC ?= $(CCACHE) $(ROCM_PATH)/bin/hipcc
  670. HIPFLAGS += $(addprefix --offload-arch=,$(AMDGPU_TARGETS))
  671. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  672. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  673. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  674. ifdef GGML_CUDA_FORCE_DMMV
  675. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  676. endif # GGML_CUDA_FORCE_DMMV
  677. ifdef GGML_CUDA_FORCE_MMQ
  678. HIPFLAGS += -DGGML_CUDA_FORCE_MMQ
  679. endif # GGML_CUDA_FORCE_MMQ
  680. ifdef GGML_CUDA_FORCE_CUBLAS
  681. HIPFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  682. endif # GGML_CUDA_FORCE_CUBLAS
  683. ifdef GGML_CUDA_NO_PEER_COPY
  684. HIPFLAGS += -DGGML_CUDA_NO_PEER_COPY
  685. endif # GGML_CUDA_NO_PEER_COPY
  686. OBJ_GGML_EXT += ggml/src/ggml-cuda/ggml-cuda.o
  687. OBJ_GGML_EXT += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  688. OBJ_GGML_EXT += $(OBJ_CUDA_TMPL)
  689. ggml/src/ggml-cuda/ggml-cuda.o: \
  690. ggml/src/ggml-cuda/ggml-cuda.cu \
  691. ggml/include/ggml-cuda.h \
  692. ggml/include/ggml.h \
  693. ggml/include/ggml-backend.h \
  694. ggml/src/ggml-backend-impl.h \
  695. ggml/src/ggml-common.h \
  696. $(wildcard ggml/src/ggml-cuda/*.cuh)
  697. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  698. ggml/src/ggml-cuda/%.o: \
  699. ggml/src/ggml-cuda/%.cu \
  700. ggml/include/ggml.h \
  701. ggml/src/ggml-common.h \
  702. ggml/src/ggml-cuda/common.cuh
  703. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  704. endif # GGML_HIPBLAS
  705. ifdef GGML_MUSA
  706. ifeq ($(wildcard /opt/musa),)
  707. MUSA_PATH ?= /usr/local/musa
  708. else
  709. MUSA_PATH ?= /opt/musa
  710. endif
  711. MTGPU_TARGETS ?= mp_21 mp_22
  712. MK_CPPFLAGS += -DGGML_USE_MUSA -DGGML_USE_CUDA
  713. MK_LDFLAGS += -L$(MUSA_PATH)/lib -Wl,-rpath=$(MUSA_PATH)/lib
  714. MK_LDFLAGS += -lmusa -lmusart -lmublas
  715. ifndef GGML_NO_OPENMP
  716. # For Ubuntu Focal
  717. MK_CPPFLAGS += -I/usr/lib/llvm-10/include/openmp
  718. MK_LDFLAGS += -L/usr/lib/llvm-10/lib
  719. # For Ubuntu Jammy
  720. MK_CPPFLAGS += -I/usr/lib/llvm-14/lib/clang/14.0.0/include
  721. MK_LDFLAGS += -L/usr/lib/llvm-14/lib
  722. endif # GGML_NO_OPENMP
  723. CC := $(MUSA_PATH)/bin/clang
  724. CXX := $(MUSA_PATH)/bin/clang++
  725. MCC := $(CCACHE) $(MUSA_PATH)/bin/mcc
  726. MUSAFLAGS += $(addprefix --cuda-gpu-arch=, $(MTGPU_TARGETS))
  727. ifdef GGML_CUDA_FORCE_DMMV
  728. MUSAFLAGS += -DGGML_CUDA_FORCE_DMMV
  729. endif # GGML_CUDA_FORCE_DMMV
  730. ifdef GGML_CUDA_FORCE_MMQ
  731. MUSAFLAGS += -DGGML_CUDA_FORCE_MMQ
  732. endif # GGML_CUDA_FORCE_MMQ
  733. ifdef GGML_CUDA_FORCE_CUBLAS
  734. MUSAFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  735. endif # GGML_CUDA_FORCE_CUBLAS
  736. ifdef GGML_CUDA_DMMV_X
  737. MUSAFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  738. else
  739. MUSAFLAGS += -DGGML_CUDA_DMMV_X=32
  740. endif # GGML_CUDA_DMMV_X
  741. ifdef GGML_CUDA_MMV_Y
  742. MUSAFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  743. else
  744. MUSAFLAGS += -DGGML_CUDA_MMV_Y=1
  745. endif # GGML_CUDA_MMV_Y
  746. ifdef GGML_CUDA_F16
  747. MUSAFLAGS += -DGGML_CUDA_F16
  748. endif # GGML_CUDA_F16
  749. ifdef GGML_CUDA_DMMV_F16
  750. MUSAFLAGS += -DGGML_CUDA_F16
  751. endif # GGML_CUDA_DMMV_F16
  752. ifdef GGML_CUDA_KQUANTS_ITER
  753. MUSAFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  754. else
  755. MUSAFLAGS += -DK_QUANTS_PER_ITERATION=2
  756. endif
  757. ifdef GGML_CUDA_PEER_MAX_BATCH_SIZE
  758. MUSAFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(GGML_CUDA_PEER_MAX_BATCH_SIZE)
  759. else
  760. MUSAFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  761. endif # GGML_CUDA_PEER_MAX_BATCH_SIZE
  762. ifdef GGML_CUDA_NO_PEER_COPY
  763. MUSAFLAGS += -DGGML_CUDA_NO_PEER_COPY
  764. endif # GGML_CUDA_NO_PEER_COPY
  765. ifdef GGML_CUDA_FA_ALL_QUANTS
  766. MUSAFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  767. endif # GGML_CUDA_FA_ALL_QUANTS
  768. OBJ_GGML_EXT += ggml/src/ggml-cuda/ggml-cuda.o
  769. OBJ_GGML_EXT += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  770. OBJ_GGML_EXT += $(OBJ_CUDA_TMPL)
  771. ggml/src/ggml-cuda/ggml-cuda.o: \
  772. ggml/src/ggml-cuda/ggml-cuda.cu \
  773. ggml/include/ggml-cuda.h \
  774. ggml/include/ggml.h \
  775. ggml/include/ggml-backend.h \
  776. ggml/src/ggml-backend-impl.h \
  777. ggml/src/ggml-common.h \
  778. $(wildcard ggml/src/ggml-cuda/*.cuh)
  779. $(MCC) $(CXXFLAGS) $(MUSAFLAGS) -x musa -mtgpu -c -o $@ $<
  780. ggml/src/ggml-cuda/%.o: \
  781. ggml/src/ggml-cuda/%.cu \
  782. ggml/include/ggml.h \
  783. ggml/src/ggml-common.h \
  784. ggml/src/ggml-cuda/common.cuh
  785. $(MCC) $(CXXFLAGS) $(MUSAFLAGS) -x musa -mtgpu -c -o $@ $<
  786. endif # GGML_MUSA
  787. ifdef GGML_METAL
  788. MK_CPPFLAGS += -DGGML_USE_METAL
  789. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  790. OBJ_GGML_EXT += ggml/src/ggml-metal/ggml-metal.o
  791. ifdef GGML_METAL_USE_BF16
  792. MK_CPPFLAGS += -DGGML_METAL_USE_BF16
  793. endif # GGML_METAL_USE_BF16
  794. ifdef GGML_METAL_NDEBUG
  795. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  796. endif
  797. ifdef GGML_METAL_EMBED_LIBRARY
  798. MK_CPPFLAGS += -DGGML_METAL_EMBED_LIBRARY
  799. OBJ_GGML_EXT += ggml/src/ggml-metal-embed.o
  800. endif
  801. endif # GGML_METAL
  802. ifdef GGML_METAL
  803. ggml/src/ggml-metal/ggml-metal.o: \
  804. ggml/src/ggml-metal/ggml-metal.m \
  805. ggml/include/ggml-metal.h \
  806. ggml/include/ggml.h
  807. $(CC) $(CFLAGS) -c $< -o $@
  808. ifdef GGML_METAL_EMBED_LIBRARY
  809. ggml/src/ggml-metal-embed.o: \
  810. ggml/src/ggml-metal/ggml-metal.metal \
  811. ggml/src/ggml-common.h
  812. @echo "Embedding Metal library"
  813. @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
  814. $(eval TEMP_ASSEMBLY=$(shell mktemp -d))
  815. @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  816. @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  817. @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  818. @echo ".incbin \"ggml/src/ggml-metal/ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  819. @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  820. @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  821. $(CC) $(CFLAGS) -c $(TEMP_ASSEMBLY)/ggml-metal-embed.s -o $@
  822. @rm -f ${TEMP_ASSEMBLY}/ggml-metal-embed.s
  823. @rmdir ${TEMP_ASSEMBLY}
  824. endif
  825. endif # GGML_METAL
  826. DIR_GGML = ggml
  827. DIR_LLAMA = src
  828. DIR_COMMON = common
  829. OBJ_GGML = \
  830. $(DIR_GGML)/src/ggml.o \
  831. $(DIR_GGML)/src/ggml-aarch64.o \
  832. $(DIR_GGML)/src/ggml-alloc.o \
  833. $(DIR_GGML)/src/ggml-backend.o \
  834. $(DIR_GGML)/src/ggml-backend-reg.o \
  835. $(DIR_GGML)/src/ggml-quants.o \
  836. $(DIR_GGML)/src/ggml-threading.o \
  837. $(DIR_GGML)/src/ggml-cpu/ggml-cpu.o \
  838. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-cpp.o \
  839. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-aarch64.o \
  840. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-quants.o \
  841. $(OBJ_GGML_EXT)
  842. OBJ_LLAMA = \
  843. $(DIR_LLAMA)/llama.o \
  844. $(DIR_LLAMA)/llama-vocab.o \
  845. $(DIR_LLAMA)/llama-grammar.o \
  846. $(DIR_LLAMA)/llama-sampling.o \
  847. $(DIR_LLAMA)/unicode.o \
  848. $(DIR_LLAMA)/unicode-data.o
  849. OBJ_COMMON = \
  850. $(DIR_COMMON)/common.o \
  851. $(DIR_COMMON)/arg.o \
  852. $(DIR_COMMON)/log.o \
  853. $(DIR_COMMON)/console.o \
  854. $(DIR_COMMON)/ngram-cache.o \
  855. $(DIR_COMMON)/sampling.o \
  856. $(DIR_COMMON)/build-info.o \
  857. $(DIR_COMMON)/json-schema-to-grammar.o
  858. OBJ_ALL = $(OBJ_GGML) $(OBJ_LLAMA) $(OBJ_COMMON)
  859. LIB_GGML = $(LIB_PRE)ggml$(DSO_EXT)
  860. LIB_GGML_S = $(LIB_PRE)ggml.a
  861. LIB_LLAMA = $(LIB_PRE)llama$(DSO_EXT)
  862. LIB_LLAMA_S = $(LIB_PRE)llama.a
  863. LIB_COMMON = $(LIB_PRE)common$(DSO_EXT)
  864. LIB_COMMON_S = $(LIB_PRE)common.a
  865. LIB_ALL = $(LIB_GGML) $(LIB_LLAMA) $(LIB_COMMON)
  866. LIB_ALL_S = $(LIB_GGML_S) $(LIB_LLAMA_S) $(LIB_COMMON_S)
  867. GF_CC := $(CC)
  868. include scripts/get-flags.mk
  869. # combine build flags with cmdline overrides
  870. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  871. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  872. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  873. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  874. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  875. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  876. # identify CUDA host compiler
  877. ifdef GGML_CUDA
  878. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  879. include scripts/get-flags.mk
  880. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  881. endif
  882. ifdef LLAMA_CURL
  883. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  884. override LDFLAGS := $(LDFLAGS) -lcurl
  885. endif
  886. #
  887. # Print build information
  888. #
  889. $(info I llama.cpp build info: )
  890. $(info I UNAME_S: $(UNAME_S))
  891. $(info I UNAME_P: $(UNAME_P))
  892. $(info I UNAME_M: $(UNAME_M))
  893. $(info I CFLAGS: $(CFLAGS))
  894. $(info I CXXFLAGS: $(CXXFLAGS))
  895. $(info I NVCCFLAGS: $(NVCCFLAGS))
  896. $(info I LDFLAGS: $(LDFLAGS))
  897. $(info I CC: $(shell $(CC) --version | head -n 1))
  898. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  899. ifdef GGML_CUDA
  900. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  901. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  902. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  903. ifndef CUDA_DOCKER_ARCH
  904. ifndef CUDA_POWER_ARCH
  905. $(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 )
  906. endif # CUDA_POWER_ARCH
  907. endif # CUDA_DOCKER_ARCH
  908. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  909. endif # GGML_CUDA
  910. $(info )
  911. ifdef DEPRECATE_WARNING
  912. $(info !!! DEPRECATION WARNING !!!)
  913. $(info The following LLAMA_ options are deprecated and will be removed in the future. Use the GGML_ prefix instead)
  914. $(info - LLAMA_CUDA)
  915. $(info - LLAMA_METAL)
  916. $(info - LLAMA_METAL_EMBED_LIBRARY)
  917. $(info - LLAMA_OPENMP)
  918. $(info - LLAMA_RPC)
  919. $(info - LLAMA_SYCL)
  920. $(info - LLAMA_SYCL_F16)
  921. $(info - LLAMA_OPENBLAS)
  922. $(info - LLAMA_OPENBLAS64)
  923. $(info - LLAMA_BLIS)
  924. $(info - LLAMA_NO_LLAMAFILE)
  925. $(info - LLAMA_NO_ACCELERATE)
  926. $(info - LLAMA_NO_OPENMP)
  927. $(info - LLAMA_NO_METAL)
  928. $(info - LLAMA_NO_CCACHE)
  929. $(info )
  930. endif
  931. ifdef REMOVE_WARNING
  932. $(info !!! REMOVAL WARNING !!!)
  933. $(info The following LLAMA_ options have been removed and are no longer supported)
  934. $(info - LLAMA_DISABLE_LOGS (https://github.com/ggerganov/llama.cpp/pull/9418))
  935. $(info - LLAMA_SERVER_VERBOSE (https://github.com/ggerganov/llama.cpp/pull/9418))
  936. $(info )
  937. endif
  938. #
  939. # Build libraries
  940. #
  941. # Libraries
  942. LIB_GGML = libggml.so
  943. LIB_GGML_S = libggml.a
  944. LIB_LLAMA = libllama.so
  945. LIB_LLAMA_S = libllama.a
  946. LIB_COMMON = libcommon.so
  947. LIB_COMMON_S = libcommon.a
  948. # Targets
  949. BUILD_TARGETS += $(LIB_GGML) $(LIB_GGML_S) $(LIB_LLAMA) $(LIB_LLAMA_S) $(LIB_COMMON) $(LIB_COMMON_S)
  950. # Dependency files
  951. DEP_FILES = $(OBJ_GGML:.o=.d) $(OBJ_LLAMA:.o=.d) $(OBJ_COMMON:.o=.d)
  952. # Default target
  953. all: $(BUILD_TARGETS)
  954. # Note: need this exception because `ggml-cpu.c` and `ggml-cpu.cpp` both produce the same obj/dep files
  955. # g++ -M -I ./ggml/include/ -I ./ggml/src ggml/src/ggml-cpu/ggml-cpu.cpp | grep ggml
  956. $(DIR_GGML)/src/ggml-cpu/ggml-cpu-cpp.o: \
  957. ggml/src/ggml-cpu/ggml-cpu.cpp \
  958. ggml/include/ggml-backend.h \
  959. ggml/include/ggml.h \
  960. ggml/include/ggml-alloc.h \
  961. ggml/src/ggml-backend-impl.h \
  962. ggml/include/ggml-cpu.h \
  963. ggml/src/ggml-impl.h
  964. $(CXX) $(CXXFLAGS) -c $< -o $@
  965. # Rules for building object files
  966. $(DIR_GGML)/%.o: $(DIR_GGML)/%.c
  967. $(CC) $(CFLAGS) -MMD -c $< -o $@
  968. $(DIR_GGML)/%.o: $(DIR_GGML)/%.cpp
  969. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  970. $(DIR_LLAMA)/%.o: $(DIR_LLAMA)/%.cpp
  971. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  972. $(DIR_COMMON)/%.o: $(DIR_COMMON)/%.cpp
  973. $(CXX) $(CXXFLAGS) -MMD -c $< -o $@
  974. # Rules for building libraries
  975. $(LIB_GGML): $(OBJ_GGML)
  976. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  977. $(LIB_GGML_S): $(OBJ_GGML)
  978. ar rcs $(LIB_GGML_S) $^
  979. $(LIB_LLAMA): $(OBJ_LLAMA) $(LIB_GGML)
  980. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  981. $(LIB_LLAMA_S): $(OBJ_LLAMA)
  982. ar rcs $(LIB_LLAMA_S) $^
  983. $(LIB_COMMON): $(OBJ_COMMON) $(LIB_LLAMA) $(LIB_GGML)
  984. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  985. $(LIB_COMMON_S): $(OBJ_COMMON)
  986. ar rcs $(LIB_COMMON_S) $^
  987. # Include dependency files
  988. -include $(DEP_FILES)
  989. # Clean rule
  990. clean:
  991. rm -vrf $(BUILD_TARGETS) $(TEST_TARGETS)
  992. rm -rvf *.a *.dll *.so *.dot
  993. find ggml src common tests examples pocs -type f -name "*.o" -delete
  994. find ggml src common tests examples pocs -type f -name "*.d" -delete
  995. #
  996. # Examples
  997. #
  998. # $< is the first prerequisite, i.e. the source file.
  999. # Explicitly compile this to an object file so that it can be cached with ccache.
  1000. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  1001. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  1002. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  1003. llama-cli: examples/main/main.cpp \
  1004. $(OBJ_ALL)
  1005. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1006. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1007. @echo
  1008. @echo '==== Run ./llama-cli -h for help. ===='
  1009. @echo
  1010. llama-infill: examples/infill/infill.cpp \
  1011. $(OBJ_ALL)
  1012. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1013. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1014. llama-simple: examples/simple/simple.cpp \
  1015. $(OBJ_ALL)
  1016. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1017. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1018. llama-simple-chat: examples/simple-chat/simple-chat.cpp \
  1019. $(OBJ_ALL)
  1020. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1021. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1022. llama-tokenize: examples/tokenize/tokenize.cpp \
  1023. $(OBJ_ALL)
  1024. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1025. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1026. llama-batched: examples/batched/batched.cpp \
  1027. $(OBJ_ALL)
  1028. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1029. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1030. llama-batched-bench: examples/batched-bench/batched-bench.cpp \
  1031. $(OBJ_ALL)
  1032. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1033. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1034. llama-quantize: examples/quantize/quantize.cpp \
  1035. $(OBJ_ALL)
  1036. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1037. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1038. llama-quantize-stats: examples/quantize-stats/quantize-stats.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-perplexity: examples/perplexity/perplexity.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-imatrix: examples/imatrix/imatrix.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-embedding: examples/embedding/embedding.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-gritlm: examples/gritlm/gritlm.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-save-load-state: examples/save-load-state/save-load-state.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-gguf: examples/gguf/gguf.cpp \
  1063. $(OBJ_GGML)
  1064. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1065. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1066. examples/gguf-hash/deps/sha1/sha1.o: \
  1067. examples/gguf-hash/deps/sha1/sha1.c
  1068. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1069. examples/gguf-hash/deps/xxhash/xxhash.o: \
  1070. examples/gguf-hash/deps/xxhash/xxhash.c
  1071. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1072. examples/gguf-hash/deps/sha256/sha256.o: \
  1073. examples/gguf-hash/deps/sha256/sha256.c
  1074. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1075. 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\
  1076. $(OBJ_ALL)
  1077. $(CXX) $(CXXFLAGS) -Iexamples/gguf-hash/deps -c $< -o $(call GET_OBJ_FILE, $<)
  1078. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1079. llama-gguf-split: examples/gguf-split/gguf-split.cpp \
  1080. $(OBJ_ALL)
  1081. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1082. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1083. llama-eval-callback: examples/eval-callback/eval-callback.cpp \
  1084. $(OBJ_ALL)
  1085. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1086. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1087. llama-cvector-generator: examples/cvector-generator/cvector-generator.cpp \
  1088. $(OBJ_ALL)
  1089. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1090. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1091. llama-convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp \
  1092. $(OBJ_ALL)
  1093. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1094. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1095. llama-bench: examples/llama-bench/llama-bench.cpp \
  1096. $(OBJ_ALL)
  1097. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1098. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1099. llama-export-lora: examples/export-lora/export-lora.cpp \
  1100. $(OBJ_ALL)
  1101. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1102. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1103. llama-retrieval: examples/retrieval/retrieval.cpp \
  1104. $(OBJ_ALL)
  1105. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1106. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1107. llama-speculative: examples/speculative/speculative.cpp \
  1108. $(OBJ_ALL)
  1109. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1110. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1111. llama-parallel: examples/parallel/parallel.cpp \
  1112. $(OBJ_ALL)
  1113. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1114. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1115. llama-lookahead: examples/lookahead/lookahead.cpp \
  1116. $(OBJ_ALL)
  1117. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1118. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1119. llama-lookup: examples/lookup/lookup.cpp \
  1120. $(OBJ_ALL)
  1121. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1122. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1123. llama-lookup-create: examples/lookup/lookup-create.cpp \
  1124. $(OBJ_ALL)
  1125. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1126. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1127. llama-lookup-merge: examples/lookup/lookup-merge.cpp \
  1128. $(OBJ_ALL)
  1129. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1130. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1131. llama-lookup-stats: examples/lookup/lookup-stats.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. llama-passkey: examples/passkey/passkey.cpp \
  1136. $(OBJ_ALL)
  1137. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1138. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1139. llama-gbnf-validator: examples/gbnf-validator/gbnf-validator.cpp \
  1140. $(OBJ_ALL)
  1141. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1142. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1143. ifdef GGML_RPC
  1144. rpc-server: examples/rpc/rpc-server.cpp \
  1145. $(OBJ_GGML)
  1146. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  1147. endif # GGML_RPC
  1148. llama-server: \
  1149. examples/server/server.cpp \
  1150. examples/server/utils.hpp \
  1151. examples/server/httplib.h \
  1152. examples/server/index.html.hpp \
  1153. examples/server/completion.js.hpp \
  1154. examples/server/loading.html.hpp \
  1155. examples/server/deps_daisyui.min.css.hpp \
  1156. examples/server/deps_markdown-it.js.hpp \
  1157. examples/server/deps_tailwindcss.js.hpp \
  1158. examples/server/deps_vue.esm-browser.js.hpp \
  1159. common/json.hpp \
  1160. common/stb_image.h \
  1161. $(OBJ_ALL)
  1162. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1163. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  1164. # Portable equivalent of `cd examples/server/public && xxd -i $(notdir $<) ../$(notdir $<).hpp`:
  1165. examples/server/%.hpp: examples/server/public/% Makefile
  1166. @( export NAME=$(subst .,_,$(subst -,_,$(notdir $<))) && \
  1167. echo "unsigned char $${NAME}[] = {" && \
  1168. cat $< | od -v -t x1 -An | sed -E 's/([0-9a-fA-F]+)/0x\1, /g' && \
  1169. echo "};" && \
  1170. echo "unsigned int $${NAME}_len = $(shell cat $< | wc -c );" \
  1171. ) > $@
  1172. llama-gen-docs: examples/gen-docs/gen-docs.cpp \
  1173. $(OBJ_ALL)
  1174. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1175. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1176. libllava.a: examples/llava/llava.cpp \
  1177. examples/llava/llava.h \
  1178. examples/llava/clip.cpp \
  1179. examples/llava/clip.h \
  1180. common/stb_image.h \
  1181. common/base64.hpp \
  1182. $(OBJ_ALL)
  1183. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  1184. llama-llava-cli: examples/llava/llava-cli.cpp \
  1185. examples/llava/llava.cpp \
  1186. examples/llava/llava.h \
  1187. examples/llava/clip.cpp \
  1188. examples/llava/clip.h \
  1189. $(OBJ_ALL)
  1190. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1191. llama-minicpmv-cli: examples/llava/minicpmv-cli.cpp \
  1192. examples/llava/llava.cpp \
  1193. examples/llava/llava.h \
  1194. examples/llava/clip.cpp \
  1195. examples/llava/clip.h \
  1196. $(OBJ_ALL)
  1197. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1198. ifeq ($(UNAME_S),Darwin)
  1199. swift: examples/batched.swift
  1200. (cd examples/batched.swift; make build)
  1201. endif
  1202. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  1203. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  1204. @if ! cmp -s $@.tmp $@; then \
  1205. mv $@.tmp $@; \
  1206. else \
  1207. rm $@.tmp; \
  1208. fi
  1209. common/build-info.o: common/build-info.cpp
  1210. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  1211. #
  1212. # Tests
  1213. #
  1214. tests: $(TEST_TARGETS)
  1215. tests/test-arg-parser: tests/test-arg-parser.cpp \
  1216. $(OBJ_ALL)
  1217. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1218. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1219. tests/test-llama-grammar: tests/test-llama-grammar.cpp \
  1220. $(OBJ_ALL)
  1221. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1222. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1223. tests/test-log: tests/test-log.cpp \
  1224. $(OBJ_ALL)
  1225. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1226. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1227. tests/test-grammar-parser: tests/test-grammar-parser.cpp \
  1228. $(OBJ_ALL)
  1229. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1230. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1231. tests/test-grammar-integration: tests/test-grammar-integration.cpp \
  1232. $(OBJ_ALL)
  1233. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1234. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1235. tests/test-double-float: tests/test-double-float.cpp
  1236. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1237. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1238. tests/test-json-schema-to-grammar: tests/test-json-schema-to-grammar.cpp \
  1239. $(OBJ_ALL)
  1240. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  1241. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1242. tests/test-opt: tests/test-opt.cpp \
  1243. $(OBJ_GGML)
  1244. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1245. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1246. tests/test-quantize-fns: tests/test-quantize-fns.cpp \
  1247. $(OBJ_GGML)
  1248. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1249. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1250. tests/test-quantize-perf: tests/test-quantize-perf.cpp \
  1251. $(OBJ_GGML)
  1252. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1253. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1254. tests/test-sampling: tests/test-sampling.cpp \
  1255. $(OBJ_ALL)
  1256. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1257. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1258. tests/test-tokenizer-0: tests/test-tokenizer-0.cpp \
  1259. $(OBJ_ALL)
  1260. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1261. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1262. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp \
  1263. $(OBJ_ALL)
  1264. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1265. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1266. tests/test-tokenizer-1-spm: tests/test-tokenizer-1-spm.cpp \
  1267. $(OBJ_ALL)
  1268. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1269. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1270. tests/test-rope: tests/test-rope.cpp ggml/src/ggml.o \
  1271. $(OBJ_GGML)
  1272. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1273. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1274. tests/test-c.o: tests/test-c.c include/llama.h
  1275. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  1276. tests/test-backend-ops: tests/test-backend-ops.cpp \
  1277. $(OBJ_GGML)
  1278. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1279. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1280. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp tests/get-model.cpp \
  1281. $(OBJ_ALL)
  1282. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1283. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1284. tests/test-autorelease: tests/test-autorelease.cpp tests/get-model.cpp \
  1285. $(OBJ_ALL)
  1286. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1287. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1288. tests/test-chat-template: tests/test-chat-template.cpp \
  1289. $(OBJ_ALL)
  1290. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1291. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1292. #
  1293. # PoCs
  1294. #
  1295. llama-vdot: pocs/vdot/vdot.cpp ggml/src/ggml.o \
  1296. $(OBJ_GGML)
  1297. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1298. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1299. llama-q8dot: pocs/vdot/q8dot.cpp ggml/src/ggml.o \
  1300. $(OBJ_GGML)
  1301. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1302. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1303. #
  1304. # Deprecated binaries that we want to keep around long enough for people to migrate to the new filenames, then these can be removed.
  1305. #
  1306. # Mark legacy binary targets as .PHONY so that they are always checked.
  1307. .PHONY: main quantize perplexity embedding server
  1308. # Define the object file target
  1309. examples/deprecation-warning/deprecation-warning.o: examples/deprecation-warning/deprecation-warning.cpp
  1310. $(CXX) $(CXXFLAGS) -c $< -o $@
  1311. # NOTE: We currently will always build the deprecation-warning `main` and `server` binaries to help users migrate.
  1312. # Eventually we will want to remove these target from building all the time.
  1313. main: examples/deprecation-warning/deprecation-warning.o
  1314. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1315. @echo "NOTICE: The 'main' binary is deprecated. Please use 'llama-cli' instead."
  1316. server: examples/deprecation-warning/deprecation-warning.o
  1317. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1318. @echo "NOTICE: The 'server' binary is deprecated. Please use 'llama-server' instead."
  1319. quantize: examples/deprecation-warning/deprecation-warning.o
  1320. ifneq (,$(wildcard quantize))
  1321. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1322. @echo "#########"
  1323. @echo "WARNING: The 'quantize' binary is deprecated. Please use 'llama-quantize' instead."
  1324. @echo " Remove the 'quantize' binary to remove this warning."
  1325. @echo "#########"
  1326. endif
  1327. perplexity: examples/deprecation-warning/deprecation-warning.o
  1328. ifneq (,$(wildcard perplexity))
  1329. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1330. @echo "#########"
  1331. @echo "WARNING: The 'perplexity' binary is deprecated. Please use 'llama-perplexity' instead."
  1332. @echo " Remove the 'perplexity' binary to remove this warning."
  1333. @echo "#########"
  1334. endif
  1335. embedding: examples/deprecation-warning/deprecation-warning.o
  1336. ifneq (,$(wildcard embedding))
  1337. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1338. @echo "#########"
  1339. @echo "WARNING: The 'embedding' binary is deprecated. Please use 'llama-embedding' instead."
  1340. @echo " Remove the 'embedding' binary to remove this warning."
  1341. @echo "#########"
  1342. endif