1
0

Makefile 49 KB

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