Makefile 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. # Define the default target now so that it is always the first target
  2. BUILD_TARGETS = \
  3. libllava.a \
  4. llama-baby-llama \
  5. llama-batched \
  6. llama-batched-bench \
  7. llama-bench \
  8. llama-benchmark-matmult \
  9. llama-cli \
  10. llama-convert-llama2c-to-ggml \
  11. llama-embedding \
  12. llama-eval-callback \
  13. llama-export-lora \
  14. llama-gbnf-validator \
  15. llama-gguf \
  16. llama-gguf-hash \
  17. llama-gguf-split \
  18. llama-gritlm \
  19. llama-imatrix \
  20. llama-infill \
  21. llama-llava-cli \
  22. llama-minicpmv-cli\
  23. llama-lookahead \
  24. llama-lookup \
  25. llama-lookup-create \
  26. llama-lookup-merge \
  27. llama-lookup-stats \
  28. llama-parallel \
  29. llama-passkey \
  30. llama-perplexity \
  31. llama-q8dot \
  32. llama-quantize \
  33. llama-quantize-stats \
  34. llama-retrieval \
  35. llama-save-load-state \
  36. llama-server \
  37. llama-simple \
  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-grad0 \
  52. tests/test-grammar-integration \
  53. tests/test-grammar-parser \
  54. tests/test-json-schema-to-grammar \
  55. tests/test-llama-grammar \
  56. tests/test-model-load-cancel \
  57. tests/test-opt \
  58. tests/test-quantize-fns \
  59. tests/test-quantize-perf \
  60. tests/test-rope \
  61. tests/test-sampling \
  62. tests/test-tokenizer-0 \
  63. tests/test-tokenizer-1-bpe \
  64. tests/test-tokenizer-1-spm
  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 benchmark-matmult 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_OPENMP
  89. GGML_OPENMP := 1
  90. DEPRECATE_WARNING := 1
  91. endif
  92. ifdef LLAMA_RPC
  93. GGML_RPC := 1
  94. DEPRECATE_WARNING := 1
  95. endif
  96. ifdef LLAMA_SYCL
  97. GGML_SYCL := 1
  98. DEPRECATE_WARNING := 1
  99. endif
  100. ifdef LLAMA_SYCL_F16
  101. GGML_SYCL_F16 := 1
  102. DEPRECATE_WARNING := 1
  103. endif
  104. ifdef LLAMA_OPENBLAS
  105. GGML_OPENBLAS := 1
  106. DEPRECATE_WARNING := 1
  107. endif
  108. ifdef LLAMA_OPENBLAS64
  109. GGML_OPENBLAS64 := 1
  110. DEPRECATE_WARNING := 1
  111. endif
  112. ifdef LLAMA_BLIS
  113. GGML_BLIS := 1
  114. DEPRECATE_WARNING := 1
  115. endif
  116. ifdef LLAMA_NO_LLAMAFILE
  117. GGML_NO_LLAMAFILE := 1
  118. DEPRECATE_WARNING := 1
  119. endif
  120. ifdef LLAMA_NO_ACCELERATE
  121. GGML_NO_ACCELERATE := 1
  122. DEPRECATE_WARNING := 1
  123. endif
  124. ifdef LLAMA_NO_OPENMP
  125. GGML_NO_OPENMP := 1
  126. DEPRECATE_WARNING := 1
  127. endif
  128. ifdef LLAMA_NO_METAL
  129. GGML_NO_METAL := 1
  130. DEPRECATE_WARNING := 1
  131. endif
  132. ifndef UNAME_S
  133. UNAME_S := $(shell uname -s)
  134. endif
  135. ifndef UNAME_P
  136. UNAME_P := $(shell uname -p)
  137. endif
  138. ifndef UNAME_M
  139. UNAME_M := $(shell uname -m)
  140. endif
  141. # In GNU make default CXX is g++ instead of c++. Let's fix that so that users
  142. # of non-gcc compilers don't have to provide g++ alias or wrapper.
  143. DEFCC := cc
  144. DEFCXX := c++
  145. ifeq ($(origin CC),default)
  146. CC := $(DEFCC)
  147. endif
  148. ifeq ($(origin CXX),default)
  149. CXX := $(DEFCXX)
  150. endif
  151. # Mac OS + Arm can report x86_64
  152. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  153. ifeq ($(UNAME_S),Darwin)
  154. ifndef GGML_NO_METAL
  155. GGML_METAL := 1
  156. endif
  157. GGML_NO_OPENMP := 1
  158. ifneq ($(UNAME_P),arm)
  159. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  160. ifeq ($(SYSCTL_M),1)
  161. # UNAME_P := arm
  162. # UNAME_M := arm64
  163. 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)
  164. endif
  165. endif
  166. endif
  167. ifdef GGML_METAL
  168. GGML_METAL_EMBED_LIBRARY := 1
  169. endif
  170. ifdef GGML_RPC
  171. BUILD_TARGETS += rpc-server
  172. endif
  173. ifdef GGML_VULKAN
  174. BUILD_TARGETS += vulkan-shaders-gen
  175. endif
  176. default: $(BUILD_TARGETS) $(LEGACY_TARGETS_BUILD)
  177. test: $(TEST_TARGETS)
  178. @failures=0; \
  179. for test_target in $(TEST_TARGETS); do \
  180. if [ "$$test_target" = "tests/test-tokenizer-0" ]; then \
  181. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-spm.gguf; \
  182. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-bpe.gguf; \
  183. ./$$test_target $(CURDIR)/models/ggml-vocab-phi-3.gguf; \
  184. ./$$test_target $(CURDIR)/models/ggml-vocab-falcon.gguf; \
  185. ./$$test_target $(CURDIR)/models/ggml-vocab-bert-bge.gguf; \
  186. ./$$test_target $(CURDIR)/models/ggml-vocab-starcoder.gguf; \
  187. ./$$test_target $(CURDIR)/models/ggml-vocab-gpt-2.gguf; \
  188. ./$$test_target $(CURDIR)/models/ggml-vocab-refact.gguf; \
  189. elif [ "$$test_target" = "tests/test-tokenizer-1-spm" ]; then \
  190. continue; \
  191. elif [ "$$test_target" = "tests/test-tokenizer-1-bpe" ]; then \
  192. continue; \
  193. else \
  194. echo "Running test $$test_target..."; \
  195. ./$$test_target; \
  196. fi; \
  197. if [ $$? -ne 0 ]; then \
  198. printf 'Test %s FAILED!\n\n' $$test_target; \
  199. failures=$$(( failures + 1 )); \
  200. else \
  201. printf 'Test %s passed.\n\n' $$test_target; \
  202. fi; \
  203. done; \
  204. if [ $$failures -gt 0 ]; then \
  205. printf '\n%s tests failed.\n' $$failures; \
  206. exit 1; \
  207. fi
  208. @echo 'All tests passed.'
  209. all: $(BUILD_TARGETS) $(TEST_TARGETS) $(LEGACY_TARGETS_BUILD)
  210. ifdef RISCV_CROSS_COMPILE
  211. CC := riscv64-unknown-linux-gnu-gcc
  212. CXX := riscv64-unknown-linux-gnu-g++
  213. endif
  214. #
  215. # Compile flags
  216. #
  217. # keep standard at C11 and C++11
  218. MK_CPPFLAGS = -Iggml/include -Iggml/src -Iinclude -Isrc -Icommon
  219. MK_CFLAGS = -std=c11 -fPIC
  220. MK_CXXFLAGS = -std=c++11 -fPIC
  221. MK_NVCCFLAGS = -std=c++11
  222. ifdef LLAMA_NO_CCACHE
  223. GGML_NO_CCACHE := 1
  224. DEPRECATE_WARNING := 1
  225. endif
  226. ifndef GGML_NO_CCACHE
  227. CCACHE := $(shell which ccache)
  228. ifdef CCACHE
  229. export CCACHE_SLOPPINESS = time_macros
  230. $(info I ccache found, compilation results will be cached. Disable with GGML_NO_CCACHE.)
  231. CC := $(CCACHE) $(CC)
  232. CXX := $(CCACHE) $(CXX)
  233. else
  234. $(info I ccache not found. Consider installing it for faster compilation.)
  235. endif # CCACHE
  236. endif # GGML_NO_CCACHE
  237. # clock_gettime came in POSIX.1b (1993)
  238. # CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
  239. # posix_memalign came in POSIX.1-2001 / SUSv3
  240. # M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
  241. MK_CPPFLAGS += -D_XOPEN_SOURCE=600
  242. # Somehow in OpenBSD whenever POSIX conformance is specified
  243. # some string functions rely on locale_t availability,
  244. # which was introduced in POSIX.1-2008, forcing us to go higher
  245. ifeq ($(UNAME_S),OpenBSD)
  246. MK_CPPFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
  247. endif
  248. # Data types, macros and functions related to controlling CPU affinity and
  249. # some memory allocation are available on Linux through GNU extensions in libc
  250. ifeq ($(UNAME_S),Linux)
  251. MK_CPPFLAGS += -D_GNU_SOURCE
  252. endif
  253. # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
  254. # and on macOS its availability depends on enabling Darwin extensions
  255. # similarly on DragonFly, enabling BSD extensions is necessary
  256. ifeq ($(UNAME_S),Darwin)
  257. MK_CPPFLAGS += -D_DARWIN_C_SOURCE
  258. endif
  259. ifeq ($(UNAME_S),DragonFly)
  260. MK_CPPFLAGS += -D__BSD_VISIBLE
  261. endif
  262. # alloca is a non-standard interface that is not visible on BSDs when
  263. # POSIX conformance is specified, but not all of them provide a clean way
  264. # to enable it in such cases
  265. ifeq ($(UNAME_S),FreeBSD)
  266. MK_CPPFLAGS += -D__BSD_VISIBLE
  267. endif
  268. ifeq ($(UNAME_S),NetBSD)
  269. MK_CPPFLAGS += -D_NETBSD_SOURCE
  270. endif
  271. ifeq ($(UNAME_S),OpenBSD)
  272. MK_CPPFLAGS += -D_BSD_SOURCE
  273. endif
  274. ifdef GGML_SCHED_MAX_COPIES
  275. MK_CPPFLAGS += -DGGML_SCHED_MAX_COPIES=$(GGML_SCHED_MAX_COPIES)
  276. endif
  277. ifdef LLAMA_DEBUG
  278. MK_CFLAGS += -O0 -g
  279. MK_CXXFLAGS += -O0 -g
  280. MK_LDFLAGS += -g
  281. MK_NVCCFLAGS += -O0 -g
  282. ifeq ($(UNAME_S),Linux)
  283. MK_CPPFLAGS += -D_GLIBCXX_ASSERTIONS
  284. endif
  285. else
  286. MK_CPPFLAGS += -DNDEBUG
  287. MK_CFLAGS += -O3 -g
  288. MK_CXXFLAGS += -O3 -g
  289. MK_NVCCFLAGS += -O3 -g
  290. endif
  291. ifdef LLAMA_SANITIZE_THREAD
  292. MK_CFLAGS += -fsanitize=thread -g
  293. MK_CXXFLAGS += -fsanitize=thread -g
  294. MK_LDFLAGS += -fsanitize=thread -g
  295. endif
  296. ifdef LLAMA_SANITIZE_ADDRESS
  297. MK_CFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  298. MK_CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  299. MK_LDFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  300. endif
  301. ifdef LLAMA_SANITIZE_UNDEFINED
  302. MK_CFLAGS += -fsanitize=undefined -g
  303. MK_CXXFLAGS += -fsanitize=undefined -g
  304. MK_LDFLAGS += -fsanitize=undefined -g
  305. endif
  306. ifdef LLAMA_SERVER_VERBOSE
  307. MK_CPPFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
  308. endif
  309. ifdef LLAMA_SERVER_SSL
  310. MK_CPPFLAGS += -DCPPHTTPLIB_OPENSSL_SUPPORT
  311. MK_LDFLAGS += -lssl -lcrypto
  312. endif
  313. ifdef LLAMA_DISABLE_LOGS
  314. MK_CPPFLAGS += -DLOG_DISABLE_LOGS
  315. endif # LLAMA_DISABLE_LOGS
  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
  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. else
  442. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  443. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  444. endif
  445. ifndef GGML_NO_ACCELERATE
  446. # Mac OS - include Accelerate framework.
  447. # `-framework Accelerate` works both with Apple Silicon and Mac Intel
  448. ifeq ($(UNAME_S),Darwin)
  449. MK_CPPFLAGS += -DGGML_USE_ACCELERATE -DGGML_USE_BLAS
  450. MK_CPPFLAGS += -DACCELERATE_NEW_LAPACK
  451. MK_CPPFLAGS += -DACCELERATE_LAPACK_ILP64
  452. MK_LDFLAGS += -framework Accelerate
  453. OBJ_GGML += ggml/src/ggml-blas.o
  454. endif
  455. endif # GGML_NO_ACCELERATE
  456. ifdef GGML_MUSA
  457. CC := clang
  458. CXX := clang++
  459. GGML_CUDA := 1
  460. MK_CPPFLAGS += -DGGML_USE_MUSA
  461. endif
  462. ifndef GGML_NO_OPENMP
  463. MK_CPPFLAGS += -DGGML_USE_OPENMP
  464. MK_CFLAGS += -fopenmp
  465. MK_CXXFLAGS += -fopenmp
  466. ifdef GGML_MUSA
  467. MK_CPPFLAGS += -I/usr/lib/llvm-10/include/openmp
  468. MK_LDFLAGS += -L/usr/lib/llvm-10/lib
  469. endif # GGML_MUSA
  470. endif # GGML_NO_OPENMP
  471. ifdef GGML_OPENBLAS
  472. MK_CPPFLAGS += -DGGML_USE_BLAS $(shell pkg-config --cflags-only-I openblas)
  473. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
  474. MK_LDFLAGS += $(shell pkg-config --libs openblas)
  475. OBJ_GGML += ggml/src/ggml-blas.o
  476. endif # GGML_OPENBLAS
  477. ifdef GGML_OPENBLAS64
  478. MK_CPPFLAGS += -DGGML_USE_BLAS $(shell pkg-config --cflags-only-I openblas64)
  479. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas64)
  480. MK_LDFLAGS += $(shell pkg-config --libs openblas64)
  481. OBJ_GGML += ggml/src/ggml-blas.o
  482. endif # GGML_OPENBLAS64
  483. ifdef GGML_BLIS
  484. MK_CPPFLAGS += -DGGML_USE_BLAS -DGGML_BLAS_USE_BLIS -I/usr/local/include/blis -I/usr/include/blis
  485. MK_LDFLAGS += -lblis -L/usr/local/lib
  486. OBJ_GGML += ggml/src/ggml-blas.o
  487. endif # GGML_BLIS
  488. ifdef GGML_NVPL
  489. MK_CPPFLAGS += -DGGML_USE_BLAS -DGGML_BLAS_USE_NVPL -DNVPL_ILP64 -I/usr/local/include/nvpl_blas -I/usr/include/nvpl_blas
  490. MK_LDFLAGS += -L/usr/local/lib -lnvpl_blas_core -lnvpl_blas_ilp64_gomp
  491. OBJ_GGML += ggml/src/ggml-blas.o
  492. endif # GGML_NVPL
  493. ifndef GGML_NO_LLAMAFILE
  494. MK_CPPFLAGS += -DGGML_USE_LLAMAFILE
  495. OBJ_GGML += ggml/src/llamafile/sgemm.o
  496. endif
  497. ifdef GGML_RPC
  498. MK_CPPFLAGS += -DGGML_USE_RPC
  499. OBJ_GGML += ggml/src/ggml-rpc.o
  500. endif # GGML_RPC
  501. OBJ_CUDA_TMPL = $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-wmma*.cu))
  502. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/mmq*.cu))
  503. ifdef GGML_CUDA_FA_ALL_QUANTS
  504. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*.cu))
  505. else
  506. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*q4_0-q4_0.cu))
  507. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*q8_0-q8_0.cu))
  508. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*f16-f16.cu))
  509. endif # GGML_CUDA_FA_ALL_QUANTS
  510. ifdef GGML_CUDA
  511. ifdef GGML_MUSA
  512. ifneq ('', '$(wildcard /opt/musa)')
  513. CUDA_PATH ?= /opt/musa
  514. else
  515. CUDA_PATH ?= /usr/local/musa
  516. endif
  517. MK_CPPFLAGS += -DGGML_USE_CUDA -I$(CUDA_PATH)/include
  518. MK_LDFLAGS += -lmusa -lmublas -lmusart -lpthread -ldl -lrt -L$(CUDA_PATH)/lib -L/usr/lib64
  519. MK_NVCCFLAGS += -x musa -mtgpu --cuda-gpu-arch=mp_22
  520. else
  521. ifneq ('', '$(wildcard /opt/cuda)')
  522. CUDA_PATH ?= /opt/cuda
  523. else
  524. CUDA_PATH ?= /usr/local/cuda
  525. endif
  526. MK_CPPFLAGS += -DGGML_USE_CUDA -I$(CUDA_PATH)/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include -DGGML_CUDA_USE_GRAPHS
  527. 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
  528. MK_NVCCFLAGS += -use_fast_math
  529. endif # GGML_MUSA
  530. OBJ_GGML += ggml/src/ggml-cuda.o
  531. OBJ_GGML += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  532. OBJ_GGML += $(OBJ_CUDA_TMPL)
  533. ifdef LLAMA_FATAL_WARNINGS
  534. MK_NVCCFLAGS += -Werror all-warnings
  535. endif # LLAMA_FATAL_WARNINGS
  536. ifndef GGML_MUSA
  537. ifndef JETSON_EOL_MODULE_DETECT
  538. MK_NVCCFLAGS += --forward-unknown-to-host-compiler
  539. endif # JETSON_EOL_MODULE_DETECT
  540. endif # GGML_MUSA
  541. ifdef LLAMA_DEBUG
  542. MK_NVCCFLAGS += -lineinfo
  543. endif # LLAMA_DEBUG
  544. ifdef GGML_CUDA_DEBUG
  545. MK_NVCCFLAGS += --device-debug
  546. endif # GGML_CUDA_DEBUG
  547. ifdef GGML_CUDA_NVCC
  548. NVCC = $(CCACHE) $(GGML_CUDA_NVCC)
  549. else
  550. ifdef GGML_MUSA
  551. NVCC = $(CCACHE) mcc
  552. else
  553. NVCC = $(CCACHE) nvcc
  554. endif # GGML_MUSA
  555. endif # GGML_CUDA_NVCC
  556. ifdef CUDA_DOCKER_ARCH
  557. MK_NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  558. else ifndef CUDA_POWER_ARCH
  559. MK_NVCCFLAGS += -arch=native
  560. endif # CUDA_DOCKER_ARCH
  561. ifdef GGML_CUDA_FORCE_DMMV
  562. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  563. endif # GGML_CUDA_FORCE_DMMV
  564. ifdef GGML_CUDA_FORCE_MMQ
  565. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  566. endif # GGML_CUDA_FORCE_MMQ
  567. ifdef GGML_CUDA_FORCE_CUBLAS
  568. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  569. endif # GGML_CUDA_FORCE_CUBLAS
  570. ifdef GGML_CUDA_DMMV_X
  571. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  572. else
  573. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  574. endif # GGML_CUDA_DMMV_X
  575. ifdef GGML_CUDA_MMV_Y
  576. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  577. else ifdef GGML_CUDA_DMMV_Y
  578. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_DMMV_Y) # for backwards compatibility
  579. else
  580. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  581. endif # GGML_CUDA_MMV_Y
  582. ifdef GGML_CUDA_F16
  583. MK_NVCCFLAGS += -DGGML_CUDA_F16
  584. endif # GGML_CUDA_F16
  585. ifdef GGML_CUDA_DMMV_F16
  586. MK_NVCCFLAGS += -DGGML_CUDA_F16
  587. endif # GGML_CUDA_DMMV_F16
  588. ifdef GGML_CUDA_KQUANTS_ITER
  589. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  590. else
  591. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  592. endif
  593. ifdef GGML_CUDA_PEER_MAX_BATCH_SIZE
  594. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(GGML_CUDA_PEER_MAX_BATCH_SIZE)
  595. else
  596. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  597. endif # GGML_CUDA_PEER_MAX_BATCH_SIZE
  598. ifdef GGML_CUDA_NO_PEER_COPY
  599. MK_NVCCFLAGS += -DGGML_CUDA_NO_PEER_COPY
  600. endif # GGML_CUDA_NO_PEER_COPY
  601. ifdef GGML_CUDA_CCBIN
  602. MK_NVCCFLAGS += -ccbin $(GGML_CUDA_CCBIN)
  603. endif # GGML_CUDA_CCBIN
  604. ifdef GGML_CUDA_FA_ALL_QUANTS
  605. MK_NVCCFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  606. endif # GGML_CUDA_FA_ALL_QUANTS
  607. ifdef JETSON_EOL_MODULE_DETECT
  608. define NVCC_COMPILE
  609. $(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 $@
  610. endef # NVCC_COMPILE
  611. else
  612. ifdef GGML_MUSA
  613. define NVCC_COMPILE
  614. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -c $< -o $@
  615. endef # NVCC_COMPILE
  616. else
  617. define NVCC_COMPILE
  618. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  619. endef # NVCC_COMPILE
  620. endif # GGML_MUSA
  621. endif # JETSON_EOL_MODULE_DETECT
  622. ggml/src/ggml-cuda/%.o: \
  623. ggml/src/ggml-cuda/%.cu \
  624. ggml/include/ggml.h \
  625. ggml/src/ggml-common.h \
  626. ggml/src/ggml-cuda/common.cuh
  627. $(NVCC_COMPILE)
  628. ggml/src/ggml-cuda.o: \
  629. ggml/src/ggml-cuda.cu \
  630. ggml/include/ggml-cuda.h \
  631. ggml/include/ggml.h \
  632. ggml/include/ggml-backend.h \
  633. ggml/src/ggml-backend-impl.h \
  634. ggml/src/ggml-common.h \
  635. $(wildcard ggml/src/ggml-cuda/*.cuh)
  636. $(NVCC_COMPILE)
  637. endif # GGML_CUDA
  638. ifdef GGML_VULKAN
  639. MK_CPPFLAGS += -DGGML_USE_VULKAN
  640. MK_LDFLAGS += $(shell pkg-config --libs vulkan)
  641. OBJ_GGML += ggml/src/ggml-vulkan.o ggml/src/ggml-vulkan-shaders.o
  642. ifdef GGML_VULKAN_CHECK_RESULTS
  643. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  644. endif
  645. ifdef GGML_VULKAN_DEBUG
  646. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  647. endif
  648. ifdef GGML_VULKAN_MEMORY_DEBUG
  649. MK_CPPFLAGS += -DGGML_VULKAN_MEMORY_DEBUG
  650. endif
  651. ifdef GGML_VULKAN_PERF
  652. MK_CPPFLAGS += -DGGML_VULKAN_PERF
  653. endif
  654. ifdef GGML_VULKAN_VALIDATE
  655. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  656. endif
  657. ifdef GGML_VULKAN_RUN_TESTS
  658. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  659. endif
  660. GLSLC_CMD = glslc
  661. _ggml_vk_genshaders_cmd = $(shell pwd)/vulkan-shaders-gen
  662. _ggml_vk_header = ggml/src/ggml-vulkan-shaders.hpp
  663. _ggml_vk_source = ggml/src/ggml-vulkan-shaders.cpp
  664. _ggml_vk_input_dir = ggml/src/vulkan-shaders
  665. _ggml_vk_shader_deps = $(echo $(_ggml_vk_input_dir)/*.comp)
  666. ggml/src/ggml-vulkan.o: ggml/src/ggml-vulkan.cpp ggml/include/ggml-vulkan.h $(_ggml_vk_header) $(_ggml_vk_source)
  667. $(CXX) $(CXXFLAGS) $(shell pkg-config --cflags vulkan) -c $< -o $@
  668. $(_ggml_vk_header): $(_ggml_vk_source)
  669. $(_ggml_vk_source): $(_ggml_vk_shader_deps) vulkan-shaders-gen
  670. $(_ggml_vk_genshaders_cmd) \
  671. --glslc $(GLSLC_CMD) \
  672. --input-dir $(_ggml_vk_input_dir) \
  673. --target-hpp $(_ggml_vk_header) \
  674. --target-cpp $(_ggml_vk_source)
  675. vulkan-shaders-gen: ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
  676. $(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
  677. endif # GGML_VULKAN
  678. ifdef GGML_HIPBLAS
  679. ifeq ($(wildcard /opt/rocm),)
  680. ROCM_PATH ?= /usr
  681. AMDGPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  682. else
  683. ROCM_PATH ?= /opt/rocm
  684. AMDGPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  685. endif
  686. GGML_CUDA_DMMV_X ?= 32
  687. GGML_CUDA_MMV_Y ?= 1
  688. GGML_CUDA_KQUANTS_ITER ?= 2
  689. MK_CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUDA
  690. ifdef GGML_HIP_UMA
  691. MK_CPPFLAGS += -DGGML_HIP_UMA
  692. endif # GGML_HIP_UMA
  693. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  694. MK_LDFLAGS += -L$(ROCM_PATH)/lib64 -Wl,-rpath=$(ROCM_PATH)/lib64
  695. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  696. HIPCC ?= $(CCACHE) $(ROCM_PATH)/bin/hipcc
  697. HIPFLAGS += $(addprefix --offload-arch=,$(AMDGPU_TARGETS))
  698. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  699. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  700. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  701. ifdef GGML_CUDA_FORCE_DMMV
  702. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  703. endif # GGML_CUDA_FORCE_DMMV
  704. ifdef GGML_CUDA_FORCE_MMQ
  705. HIPFLAGS += -DGGML_CUDA_FORCE_MMQ
  706. endif # GGML_CUDA_FORCE_MMQ
  707. ifdef GGML_CUDA_FORCE_CUBLAS
  708. HIPFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  709. endif # GGML_CUDA_FORCE_CUBLAS
  710. ifdef GGML_CUDA_NO_PEER_COPY
  711. HIPFLAGS += -DGGML_CUDA_NO_PEER_COPY
  712. endif # GGML_CUDA_NO_PEER_COPY
  713. OBJ_GGML += ggml/src/ggml-cuda.o
  714. OBJ_GGML += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  715. OBJ_GGML += $(OBJ_CUDA_TMPL)
  716. ggml/src/ggml-cuda.o: \
  717. ggml/src/ggml-cuda.cu \
  718. ggml/include/ggml-cuda.h \
  719. ggml/include/ggml.h \
  720. ggml/include/ggml-backend.h \
  721. ggml/src/ggml-backend-impl.h \
  722. ggml/src/ggml-common.h \
  723. $(wildcard ggml/src/ggml-cuda/*.cuh)
  724. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  725. ggml/src/ggml-cuda/%.o: \
  726. ggml/src/ggml-cuda/%.cu \
  727. ggml/include/ggml.h \
  728. ggml/src/ggml-common.h \
  729. ggml/src/ggml-cuda/common.cuh
  730. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  731. endif # GGML_HIPBLAS
  732. ifdef GGML_METAL
  733. MK_CPPFLAGS += -DGGML_USE_METAL
  734. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  735. OBJ_GGML += ggml/src/ggml-metal.o
  736. ifdef GGML_METAL_NDEBUG
  737. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  738. endif
  739. ifdef GGML_METAL_EMBED_LIBRARY
  740. MK_CPPFLAGS += -DGGML_METAL_EMBED_LIBRARY
  741. OBJ_GGML += ggml/src/ggml-metal-embed.o
  742. endif
  743. endif # GGML_METAL
  744. ifdef GGML_METAL
  745. ggml/src/ggml-metal.o: \
  746. ggml/src/ggml-metal.m \
  747. ggml/include/ggml-metal.h \
  748. ggml/include/ggml.h
  749. $(CC) $(CFLAGS) -c $< -o $@
  750. ifdef GGML_METAL_EMBED_LIBRARY
  751. ggml/src/ggml-metal-embed.o: \
  752. ggml/src/ggml-metal.metal \
  753. ggml/src/ggml-common.h
  754. @echo "Embedding Metal library"
  755. @sed -e '/#include "ggml-common.h"/r ggml/src/ggml-common.h' -e '/#include "ggml-common.h"/d' < ggml/src/ggml-metal.metal > ggml/src/ggml-metal-embed.metal
  756. $(eval TEMP_ASSEMBLY=$(shell mktemp -d))
  757. @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  758. @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  759. @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  760. @echo ".incbin \"ggml/src/ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  761. @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  762. @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  763. $(CC) $(CFLAGS) -c $(TEMP_ASSEMBLY)/ggml-metal-embed.s -o $@
  764. @rm -f ${TEMP_ASSEMBLY}/ggml-metal-embed.s
  765. @rmdir ${TEMP_ASSEMBLY}
  766. endif
  767. endif # GGML_METAL
  768. OBJ_GGML += \
  769. ggml/src/ggml.o \
  770. ggml/src/ggml-alloc.o \
  771. ggml/src/ggml-backend.o \
  772. ggml/src/ggml-quants.o \
  773. ggml/src/ggml-aarch64.o
  774. OBJ_LLAMA = \
  775. src/llama.o \
  776. src/llama-vocab.o \
  777. src/llama-grammar.o \
  778. src/llama-sampling.o \
  779. src/unicode.o \
  780. src/unicode-data.o
  781. OBJ_COMMON = \
  782. common/common.o \
  783. common/arg.o \
  784. common/console.o \
  785. common/ngram-cache.o \
  786. common/sampling.o \
  787. common/train.o \
  788. common/build-info.o \
  789. common/json-schema-to-grammar.o
  790. OBJ_ALL = $(OBJ_GGML) $(OBJ_LLAMA) $(OBJ_COMMON)
  791. LIB_GGML = $(LIB_PRE)ggml$(DSO_EXT)
  792. LIB_GGML_S = $(LIB_PRE)ggml.a
  793. LIB_LLAMA = $(LIB_PRE)llama$(DSO_EXT)
  794. LIB_LLAMA_S = $(LIB_PRE)llama.a
  795. LIB_COMMON = $(LIB_PRE)common$(DSO_EXT)
  796. LIB_COMMON_S = $(LIB_PRE)common.a
  797. LIB_ALL = $(LIB_GGML) $(LIB_LLAMA) $(LIB_COMMON)
  798. LIB_ALL_S = $(LIB_GGML_S) $(LIB_LLAMA_S) $(LIB_COMMON_S)
  799. GF_CC := $(CC)
  800. include scripts/get-flags.mk
  801. # combine build flags with cmdline overrides
  802. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  803. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  804. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  805. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  806. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  807. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  808. # identify CUDA host compiler
  809. ifdef GGML_CUDA
  810. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  811. include scripts/get-flags.mk
  812. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  813. endif
  814. ifdef LLAMA_CURL
  815. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  816. override LDFLAGS := $(LDFLAGS) -lcurl
  817. endif
  818. #
  819. # Print build information
  820. #
  821. $(info I llama.cpp build info: )
  822. $(info I UNAME_S: $(UNAME_S))
  823. $(info I UNAME_P: $(UNAME_P))
  824. $(info I UNAME_M: $(UNAME_M))
  825. $(info I CFLAGS: $(CFLAGS))
  826. $(info I CXXFLAGS: $(CXXFLAGS))
  827. $(info I NVCCFLAGS: $(NVCCFLAGS))
  828. $(info I LDFLAGS: $(LDFLAGS))
  829. $(info I CC: $(shell $(CC) --version | head -n 1))
  830. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  831. ifdef GGML_CUDA
  832. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  833. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  834. ifndef GGML_MUSA
  835. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  836. ifndef CUDA_DOCKER_ARCH
  837. ifndef CUDA_POWER_ARCH
  838. $(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 )
  839. endif # CUDA_POWER_ARCH
  840. endif # CUDA_DOCKER_ARCH
  841. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  842. endif # GGML_MUSA
  843. endif # GGML_CUDA
  844. $(info )
  845. ifdef DEPRECATE_WARNING
  846. $(info !!! DEPRECATION WARNING !!!)
  847. $(info The following LLAMA_ options are deprecated and will be removed in the future. Use the GGML_ prefix instead)
  848. $(info - LLAMA_CUDA)
  849. $(info - LLAMA_METAL)
  850. $(info - LLAMA_METAL_EMBED_LIBRARY)
  851. $(info - LLAMA_OPENMP)
  852. $(info - LLAMA_RPC)
  853. $(info - LLAMA_SYCL)
  854. $(info - LLAMA_SYCL_F16)
  855. $(info - LLAMA_OPENBLAS)
  856. $(info - LLAMA_OPENBLAS64)
  857. $(info - LLAMA_BLIS)
  858. $(info - LLAMA_NO_LLAMAFILE)
  859. $(info - LLAMA_NO_ACCELERATE)
  860. $(info - LLAMA_NO_OPENMP)
  861. $(info - LLAMA_NO_METAL)
  862. $(info - LLAMA_NO_CCACHE)
  863. $(info )
  864. endif
  865. #
  866. # Build libraries
  867. #
  868. # ggml
  869. ggml/src/ggml.o: \
  870. ggml/src/ggml.c \
  871. ggml/include/ggml.h
  872. $(CC) $(CFLAGS) -c $< -o $@
  873. ggml/src/ggml-alloc.o: \
  874. ggml/src/ggml-alloc.c \
  875. ggml/include/ggml.h \
  876. ggml/include/ggml-alloc.h
  877. $(CC) $(CFLAGS) -c $< -o $@
  878. ggml/src/ggml-backend.o: \
  879. ggml/src/ggml-backend.c \
  880. ggml/include/ggml.h \
  881. ggml/include/ggml-backend.h
  882. $(CC) $(CFLAGS) -c $< -o $@
  883. ggml/src/ggml-quants.o: \
  884. ggml/src/ggml-quants.c \
  885. ggml/include/ggml.h \
  886. ggml/src/ggml-quants.h \
  887. ggml/src/ggml-common.h
  888. $(CC) $(CFLAGS) -c $< -o $@
  889. ggml/src/ggml-aarch64.o: \
  890. ggml/src/ggml-aarch64.c \
  891. ggml/include/ggml.h \
  892. ggml/src/ggml-aarch64.h \
  893. ggml/src/ggml-common.h
  894. $(CC) $(CFLAGS) -c $< -o $@
  895. ggml/src/ggml-blas.o: \
  896. ggml/src/ggml-blas.cpp \
  897. ggml/include/ggml-blas.h
  898. $(CXX) $(CXXFLAGS) -c $< -o $@
  899. ifndef GGML_NO_LLAMAFILE
  900. ggml/src/llamafile/sgemm.o: \
  901. ggml/src/llamafile/sgemm.cpp \
  902. ggml/src/llamafile/sgemm.h \
  903. ggml/include/ggml.h
  904. $(CXX) $(CXXFLAGS) -c $< -o $@
  905. endif # GGML_NO_LLAMAFILE
  906. ifdef GGML_RPC
  907. ggml/src/ggml-rpc.o: \
  908. ggml/src/ggml-rpc.cpp \
  909. ggml/include/ggml-rpc.h
  910. $(CXX) $(CXXFLAGS) -c $< -o $@
  911. endif # GGML_RPC
  912. $(LIB_GGML): \
  913. $(OBJ_GGML)
  914. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  915. $(LIB_GGML_S): \
  916. $(OBJ_GGML)
  917. ar rcs $(LIB_GGML_S) $^
  918. # llama
  919. src/unicode.o: \
  920. src/unicode.cpp \
  921. src/unicode.h
  922. $(CXX) $(CXXFLAGS) -c $< -o $@
  923. src/unicode-data.o: \
  924. src/unicode-data.cpp \
  925. src/unicode-data.h
  926. $(CXX) $(CXXFLAGS) -c $< -o $@
  927. src/llama.o: \
  928. src/llama.cpp \
  929. src/llama-impl.h \
  930. src/llama-vocab.h \
  931. src/llama-grammar.h \
  932. src/llama-sampling.h \
  933. src/unicode.h \
  934. include/llama.h \
  935. ggml/include/ggml-cuda.h \
  936. ggml/include/ggml-metal.h \
  937. ggml/include/ggml.h \
  938. ggml/include/ggml-alloc.h \
  939. ggml/include/ggml-backend.h
  940. $(CXX) $(CXXFLAGS) -c $< -o $@
  941. src/llama-vocab.o: \
  942. src/llama-vocab.cpp \
  943. src/llama-vocab.h \
  944. src/llama-impl.h \
  945. include/llama.h
  946. $(CXX) $(CXXFLAGS) -c $< -o $@
  947. src/llama-grammar.o: \
  948. src/llama-grammar.cpp \
  949. src/llama-grammar.h \
  950. src/llama-impl.h \
  951. src/llama-vocab.h \
  952. src/llama-sampling.h \
  953. include/llama.h
  954. $(CXX) $(CXXFLAGS) -c $< -o $@
  955. src/llama-sampling.o: \
  956. src/llama-sampling.cpp \
  957. src/llama-sampling.h \
  958. src/llama-impl.h \
  959. include/llama.h
  960. $(CXX) $(CXXFLAGS) -c $< -o $@
  961. $(LIB_LLAMA): \
  962. $(OBJ_LLAMA) \
  963. $(LIB_GGML)
  964. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  965. $(LIB_LLAMA_S): \
  966. $(OBJ_LLAMA)
  967. ar rcs $(LIB_LLAMA_S) $^
  968. # common
  969. common/common.o: \
  970. common/common.cpp \
  971. common/common.h \
  972. common/console.h \
  973. common/sampling.h \
  974. common/json.hpp \
  975. common/json-schema-to-grammar.h \
  976. include/llama.h
  977. $(CXX) $(CXXFLAGS) -c $< -o $@
  978. common/arg.o: \
  979. common/arg.cpp \
  980. common/arg.h
  981. $(CXX) $(CXXFLAGS) -c $< -o $@
  982. common/sampling.o: \
  983. common/sampling.cpp \
  984. common/sampling.h \
  985. include/llama.h
  986. $(CXX) $(CXXFLAGS) -c $< -o $@
  987. common/console.o: \
  988. common/console.cpp \
  989. common/console.h
  990. $(CXX) $(CXXFLAGS) -c $< -o $@
  991. common/json-schema-to-grammar.o: \
  992. common/json-schema-to-grammar.cpp \
  993. common/json-schema-to-grammar.h
  994. $(CXX) $(CXXFLAGS) -c $< -o $@
  995. common/train.o: \
  996. common/train.cpp \
  997. common/train.h
  998. $(CXX) $(CXXFLAGS) -c $< -o $@
  999. common/ngram-cache.o: \
  1000. common/ngram-cache.cpp \
  1001. common/ngram-cache.h
  1002. $(CXX) $(CXXFLAGS) -c $< -o $@
  1003. $(LIB_COMMON): \
  1004. $(OBJ_COMMON) \
  1005. $(LIB_LLAMA) \
  1006. $(LIB_GGML)
  1007. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  1008. $(LIB_COMMON_S): \
  1009. $(OBJ_COMMON)
  1010. ar rcs $(LIB_COMMON_S) $^
  1011. clean:
  1012. rm -vrf *.dot $(BUILD_TARGETS) $(TEST_TARGETS)
  1013. rm -rvf src/*.o
  1014. rm -rvf tests/*.o
  1015. rm -rvf examples/*.o
  1016. rm -rvf common/*.o
  1017. rm -rvf *.a
  1018. rm -rvf *.dll
  1019. rm -rvf *.so
  1020. rm -rvf *.dot
  1021. rm -rvf ggml/*.a
  1022. rm -rvf ggml/*.dll
  1023. rm -rvf ggml/*.so
  1024. rm -vrf ggml/src/*.o
  1025. rm -rvf ggml/src/llamafile/*.o
  1026. rm -rvf common/build-info.cpp
  1027. rm -vrf ggml/src/ggml-metal-embed.metal
  1028. rm -vrf ggml/src/ggml-cuda/*.o
  1029. rm -vrf ggml/src/ggml-cuda/template-instances/*.o
  1030. rm -rvf $(BUILD_TARGETS)
  1031. rm -rvf $(TEST_TARGETS)
  1032. rm -f vulkan-shaders-gen ggml/src/ggml-vulkan-shaders.hpp ggml/src/ggml-vulkan-shaders.cpp
  1033. rm -rvf $(LEGACY_TARGETS_CLEAN)
  1034. find examples pocs -type f -name "*.o" -delete
  1035. #
  1036. # Examples
  1037. #
  1038. # $< is the first prerequisite, i.e. the source file.
  1039. # Explicitly compile this to an object file so that it can be cached with ccache.
  1040. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  1041. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  1042. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  1043. llama-cli: examples/main/main.cpp \
  1044. $(OBJ_ALL)
  1045. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1046. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1047. @echo
  1048. @echo '==== Run ./llama-cli -h for help. ===='
  1049. @echo
  1050. llama-infill: examples/infill/infill.cpp \
  1051. $(OBJ_ALL)
  1052. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1053. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1054. llama-simple: examples/simple/simple.cpp \
  1055. $(OBJ_ALL)
  1056. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1057. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1058. llama-tokenize: examples/tokenize/tokenize.cpp \
  1059. $(OBJ_ALL)
  1060. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1061. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1062. llama-batched: examples/batched/batched.cpp \
  1063. $(OBJ_ALL)
  1064. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1065. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1066. llama-batched-bench: examples/batched-bench/batched-bench.cpp \
  1067. $(OBJ_ALL)
  1068. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1069. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1070. llama-quantize: examples/quantize/quantize.cpp \
  1071. $(OBJ_ALL)
  1072. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1073. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1074. llama-quantize-stats: examples/quantize-stats/quantize-stats.cpp \
  1075. $(OBJ_ALL)
  1076. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1077. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1078. llama-perplexity: examples/perplexity/perplexity.cpp \
  1079. $(OBJ_ALL)
  1080. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1081. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1082. llama-imatrix: examples/imatrix/imatrix.cpp \
  1083. $(OBJ_ALL)
  1084. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1085. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1086. llama-embedding: examples/embedding/embedding.cpp \
  1087. $(OBJ_ALL)
  1088. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1089. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1090. llama-gritlm: examples/gritlm/gritlm.cpp \
  1091. $(OBJ_ALL)
  1092. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1093. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1094. llama-save-load-state: examples/save-load-state/save-load-state.cpp \
  1095. $(OBJ_ALL)
  1096. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1097. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1098. llama-gguf: examples/gguf/gguf.cpp \
  1099. $(OBJ_GGML)
  1100. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1101. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1102. examples/gguf-hash/deps/sha1/sha1.o: \
  1103. examples/gguf-hash/deps/sha1/sha1.c
  1104. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1105. examples/gguf-hash/deps/xxhash/xxhash.o: \
  1106. examples/gguf-hash/deps/xxhash/xxhash.c
  1107. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1108. examples/gguf-hash/deps/sha256/sha256.o: \
  1109. examples/gguf-hash/deps/sha256/sha256.c
  1110. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1111. 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\
  1112. $(OBJ_ALL)
  1113. $(CXX) $(CXXFLAGS) -Iexamples/gguf-hash/deps -c $< -o $(call GET_OBJ_FILE, $<)
  1114. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1115. llama-gguf-split: examples/gguf-split/gguf-split.cpp \
  1116. $(OBJ_ALL)
  1117. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1118. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1119. llama-eval-callback: examples/eval-callback/eval-callback.cpp \
  1120. $(OBJ_ALL)
  1121. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1122. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1123. llama-cvector-generator: examples/cvector-generator/cvector-generator.cpp \
  1124. $(OBJ_ALL)
  1125. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1126. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1127. llama-convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp \
  1128. $(OBJ_GGML) $(OBJ_LLAMA)
  1129. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1130. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1131. llama-bench: examples/llama-bench/llama-bench.cpp \
  1132. $(OBJ_ALL)
  1133. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1134. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1135. llama-baby-llama: examples/baby-llama/baby-llama.cpp \
  1136. $(OBJ_ALL)
  1137. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1138. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1139. llama-export-lora: examples/export-lora/export-lora.cpp \
  1140. $(OBJ_ALL)
  1141. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1142. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1143. llama-retrieval: examples/retrieval/retrieval.cpp \
  1144. $(OBJ_ALL)
  1145. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1146. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1147. llama-speculative: examples/speculative/speculative.cpp \
  1148. $(OBJ_ALL)
  1149. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1150. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1151. llama-parallel: examples/parallel/parallel.cpp \
  1152. $(OBJ_ALL)
  1153. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1154. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1155. llama-lookahead: examples/lookahead/lookahead.cpp \
  1156. $(OBJ_ALL)
  1157. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1158. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1159. llama-lookup: examples/lookup/lookup.cpp \
  1160. $(OBJ_ALL)
  1161. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1162. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1163. llama-lookup-create: examples/lookup/lookup-create.cpp \
  1164. $(OBJ_ALL)
  1165. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1166. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1167. llama-lookup-merge: examples/lookup/lookup-merge.cpp \
  1168. $(OBJ_ALL)
  1169. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1170. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1171. llama-lookup-stats: examples/lookup/lookup-stats.cpp \
  1172. $(OBJ_ALL)
  1173. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1174. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1175. llama-passkey: examples/passkey/passkey.cpp \
  1176. $(OBJ_ALL)
  1177. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1178. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1179. llama-gbnf-validator: examples/gbnf-validator/gbnf-validator.cpp \
  1180. $(OBJ_ALL)
  1181. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1182. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1183. ifdef GGML_RPC
  1184. rpc-server: examples/rpc/rpc-server.cpp \
  1185. $(OBJ_GGML)
  1186. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  1187. endif # GGML_RPC
  1188. llama-server: \
  1189. examples/server/server.cpp \
  1190. examples/server/utils.hpp \
  1191. examples/server/httplib.h \
  1192. examples/server/colorthemes.css.hpp \
  1193. examples/server/style.css.hpp \
  1194. examples/server/theme-beeninorder.css.hpp \
  1195. examples/server/theme-ketivah.css.hpp \
  1196. examples/server/theme-mangotango.css.hpp \
  1197. examples/server/theme-playground.css.hpp \
  1198. examples/server/theme-polarnight.css.hpp \
  1199. examples/server/theme-snowstorm.css.hpp \
  1200. examples/server/index.html.hpp \
  1201. examples/server/index-new.html.hpp \
  1202. examples/server/index.js.hpp \
  1203. examples/server/completion.js.hpp \
  1204. examples/server/system-prompts.js.hpp \
  1205. examples/server/prompt-formats.js.hpp \
  1206. examples/server/json-schema-to-grammar.mjs.hpp \
  1207. common/json.hpp \
  1208. common/stb_image.h \
  1209. $(OBJ_ALL)
  1210. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1211. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  1212. # Portable equivalent of `cd examples/server/public && xxd -i $(notdir $<) ../$(notdir $<).hpp`:
  1213. examples/server/%.hpp: examples/server/public/% Makefile
  1214. @( export NAME=$(subst .,_,$(subst -,_,$(notdir $<))) && \
  1215. echo "unsigned char $${NAME}[] = {" && \
  1216. cat $< | od -v -t x1 -An | sed -E 's/([0-9a-fA-F]+)/0x\1, /g' && \
  1217. echo "};" && \
  1218. echo "unsigned int $${NAME}_len = $(shell cat $< | wc -c );" \
  1219. ) > $@
  1220. llama-gen-docs: examples/gen-docs/gen-docs.cpp \
  1221. $(OBJ_ALL)
  1222. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1223. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1224. ./llama-gen-docs
  1225. libllava.a: examples/llava/llava.cpp \
  1226. examples/llava/llava.h \
  1227. examples/llava/clip.cpp \
  1228. examples/llava/clip.h \
  1229. common/stb_image.h \
  1230. common/base64.hpp \
  1231. $(OBJ_ALL)
  1232. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  1233. llama-llava-cli: examples/llava/llava-cli.cpp \
  1234. examples/llava/llava.cpp \
  1235. examples/llava/llava.h \
  1236. examples/llava/clip.cpp \
  1237. examples/llava/clip.h \
  1238. $(OBJ_ALL)
  1239. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1240. llama-minicpmv-cli: examples/llava/minicpmv-cli.cpp \
  1241. examples/llava/llava.cpp \
  1242. examples/llava/llava.h \
  1243. examples/llava/clip.cpp \
  1244. examples/llava/clip.h \
  1245. $(OBJ_ALL)
  1246. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1247. ifeq ($(UNAME_S),Darwin)
  1248. swift: examples/batched.swift
  1249. (cd examples/batched.swift; make build)
  1250. endif
  1251. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  1252. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  1253. @if ! cmp -s $@.tmp $@; then \
  1254. mv $@.tmp $@; \
  1255. else \
  1256. rm $@.tmp; \
  1257. fi
  1258. common/build-info.o: common/build-info.cpp
  1259. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  1260. #
  1261. # Tests
  1262. #
  1263. tests: $(TEST_TARGETS)
  1264. llama-benchmark-matmult: examples/benchmark/benchmark-matmult.cpp \
  1265. $(OBJ_GGML) common/build-info.o
  1266. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1267. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1268. run-benchmark-matmult: llama-benchmark-matmult
  1269. ./$@
  1270. .PHONY: run-benchmark-matmult swift
  1271. tests/test-arg-parser: tests/test-arg-parser.cpp \
  1272. $(OBJ_ALL)
  1273. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1274. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1275. tests/test-llama-grammar: tests/test-llama-grammar.cpp \
  1276. $(OBJ_ALL)
  1277. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1278. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1279. tests/test-grammar-parser: tests/test-grammar-parser.cpp \
  1280. $(OBJ_ALL)
  1281. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1282. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1283. tests/test-grammar-integration: tests/test-grammar-integration.cpp \
  1284. $(OBJ_ALL)
  1285. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1286. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1287. tests/test-double-float: tests/test-double-float.cpp
  1288. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1289. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1290. tests/test-json-schema-to-grammar: tests/test-json-schema-to-grammar.cpp \
  1291. $(OBJ_ALL)
  1292. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  1293. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1294. tests/test-grad0: tests/test-grad0.cpp \
  1295. $(OBJ_GGML)
  1296. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1297. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1298. tests/test-opt: tests/test-opt.cpp \
  1299. $(OBJ_GGML)
  1300. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1301. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1302. tests/test-quantize-fns: tests/test-quantize-fns.cpp \
  1303. $(OBJ_GGML)
  1304. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1305. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1306. tests/test-quantize-perf: tests/test-quantize-perf.cpp \
  1307. $(OBJ_GGML)
  1308. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1309. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1310. tests/test-sampling: tests/test-sampling.cpp \
  1311. $(OBJ_ALL)
  1312. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1313. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1314. tests/test-tokenizer-0: tests/test-tokenizer-0.cpp \
  1315. $(OBJ_ALL)
  1316. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1317. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1318. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp \
  1319. $(OBJ_ALL)
  1320. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1321. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1322. tests/test-tokenizer-1-spm: tests/test-tokenizer-1-spm.cpp \
  1323. $(OBJ_ALL)
  1324. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1325. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1326. tests/test-rope: tests/test-rope.cpp ggml/src/ggml.o \
  1327. $(OBJ_GGML)
  1328. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1329. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1330. tests/test-c.o: tests/test-c.c include/llama.h
  1331. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  1332. tests/test-backend-ops: tests/test-backend-ops.cpp \
  1333. $(OBJ_GGML)
  1334. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1335. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1336. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp tests/get-model.cpp \
  1337. $(OBJ_ALL)
  1338. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1339. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1340. tests/test-autorelease: tests/test-autorelease.cpp tests/get-model.cpp \
  1341. $(OBJ_ALL)
  1342. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1343. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1344. tests/test-chat-template: tests/test-chat-template.cpp \
  1345. $(OBJ_ALL)
  1346. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1347. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1348. #
  1349. # PoCs
  1350. #
  1351. llama-vdot: pocs/vdot/vdot.cpp ggml/src/ggml.o \
  1352. $(OBJ_GGML)
  1353. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1354. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1355. llama-q8dot: pocs/vdot/q8dot.cpp ggml/src/ggml.o \
  1356. $(OBJ_GGML)
  1357. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1358. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1359. #
  1360. # Deprecated binaries that we want to keep around long enough for people to migrate to the new filenames, then these can be removed.
  1361. #
  1362. # Mark legacy binary targets as .PHONY so that they are always checked.
  1363. .PHONY: main quantize perplexity embedding server
  1364. # Define the object file target
  1365. examples/deprecation-warning/deprecation-warning.o: examples/deprecation-warning/deprecation-warning.cpp
  1366. $(CXX) $(CXXFLAGS) -c $< -o $@
  1367. # NOTE: We currently will always build the deprecation-warning `main` and `server` binaries to help users migrate.
  1368. # Eventually we will want to remove these target from building all the time.
  1369. main: examples/deprecation-warning/deprecation-warning.o
  1370. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1371. @echo "NOTICE: The 'main' binary is deprecated. Please use 'llama-cli' instead."
  1372. server: examples/deprecation-warning/deprecation-warning.o
  1373. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1374. @echo "NOTICE: The 'server' binary is deprecated. Please use 'llama-server' instead."
  1375. quantize: examples/deprecation-warning/deprecation-warning.o
  1376. ifneq (,$(wildcard quantize))
  1377. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1378. @echo "#########"
  1379. @echo "WARNING: The 'quantize' binary is deprecated. Please use 'llama-quantize' instead."
  1380. @echo " Remove the 'quantize' binary to remove this warning."
  1381. @echo "#########"
  1382. endif
  1383. perplexity: examples/deprecation-warning/deprecation-warning.o
  1384. ifneq (,$(wildcard perplexity))
  1385. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1386. @echo "#########"
  1387. @echo "WARNING: The 'perplexity' binary is deprecated. Please use 'llama-perplexity' instead."
  1388. @echo " Remove the 'perplexity' binary to remove this warning."
  1389. @echo "#########"
  1390. endif
  1391. embedding: examples/deprecation-warning/deprecation-warning.o
  1392. ifneq (,$(wildcard embedding))
  1393. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1394. @echo "#########"
  1395. @echo "WARNING: The 'embedding' binary is deprecated. Please use 'llama-embedding' instead."
  1396. @echo " Remove the 'embedding' binary to remove this warning."
  1397. @echo "#########"
  1398. endif