Makefile 49 KB

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