Makefile 49 KB

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