Makefile 49 KB

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