Makefile 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  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 -g
  285. MK_CXXFLAGS += -O3 -g
  286. MK_NVCCFLAGS += -O3 -g
  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. ifdef GGML_MUSA
  454. CC := clang
  455. CXX := clang++
  456. GGML_CUDA := 1
  457. MK_CPPFLAGS += -DGGML_USE_MUSA
  458. endif
  459. ifndef GGML_NO_OPENMP
  460. MK_CPPFLAGS += -DGGML_USE_OPENMP
  461. MK_CFLAGS += -fopenmp
  462. MK_CXXFLAGS += -fopenmp
  463. ifdef GGML_MUSA
  464. MK_CPPFLAGS += -I/usr/lib/llvm-10/include/openmp
  465. MK_LDFLAGS += -L/usr/lib/llvm-10/lib
  466. endif # GGML_MUSA
  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 += ggml/src/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 += ggml/src/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 += ggml/src/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 += ggml/src/ggml-blas.o
  489. endif # GGML_NVPL
  490. ifndef GGML_NO_LLAMAFILE
  491. MK_CPPFLAGS += -DGGML_USE_LLAMAFILE
  492. OBJ_GGML += ggml/src/llamafile/sgemm.o
  493. endif
  494. ifdef GGML_RPC
  495. MK_CPPFLAGS += -DGGML_USE_RPC
  496. OBJ_GGML += ggml/src/ggml-rpc.o
  497. endif # GGML_RPC
  498. OBJ_CUDA_TMPL = $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-wmma*.cu))
  499. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/mmq*.cu))
  500. ifdef GGML_CUDA_FA_ALL_QUANTS
  501. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*.cu))
  502. else
  503. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*q4_0-q4_0.cu))
  504. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*q8_0-q8_0.cu))
  505. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*f16-f16.cu))
  506. endif # GGML_CUDA_FA_ALL_QUANTS
  507. ifdef GGML_CUDA
  508. ifdef GGML_MUSA
  509. ifneq ('', '$(wildcard /opt/musa)')
  510. CUDA_PATH ?= /opt/musa
  511. else
  512. CUDA_PATH ?= /usr/local/musa
  513. endif
  514. MK_CPPFLAGS += -DGGML_USE_CUDA -I$(CUDA_PATH)/include
  515. MK_LDFLAGS += -lmusa -lmublas -lmusart -lpthread -ldl -lrt -L$(CUDA_PATH)/lib -L/usr/lib64
  516. MK_NVCCFLAGS += -x musa -mtgpu --cuda-gpu-arch=mp_22
  517. else
  518. ifneq ('', '$(wildcard /opt/cuda)')
  519. CUDA_PATH ?= /opt/cuda
  520. else
  521. CUDA_PATH ?= /usr/local/cuda
  522. endif
  523. MK_CPPFLAGS += -DGGML_USE_CUDA -I$(CUDA_PATH)/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include -DGGML_CUDA_USE_GRAPHS
  524. 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
  525. MK_NVCCFLAGS += -use_fast_math
  526. endif # GGML_MUSA
  527. OBJ_GGML += ggml/src/ggml-cuda.o
  528. OBJ_GGML += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  529. OBJ_GGML += $(OBJ_CUDA_TMPL)
  530. ifdef LLAMA_FATAL_WARNINGS
  531. MK_NVCCFLAGS += -Werror all-warnings
  532. endif # LLAMA_FATAL_WARNINGS
  533. ifndef GGML_MUSA
  534. ifndef JETSON_EOL_MODULE_DETECT
  535. MK_NVCCFLAGS += --forward-unknown-to-host-compiler
  536. endif # JETSON_EOL_MODULE_DETECT
  537. endif # GGML_MUSA
  538. ifdef LLAMA_DEBUG
  539. MK_NVCCFLAGS += -lineinfo
  540. endif # LLAMA_DEBUG
  541. ifdef GGML_CUDA_DEBUG
  542. MK_NVCCFLAGS += --device-debug
  543. endif # GGML_CUDA_DEBUG
  544. ifdef GGML_CUDA_NVCC
  545. NVCC = $(CCACHE) $(GGML_CUDA_NVCC)
  546. else
  547. ifdef GGML_MUSA
  548. NVCC = $(CCACHE) mcc
  549. else
  550. NVCC = $(CCACHE) nvcc
  551. endif # GGML_MUSA
  552. endif # GGML_CUDA_NVCC
  553. ifdef CUDA_DOCKER_ARCH
  554. MK_NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  555. else ifndef CUDA_POWER_ARCH
  556. MK_NVCCFLAGS += -arch=native
  557. endif # CUDA_DOCKER_ARCH
  558. ifdef GGML_CUDA_FORCE_DMMV
  559. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  560. endif # GGML_CUDA_FORCE_DMMV
  561. ifdef GGML_CUDA_FORCE_MMQ
  562. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  563. endif # GGML_CUDA_FORCE_MMQ
  564. ifdef GGML_CUDA_FORCE_CUBLAS
  565. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  566. endif # GGML_CUDA_FORCE_CUBLAS
  567. ifdef GGML_CUDA_DMMV_X
  568. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  569. else
  570. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  571. endif # GGML_CUDA_DMMV_X
  572. ifdef GGML_CUDA_MMV_Y
  573. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  574. else ifdef GGML_CUDA_DMMV_Y
  575. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_DMMV_Y) # for backwards compatibility
  576. else
  577. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  578. endif # GGML_CUDA_MMV_Y
  579. ifdef GGML_CUDA_F16
  580. MK_NVCCFLAGS += -DGGML_CUDA_F16
  581. endif # GGML_CUDA_F16
  582. ifdef GGML_CUDA_DMMV_F16
  583. MK_NVCCFLAGS += -DGGML_CUDA_F16
  584. endif # GGML_CUDA_DMMV_F16
  585. ifdef GGML_CUDA_KQUANTS_ITER
  586. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  587. else
  588. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  589. endif
  590. ifdef GGML_CUDA_PEER_MAX_BATCH_SIZE
  591. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(GGML_CUDA_PEER_MAX_BATCH_SIZE)
  592. else
  593. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  594. endif # GGML_CUDA_PEER_MAX_BATCH_SIZE
  595. ifdef GGML_CUDA_NO_PEER_COPY
  596. MK_NVCCFLAGS += -DGGML_CUDA_NO_PEER_COPY
  597. endif # GGML_CUDA_NO_PEER_COPY
  598. ifdef GGML_CUDA_CCBIN
  599. MK_NVCCFLAGS += -ccbin $(GGML_CUDA_CCBIN)
  600. endif # GGML_CUDA_CCBIN
  601. ifdef GGML_CUDA_FA_ALL_QUANTS
  602. MK_NVCCFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  603. endif # GGML_CUDA_FA_ALL_QUANTS
  604. ifdef JETSON_EOL_MODULE_DETECT
  605. define NVCC_COMPILE
  606. $(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 $@
  607. endef # NVCC_COMPILE
  608. else
  609. ifdef GGML_MUSA
  610. define NVCC_COMPILE
  611. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -c $< -o $@
  612. endef # NVCC_COMPILE
  613. else
  614. define NVCC_COMPILE
  615. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  616. endef # NVCC_COMPILE
  617. endif # GGML_MUSA
  618. endif # JETSON_EOL_MODULE_DETECT
  619. ggml/src/ggml-cuda/%.o: \
  620. ggml/src/ggml-cuda/%.cu \
  621. ggml/include/ggml.h \
  622. ggml/src/ggml-common.h \
  623. ggml/src/ggml-cuda/common.cuh
  624. $(NVCC_COMPILE)
  625. ggml/src/ggml-cuda.o: \
  626. ggml/src/ggml-cuda.cu \
  627. ggml/include/ggml-cuda.h \
  628. ggml/include/ggml.h \
  629. ggml/include/ggml-backend.h \
  630. ggml/src/ggml-backend-impl.h \
  631. ggml/src/ggml-common.h \
  632. $(wildcard ggml/src/ggml-cuda/*.cuh)
  633. $(NVCC_COMPILE)
  634. endif # GGML_CUDA
  635. ifdef GGML_VULKAN
  636. MK_CPPFLAGS += -DGGML_USE_VULKAN
  637. MK_LDFLAGS += $(shell pkg-config --libs vulkan)
  638. OBJ_GGML += ggml/src/ggml-vulkan.o ggml/src/ggml-vulkan-shaders.o
  639. ifdef GGML_VULKAN_CHECK_RESULTS
  640. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  641. endif
  642. ifdef GGML_VULKAN_DEBUG
  643. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  644. endif
  645. ifdef GGML_VULKAN_MEMORY_DEBUG
  646. MK_CPPFLAGS += -DGGML_VULKAN_MEMORY_DEBUG
  647. endif
  648. ifdef GGML_VULKAN_VALIDATE
  649. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  650. endif
  651. ifdef GGML_VULKAN_RUN_TESTS
  652. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  653. endif
  654. GLSLC_CMD = glslc
  655. _ggml_vk_genshaders_cmd = $(shell pwd)/vulkan-shaders-gen
  656. _ggml_vk_header = ggml/src/ggml-vulkan-shaders.hpp
  657. _ggml_vk_source = ggml/src/ggml-vulkan-shaders.cpp
  658. _ggml_vk_input_dir = ggml/src/vulkan-shaders
  659. _ggml_vk_shader_deps = $(echo $(_ggml_vk_input_dir)/*.comp)
  660. ggml/src/ggml-vulkan.o: ggml/src/ggml-vulkan.cpp ggml/include/ggml-vulkan.h $(_ggml_vk_header) $(_ggml_vk_source)
  661. $(CXX) $(CXXFLAGS) $(shell pkg-config --cflags vulkan) -c $< -o $@
  662. $(_ggml_vk_header): $(_ggml_vk_source)
  663. $(_ggml_vk_source): $(_ggml_vk_shader_deps) vulkan-shaders-gen
  664. $(_ggml_vk_genshaders_cmd) \
  665. --glslc $(GLSLC_CMD) \
  666. --input-dir $(_ggml_vk_input_dir) \
  667. --target-hpp $(_ggml_vk_header) \
  668. --target-cpp $(_ggml_vk_source)
  669. vulkan-shaders-gen: ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
  670. $(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
  671. endif # GGML_VULKAN
  672. ifdef GGML_HIPBLAS
  673. ifeq ($(wildcard /opt/rocm),)
  674. ROCM_PATH ?= /usr
  675. AMDGPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  676. else
  677. ROCM_PATH ?= /opt/rocm
  678. AMDGPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  679. endif
  680. GGML_CUDA_DMMV_X ?= 32
  681. GGML_CUDA_MMV_Y ?= 1
  682. GGML_CUDA_KQUANTS_ITER ?= 2
  683. MK_CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUDA
  684. ifdef GGML_HIP_UMA
  685. MK_CPPFLAGS += -DGGML_HIP_UMA
  686. endif # GGML_HIP_UMA
  687. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  688. MK_LDFLAGS += -L$(ROCM_PATH)/lib64 -Wl,-rpath=$(ROCM_PATH)/lib64
  689. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  690. HIPCC ?= $(CCACHE) $(ROCM_PATH)/bin/hipcc
  691. HIPFLAGS += $(addprefix --offload-arch=,$(AMDGPU_TARGETS))
  692. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  693. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  694. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  695. ifdef GGML_CUDA_FORCE_DMMV
  696. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  697. endif # GGML_CUDA_FORCE_DMMV
  698. ifdef GGML_CUDA_FORCE_MMQ
  699. HIPFLAGS += -DGGML_CUDA_FORCE_MMQ
  700. endif # GGML_CUDA_FORCE_MMQ
  701. ifdef GGML_CUDA_FORCE_CUBLAS
  702. HIPFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  703. endif # GGML_CUDA_FORCE_CUBLAS
  704. ifdef GGML_CUDA_NO_PEER_COPY
  705. HIPFLAGS += -DGGML_CUDA_NO_PEER_COPY
  706. endif # GGML_CUDA_NO_PEER_COPY
  707. OBJ_GGML += ggml/src/ggml-cuda.o
  708. OBJ_GGML += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  709. OBJ_GGML += $(OBJ_CUDA_TMPL)
  710. ggml/src/ggml-cuda.o: \
  711. ggml/src/ggml-cuda.cu \
  712. ggml/include/ggml-cuda.h \
  713. ggml/include/ggml.h \
  714. ggml/include/ggml-backend.h \
  715. ggml/src/ggml-backend-impl.h \
  716. ggml/src/ggml-common.h \
  717. $(wildcard ggml/src/ggml-cuda/*.cuh)
  718. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  719. ggml/src/ggml-cuda/%.o: \
  720. ggml/src/ggml-cuda/%.cu \
  721. ggml/include/ggml.h \
  722. ggml/src/ggml-common.h \
  723. ggml/src/ggml-cuda/common.cuh
  724. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  725. endif # GGML_HIPBLAS
  726. ifdef GGML_METAL
  727. MK_CPPFLAGS += -DGGML_USE_METAL
  728. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  729. OBJ_GGML += ggml/src/ggml-metal.o
  730. ifdef GGML_METAL_NDEBUG
  731. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  732. endif
  733. ifdef GGML_METAL_EMBED_LIBRARY
  734. MK_CPPFLAGS += -DGGML_METAL_EMBED_LIBRARY
  735. OBJ_GGML += ggml/src/ggml-metal-embed.o
  736. endif
  737. endif # GGML_METAL
  738. ifdef GGML_METAL
  739. ggml/src/ggml-metal.o: \
  740. ggml/src/ggml-metal.m \
  741. ggml/include/ggml-metal.h \
  742. ggml/include/ggml.h
  743. $(CC) $(CFLAGS) -c $< -o $@
  744. ifdef GGML_METAL_EMBED_LIBRARY
  745. ggml/src/ggml-metal-embed.o: \
  746. ggml/src/ggml-metal.metal \
  747. ggml/src/ggml-common.h
  748. @echo "Embedding Metal library"
  749. @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
  750. $(eval TEMP_ASSEMBLY=$(shell mktemp))
  751. @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)
  752. @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)
  753. @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)
  754. @echo ".incbin \"ggml/src/ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY)
  755. @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)
  756. @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)
  757. @$(AS) $(TEMP_ASSEMBLY) -o $@
  758. @rm -f ${TEMP_ASSEMBLY}
  759. endif
  760. endif # GGML_METAL
  761. OBJ_GGML += \
  762. ggml/src/ggml.o \
  763. ggml/src/ggml-alloc.o \
  764. ggml/src/ggml-backend.o \
  765. ggml/src/ggml-quants.o \
  766. ggml/src/ggml-aarch64.o
  767. OBJ_LLAMA = \
  768. src/llama.o \
  769. src/llama-vocab.o \
  770. src/llama-grammar.o \
  771. src/llama-sampling.o \
  772. src/unicode.o \
  773. src/unicode-data.o
  774. OBJ_COMMON = \
  775. common/common.o \
  776. common/console.o \
  777. common/ngram-cache.o \
  778. common/sampling.o \
  779. common/train.o \
  780. common/grammar-parser.o \
  781. common/build-info.o \
  782. common/json-schema-to-grammar.o
  783. OBJ_ALL = $(OBJ_GGML) $(OBJ_LLAMA) $(OBJ_COMMON)
  784. LIB_GGML = $(LIB_PRE)ggml$(DSO_EXT)
  785. LIB_GGML_S = $(LIB_PRE)ggml.a
  786. LIB_LLAMA = $(LIB_PRE)llama$(DSO_EXT)
  787. LIB_LLAMA_S = $(LIB_PRE)llama.a
  788. LIB_COMMON = $(LIB_PRE)common$(DSO_EXT)
  789. LIB_COMMON_S = $(LIB_PRE)common.a
  790. LIB_ALL = $(LIB_GGML) $(LIB_LLAMA) $(LIB_COMMON)
  791. LIB_ALL_S = $(LIB_GGML_S) $(LIB_LLAMA_S) $(LIB_COMMON_S)
  792. GF_CC := $(CC)
  793. include scripts/get-flags.mk
  794. # combine build flags with cmdline overrides
  795. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  796. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  797. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  798. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  799. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  800. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  801. # identify CUDA host compiler
  802. ifdef GGML_CUDA
  803. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  804. include scripts/get-flags.mk
  805. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  806. endif
  807. ifdef LLAMA_CURL
  808. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  809. override LDFLAGS := $(LDFLAGS) -lcurl
  810. endif
  811. #
  812. # Print build information
  813. #
  814. $(info I llama.cpp build info: )
  815. $(info I UNAME_S: $(UNAME_S))
  816. $(info I UNAME_P: $(UNAME_P))
  817. $(info I UNAME_M: $(UNAME_M))
  818. $(info I CFLAGS: $(CFLAGS))
  819. $(info I CXXFLAGS: $(CXXFLAGS))
  820. $(info I NVCCFLAGS: $(NVCCFLAGS))
  821. $(info I LDFLAGS: $(LDFLAGS))
  822. $(info I CC: $(shell $(CC) --version | head -n 1))
  823. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  824. ifdef GGML_CUDA
  825. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  826. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  827. ifndef GGML_MUSA
  828. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  829. ifndef CUDA_DOCKER_ARCH
  830. ifndef CUDA_POWER_ARCH
  831. $(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 )
  832. endif # CUDA_POWER_ARCH
  833. endif # CUDA_DOCKER_ARCH
  834. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  835. endif # GGML_MUSA
  836. endif # GGML_CUDA
  837. $(info )
  838. ifdef DEPRECATE_WARNING
  839. $(info !!! DEPRECATION WARNING !!!)
  840. $(info The following LLAMA_ options are deprecated and will be removed in the future. Use the GGML_ prefix instead)
  841. $(info - LLAMA_CUDA)
  842. $(info - LLAMA_METAL)
  843. $(info - LLAMA_METAL_EMBED_LIBRARY)
  844. $(info - LLAMA_OPENMP)
  845. $(info - LLAMA_RPC)
  846. $(info - LLAMA_SYCL)
  847. $(info - LLAMA_SYCL_F16)
  848. $(info - LLAMA_OPENBLAS)
  849. $(info - LLAMA_OPENBLAS64)
  850. $(info - LLAMA_BLIS)
  851. $(info - LLAMA_NO_LLAMAFILE)
  852. $(info - LLAMA_NO_ACCELERATE)
  853. $(info - LLAMA_NO_OPENMP)
  854. $(info - LLAMA_NO_METAL)
  855. $(info - LLAMA_NO_CCACHE)
  856. $(info )
  857. endif
  858. #
  859. # Build libraries
  860. #
  861. # ggml
  862. ggml/src/ggml.o: \
  863. ggml/src/ggml.c \
  864. ggml/include/ggml.h
  865. $(CC) $(CFLAGS) -c $< -o $@
  866. ggml/src/ggml-alloc.o: \
  867. ggml/src/ggml-alloc.c \
  868. ggml/include/ggml.h \
  869. ggml/include/ggml-alloc.h
  870. $(CC) $(CFLAGS) -c $< -o $@
  871. ggml/src/ggml-backend.o: \
  872. ggml/src/ggml-backend.c \
  873. ggml/include/ggml.h \
  874. ggml/include/ggml-backend.h
  875. $(CC) $(CFLAGS) -c $< -o $@
  876. ggml/src/ggml-quants.o: \
  877. ggml/src/ggml-quants.c \
  878. ggml/include/ggml.h \
  879. ggml/src/ggml-quants.h \
  880. ggml/src/ggml-common.h
  881. $(CC) $(CFLAGS) -c $< -o $@
  882. ggml/src/ggml-aarch64.o: \
  883. ggml/src/ggml-aarch64.c \
  884. ggml/include/ggml.h \
  885. ggml/src/ggml-aarch64.h \
  886. ggml/src/ggml-common.h
  887. $(CC) $(CFLAGS) -c $< -o $@
  888. ggml/src/ggml-blas.o: \
  889. ggml/src/ggml-blas.cpp \
  890. ggml/include/ggml-blas.h
  891. $(CXX) $(CXXFLAGS) -c $< -o $@
  892. ifndef GGML_NO_LLAMAFILE
  893. ggml/src/llamafile/sgemm.o: \
  894. ggml/src/llamafile/sgemm.cpp \
  895. ggml/src/llamafile/sgemm.h \
  896. ggml/include/ggml.h
  897. $(CXX) $(CXXFLAGS) -c $< -o $@
  898. endif # GGML_NO_LLAMAFILE
  899. ifdef GGML_RPC
  900. ggml/src/ggml-rpc.o: \
  901. ggml/src/ggml-rpc.cpp \
  902. ggml/include/ggml-rpc.h
  903. $(CXX) $(CXXFLAGS) -c $< -o $@
  904. endif # GGML_RPC
  905. $(LIB_GGML): \
  906. $(OBJ_GGML)
  907. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  908. $(LIB_GGML_S): \
  909. $(OBJ_GGML)
  910. ar rcs $(LIB_GGML_S) $^
  911. # llama
  912. src/unicode.o: \
  913. src/unicode.cpp \
  914. src/unicode.h
  915. $(CXX) $(CXXFLAGS) -c $< -o $@
  916. src/unicode-data.o: \
  917. src/unicode-data.cpp \
  918. src/unicode-data.h
  919. $(CXX) $(CXXFLAGS) -c $< -o $@
  920. src/llama.o: \
  921. src/llama.cpp \
  922. src/llama-impl.h \
  923. src/llama-vocab.h \
  924. src/llama-grammar.h \
  925. src/llama-sampling.h \
  926. src/unicode.h \
  927. include/llama.h \
  928. ggml/include/ggml-cuda.h \
  929. ggml/include/ggml-metal.h \
  930. ggml/include/ggml.h \
  931. ggml/include/ggml-alloc.h \
  932. ggml/include/ggml-backend.h
  933. $(CXX) $(CXXFLAGS) -c $< -o $@
  934. src/llama-vocab.o: \
  935. src/llama-vocab.cpp \
  936. src/llama-vocab.h \
  937. src/llama-impl.h \
  938. include/llama.h
  939. $(CXX) $(CXXFLAGS) -c $< -o $@
  940. src/llama-grammar.o: \
  941. src/llama-grammar.cpp \
  942. src/llama-grammar.h \
  943. src/llama-impl.h \
  944. src/llama-vocab.h \
  945. src/llama-sampling.h \
  946. include/llama.h
  947. $(CXX) $(CXXFLAGS) -c $< -o $@
  948. src/llama-sampling.o: \
  949. src/llama-sampling.cpp \
  950. src/llama-sampling.h \
  951. src/llama-impl.h \
  952. include/llama.h
  953. $(CXX) $(CXXFLAGS) -c $< -o $@
  954. $(LIB_LLAMA): \
  955. $(OBJ_LLAMA) \
  956. $(LIB_GGML)
  957. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  958. $(LIB_LLAMA_S): \
  959. $(OBJ_LLAMA)
  960. ar rcs $(LIB_LLAMA_S) $^
  961. # common
  962. common/common.o: \
  963. common/common.cpp \
  964. common/common.h \
  965. common/console.h \
  966. common/sampling.h \
  967. common/json.hpp \
  968. common/json-schema-to-grammar.h \
  969. include/llama.h
  970. $(CXX) $(CXXFLAGS) -c $< -o $@
  971. common/sampling.o: \
  972. common/sampling.cpp \
  973. common/sampling.h \
  974. include/llama.h
  975. $(CXX) $(CXXFLAGS) -c $< -o $@
  976. common/console.o: \
  977. common/console.cpp \
  978. common/console.h
  979. $(CXX) $(CXXFLAGS) -c $< -o $@
  980. common/grammar-parser.o: \
  981. common/grammar-parser.cpp \
  982. common/grammar-parser.h
  983. $(CXX) $(CXXFLAGS) -c $< -o $@
  984. common/json-schema-to-grammar.o: \
  985. common/json-schema-to-grammar.cpp \
  986. common/json-schema-to-grammar.h
  987. $(CXX) $(CXXFLAGS) -c $< -o $@
  988. common/train.o: \
  989. common/train.cpp \
  990. common/train.h
  991. $(CXX) $(CXXFLAGS) -c $< -o $@
  992. common/ngram-cache.o: \
  993. common/ngram-cache.cpp \
  994. common/ngram-cache.h
  995. $(CXX) $(CXXFLAGS) -c $< -o $@
  996. $(LIB_COMMON): \
  997. $(OBJ_COMMON) \
  998. $(LIB_LLAMA) \
  999. $(LIB_GGML)
  1000. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  1001. $(LIB_COMMON_S): \
  1002. $(OBJ_COMMON)
  1003. ar rcs $(LIB_COMMON_S) $^
  1004. clean:
  1005. rm -vrf *.dot $(BUILD_TARGETS) $(TEST_TARGETS)
  1006. rm -rvf src/*.o
  1007. rm -rvf tests/*.o
  1008. rm -rvf examples/*.o
  1009. rm -rvf common/*.o
  1010. rm -rvf *.a
  1011. rm -rvf *.dll
  1012. rm -rvf *.so
  1013. rm -rvf *.dot
  1014. rm -rvf ggml/*.a
  1015. rm -rvf ggml/*.dll
  1016. rm -rvf ggml/*.so
  1017. rm -vrf ggml/src/*.o
  1018. rm -rvf common/build-info.cpp
  1019. rm -vrf ggml/src/ggml-metal-embed.metal
  1020. rm -vrf ggml/src/ggml-cuda/*.o
  1021. rm -vrf ggml/src/ggml-cuda/template-instances/*.o
  1022. rm -rvf $(BUILD_TARGETS)
  1023. rm -rvf $(TEST_TARGETS)
  1024. rm -f vulkan-shaders-gen ggml/src/ggml-vulkan-shaders.hpp ggml/src/ggml-vulkan-shaders.cpp
  1025. rm -rvf $(LEGACY_TARGETS_CLEAN)
  1026. find examples pocs -type f -name "*.o" -delete
  1027. #
  1028. # Examples
  1029. #
  1030. # $< is the first prerequisite, i.e. the source file.
  1031. # Explicitly compile this to an object file so that it can be cached with ccache.
  1032. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  1033. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  1034. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  1035. llama-cli: examples/main/main.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. @echo
  1040. @echo '==== Run ./llama-cli -h for help. ===='
  1041. @echo
  1042. llama-infill: examples/infill/infill.cpp \
  1043. $(OBJ_ALL)
  1044. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1045. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1046. llama-simple: examples/simple/simple.cpp \
  1047. $(OBJ_ALL)
  1048. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1049. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1050. llama-tokenize: examples/tokenize/tokenize.cpp \
  1051. $(OBJ_ALL)
  1052. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1053. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1054. llama-batched: examples/batched/batched.cpp \
  1055. $(OBJ_ALL)
  1056. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1057. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1058. llama-batched-bench: examples/batched-bench/batched-bench.cpp \
  1059. $(OBJ_ALL)
  1060. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1061. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1062. llama-quantize: examples/quantize/quantize.cpp \
  1063. $(OBJ_ALL)
  1064. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1065. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1066. llama-quantize-stats: examples/quantize-stats/quantize-stats.cpp \
  1067. $(OBJ_ALL)
  1068. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1069. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1070. llama-perplexity: examples/perplexity/perplexity.cpp \
  1071. $(OBJ_ALL)
  1072. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1073. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1074. llama-imatrix: examples/imatrix/imatrix.cpp \
  1075. $(OBJ_ALL)
  1076. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1077. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1078. llama-embedding: examples/embedding/embedding.cpp \
  1079. $(OBJ_ALL)
  1080. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1081. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1082. llama-gritlm: examples/gritlm/gritlm.cpp \
  1083. $(OBJ_ALL)
  1084. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1085. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1086. llama-save-load-state: examples/save-load-state/save-load-state.cpp \
  1087. $(OBJ_ALL)
  1088. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1089. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1090. llama-gguf: examples/gguf/gguf.cpp \
  1091. $(OBJ_GGML)
  1092. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1093. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1094. examples/gguf-hash/deps/sha1/sha1.o: \
  1095. examples/gguf-hash/deps/sha1/sha1.c
  1096. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1097. examples/gguf-hash/deps/xxhash/xxhash.o: \
  1098. examples/gguf-hash/deps/xxhash/xxhash.c
  1099. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1100. examples/gguf-hash/deps/sha256/sha256.o: \
  1101. examples/gguf-hash/deps/sha256/sha256.c
  1102. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1103. 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\
  1104. $(OBJ_ALL)
  1105. $(CXX) $(CXXFLAGS) -Iexamples/gguf-hash/deps -c $< -o $(call GET_OBJ_FILE, $<)
  1106. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1107. llama-gguf-split: examples/gguf-split/gguf-split.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-eval-callback: examples/eval-callback/eval-callback.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-cvector-generator: examples/cvector-generator/cvector-generator.cpp \
  1116. $(OBJ_ALL)
  1117. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1118. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1119. llama-convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp \
  1120. $(OBJ_GGML) $(OBJ_LLAMA)
  1121. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1122. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1123. llama-bench: examples/llama-bench/llama-bench.cpp \
  1124. $(OBJ_ALL)
  1125. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1126. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1127. llama-baby-llama: examples/baby-llama/baby-llama.cpp \
  1128. $(OBJ_ALL)
  1129. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1130. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1131. llama-export-lora: examples/export-lora/export-lora.cpp \
  1132. $(OBJ_ALL)
  1133. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1134. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1135. llama-retrieval: examples/retrieval/retrieval.cpp \
  1136. $(OBJ_ALL)
  1137. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1138. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1139. llama-speculative: examples/speculative/speculative.cpp \
  1140. $(OBJ_ALL)
  1141. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1142. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1143. llama-parallel: examples/parallel/parallel.cpp \
  1144. $(OBJ_ALL)
  1145. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1146. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1147. llama-lookahead: examples/lookahead/lookahead.cpp \
  1148. $(OBJ_ALL)
  1149. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1150. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1151. llama-lookup: examples/lookup/lookup.cpp \
  1152. $(OBJ_ALL)
  1153. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1154. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1155. llama-lookup-create: examples/lookup/lookup-create.cpp \
  1156. $(OBJ_ALL)
  1157. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1158. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1159. llama-lookup-merge: examples/lookup/lookup-merge.cpp \
  1160. $(OBJ_ALL)
  1161. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1162. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1163. llama-lookup-stats: examples/lookup/lookup-stats.cpp \
  1164. $(OBJ_ALL)
  1165. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1166. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1167. llama-passkey: examples/passkey/passkey.cpp \
  1168. $(OBJ_ALL)
  1169. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1170. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1171. llama-gbnf-validator: examples/gbnf-validator/gbnf-validator.cpp \
  1172. $(OBJ_ALL)
  1173. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1174. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1175. ifdef GGML_RPC
  1176. rpc-server: examples/rpc/rpc-server.cpp \
  1177. $(OBJ_GGML)
  1178. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  1179. endif # GGML_RPC
  1180. llama-server: \
  1181. examples/server/server.cpp \
  1182. examples/server/utils.hpp \
  1183. examples/server/httplib.h \
  1184. examples/server/colorthemes.css.hpp \
  1185. examples/server/style.css.hpp \
  1186. examples/server/theme-beeninorder.css.hpp \
  1187. examples/server/theme-ketivah.css.hpp \
  1188. examples/server/theme-mangotango.css.hpp \
  1189. examples/server/theme-playground.css.hpp \
  1190. examples/server/theme-polarnight.css.hpp \
  1191. examples/server/theme-snowstorm.css.hpp \
  1192. examples/server/index.html.hpp \
  1193. examples/server/index-new.html.hpp \
  1194. examples/server/index.js.hpp \
  1195. examples/server/completion.js.hpp \
  1196. examples/server/system-prompts.js.hpp \
  1197. examples/server/prompt-formats.js.hpp \
  1198. examples/server/json-schema-to-grammar.mjs.hpp \
  1199. common/json.hpp \
  1200. common/stb_image.h \
  1201. $(OBJ_ALL)
  1202. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1203. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  1204. # Portable equivalent of `cd examples/server/public && xxd -i $(notdir $<) ../$(notdir $<).hpp`:
  1205. examples/server/%.hpp: examples/server/public/% Makefile
  1206. @( export NAME=$(subst .,_,$(subst -,_,$(notdir $<))) && \
  1207. echo "unsigned char $${NAME}[] = {" && \
  1208. cat $< | od -v -t x1 -An | sed -E 's/([0-9a-fA-F]+)/0x\1, /g' && \
  1209. echo "};" && \
  1210. echo "unsigned int $${NAME}_len = $(shell cat $< | wc -c );" \
  1211. ) > $@
  1212. libllava.a: examples/llava/llava.cpp \
  1213. examples/llava/llava.h \
  1214. examples/llava/clip.cpp \
  1215. examples/llava/clip.h \
  1216. common/stb_image.h \
  1217. common/base64.hpp \
  1218. $(OBJ_ALL)
  1219. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  1220. llama-llava-cli: examples/llava/llava-cli.cpp \
  1221. examples/llava/clip.h \
  1222. examples/llava/clip.cpp \
  1223. examples/llava/llava.h \
  1224. examples/llava/llava.cpp \
  1225. $(OBJ_ALL)
  1226. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1227. $(CXX) $(CXXFLAGS) -c examples/llava/clip.cpp -o $(call GET_OBJ_FILE, examples/llava/clip.cpp) -Wno-cast-qual
  1228. $(CXX) $(CXXFLAGS) -c examples/llava/llava.cpp -o $(call GET_OBJ_FILE, examples/llava/llava.cpp)
  1229. $(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)
  1230. ifeq ($(UNAME_S),Darwin)
  1231. swift: examples/batched.swift
  1232. (cd examples/batched.swift; make build)
  1233. endif
  1234. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  1235. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  1236. @if ! cmp -s $@.tmp $@; then \
  1237. mv $@.tmp $@; \
  1238. else \
  1239. rm $@.tmp; \
  1240. fi
  1241. common/build-info.o: common/build-info.cpp
  1242. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  1243. #
  1244. # Tests
  1245. #
  1246. tests: $(TEST_TARGETS)
  1247. llama-benchmark-matmult: examples/benchmark/benchmark-matmult.cpp \
  1248. $(OBJ_GGML) common/build-info.o
  1249. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1250. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1251. run-benchmark-matmult: llama-benchmark-matmult
  1252. ./$@
  1253. .PHONY: run-benchmark-matmult swift
  1254. tests/test-llama-grammar: tests/test-llama-grammar.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-grammar-parser: tests/test-grammar-parser.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-grammar-integration: tests/test-grammar-integration.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-double-float: tests/test-double-float.cpp
  1267. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1268. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1269. tests/test-json-schema-to-grammar: tests/test-json-schema-to-grammar.cpp \
  1270. $(OBJ_ALL)
  1271. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  1272. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1273. tests/test-grad0: tests/test-grad0.cpp \
  1274. $(OBJ_GGML)
  1275. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1276. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1277. tests/test-opt: tests/test-opt.cpp \
  1278. $(OBJ_GGML)
  1279. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1280. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1281. tests/test-quantize-fns: tests/test-quantize-fns.cpp \
  1282. $(OBJ_GGML)
  1283. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1284. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1285. tests/test-quantize-perf: tests/test-quantize-perf.cpp \
  1286. $(OBJ_GGML)
  1287. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1288. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1289. tests/test-sampling: tests/test-sampling.cpp \
  1290. $(OBJ_ALL)
  1291. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1292. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1293. tests/test-tokenizer-0: tests/test-tokenizer-0.cpp \
  1294. $(OBJ_ALL)
  1295. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1296. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1297. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp \
  1298. $(OBJ_ALL)
  1299. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1300. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1301. tests/test-tokenizer-1-spm: tests/test-tokenizer-1-spm.cpp \
  1302. $(OBJ_ALL)
  1303. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1304. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1305. tests/test-rope: tests/test-rope.cpp ggml/src/ggml.o \
  1306. $(OBJ_GGML)
  1307. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1308. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1309. tests/test-c.o: tests/test-c.c include/llama.h
  1310. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  1311. tests/test-backend-ops: tests/test-backend-ops.cpp \
  1312. $(OBJ_GGML)
  1313. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1314. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1315. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp tests/get-model.cpp \
  1316. $(OBJ_ALL)
  1317. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1318. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1319. tests/test-autorelease: tests/test-autorelease.cpp tests/get-model.cpp \
  1320. $(OBJ_ALL)
  1321. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1322. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1323. tests/test-chat-template: tests/test-chat-template.cpp \
  1324. $(OBJ_ALL)
  1325. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1326. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1327. #
  1328. # PoCs
  1329. #
  1330. llama-vdot: pocs/vdot/vdot.cpp ggml/src/ggml.o \
  1331. $(OBJ_GGML)
  1332. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1333. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1334. llama-q8dot: pocs/vdot/q8dot.cpp ggml/src/ggml.o \
  1335. $(OBJ_GGML)
  1336. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1337. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1338. #
  1339. # Deprecated binaries that we want to keep around long enough for people to migrate to the new filenames, then these can be removed.
  1340. #
  1341. # Mark legacy binary targets as .PHONY so that they are always checked.
  1342. .PHONY: main quantize perplexity embedding server
  1343. # Define the object file target
  1344. examples/deprecation-warning/deprecation-warning.o: examples/deprecation-warning/deprecation-warning.cpp
  1345. $(CXX) $(CXXFLAGS) -c $< -o $@
  1346. # NOTE: We currently will always build the deprecation-warning `main` and `server` binaries to help users migrate.
  1347. # Eventually we will want to remove these target from building all the time.
  1348. main: examples/deprecation-warning/deprecation-warning.o
  1349. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1350. @echo "NOTICE: The 'main' binary is deprecated. Please use 'llama-cli' instead."
  1351. server: examples/deprecation-warning/deprecation-warning.o
  1352. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1353. @echo "NOTICE: The 'server' binary is deprecated. Please use 'llama-server' instead."
  1354. quantize: examples/deprecation-warning/deprecation-warning.o
  1355. ifneq (,$(wildcard quantize))
  1356. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1357. @echo "#########"
  1358. @echo "WARNING: The 'quantize' binary is deprecated. Please use 'llama-quantize' instead."
  1359. @echo " Remove the 'quantize' binary to remove this warning."
  1360. @echo "#########"
  1361. endif
  1362. perplexity: examples/deprecation-warning/deprecation-warning.o
  1363. ifneq (,$(wildcard perplexity))
  1364. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1365. @echo "#########"
  1366. @echo "WARNING: The 'perplexity' binary is deprecated. Please use 'llama-perplexity' instead."
  1367. @echo " Remove the 'perplexity' binary to remove this warning."
  1368. @echo "#########"
  1369. endif
  1370. embedding: examples/deprecation-warning/deprecation-warning.o
  1371. ifneq (,$(wildcard embedding))
  1372. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1373. @echo "#########"
  1374. @echo "WARNING: The 'embedding' binary is deprecated. Please use 'llama-embedding' instead."
  1375. @echo " Remove the 'embedding' binary to remove this warning."
  1376. @echo "#########"
  1377. endif