Makefile 49 KB

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