Makefile 48 KB

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