Makefile 49 KB

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