Makefile 50 KB

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