Makefile 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. # Define the default target now so that it is always the first target
  2. BUILD_TARGETS = \
  3. libllava.a \
  4. llama-baby-llama \
  5. llama-batched \
  6. llama-batched-bench \
  7. llama-bench \
  8. llama-benchmark-matmult \
  9. llama-cli \
  10. llama-convert-llama2c-to-ggml \
  11. llama-embedding \
  12. llama-eval-callback \
  13. llama-export-lora \
  14. llama-gbnf-validator \
  15. llama-gguf \
  16. llama-gguf-hash \
  17. llama-gguf-split \
  18. llama-gritlm \
  19. llama-imatrix \
  20. llama-infill \
  21. llama-llava-cli \
  22. llama-minicpmv-cli\
  23. llama-lookahead \
  24. llama-lookup \
  25. llama-lookup-create \
  26. llama-lookup-merge \
  27. llama-lookup-stats \
  28. llama-parallel \
  29. llama-passkey \
  30. llama-perplexity \
  31. llama-q8dot \
  32. llama-quantize \
  33. llama-quantize-stats \
  34. llama-retrieval \
  35. llama-save-load-state \
  36. llama-server \
  37. llama-simple \
  38. llama-speculative \
  39. llama-tokenize \
  40. llama-vdot \
  41. llama-cvector-generator \
  42. llama-gen-docs \
  43. tests/test-c.o
  44. # Binaries only useful for tests
  45. TEST_TARGETS = \
  46. tests/test-arg-parser \
  47. tests/test-autorelease \
  48. tests/test-backend-ops \
  49. tests/test-chat-template \
  50. tests/test-double-float \
  51. tests/test-grad0 \
  52. tests/test-grammar-integration \
  53. tests/test-grammar-parser \
  54. tests/test-json-schema-to-grammar \
  55. tests/test-llama-grammar \
  56. tests/test-model-load-cancel \
  57. tests/test-opt \
  58. tests/test-quantize-fns \
  59. tests/test-quantize-perf \
  60. tests/test-rope \
  61. tests/test-sampling \
  62. tests/test-tokenizer-0 \
  63. tests/test-tokenizer-1-bpe \
  64. tests/test-tokenizer-1-spm
  65. # Legacy build targets that were renamed in #7809, but should still be removed when the project is cleaned
  66. LEGACY_TARGETS_CLEAN = main quantize quantize-stats perplexity imatrix embedding vdot q8dot convert-llama2c-to-ggml \
  67. simple batched batched-bench save-load-state server gguf gguf-split eval-callback llama-bench libllava.a llava-cli baby-llama \
  68. retrieval speculative infill tokenize benchmark-matmult parallel export-lora lookahead lookup passkey gritlm
  69. # Legacy build targets that were renamed in #7809, but we want to build binaries that for them that output a deprecation warning if people try to use them.
  70. # We don't want to clutter things too much, so we only build replacements for the most commonly used binaries.
  71. LEGACY_TARGETS_BUILD = main quantize perplexity embedding server
  72. # Deprecation aliases
  73. ifdef LLAMA_CUBLAS
  74. $(error LLAMA_CUBLAS is removed. Use GGML_CUDA instead.)
  75. endif
  76. ifdef LLAMA_CUDA
  77. GGML_CUDA := 1
  78. DEPRECATE_WARNING := 1
  79. endif
  80. ifdef LLAMA_KOMPUTE
  81. GGML_KOMPUTE := 1
  82. DEPRECATE_WARNING := 1
  83. endif
  84. ifdef LLAMA_METAL
  85. GGML_METAL := 1
  86. DEPRECATE_WARNING := 1
  87. endif
  88. ifdef LLAMA_OPENMP
  89. GGML_OPENMP := 1
  90. DEPRECATE_WARNING := 1
  91. endif
  92. ifdef LLAMA_RPC
  93. GGML_RPC := 1
  94. DEPRECATE_WARNING := 1
  95. endif
  96. ifdef LLAMA_SYCL
  97. GGML_SYCL := 1
  98. DEPRECATE_WARNING := 1
  99. endif
  100. ifdef LLAMA_SYCL_F16
  101. GGML_SYCL_F16 := 1
  102. DEPRECATE_WARNING := 1
  103. endif
  104. ifdef LLAMA_OPENBLAS
  105. GGML_OPENBLAS := 1
  106. DEPRECATE_WARNING := 1
  107. endif
  108. ifdef LLAMA_OPENBLAS64
  109. GGML_OPENBLAS64 := 1
  110. DEPRECATE_WARNING := 1
  111. endif
  112. ifdef LLAMA_BLIS
  113. GGML_BLIS := 1
  114. DEPRECATE_WARNING := 1
  115. endif
  116. ifdef LLAMA_NO_LLAMAFILE
  117. GGML_NO_LLAMAFILE := 1
  118. DEPRECATE_WARNING := 1
  119. endif
  120. ifdef LLAMA_NO_ACCELERATE
  121. GGML_NO_ACCELERATE := 1
  122. DEPRECATE_WARNING := 1
  123. endif
  124. ifdef LLAMA_NO_OPENMP
  125. GGML_NO_OPENMP := 1
  126. DEPRECATE_WARNING := 1
  127. endif
  128. ifdef LLAMA_NO_METAL
  129. GGML_NO_METAL := 1
  130. DEPRECATE_WARNING := 1
  131. endif
  132. ifndef UNAME_S
  133. UNAME_S := $(shell uname -s)
  134. endif
  135. ifndef UNAME_P
  136. UNAME_P := $(shell uname -p)
  137. endif
  138. ifndef UNAME_M
  139. UNAME_M := $(shell uname -m)
  140. endif
  141. # In GNU make default CXX is g++ instead of c++. Let's fix that so that users
  142. # of non-gcc compilers don't have to provide g++ alias or wrapper.
  143. DEFCC := cc
  144. DEFCXX := c++
  145. ifeq ($(origin CC),default)
  146. CC := $(DEFCC)
  147. endif
  148. ifeq ($(origin CXX),default)
  149. CXX := $(DEFCXX)
  150. endif
  151. # Mac OS + Arm can report x86_64
  152. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  153. ifeq ($(UNAME_S),Darwin)
  154. ifndef GGML_NO_METAL
  155. GGML_METAL := 1
  156. endif
  157. GGML_NO_OPENMP := 1
  158. ifneq ($(UNAME_P),arm)
  159. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  160. ifeq ($(SYSCTL_M),1)
  161. # UNAME_P := arm
  162. # UNAME_M := arm64
  163. warn := $(warning Your arch is announced as x86_64, but it seems to actually be ARM64. Not fixing that can lead to bad performance. For more info see: https://github.com/ggerganov/whisper.cpp/issues/66\#issuecomment-1282546789)
  164. endif
  165. endif
  166. endif
  167. ifdef GGML_METAL
  168. GGML_METAL_EMBED_LIBRARY := 1
  169. endif
  170. ifdef GGML_RPC
  171. BUILD_TARGETS += rpc-server
  172. endif
  173. ifdef GGML_VULKAN
  174. BUILD_TARGETS += vulkan-shaders-gen
  175. endif
  176. default: $(BUILD_TARGETS) $(LEGACY_TARGETS_BUILD)
  177. test: $(TEST_TARGETS)
  178. @failures=0; \
  179. for test_target in $(TEST_TARGETS); do \
  180. if [ "$$test_target" = "tests/test-tokenizer-0" ]; then \
  181. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-spm.gguf; \
  182. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-bpe.gguf; \
  183. ./$$test_target $(CURDIR)/models/ggml-vocab-phi-3.gguf; \
  184. ./$$test_target $(CURDIR)/models/ggml-vocab-falcon.gguf; \
  185. ./$$test_target $(CURDIR)/models/ggml-vocab-bert-bge.gguf; \
  186. ./$$test_target $(CURDIR)/models/ggml-vocab-starcoder.gguf; \
  187. ./$$test_target $(CURDIR)/models/ggml-vocab-gpt-2.gguf; \
  188. ./$$test_target $(CURDIR)/models/ggml-vocab-refact.gguf; \
  189. elif [ "$$test_target" = "tests/test-tokenizer-1-spm" ]; then \
  190. continue; \
  191. elif [ "$$test_target" = "tests/test-tokenizer-1-bpe" ]; then \
  192. continue; \
  193. else \
  194. echo "Running test $$test_target..."; \
  195. ./$$test_target; \
  196. fi; \
  197. if [ $$? -ne 0 ]; then \
  198. printf 'Test %s FAILED!\n\n' $$test_target; \
  199. failures=$$(( failures + 1 )); \
  200. else \
  201. printf 'Test %s passed.\n\n' $$test_target; \
  202. fi; \
  203. done; \
  204. if [ $$failures -gt 0 ]; then \
  205. printf '\n%s tests failed.\n' $$failures; \
  206. exit 1; \
  207. fi
  208. @echo 'All tests passed.'
  209. all: $(BUILD_TARGETS) $(TEST_TARGETS) $(LEGACY_TARGETS_BUILD)
  210. ifdef RISCV_CROSS_COMPILE
  211. CC := riscv64-unknown-linux-gnu-gcc
  212. CXX := riscv64-unknown-linux-gnu-g++
  213. endif
  214. #
  215. # Compile flags
  216. #
  217. # keep standard at C11 and C++11
  218. MK_CPPFLAGS = -Iggml/include -Iggml/src -Iinclude -Isrc -Icommon
  219. MK_CFLAGS = -std=c11 -fPIC
  220. MK_CXXFLAGS = -std=c++11 -fPIC
  221. MK_NVCCFLAGS = -std=c++11
  222. ifdef LLAMA_NO_CCACHE
  223. GGML_NO_CCACHE := 1
  224. DEPRECATE_WARNING := 1
  225. endif
  226. ifndef GGML_NO_CCACHE
  227. CCACHE := $(shell which ccache)
  228. ifdef CCACHE
  229. export CCACHE_SLOPPINESS = time_macros
  230. $(info I ccache found, compilation results will be cached. Disable with GGML_NO_CCACHE.)
  231. CC := $(CCACHE) $(CC)
  232. CXX := $(CCACHE) $(CXX)
  233. else
  234. $(info I ccache not found. Consider installing it for faster compilation.)
  235. endif # CCACHE
  236. endif # GGML_NO_CCACHE
  237. # clock_gettime came in POSIX.1b (1993)
  238. # CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
  239. # posix_memalign came in POSIX.1-2001 / SUSv3
  240. # M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
  241. MK_CPPFLAGS += -D_XOPEN_SOURCE=600
  242. # Somehow in OpenBSD whenever POSIX conformance is specified
  243. # some string functions rely on locale_t availability,
  244. # which was introduced in POSIX.1-2008, forcing us to go higher
  245. ifeq ($(UNAME_S),OpenBSD)
  246. MK_CPPFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
  247. endif
  248. # Data types, macros and functions related to controlling CPU affinity and
  249. # some memory allocation are available on Linux through GNU extensions in libc
  250. ifeq ($(UNAME_S),Linux)
  251. MK_CPPFLAGS += -D_GNU_SOURCE
  252. endif
  253. # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
  254. # and on macOS its availability depends on enabling Darwin extensions
  255. # similarly on DragonFly, enabling BSD extensions is necessary
  256. ifeq ($(UNAME_S),Darwin)
  257. MK_CPPFLAGS += -D_DARWIN_C_SOURCE
  258. endif
  259. ifeq ($(UNAME_S),DragonFly)
  260. MK_CPPFLAGS += -D__BSD_VISIBLE
  261. endif
  262. # alloca is a non-standard interface that is not visible on BSDs when
  263. # POSIX conformance is specified, but not all of them provide a clean way
  264. # to enable it in such cases
  265. ifeq ($(UNAME_S),FreeBSD)
  266. MK_CPPFLAGS += -D__BSD_VISIBLE
  267. endif
  268. ifeq ($(UNAME_S),NetBSD)
  269. MK_CPPFLAGS += -D_NETBSD_SOURCE
  270. endif
  271. ifeq ($(UNAME_S),OpenBSD)
  272. MK_CPPFLAGS += -D_BSD_SOURCE
  273. endif
  274. ifdef GGML_SCHED_MAX_COPIES
  275. MK_CPPFLAGS += -DGGML_SCHED_MAX_COPIES=$(GGML_SCHED_MAX_COPIES)
  276. endif
  277. ifdef LLAMA_DEBUG
  278. MK_CFLAGS += -O0 -g
  279. MK_CXXFLAGS += -O0 -g
  280. MK_LDFLAGS += -g
  281. MK_NVCCFLAGS += -O0 -g
  282. ifeq ($(UNAME_S),Linux)
  283. MK_CPPFLAGS += -D_GLIBCXX_ASSERTIONS
  284. endif
  285. else
  286. MK_CPPFLAGS += -DNDEBUG
  287. MK_CFLAGS += -O3 -g
  288. MK_CXXFLAGS += -O3 -g
  289. MK_NVCCFLAGS += -O3 -g
  290. endif
  291. ifdef LLAMA_SANITIZE_THREAD
  292. MK_CFLAGS += -fsanitize=thread -g
  293. MK_CXXFLAGS += -fsanitize=thread -g
  294. MK_LDFLAGS += -fsanitize=thread -g
  295. endif
  296. ifdef LLAMA_SANITIZE_ADDRESS
  297. MK_CFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  298. MK_CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  299. MK_LDFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  300. endif
  301. ifdef LLAMA_SANITIZE_UNDEFINED
  302. MK_CFLAGS += -fsanitize=undefined -g
  303. MK_CXXFLAGS += -fsanitize=undefined -g
  304. MK_LDFLAGS += -fsanitize=undefined -g
  305. endif
  306. ifdef LLAMA_SERVER_VERBOSE
  307. MK_CPPFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
  308. endif
  309. ifdef LLAMA_SERVER_SSL
  310. MK_CPPFLAGS += -DCPPHTTPLIB_OPENSSL_SUPPORT
  311. MK_LDFLAGS += -lssl -lcrypto
  312. endif
  313. ifdef LLAMA_DISABLE_LOGS
  314. MK_CPPFLAGS += -DLOG_DISABLE_LOGS
  315. endif # LLAMA_DISABLE_LOGS
  316. # warnings
  317. WARN_FLAGS = \
  318. -Wall \
  319. -Wextra \
  320. -Wpedantic \
  321. -Wcast-qual \
  322. -Wno-unused-function
  323. MK_CFLAGS += \
  324. $(WARN_FLAGS) \
  325. -Wshadow \
  326. -Wstrict-prototypes \
  327. -Wpointer-arith \
  328. -Wmissing-prototypes \
  329. -Werror=implicit-int \
  330. -Werror=implicit-function-declaration
  331. MK_CXXFLAGS += \
  332. $(WARN_FLAGS) \
  333. -Wmissing-declarations \
  334. -Wmissing-noreturn
  335. ifeq ($(LLAMA_FATAL_WARNINGS),1)
  336. MK_CFLAGS += -Werror
  337. MK_CXXFLAGS += -Werror
  338. endif
  339. # this version of Apple ld64 is buggy
  340. ifneq '' '$(findstring dyld-1015.7,$(shell $(CC) $(LDFLAGS) -Wl,-v 2>&1))'
  341. MK_CPPFLAGS += -DHAVE_BUGGY_APPLE_LINKER
  342. endif
  343. # OS specific
  344. # TODO: support Windows
  345. ifneq '' '$(filter $(UNAME_S),Linux Darwin FreeBSD NetBSD OpenBSD Haiku)'
  346. MK_CFLAGS += -pthread
  347. MK_CXXFLAGS += -pthread
  348. endif
  349. # detect Windows
  350. ifneq ($(findstring _NT,$(UNAME_S)),)
  351. _WIN32 := 1
  352. endif
  353. # library name prefix
  354. ifneq ($(_WIN32),1)
  355. LIB_PRE := lib
  356. endif
  357. # Dynamic Shared Object extension
  358. ifneq ($(_WIN32),1)
  359. DSO_EXT := .so
  360. else
  361. DSO_EXT := .dll
  362. endif
  363. # Windows Sockets 2 (Winsock) for network-capable apps
  364. ifeq ($(_WIN32),1)
  365. LWINSOCK2 := -lws2_32
  366. endif
  367. ifdef LLAMA_GPROF
  368. MK_CFLAGS += -pg
  369. MK_CXXFLAGS += -pg
  370. endif
  371. # Architecture specific
  372. # TODO: probably these flags need to be tweaked on some architectures
  373. # feel free to update the Makefile for your architecture and send a pull request or issue
  374. ifndef RISCV_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_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 -I$(CUDA_PATH)/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include -DGGML_CUDA_USE_GRAPHS
  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/console.o \
  789. common/ngram-cache.o \
  790. common/sampling.o \
  791. common/train.o \
  792. common/build-info.o \
  793. common/json-schema-to-grammar.o
  794. OBJ_ALL = $(OBJ_GGML) $(OBJ_LLAMA) $(OBJ_COMMON)
  795. LIB_GGML = $(LIB_PRE)ggml$(DSO_EXT)
  796. LIB_GGML_S = $(LIB_PRE)ggml.a
  797. LIB_LLAMA = $(LIB_PRE)llama$(DSO_EXT)
  798. LIB_LLAMA_S = $(LIB_PRE)llama.a
  799. LIB_COMMON = $(LIB_PRE)common$(DSO_EXT)
  800. LIB_COMMON_S = $(LIB_PRE)common.a
  801. LIB_ALL = $(LIB_GGML) $(LIB_LLAMA) $(LIB_COMMON)
  802. LIB_ALL_S = $(LIB_GGML_S) $(LIB_LLAMA_S) $(LIB_COMMON_S)
  803. GF_CC := $(CC)
  804. include scripts/get-flags.mk
  805. # combine build flags with cmdline overrides
  806. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  807. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  808. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  809. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  810. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  811. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  812. # identify CUDA host compiler
  813. ifdef GGML_CUDA
  814. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  815. include scripts/get-flags.mk
  816. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  817. endif
  818. ifdef LLAMA_CURL
  819. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  820. override LDFLAGS := $(LDFLAGS) -lcurl
  821. endif
  822. #
  823. # Print build information
  824. #
  825. $(info I llama.cpp build info: )
  826. $(info I UNAME_S: $(UNAME_S))
  827. $(info I UNAME_P: $(UNAME_P))
  828. $(info I UNAME_M: $(UNAME_M))
  829. $(info I CFLAGS: $(CFLAGS))
  830. $(info I CXXFLAGS: $(CXXFLAGS))
  831. $(info I NVCCFLAGS: $(NVCCFLAGS))
  832. $(info I LDFLAGS: $(LDFLAGS))
  833. $(info I CC: $(shell $(CC) --version | head -n 1))
  834. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  835. ifdef GGML_CUDA
  836. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  837. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  838. ifndef GGML_MUSA
  839. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  840. ifndef CUDA_DOCKER_ARCH
  841. ifndef CUDA_POWER_ARCH
  842. $(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 )
  843. endif # CUDA_POWER_ARCH
  844. endif # CUDA_DOCKER_ARCH
  845. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  846. endif # GGML_MUSA
  847. endif # GGML_CUDA
  848. $(info )
  849. ifdef DEPRECATE_WARNING
  850. $(info !!! DEPRECATION WARNING !!!)
  851. $(info The following LLAMA_ options are deprecated and will be removed in the future. Use the GGML_ prefix instead)
  852. $(info - LLAMA_CUDA)
  853. $(info - LLAMA_METAL)
  854. $(info - LLAMA_METAL_EMBED_LIBRARY)
  855. $(info - LLAMA_OPENMP)
  856. $(info - LLAMA_RPC)
  857. $(info - LLAMA_SYCL)
  858. $(info - LLAMA_SYCL_F16)
  859. $(info - LLAMA_OPENBLAS)
  860. $(info - LLAMA_OPENBLAS64)
  861. $(info - LLAMA_BLIS)
  862. $(info - LLAMA_NO_LLAMAFILE)
  863. $(info - LLAMA_NO_ACCELERATE)
  864. $(info - LLAMA_NO_OPENMP)
  865. $(info - LLAMA_NO_METAL)
  866. $(info - LLAMA_NO_CCACHE)
  867. $(info )
  868. endif
  869. #
  870. # Build libraries
  871. #
  872. # ggml
  873. ggml/src/ggml.o: \
  874. ggml/src/ggml.c \
  875. ggml/include/ggml.h
  876. $(CC) $(CFLAGS) -c $< -o $@
  877. ggml/src/ggml-alloc.o: \
  878. ggml/src/ggml-alloc.c \
  879. ggml/include/ggml.h \
  880. ggml/include/ggml-alloc.h
  881. $(CC) $(CFLAGS) -c $< -o $@
  882. ggml/src/ggml-backend.o: \
  883. ggml/src/ggml-backend.c \
  884. ggml/include/ggml.h \
  885. ggml/include/ggml-backend.h
  886. $(CC) $(CFLAGS) -c $< -o $@
  887. ggml/src/ggml-quants.o: \
  888. ggml/src/ggml-quants.c \
  889. ggml/include/ggml.h \
  890. ggml/src/ggml-quants.h \
  891. ggml/src/ggml-common.h
  892. $(CC) $(CFLAGS) -c $< -o $@
  893. ggml/src/ggml-aarch64.o: \
  894. ggml/src/ggml-aarch64.c \
  895. ggml/include/ggml.h \
  896. ggml/src/ggml-aarch64.h \
  897. ggml/src/ggml-common.h
  898. $(CC) $(CFLAGS) -c $< -o $@
  899. ggml/src/ggml-blas.o: \
  900. ggml/src/ggml-blas.cpp \
  901. ggml/include/ggml-blas.h
  902. $(CXX) $(CXXFLAGS) -c $< -o $@
  903. ifndef GGML_NO_LLAMAFILE
  904. ggml/src/llamafile/sgemm.o: \
  905. ggml/src/llamafile/sgemm.cpp \
  906. ggml/src/llamafile/sgemm.h \
  907. ggml/include/ggml.h
  908. $(CXX) $(CXXFLAGS) -c $< -o $@
  909. endif # GGML_NO_LLAMAFILE
  910. ifdef GGML_RPC
  911. ggml/src/ggml-rpc.o: \
  912. ggml/src/ggml-rpc.cpp \
  913. ggml/include/ggml-rpc.h
  914. $(CXX) $(CXXFLAGS) -c $< -o $@
  915. endif # GGML_RPC
  916. $(LIB_GGML): \
  917. $(OBJ_GGML)
  918. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  919. $(LIB_GGML_S): \
  920. $(OBJ_GGML)
  921. ar rcs $(LIB_GGML_S) $^
  922. # llama
  923. src/unicode.o: \
  924. src/unicode.cpp \
  925. src/unicode.h
  926. $(CXX) $(CXXFLAGS) -c $< -o $@
  927. src/unicode-data.o: \
  928. src/unicode-data.cpp \
  929. src/unicode-data.h
  930. $(CXX) $(CXXFLAGS) -c $< -o $@
  931. src/llama.o: \
  932. src/llama.cpp \
  933. src/llama-impl.h \
  934. src/llama-vocab.h \
  935. src/llama-grammar.h \
  936. src/llama-sampling.h \
  937. src/unicode.h \
  938. include/llama.h \
  939. ggml/include/ggml-cuda.h \
  940. ggml/include/ggml-metal.h \
  941. ggml/include/ggml.h \
  942. ggml/include/ggml-alloc.h \
  943. ggml/include/ggml-backend.h
  944. $(CXX) $(CXXFLAGS) -c $< -o $@
  945. src/llama-vocab.o: \
  946. src/llama-vocab.cpp \
  947. src/llama-vocab.h \
  948. src/llama-impl.h \
  949. include/llama.h
  950. $(CXX) $(CXXFLAGS) -c $< -o $@
  951. src/llama-grammar.o: \
  952. src/llama-grammar.cpp \
  953. src/llama-grammar.h \
  954. src/llama-impl.h \
  955. src/llama-vocab.h \
  956. src/llama-sampling.h \
  957. include/llama.h
  958. $(CXX) $(CXXFLAGS) -c $< -o $@
  959. src/llama-sampling.o: \
  960. src/llama-sampling.cpp \
  961. src/llama-sampling.h \
  962. src/llama-impl.h \
  963. include/llama.h
  964. $(CXX) $(CXXFLAGS) -c $< -o $@
  965. $(LIB_LLAMA): \
  966. $(OBJ_LLAMA) \
  967. $(LIB_GGML)
  968. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  969. $(LIB_LLAMA_S): \
  970. $(OBJ_LLAMA)
  971. ar rcs $(LIB_LLAMA_S) $^
  972. # common
  973. common/common.o: \
  974. common/common.cpp \
  975. common/common.h \
  976. common/console.h \
  977. common/sampling.h \
  978. common/json.hpp \
  979. common/json-schema-to-grammar.h \
  980. include/llama.h
  981. $(CXX) $(CXXFLAGS) -c $< -o $@
  982. common/arg.o: \
  983. common/arg.cpp \
  984. common/arg.h
  985. $(CXX) $(CXXFLAGS) -c $< -o $@
  986. common/sampling.o: \
  987. common/sampling.cpp \
  988. common/sampling.h \
  989. include/llama.h
  990. $(CXX) $(CXXFLAGS) -c $< -o $@
  991. common/console.o: \
  992. common/console.cpp \
  993. common/console.h
  994. $(CXX) $(CXXFLAGS) -c $< -o $@
  995. common/json-schema-to-grammar.o: \
  996. common/json-schema-to-grammar.cpp \
  997. common/json-schema-to-grammar.h
  998. $(CXX) $(CXXFLAGS) -c $< -o $@
  999. common/train.o: \
  1000. common/train.cpp \
  1001. common/train.h
  1002. $(CXX) $(CXXFLAGS) -c $< -o $@
  1003. common/ngram-cache.o: \
  1004. common/ngram-cache.cpp \
  1005. common/ngram-cache.h
  1006. $(CXX) $(CXXFLAGS) -c $< -o $@
  1007. $(LIB_COMMON): \
  1008. $(OBJ_COMMON) \
  1009. $(LIB_LLAMA) \
  1010. $(LIB_GGML)
  1011. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  1012. $(LIB_COMMON_S): \
  1013. $(OBJ_COMMON)
  1014. ar rcs $(LIB_COMMON_S) $^
  1015. clean:
  1016. rm -vrf *.dot $(BUILD_TARGETS) $(TEST_TARGETS)
  1017. rm -rvf src/*.o
  1018. rm -rvf tests/*.o
  1019. rm -rvf examples/*.o
  1020. rm -rvf common/*.o
  1021. rm -rvf *.a
  1022. rm -rvf *.dll
  1023. rm -rvf *.so
  1024. rm -rvf *.dot
  1025. rm -rvf ggml/*.a
  1026. rm -rvf ggml/*.dll
  1027. rm -rvf ggml/*.so
  1028. rm -vrf ggml/src/*.o
  1029. rm -rvf ggml/src/llamafile/*.o
  1030. rm -rvf common/build-info.cpp
  1031. rm -vrf ggml/src/ggml-metal-embed.metal
  1032. rm -vrf ggml/src/ggml-cuda/*.o
  1033. rm -vrf ggml/src/ggml-cuda/template-instances/*.o
  1034. rm -rvf $(BUILD_TARGETS)
  1035. rm -rvf $(TEST_TARGETS)
  1036. rm -f vulkan-shaders-gen ggml/src/ggml-vulkan-shaders.hpp ggml/src/ggml-vulkan-shaders.cpp
  1037. rm -rvf $(LEGACY_TARGETS_CLEAN)
  1038. find examples pocs -type f -name "*.o" -delete
  1039. #
  1040. # Examples
  1041. #
  1042. # $< is the first prerequisite, i.e. the source file.
  1043. # Explicitly compile this to an object file so that it can be cached with ccache.
  1044. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  1045. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  1046. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  1047. llama-cli: examples/main/main.cpp \
  1048. $(OBJ_ALL)
  1049. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1050. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1051. @echo
  1052. @echo '==== Run ./llama-cli -h for help. ===='
  1053. @echo
  1054. llama-infill: examples/infill/infill.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-simple: examples/simple/simple.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-tokenize: examples/tokenize/tokenize.cpp \
  1063. $(OBJ_ALL)
  1064. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1065. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1066. llama-batched: examples/batched/batched.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-batched-bench: examples/batched-bench/batched-bench.cpp \
  1071. $(OBJ_ALL)
  1072. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1073. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1074. llama-quantize: examples/quantize/quantize.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-quantize-stats: examples/quantize-stats/quantize-stats.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-perplexity: examples/perplexity/perplexity.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-imatrix: examples/imatrix/imatrix.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-embedding: examples/embedding/embedding.cpp \
  1091. $(OBJ_ALL)
  1092. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1093. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1094. llama-gritlm: examples/gritlm/gritlm.cpp \
  1095. $(OBJ_ALL)
  1096. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1097. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1098. llama-save-load-state: examples/save-load-state/save-load-state.cpp \
  1099. $(OBJ_ALL)
  1100. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1101. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1102. llama-gguf: examples/gguf/gguf.cpp \
  1103. $(OBJ_GGML)
  1104. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1105. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1106. examples/gguf-hash/deps/sha1/sha1.o: \
  1107. examples/gguf-hash/deps/sha1/sha1.c
  1108. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1109. examples/gguf-hash/deps/xxhash/xxhash.o: \
  1110. examples/gguf-hash/deps/xxhash/xxhash.c
  1111. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1112. examples/gguf-hash/deps/sha256/sha256.o: \
  1113. examples/gguf-hash/deps/sha256/sha256.c
  1114. $(CC) $(CFLAGS) -Iexamples/gguf-hash/deps -c $< -o $@
  1115. 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\
  1116. $(OBJ_ALL)
  1117. $(CXX) $(CXXFLAGS) -Iexamples/gguf-hash/deps -c $< -o $(call GET_OBJ_FILE, $<)
  1118. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1119. llama-gguf-split: examples/gguf-split/gguf-split.cpp \
  1120. $(OBJ_ALL)
  1121. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1122. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1123. llama-eval-callback: examples/eval-callback/eval-callback.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-cvector-generator: examples/cvector-generator/cvector-generator.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-convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp \
  1132. $(OBJ_GGML) $(OBJ_LLAMA)
  1133. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1134. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1135. llama-bench: examples/llama-bench/llama-bench.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-baby-llama: examples/baby-llama/baby-llama.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-export-lora: examples/export-lora/export-lora.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-retrieval: examples/retrieval/retrieval.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-speculative: examples/speculative/speculative.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-parallel: examples/parallel/parallel.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-lookahead: examples/lookahead/lookahead.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: examples/lookup/lookup.cpp \
  1164. $(OBJ_ALL)
  1165. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1166. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1167. llama-lookup-create: examples/lookup/lookup-create.cpp \
  1168. $(OBJ_ALL)
  1169. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1170. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1171. llama-lookup-merge: examples/lookup/lookup-merge.cpp \
  1172. $(OBJ_ALL)
  1173. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1174. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1175. llama-lookup-stats: examples/lookup/lookup-stats.cpp \
  1176. $(OBJ_ALL)
  1177. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1178. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1179. llama-passkey: examples/passkey/passkey.cpp \
  1180. $(OBJ_ALL)
  1181. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1182. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1183. llama-gbnf-validator: examples/gbnf-validator/gbnf-validator.cpp \
  1184. $(OBJ_ALL)
  1185. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1186. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1187. ifdef GGML_RPC
  1188. rpc-server: examples/rpc/rpc-server.cpp \
  1189. $(OBJ_GGML)
  1190. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  1191. endif # GGML_RPC
  1192. llama-server: \
  1193. examples/server/server.cpp \
  1194. examples/server/utils.hpp \
  1195. examples/server/httplib.h \
  1196. examples/server/colorthemes.css.hpp \
  1197. examples/server/style.css.hpp \
  1198. examples/server/theme-beeninorder.css.hpp \
  1199. examples/server/theme-ketivah.css.hpp \
  1200. examples/server/theme-mangotango.css.hpp \
  1201. examples/server/theme-playground.css.hpp \
  1202. examples/server/theme-polarnight.css.hpp \
  1203. examples/server/theme-snowstorm.css.hpp \
  1204. examples/server/index.html.hpp \
  1205. examples/server/index-new.html.hpp \
  1206. examples/server/index.js.hpp \
  1207. examples/server/completion.js.hpp \
  1208. examples/server/system-prompts.js.hpp \
  1209. examples/server/prompt-formats.js.hpp \
  1210. examples/server/json-schema-to-grammar.mjs.hpp \
  1211. common/json.hpp \
  1212. common/stb_image.h \
  1213. $(OBJ_ALL)
  1214. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1215. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  1216. # Portable equivalent of `cd examples/server/public && xxd -i $(notdir $<) ../$(notdir $<).hpp`:
  1217. examples/server/%.hpp: examples/server/public/% Makefile
  1218. @( export NAME=$(subst .,_,$(subst -,_,$(notdir $<))) && \
  1219. echo "unsigned char $${NAME}[] = {" && \
  1220. cat $< | od -v -t x1 -An | sed -E 's/([0-9a-fA-F]+)/0x\1, /g' && \
  1221. echo "};" && \
  1222. echo "unsigned int $${NAME}_len = $(shell cat $< | wc -c );" \
  1223. ) > $@
  1224. llama-gen-docs: examples/gen-docs/gen-docs.cpp \
  1225. $(OBJ_ALL)
  1226. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1227. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1228. libllava.a: examples/llava/llava.cpp \
  1229. examples/llava/llava.h \
  1230. examples/llava/clip.cpp \
  1231. examples/llava/clip.h \
  1232. common/stb_image.h \
  1233. common/base64.hpp \
  1234. $(OBJ_ALL)
  1235. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  1236. llama-llava-cli: examples/llava/llava-cli.cpp \
  1237. examples/llava/llava.cpp \
  1238. examples/llava/llava.h \
  1239. examples/llava/clip.cpp \
  1240. examples/llava/clip.h \
  1241. $(OBJ_ALL)
  1242. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1243. llama-minicpmv-cli: examples/llava/minicpmv-cli.cpp \
  1244. examples/llava/llava.cpp \
  1245. examples/llava/llava.h \
  1246. examples/llava/clip.cpp \
  1247. examples/llava/clip.h \
  1248. $(OBJ_ALL)
  1249. $(CXX) $(CXXFLAGS) $< $(filter-out %.h $<,$^) -o $@ $(LDFLAGS) -Wno-cast-qual
  1250. ifeq ($(UNAME_S),Darwin)
  1251. swift: examples/batched.swift
  1252. (cd examples/batched.swift; make build)
  1253. endif
  1254. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  1255. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  1256. @if ! cmp -s $@.tmp $@; then \
  1257. mv $@.tmp $@; \
  1258. else \
  1259. rm $@.tmp; \
  1260. fi
  1261. common/build-info.o: common/build-info.cpp
  1262. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  1263. #
  1264. # Tests
  1265. #
  1266. tests: $(TEST_TARGETS)
  1267. llama-benchmark-matmult: examples/benchmark/benchmark-matmult.cpp \
  1268. $(OBJ_GGML) common/build-info.o
  1269. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1270. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1271. run-benchmark-matmult: llama-benchmark-matmult
  1272. ./$@
  1273. .PHONY: run-benchmark-matmult swift
  1274. tests/test-arg-parser: tests/test-arg-parser.cpp \
  1275. $(OBJ_ALL)
  1276. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1277. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1278. tests/test-llama-grammar: tests/test-llama-grammar.cpp \
  1279. $(OBJ_ALL)
  1280. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1281. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1282. tests/test-grammar-parser: tests/test-grammar-parser.cpp \
  1283. $(OBJ_ALL)
  1284. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1285. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1286. tests/test-grammar-integration: tests/test-grammar-integration.cpp \
  1287. $(OBJ_ALL)
  1288. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1289. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1290. tests/test-double-float: tests/test-double-float.cpp
  1291. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1292. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1293. tests/test-json-schema-to-grammar: tests/test-json-schema-to-grammar.cpp \
  1294. $(OBJ_ALL)
  1295. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  1296. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1297. tests/test-grad0: tests/test-grad0.cpp \
  1298. $(OBJ_GGML)
  1299. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1300. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1301. tests/test-opt: tests/test-opt.cpp \
  1302. $(OBJ_GGML)
  1303. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1304. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1305. tests/test-quantize-fns: tests/test-quantize-fns.cpp \
  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-quantize-perf: tests/test-quantize-perf.cpp \
  1310. $(OBJ_GGML)
  1311. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1312. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1313. tests/test-sampling: tests/test-sampling.cpp \
  1314. $(OBJ_ALL)
  1315. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1316. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1317. tests/test-tokenizer-0: tests/test-tokenizer-0.cpp \
  1318. $(OBJ_ALL)
  1319. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1320. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1321. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp \
  1322. $(OBJ_ALL)
  1323. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1324. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1325. tests/test-tokenizer-1-spm: tests/test-tokenizer-1-spm.cpp \
  1326. $(OBJ_ALL)
  1327. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1328. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1329. tests/test-rope: tests/test-rope.cpp ggml/src/ggml.o \
  1330. $(OBJ_GGML)
  1331. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1332. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1333. tests/test-c.o: tests/test-c.c include/llama.h
  1334. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  1335. tests/test-backend-ops: tests/test-backend-ops.cpp \
  1336. $(OBJ_GGML)
  1337. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1338. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1339. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp tests/get-model.cpp \
  1340. $(OBJ_ALL)
  1341. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1342. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1343. tests/test-autorelease: tests/test-autorelease.cpp tests/get-model.cpp \
  1344. $(OBJ_ALL)
  1345. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1346. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1347. tests/test-chat-template: tests/test-chat-template.cpp \
  1348. $(OBJ_ALL)
  1349. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1350. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1351. #
  1352. # PoCs
  1353. #
  1354. llama-vdot: pocs/vdot/vdot.cpp ggml/src/ggml.o \
  1355. $(OBJ_GGML)
  1356. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1357. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1358. llama-q8dot: pocs/vdot/q8dot.cpp ggml/src/ggml.o \
  1359. $(OBJ_GGML)
  1360. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1361. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1362. #
  1363. # Deprecated binaries that we want to keep around long enough for people to migrate to the new filenames, then these can be removed.
  1364. #
  1365. # Mark legacy binary targets as .PHONY so that they are always checked.
  1366. .PHONY: main quantize perplexity embedding server
  1367. # Define the object file target
  1368. examples/deprecation-warning/deprecation-warning.o: examples/deprecation-warning/deprecation-warning.cpp
  1369. $(CXX) $(CXXFLAGS) -c $< -o $@
  1370. # NOTE: We currently will always build the deprecation-warning `main` and `server` binaries to help users migrate.
  1371. # Eventually we will want to remove these target from building all the time.
  1372. main: examples/deprecation-warning/deprecation-warning.o
  1373. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1374. @echo "NOTICE: The 'main' binary is deprecated. Please use 'llama-cli' instead."
  1375. server: examples/deprecation-warning/deprecation-warning.o
  1376. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1377. @echo "NOTICE: The 'server' binary is deprecated. Please use 'llama-server' instead."
  1378. quantize: examples/deprecation-warning/deprecation-warning.o
  1379. ifneq (,$(wildcard quantize))
  1380. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1381. @echo "#########"
  1382. @echo "WARNING: The 'quantize' binary is deprecated. Please use 'llama-quantize' instead."
  1383. @echo " Remove the 'quantize' binary to remove this warning."
  1384. @echo "#########"
  1385. endif
  1386. perplexity: examples/deprecation-warning/deprecation-warning.o
  1387. ifneq (,$(wildcard perplexity))
  1388. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1389. @echo "#########"
  1390. @echo "WARNING: The 'perplexity' binary is deprecated. Please use 'llama-perplexity' instead."
  1391. @echo " Remove the 'perplexity' binary to remove this warning."
  1392. @echo "#########"
  1393. endif
  1394. embedding: examples/deprecation-warning/deprecation-warning.o
  1395. ifneq (,$(wildcard embedding))
  1396. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1397. @echo "#########"
  1398. @echo "WARNING: The 'embedding' binary is deprecated. Please use 'llama-embedding' instead."
  1399. @echo " Remove the 'embedding' binary to remove this warning."
  1400. @echo "#########"
  1401. endif