Makefile 48 KB

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