Makefile 49 KB

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