Makefile 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  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-log \
  57. tests/test-model-load-cancel \
  58. tests/test-opt \
  59. tests/test-quantize-fns \
  60. tests/test-quantize-perf \
  61. tests/test-rope \
  62. tests/test-sampling \
  63. tests/test-tokenizer-0 \
  64. tests/test-tokenizer-1-bpe \
  65. tests/test-tokenizer-1-spm
  66. # Legacy build targets that were renamed in #7809, but should still be removed when the project is cleaned
  67. LEGACY_TARGETS_CLEAN = main quantize quantize-stats perplexity imatrix embedding vdot q8dot convert-llama2c-to-ggml \
  68. simple batched batched-bench save-load-state server gguf gguf-split eval-callback llama-bench libllava.a llava-cli baby-llama \
  69. retrieval speculative infill tokenize benchmark-matmult parallel export-lora lookahead lookup passkey gritlm
  70. # 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.
  71. # We don't want to clutter things too much, so we only build replacements for the most commonly used binaries.
  72. LEGACY_TARGETS_BUILD = main quantize perplexity embedding server
  73. # Deprecation aliases
  74. ifdef LLAMA_CUBLAS
  75. $(error LLAMA_CUBLAS is removed. Use GGML_CUDA instead.)
  76. endif
  77. ifdef LLAMA_CUDA
  78. GGML_CUDA := 1
  79. DEPRECATE_WARNING := 1
  80. endif
  81. ifdef LLAMA_KOMPUTE
  82. GGML_KOMPUTE := 1
  83. DEPRECATE_WARNING := 1
  84. endif
  85. ifdef LLAMA_METAL
  86. GGML_METAL := 1
  87. DEPRECATE_WARNING := 1
  88. endif
  89. ifdef LLAMA_OPENMP
  90. GGML_OPENMP := 1
  91. DEPRECATE_WARNING := 1
  92. endif
  93. ifdef LLAMA_RPC
  94. GGML_RPC := 1
  95. DEPRECATE_WARNING := 1
  96. endif
  97. ifdef LLAMA_SYCL
  98. GGML_SYCL := 1
  99. DEPRECATE_WARNING := 1
  100. endif
  101. ifdef LLAMA_SYCL_F16
  102. GGML_SYCL_F16 := 1
  103. DEPRECATE_WARNING := 1
  104. endif
  105. ifdef LLAMA_OPENBLAS
  106. GGML_OPENBLAS := 1
  107. DEPRECATE_WARNING := 1
  108. endif
  109. ifdef LLAMA_OPENBLAS64
  110. GGML_OPENBLAS64 := 1
  111. DEPRECATE_WARNING := 1
  112. endif
  113. ifdef LLAMA_BLIS
  114. GGML_BLIS := 1
  115. DEPRECATE_WARNING := 1
  116. endif
  117. ifdef LLAMA_NO_LLAMAFILE
  118. GGML_NO_LLAMAFILE := 1
  119. DEPRECATE_WARNING := 1
  120. endif
  121. ifdef LLAMA_NO_ACCELERATE
  122. GGML_NO_ACCELERATE := 1
  123. DEPRECATE_WARNING := 1
  124. endif
  125. ifdef LLAMA_NO_OPENMP
  126. GGML_NO_OPENMP := 1
  127. DEPRECATE_WARNING := 1
  128. endif
  129. ifdef LLAMA_NO_METAL
  130. GGML_NO_METAL := 1
  131. DEPRECATE_WARNING := 1
  132. endif
  133. ifdef LLAMA_DISABLE_LOGS
  134. REMOVE_WARNING := 1
  135. endif
  136. ifdef LLAMA_SERVER_VERBOSE
  137. REMOVE_WARNING := 1
  138. endif
  139. ifndef UNAME_S
  140. UNAME_S := $(shell uname -s)
  141. endif
  142. ifndef UNAME_P
  143. UNAME_P := $(shell uname -p)
  144. endif
  145. ifndef UNAME_M
  146. UNAME_M := $(shell uname -m)
  147. endif
  148. # In GNU make default CXX is g++ instead of c++. Let's fix that so that users
  149. # of non-gcc compilers don't have to provide g++ alias or wrapper.
  150. DEFCC := cc
  151. DEFCXX := c++
  152. ifeq ($(origin CC),default)
  153. CC := $(DEFCC)
  154. endif
  155. ifeq ($(origin CXX),default)
  156. CXX := $(DEFCXX)
  157. endif
  158. # Mac OS + Arm can report x86_64
  159. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  160. ifeq ($(UNAME_S),Darwin)
  161. ifndef GGML_NO_METAL
  162. GGML_METAL := 1
  163. endif
  164. GGML_NO_OPENMP := 1
  165. ifneq ($(UNAME_P),arm)
  166. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  167. ifeq ($(SYSCTL_M),1)
  168. # UNAME_P := arm
  169. # UNAME_M := arm64
  170. 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)
  171. endif
  172. endif
  173. endif
  174. ifdef GGML_METAL
  175. GGML_METAL_EMBED_LIBRARY := 1
  176. endif
  177. ifdef GGML_RPC
  178. BUILD_TARGETS += rpc-server
  179. endif
  180. ifdef GGML_VULKAN
  181. BUILD_TARGETS += vulkan-shaders-gen
  182. endif
  183. default: $(BUILD_TARGETS) $(LEGACY_TARGETS_BUILD)
  184. test: $(TEST_TARGETS)
  185. @failures=0; \
  186. for test_target in $(TEST_TARGETS); do \
  187. if [ "$$test_target" = "tests/test-tokenizer-0" ]; then \
  188. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-spm.gguf; \
  189. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-bpe.gguf; \
  190. ./$$test_target $(CURDIR)/models/ggml-vocab-phi-3.gguf; \
  191. ./$$test_target $(CURDIR)/models/ggml-vocab-falcon.gguf; \
  192. ./$$test_target $(CURDIR)/models/ggml-vocab-bert-bge.gguf; \
  193. ./$$test_target $(CURDIR)/models/ggml-vocab-starcoder.gguf; \
  194. ./$$test_target $(CURDIR)/models/ggml-vocab-gpt-2.gguf; \
  195. ./$$test_target $(CURDIR)/models/ggml-vocab-refact.gguf; \
  196. elif [ "$$test_target" = "tests/test-tokenizer-1-spm" ]; then \
  197. continue; \
  198. elif [ "$$test_target" = "tests/test-tokenizer-1-bpe" ]; then \
  199. continue; \
  200. else \
  201. echo "Running test $$test_target..."; \
  202. ./$$test_target; \
  203. fi; \
  204. if [ $$? -ne 0 ]; then \
  205. printf 'Test %s FAILED!\n\n' $$test_target; \
  206. failures=$$(( failures + 1 )); \
  207. else \
  208. printf 'Test %s passed.\n\n' $$test_target; \
  209. fi; \
  210. done; \
  211. if [ $$failures -gt 0 ]; then \
  212. printf '\n%s tests failed.\n' $$failures; \
  213. exit 1; \
  214. fi
  215. @echo 'All tests passed.'
  216. all: $(BUILD_TARGETS) $(TEST_TARGETS) $(LEGACY_TARGETS_BUILD)
  217. ifdef RISCV_CROSS_COMPILE
  218. CC := riscv64-unknown-linux-gnu-gcc
  219. CXX := riscv64-unknown-linux-gnu-g++
  220. endif
  221. #
  222. # Compile flags
  223. #
  224. # keep standard at C11 and C++11
  225. MK_CPPFLAGS = -Iggml/include -Iggml/src -Iinclude -Isrc -Icommon
  226. MK_CFLAGS = -std=c11 -fPIC
  227. MK_CXXFLAGS = -std=c++11 -fPIC
  228. MK_NVCCFLAGS = -std=c++11
  229. ifdef LLAMA_NO_CCACHE
  230. GGML_NO_CCACHE := 1
  231. DEPRECATE_WARNING := 1
  232. endif
  233. ifndef GGML_NO_CCACHE
  234. CCACHE := $(shell which ccache)
  235. ifdef CCACHE
  236. export CCACHE_SLOPPINESS = time_macros
  237. $(info I ccache found, compilation results will be cached. Disable with GGML_NO_CCACHE.)
  238. CC := $(CCACHE) $(CC)
  239. CXX := $(CCACHE) $(CXX)
  240. else
  241. $(info I ccache not found. Consider installing it for faster compilation.)
  242. endif # CCACHE
  243. endif # GGML_NO_CCACHE
  244. # clock_gettime came in POSIX.1b (1993)
  245. # CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
  246. # posix_memalign came in POSIX.1-2001 / SUSv3
  247. # M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
  248. MK_CPPFLAGS += -D_XOPEN_SOURCE=600
  249. # Somehow in OpenBSD whenever POSIX conformance is specified
  250. # some string functions rely on locale_t availability,
  251. # which was introduced in POSIX.1-2008, forcing us to go higher
  252. ifeq ($(UNAME_S),OpenBSD)
  253. MK_CPPFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
  254. endif
  255. # Data types, macros and functions related to controlling CPU affinity and
  256. # some memory allocation are available on Linux through GNU extensions in libc
  257. ifeq ($(UNAME_S),Linux)
  258. MK_CPPFLAGS += -D_GNU_SOURCE
  259. endif
  260. # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
  261. # and on macOS its availability depends on enabling Darwin extensions
  262. # similarly on DragonFly, enabling BSD extensions is necessary
  263. ifeq ($(UNAME_S),Darwin)
  264. MK_CPPFLAGS += -D_DARWIN_C_SOURCE
  265. endif
  266. ifeq ($(UNAME_S),DragonFly)
  267. MK_CPPFLAGS += -D__BSD_VISIBLE
  268. endif
  269. # alloca is a non-standard interface that is not visible on BSDs when
  270. # POSIX conformance is specified, but not all of them provide a clean way
  271. # to enable it in such cases
  272. ifeq ($(UNAME_S),FreeBSD)
  273. MK_CPPFLAGS += -D__BSD_VISIBLE
  274. endif
  275. ifeq ($(UNAME_S),NetBSD)
  276. MK_CPPFLAGS += -D_NETBSD_SOURCE
  277. endif
  278. ifeq ($(UNAME_S),OpenBSD)
  279. MK_CPPFLAGS += -D_BSD_SOURCE
  280. endif
  281. ifdef GGML_SCHED_MAX_COPIES
  282. MK_CPPFLAGS += -DGGML_SCHED_MAX_COPIES=$(GGML_SCHED_MAX_COPIES)
  283. endif
  284. ifdef LLAMA_DEBUG
  285. MK_CFLAGS += -O0 -g
  286. MK_CXXFLAGS += -O0 -g
  287. MK_LDFLAGS += -g
  288. MK_NVCCFLAGS += -O0 -g
  289. ifeq ($(UNAME_S),Linux)
  290. MK_CPPFLAGS += -D_GLIBCXX_ASSERTIONS
  291. endif
  292. else
  293. MK_CPPFLAGS += -DNDEBUG
  294. MK_CFLAGS += -O3 -g
  295. MK_CXXFLAGS += -O3 -g
  296. MK_NVCCFLAGS += -O3 -g
  297. endif
  298. ifdef LLAMA_SANITIZE_THREAD
  299. MK_CFLAGS += -fsanitize=thread -g
  300. MK_CXXFLAGS += -fsanitize=thread -g
  301. MK_LDFLAGS += -fsanitize=thread -g
  302. endif
  303. ifdef LLAMA_SANITIZE_ADDRESS
  304. MK_CFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  305. MK_CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  306. MK_LDFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  307. endif
  308. ifdef LLAMA_SANITIZE_UNDEFINED
  309. MK_CFLAGS += -fsanitize=undefined -g
  310. MK_CXXFLAGS += -fsanitize=undefined -g
  311. MK_LDFLAGS += -fsanitize=undefined -g
  312. endif
  313. ifdef LLAMA_SERVER_SSL
  314. MK_CPPFLAGS += -DCPPHTTPLIB_OPENSSL_SUPPORT
  315. MK_LDFLAGS += -lssl -lcrypto
  316. endif
  317. # warnings
  318. WARN_FLAGS = \
  319. -Wall \
  320. -Wextra \
  321. -Wpedantic \
  322. -Wcast-qual \
  323. -Wno-unused-function
  324. MK_CFLAGS += \
  325. $(WARN_FLAGS) \
  326. -Wshadow \
  327. -Wstrict-prototypes \
  328. -Wpointer-arith \
  329. -Wmissing-prototypes \
  330. -Werror=implicit-int \
  331. -Werror=implicit-function-declaration
  332. MK_CXXFLAGS += \
  333. $(WARN_FLAGS) \
  334. -Wmissing-declarations \
  335. -Wmissing-noreturn
  336. ifeq ($(LLAMA_FATAL_WARNINGS),1)
  337. MK_CFLAGS += -Werror
  338. MK_CXXFLAGS += -Werror
  339. endif
  340. # this version of Apple ld64 is buggy
  341. ifneq '' '$(findstring dyld-1015.7,$(shell $(CC) $(LDFLAGS) -Wl,-v 2>&1))'
  342. MK_CPPFLAGS += -DHAVE_BUGGY_APPLE_LINKER
  343. endif
  344. # OS specific
  345. # TODO: support Windows
  346. ifneq '' '$(filter $(UNAME_S),Linux Darwin FreeBSD NetBSD OpenBSD Haiku)'
  347. MK_CFLAGS += -pthread
  348. MK_CXXFLAGS += -pthread
  349. endif
  350. # detect Windows
  351. ifneq ($(findstring _NT,$(UNAME_S)),)
  352. _WIN32 := 1
  353. endif
  354. # library name prefix
  355. ifneq ($(_WIN32),1)
  356. LIB_PRE := lib
  357. endif
  358. # Dynamic Shared Object extension
  359. ifneq ($(_WIN32),1)
  360. DSO_EXT := .so
  361. else
  362. DSO_EXT := .dll
  363. endif
  364. # Windows Sockets 2 (Winsock) for network-capable apps
  365. ifeq ($(_WIN32),1)
  366. LWINSOCK2 := -lws2_32
  367. endif
  368. ifdef LLAMA_GPROF
  369. MK_CFLAGS += -pg
  370. MK_CXXFLAGS += -pg
  371. endif
  372. # Architecture specific
  373. # TODO: probably these flags need to be tweaked on some architectures
  374. # feel free to update the Makefile for your architecture and send a pull request or issue
  375. ifndef RISCV_CROSS_COMPILE
  376. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
  377. # Use all CPU extensions that are available:
  378. MK_CFLAGS += -march=native -mtune=native
  379. HOST_CXXFLAGS += -march=native -mtune=native
  380. # Usage AVX-only
  381. #MK_CFLAGS += -mfma -mf16c -mavx
  382. #MK_CXXFLAGS += -mfma -mf16c -mavx
  383. # Usage SSSE3-only (Not is SSE3!)
  384. #MK_CFLAGS += -mssse3
  385. #MK_CXXFLAGS += -mssse3
  386. endif
  387. ifneq '' '$(findstring mingw,$(shell $(CC) -dumpmachine))'
  388. # The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
  389. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
  390. # https://github.com/ggerganov/llama.cpp/issues/2922
  391. MK_CFLAGS += -Xassembler -muse-unaligned-vector-move
  392. MK_CXXFLAGS += -Xassembler -muse-unaligned-vector-move
  393. # Target Windows 8 for PrefetchVirtualMemory
  394. MK_CPPFLAGS += -D_WIN32_WINNT=0x602
  395. endif
  396. ifneq ($(filter aarch64%,$(UNAME_M)),)
  397. # Apple M1, M2, etc.
  398. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  399. # Nvidia Jetson
  400. MK_CFLAGS += -mcpu=native
  401. MK_CXXFLAGS += -mcpu=native
  402. JETSON_RELEASE_INFO = $(shell jetson_release)
  403. ifdef JETSON_RELEASE_INFO
  404. ifneq ($(filter TX2%,$(JETSON_RELEASE_INFO)),)
  405. JETSON_EOL_MODULE_DETECT = 1
  406. CC = aarch64-unknown-linux-gnu-gcc
  407. cxx = aarch64-unknown-linux-gnu-g++
  408. endif
  409. endif
  410. endif
  411. ifneq ($(filter armv6%,$(UNAME_M)),)
  412. # Raspberry Pi 1, Zero
  413. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  414. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  415. endif
  416. ifneq ($(filter armv7%,$(UNAME_M)),)
  417. # Raspberry Pi 2
  418. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  419. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  420. endif
  421. ifneq ($(filter armv8%,$(UNAME_M)),)
  422. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  423. MK_CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  424. MK_CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
  425. endif
  426. ifneq ($(filter ppc64%,$(UNAME_M)),)
  427. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  428. ifneq (,$(findstring POWER9,$(POWER9_M)))
  429. MK_CFLAGS += -mcpu=power9
  430. MK_CXXFLAGS += -mcpu=power9
  431. endif
  432. endif
  433. ifneq ($(filter ppc64le%,$(UNAME_M)),)
  434. MK_CFLAGS += -mcpu=powerpc64le
  435. MK_CXXFLAGS += -mcpu=powerpc64le
  436. CUDA_POWER_ARCH = 1
  437. endif
  438. ifneq ($(filter loongarch64%,$(UNAME_M)),)
  439. MK_CFLAGS += -mlasx
  440. MK_CXXFLAGS += -mlasx
  441. endif
  442. ifneq ($(filter riscv64%,$(UNAME_M)),)
  443. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  444. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  445. endif
  446. else # RISC-V CROSS COMPILATION
  447. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  448. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  449. endif
  450. ifndef GGML_NO_ACCELERATE
  451. # Mac OS - include Accelerate framework.
  452. # `-framework Accelerate` works both with Apple Silicon and Mac Intel
  453. ifeq ($(UNAME_S),Darwin)
  454. MK_CPPFLAGS += -DGGML_USE_ACCELERATE -DGGML_USE_BLAS
  455. MK_CPPFLAGS += -DACCELERATE_NEW_LAPACK
  456. MK_CPPFLAGS += -DACCELERATE_LAPACK_ILP64
  457. MK_LDFLAGS += -framework Accelerate
  458. OBJ_GGML += ggml/src/ggml-blas.o
  459. endif
  460. endif # GGML_NO_ACCELERATE
  461. ifdef GGML_MUSA
  462. CC := clang
  463. CXX := clang++
  464. GGML_CUDA := 1
  465. MK_CPPFLAGS += -DGGML_USE_MUSA
  466. endif
  467. ifndef GGML_NO_OPENMP
  468. MK_CPPFLAGS += -DGGML_USE_OPENMP
  469. MK_CFLAGS += -fopenmp
  470. MK_CXXFLAGS += -fopenmp
  471. ifdef GGML_MUSA
  472. MK_CPPFLAGS += -I/usr/lib/llvm-10/include/openmp
  473. MK_LDFLAGS += -L/usr/lib/llvm-10/lib
  474. endif # GGML_MUSA
  475. endif # GGML_NO_OPENMP
  476. ifdef GGML_OPENBLAS
  477. MK_CPPFLAGS += -DGGML_USE_BLAS $(shell pkg-config --cflags-only-I openblas)
  478. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
  479. MK_LDFLAGS += $(shell pkg-config --libs openblas)
  480. OBJ_GGML += ggml/src/ggml-blas.o
  481. endif # GGML_OPENBLAS
  482. ifdef GGML_OPENBLAS64
  483. MK_CPPFLAGS += -DGGML_USE_BLAS $(shell pkg-config --cflags-only-I openblas64)
  484. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas64)
  485. MK_LDFLAGS += $(shell pkg-config --libs openblas64)
  486. OBJ_GGML += ggml/src/ggml-blas.o
  487. endif # GGML_OPENBLAS64
  488. ifdef GGML_BLIS
  489. MK_CPPFLAGS += -DGGML_USE_BLAS -DGGML_BLAS_USE_BLIS -I/usr/local/include/blis -I/usr/include/blis
  490. MK_LDFLAGS += -lblis -L/usr/local/lib
  491. OBJ_GGML += ggml/src/ggml-blas.o
  492. endif # GGML_BLIS
  493. ifdef GGML_NVPL
  494. MK_CPPFLAGS += -DGGML_USE_BLAS -DGGML_BLAS_USE_NVPL -DNVPL_ILP64 -I/usr/local/include/nvpl_blas -I/usr/include/nvpl_blas
  495. MK_LDFLAGS += -L/usr/local/lib -lnvpl_blas_core -lnvpl_blas_ilp64_gomp
  496. OBJ_GGML += ggml/src/ggml-blas.o
  497. endif # GGML_NVPL
  498. ifndef GGML_NO_LLAMAFILE
  499. MK_CPPFLAGS += -DGGML_USE_LLAMAFILE
  500. OBJ_GGML += ggml/src/llamafile/sgemm.o
  501. endif
  502. ifdef GGML_RPC
  503. MK_CPPFLAGS += -DGGML_USE_RPC
  504. OBJ_GGML += ggml/src/ggml-rpc.o
  505. endif # GGML_RPC
  506. OBJ_CUDA_TMPL = $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-wmma*.cu))
  507. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/mmq*.cu))
  508. ifdef GGML_CUDA_FA_ALL_QUANTS
  509. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*.cu))
  510. else
  511. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*q4_0-q4_0.cu))
  512. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*q8_0-q8_0.cu))
  513. OBJ_CUDA_TMPL += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/template-instances/fattn-vec*f16-f16.cu))
  514. endif # GGML_CUDA_FA_ALL_QUANTS
  515. ifdef GGML_CUDA
  516. ifdef GGML_MUSA
  517. ifneq ('', '$(wildcard /opt/musa)')
  518. CUDA_PATH ?= /opt/musa
  519. else
  520. CUDA_PATH ?= /usr/local/musa
  521. endif
  522. MK_CPPFLAGS += -DGGML_USE_CUDA -I$(CUDA_PATH)/include
  523. MK_LDFLAGS += -lmusa -lmublas -lmusart -lpthread -ldl -lrt -L$(CUDA_PATH)/lib -L/usr/lib64
  524. MK_NVCCFLAGS += -x musa -mtgpu --cuda-gpu-arch=mp_22
  525. else
  526. ifneq ('', '$(wildcard /opt/cuda)')
  527. CUDA_PATH ?= /opt/cuda
  528. else
  529. CUDA_PATH ?= /usr/local/cuda
  530. endif
  531. MK_CPPFLAGS += -DGGML_USE_CUDA -DGGML_CUDA_USE_GRAPHS -I$(CUDA_PATH)/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include
  532. 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
  533. MK_NVCCFLAGS += -use_fast_math
  534. endif # GGML_MUSA
  535. OBJ_GGML += ggml/src/ggml-cuda.o
  536. OBJ_GGML += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  537. OBJ_GGML += $(OBJ_CUDA_TMPL)
  538. ifdef LLAMA_FATAL_WARNINGS
  539. MK_NVCCFLAGS += -Werror all-warnings
  540. endif # LLAMA_FATAL_WARNINGS
  541. ifndef GGML_MUSA
  542. ifndef JETSON_EOL_MODULE_DETECT
  543. MK_NVCCFLAGS += --forward-unknown-to-host-compiler
  544. endif # JETSON_EOL_MODULE_DETECT
  545. endif # GGML_MUSA
  546. ifdef LLAMA_DEBUG
  547. MK_NVCCFLAGS += -lineinfo
  548. endif # LLAMA_DEBUG
  549. ifdef GGML_CUDA_DEBUG
  550. MK_NVCCFLAGS += --device-debug
  551. endif # GGML_CUDA_DEBUG
  552. ifdef GGML_CUDA_NVCC
  553. NVCC = $(CCACHE) $(GGML_CUDA_NVCC)
  554. else
  555. ifdef GGML_MUSA
  556. NVCC = $(CCACHE) mcc
  557. else
  558. NVCC = $(CCACHE) nvcc
  559. endif # GGML_MUSA
  560. endif # GGML_CUDA_NVCC
  561. ifdef CUDA_DOCKER_ARCH
  562. MK_NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  563. else ifndef CUDA_POWER_ARCH
  564. MK_NVCCFLAGS += -arch=native
  565. endif # CUDA_DOCKER_ARCH
  566. ifdef GGML_CUDA_FORCE_DMMV
  567. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  568. endif # GGML_CUDA_FORCE_DMMV
  569. ifdef GGML_CUDA_FORCE_MMQ
  570. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  571. endif # GGML_CUDA_FORCE_MMQ
  572. ifdef GGML_CUDA_FORCE_CUBLAS
  573. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  574. endif # GGML_CUDA_FORCE_CUBLAS
  575. ifdef GGML_CUDA_DMMV_X
  576. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  577. else
  578. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  579. endif # GGML_CUDA_DMMV_X
  580. ifdef GGML_CUDA_MMV_Y
  581. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  582. else ifdef GGML_CUDA_DMMV_Y
  583. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_DMMV_Y) # for backwards compatibility
  584. else
  585. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  586. endif # GGML_CUDA_MMV_Y
  587. ifdef GGML_CUDA_F16
  588. MK_NVCCFLAGS += -DGGML_CUDA_F16
  589. endif # GGML_CUDA_F16
  590. ifdef GGML_CUDA_DMMV_F16
  591. MK_NVCCFLAGS += -DGGML_CUDA_F16
  592. endif # GGML_CUDA_DMMV_F16
  593. ifdef GGML_CUDA_KQUANTS_ITER
  594. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  595. else
  596. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  597. endif
  598. ifdef GGML_CUDA_PEER_MAX_BATCH_SIZE
  599. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(GGML_CUDA_PEER_MAX_BATCH_SIZE)
  600. else
  601. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  602. endif # GGML_CUDA_PEER_MAX_BATCH_SIZE
  603. ifdef GGML_CUDA_NO_PEER_COPY
  604. MK_NVCCFLAGS += -DGGML_CUDA_NO_PEER_COPY
  605. endif # GGML_CUDA_NO_PEER_COPY
  606. ifdef GGML_CUDA_CCBIN
  607. MK_NVCCFLAGS += -ccbin $(GGML_CUDA_CCBIN)
  608. endif # GGML_CUDA_CCBIN
  609. ifdef GGML_CUDA_FA_ALL_QUANTS
  610. MK_NVCCFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  611. endif # GGML_CUDA_FA_ALL_QUANTS
  612. ifdef JETSON_EOL_MODULE_DETECT
  613. define NVCC_COMPILE
  614. $(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 $@
  615. endef # NVCC_COMPILE
  616. else
  617. ifdef GGML_MUSA
  618. define NVCC_COMPILE
  619. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -c $< -o $@
  620. endef # NVCC_COMPILE
  621. else
  622. define NVCC_COMPILE
  623. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  624. endef # NVCC_COMPILE
  625. endif # GGML_MUSA
  626. endif # JETSON_EOL_MODULE_DETECT
  627. ggml/src/ggml-cuda/%.o: \
  628. ggml/src/ggml-cuda/%.cu \
  629. ggml/include/ggml.h \
  630. ggml/src/ggml-common.h \
  631. ggml/src/ggml-cuda/common.cuh
  632. $(NVCC_COMPILE)
  633. ggml/src/ggml-cuda.o: \
  634. ggml/src/ggml-cuda.cu \
  635. ggml/include/ggml-cuda.h \
  636. ggml/include/ggml.h \
  637. ggml/include/ggml-backend.h \
  638. ggml/src/ggml-backend-impl.h \
  639. ggml/src/ggml-common.h \
  640. $(wildcard ggml/src/ggml-cuda/*.cuh)
  641. $(NVCC_COMPILE)
  642. endif # GGML_CUDA
  643. ifdef GGML_VULKAN
  644. MK_CPPFLAGS += -DGGML_USE_VULKAN
  645. MK_LDFLAGS += $(shell pkg-config --libs vulkan)
  646. OBJ_GGML += ggml/src/ggml-vulkan.o ggml/src/ggml-vulkan-shaders.o
  647. ifdef GGML_VULKAN_CHECK_RESULTS
  648. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  649. endif
  650. ifdef GGML_VULKAN_DEBUG
  651. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  652. endif
  653. ifdef GGML_VULKAN_MEMORY_DEBUG
  654. MK_CPPFLAGS += -DGGML_VULKAN_MEMORY_DEBUG
  655. endif
  656. ifdef GGML_VULKAN_PERF
  657. MK_CPPFLAGS += -DGGML_VULKAN_PERF
  658. endif
  659. ifdef GGML_VULKAN_VALIDATE
  660. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  661. endif
  662. ifdef GGML_VULKAN_RUN_TESTS
  663. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  664. endif
  665. GLSLC_CMD = glslc
  666. _ggml_vk_genshaders_cmd = $(shell pwd)/vulkan-shaders-gen
  667. _ggml_vk_header = ggml/src/ggml-vulkan-shaders.hpp
  668. _ggml_vk_source = ggml/src/ggml-vulkan-shaders.cpp
  669. _ggml_vk_input_dir = ggml/src/vulkan-shaders
  670. _ggml_vk_shader_deps = $(echo $(_ggml_vk_input_dir)/*.comp)
  671. ggml/src/ggml-vulkan.o: ggml/src/ggml-vulkan.cpp ggml/include/ggml-vulkan.h $(_ggml_vk_header) $(_ggml_vk_source)
  672. $(CXX) $(CXXFLAGS) $(shell pkg-config --cflags vulkan) -c $< -o $@
  673. $(_ggml_vk_header): $(_ggml_vk_source)
  674. $(_ggml_vk_source): $(_ggml_vk_shader_deps) vulkan-shaders-gen
  675. $(_ggml_vk_genshaders_cmd) \
  676. --glslc $(GLSLC_CMD) \
  677. --input-dir $(_ggml_vk_input_dir) \
  678. --target-hpp $(_ggml_vk_header) \
  679. --target-cpp $(_ggml_vk_source)
  680. vulkan-shaders-gen: ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
  681. $(CXX) $(CXXFLAGS) -o $@ $(LDFLAGS) ggml/src/vulkan-shaders/vulkan-shaders-gen.cpp
  682. endif # GGML_VULKAN
  683. ifdef GGML_HIPBLAS
  684. ifeq ($(wildcard /opt/rocm),)
  685. ROCM_PATH ?= /usr
  686. AMDGPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  687. else
  688. ROCM_PATH ?= /opt/rocm
  689. AMDGPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  690. endif
  691. GGML_CUDA_DMMV_X ?= 32
  692. GGML_CUDA_MMV_Y ?= 1
  693. GGML_CUDA_KQUANTS_ITER ?= 2
  694. MK_CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUDA
  695. ifdef GGML_HIP_UMA
  696. MK_CPPFLAGS += -DGGML_HIP_UMA
  697. endif # GGML_HIP_UMA
  698. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  699. MK_LDFLAGS += -L$(ROCM_PATH)/lib64 -Wl,-rpath=$(ROCM_PATH)/lib64
  700. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  701. HIPCC ?= $(CCACHE) $(ROCM_PATH)/bin/hipcc
  702. HIPFLAGS += $(addprefix --offload-arch=,$(AMDGPU_TARGETS))
  703. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(GGML_CUDA_DMMV_X)
  704. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(GGML_CUDA_MMV_Y)
  705. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(GGML_CUDA_KQUANTS_ITER)
  706. ifdef GGML_CUDA_FORCE_DMMV
  707. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  708. endif # GGML_CUDA_FORCE_DMMV
  709. ifdef GGML_CUDA_FORCE_MMQ
  710. HIPFLAGS += -DGGML_CUDA_FORCE_MMQ
  711. endif # GGML_CUDA_FORCE_MMQ
  712. ifdef GGML_CUDA_FORCE_CUBLAS
  713. HIPFLAGS += -DGGML_CUDA_FORCE_CUBLAS
  714. endif # GGML_CUDA_FORCE_CUBLAS
  715. ifdef GGML_CUDA_NO_PEER_COPY
  716. HIPFLAGS += -DGGML_CUDA_NO_PEER_COPY
  717. endif # GGML_CUDA_NO_PEER_COPY
  718. OBJ_GGML += ggml/src/ggml-cuda.o
  719. OBJ_GGML += $(patsubst %.cu,%.o,$(wildcard ggml/src/ggml-cuda/*.cu))
  720. OBJ_GGML += $(OBJ_CUDA_TMPL)
  721. ggml/src/ggml-cuda.o: \
  722. ggml/src/ggml-cuda.cu \
  723. ggml/include/ggml-cuda.h \
  724. ggml/include/ggml.h \
  725. ggml/include/ggml-backend.h \
  726. ggml/src/ggml-backend-impl.h \
  727. ggml/src/ggml-common.h \
  728. $(wildcard ggml/src/ggml-cuda/*.cuh)
  729. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  730. ggml/src/ggml-cuda/%.o: \
  731. ggml/src/ggml-cuda/%.cu \
  732. ggml/include/ggml.h \
  733. ggml/src/ggml-common.h \
  734. ggml/src/ggml-cuda/common.cuh
  735. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  736. endif # GGML_HIPBLAS
  737. ifdef GGML_METAL
  738. MK_CPPFLAGS += -DGGML_USE_METAL
  739. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  740. OBJ_GGML += ggml/src/ggml-metal.o
  741. ifdef GGML_METAL_NDEBUG
  742. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  743. endif
  744. ifdef GGML_METAL_EMBED_LIBRARY
  745. MK_CPPFLAGS += -DGGML_METAL_EMBED_LIBRARY
  746. OBJ_GGML += ggml/src/ggml-metal-embed.o
  747. endif
  748. endif # GGML_METAL
  749. ifdef GGML_METAL
  750. ggml/src/ggml-metal.o: \
  751. ggml/src/ggml-metal.m \
  752. ggml/include/ggml-metal.h \
  753. ggml/include/ggml.h
  754. $(CC) $(CFLAGS) -c $< -o $@
  755. ifdef GGML_METAL_EMBED_LIBRARY
  756. ggml/src/ggml-metal-embed.o: \
  757. ggml/src/ggml-metal.metal \
  758. ggml/src/ggml-common.h
  759. @echo "Embedding Metal library"
  760. @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
  761. $(eval TEMP_ASSEMBLY=$(shell mktemp -d))
  762. @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  763. @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  764. @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  765. @echo ".incbin \"ggml/src/ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  766. @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  767. @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)/ggml-metal-embed.s
  768. $(CC) $(CFLAGS) -c $(TEMP_ASSEMBLY)/ggml-metal-embed.s -o $@
  769. @rm -f ${TEMP_ASSEMBLY}/ggml-metal-embed.s
  770. @rmdir ${TEMP_ASSEMBLY}
  771. endif
  772. endif # GGML_METAL
  773. OBJ_GGML += \
  774. ggml/src/ggml.o \
  775. ggml/src/ggml-alloc.o \
  776. ggml/src/ggml-backend.o \
  777. ggml/src/ggml-quants.o \
  778. ggml/src/ggml-aarch64.o
  779. OBJ_LLAMA = \
  780. src/llama.o \
  781. src/llama-vocab.o \
  782. src/llama-grammar.o \
  783. src/llama-sampling.o \
  784. src/unicode.o \
  785. src/unicode-data.o
  786. OBJ_COMMON = \
  787. common/common.o \
  788. common/arg.o \
  789. common/log.o \
  790. common/console.o \
  791. common/ngram-cache.o \
  792. common/sampling.o \
  793. common/train.o \
  794. common/build-info.o \
  795. common/json-schema-to-grammar.o
  796. OBJ_ALL = $(OBJ_GGML) $(OBJ_LLAMA) $(OBJ_COMMON)
  797. LIB_GGML = $(LIB_PRE)ggml$(DSO_EXT)
  798. LIB_GGML_S = $(LIB_PRE)ggml.a
  799. LIB_LLAMA = $(LIB_PRE)llama$(DSO_EXT)
  800. LIB_LLAMA_S = $(LIB_PRE)llama.a
  801. LIB_COMMON = $(LIB_PRE)common$(DSO_EXT)
  802. LIB_COMMON_S = $(LIB_PRE)common.a
  803. LIB_ALL = $(LIB_GGML) $(LIB_LLAMA) $(LIB_COMMON)
  804. LIB_ALL_S = $(LIB_GGML_S) $(LIB_LLAMA_S) $(LIB_COMMON_S)
  805. GF_CC := $(CC)
  806. include scripts/get-flags.mk
  807. # combine build flags with cmdline overrides
  808. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  809. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  810. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  811. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  812. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  813. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  814. # identify CUDA host compiler
  815. ifdef GGML_CUDA
  816. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  817. include scripts/get-flags.mk
  818. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  819. endif
  820. ifdef LLAMA_CURL
  821. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  822. override LDFLAGS := $(LDFLAGS) -lcurl
  823. endif
  824. #
  825. # Print build information
  826. #
  827. $(info I llama.cpp build info: )
  828. $(info I UNAME_S: $(UNAME_S))
  829. $(info I UNAME_P: $(UNAME_P))
  830. $(info I UNAME_M: $(UNAME_M))
  831. $(info I CFLAGS: $(CFLAGS))
  832. $(info I CXXFLAGS: $(CXXFLAGS))
  833. $(info I NVCCFLAGS: $(NVCCFLAGS))
  834. $(info I LDFLAGS: $(LDFLAGS))
  835. $(info I CC: $(shell $(CC) --version | head -n 1))
  836. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  837. ifdef GGML_CUDA
  838. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  839. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  840. ifndef GGML_MUSA
  841. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  842. ifndef CUDA_DOCKER_ARCH
  843. ifndef CUDA_POWER_ARCH
  844. $(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 )
  845. endif # CUDA_POWER_ARCH
  846. endif # CUDA_DOCKER_ARCH
  847. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  848. endif # GGML_MUSA
  849. endif # GGML_CUDA
  850. $(info )
  851. ifdef DEPRECATE_WARNING
  852. $(info !!! DEPRECATION WARNING !!!)
  853. $(info The following LLAMA_ options are deprecated and will be removed in the future. Use the GGML_ prefix instead)
  854. $(info - LLAMA_CUDA)
  855. $(info - LLAMA_METAL)
  856. $(info - LLAMA_METAL_EMBED_LIBRARY)
  857. $(info - LLAMA_OPENMP)
  858. $(info - LLAMA_RPC)
  859. $(info - LLAMA_SYCL)
  860. $(info - LLAMA_SYCL_F16)
  861. $(info - LLAMA_OPENBLAS)
  862. $(info - LLAMA_OPENBLAS64)
  863. $(info - LLAMA_BLIS)
  864. $(info - LLAMA_NO_LLAMAFILE)
  865. $(info - LLAMA_NO_ACCELERATE)
  866. $(info - LLAMA_NO_OPENMP)
  867. $(info - LLAMA_NO_METAL)
  868. $(info - LLAMA_NO_CCACHE)
  869. $(info )
  870. endif
  871. ifdef REMOVE_WARNING
  872. $(info !!! REMOVAL WARNING !!!)
  873. $(info The following LLAMA_ options have been removed and are no longer supported)
  874. $(info - LLAMA_DISABLE_LOGS (https://github.com/ggerganov/llama.cpp/pull/9418))
  875. $(info - LLAMA_SERVER_VERBOSE (https://github.com/ggerganov/llama.cpp/pull/9418))
  876. $(info )
  877. endif
  878. #
  879. # Build libraries
  880. #
  881. # ggml
  882. ggml/src/ggml.o: \
  883. ggml/src/ggml.c \
  884. ggml/include/ggml.h
  885. $(CC) $(CFLAGS) -c $< -o $@
  886. ggml/src/ggml-alloc.o: \
  887. ggml/src/ggml-alloc.c \
  888. ggml/include/ggml.h \
  889. ggml/include/ggml-alloc.h
  890. $(CC) $(CFLAGS) -c $< -o $@
  891. ggml/src/ggml-backend.o: \
  892. ggml/src/ggml-backend.c \
  893. ggml/include/ggml.h \
  894. ggml/include/ggml-backend.h
  895. $(CC) $(CFLAGS) -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. llama-benchmark-matmult: examples/benchmark/benchmark-matmult.cpp \
  1282. $(OBJ_GGML) common/build-info.o
  1283. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1284. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1285. run-benchmark-matmult: llama-benchmark-matmult
  1286. ./$@
  1287. .PHONY: run-benchmark-matmult swift
  1288. tests/test-arg-parser: tests/test-arg-parser.cpp \
  1289. $(OBJ_ALL)
  1290. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1291. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1292. tests/test-llama-grammar: tests/test-llama-grammar.cpp \
  1293. $(OBJ_ALL)
  1294. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1295. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1296. tests/test-log: tests/test-log.cpp \
  1297. $(OBJ_ALL)
  1298. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1299. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1300. tests/test-grammar-parser: tests/test-grammar-parser.cpp \
  1301. $(OBJ_ALL)
  1302. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1303. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1304. tests/test-grammar-integration: tests/test-grammar-integration.cpp \
  1305. $(OBJ_ALL)
  1306. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1307. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1308. tests/test-double-float: tests/test-double-float.cpp
  1309. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1310. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1311. tests/test-json-schema-to-grammar: tests/test-json-schema-to-grammar.cpp \
  1312. $(OBJ_ALL)
  1313. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  1314. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1315. tests/test-grad0: tests/test-grad0.cpp \
  1316. $(OBJ_GGML)
  1317. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1318. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1319. tests/test-opt: tests/test-opt.cpp \
  1320. $(OBJ_GGML)
  1321. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1322. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1323. tests/test-quantize-fns: tests/test-quantize-fns.cpp \
  1324. $(OBJ_GGML)
  1325. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1326. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1327. tests/test-quantize-perf: tests/test-quantize-perf.cpp \
  1328. $(OBJ_GGML)
  1329. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1330. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1331. tests/test-sampling: tests/test-sampling.cpp \
  1332. $(OBJ_ALL)
  1333. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1334. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1335. tests/test-tokenizer-0: tests/test-tokenizer-0.cpp \
  1336. $(OBJ_ALL)
  1337. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1338. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1339. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.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-tokenizer-1-spm: tests/test-tokenizer-1-spm.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-rope: tests/test-rope.cpp ggml/src/ggml.o \
  1348. $(OBJ_GGML)
  1349. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1350. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1351. tests/test-c.o: tests/test-c.c include/llama.h
  1352. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  1353. tests/test-backend-ops: tests/test-backend-ops.cpp \
  1354. $(OBJ_GGML)
  1355. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1356. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1357. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp tests/get-model.cpp \
  1358. $(OBJ_ALL)
  1359. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1360. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1361. tests/test-autorelease: tests/test-autorelease.cpp tests/get-model.cpp \
  1362. $(OBJ_ALL)
  1363. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1364. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1365. tests/test-chat-template: tests/test-chat-template.cpp \
  1366. $(OBJ_ALL)
  1367. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1368. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1369. #
  1370. # PoCs
  1371. #
  1372. llama-vdot: pocs/vdot/vdot.cpp ggml/src/ggml.o \
  1373. $(OBJ_GGML)
  1374. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1375. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1376. llama-q8dot: pocs/vdot/q8dot.cpp ggml/src/ggml.o \
  1377. $(OBJ_GGML)
  1378. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  1379. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  1380. #
  1381. # Deprecated binaries that we want to keep around long enough for people to migrate to the new filenames, then these can be removed.
  1382. #
  1383. # Mark legacy binary targets as .PHONY so that they are always checked.
  1384. .PHONY: main quantize perplexity embedding server
  1385. # Define the object file target
  1386. examples/deprecation-warning/deprecation-warning.o: examples/deprecation-warning/deprecation-warning.cpp
  1387. $(CXX) $(CXXFLAGS) -c $< -o $@
  1388. # NOTE: We currently will always build the deprecation-warning `main` and `server` binaries to help users migrate.
  1389. # Eventually we will want to remove these target from building all the time.
  1390. main: examples/deprecation-warning/deprecation-warning.o
  1391. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1392. @echo "NOTICE: The 'main' binary is deprecated. Please use 'llama-cli' instead."
  1393. server: examples/deprecation-warning/deprecation-warning.o
  1394. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1395. @echo "NOTICE: The 'server' binary is deprecated. Please use 'llama-server' instead."
  1396. quantize: examples/deprecation-warning/deprecation-warning.o
  1397. ifneq (,$(wildcard quantize))
  1398. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1399. @echo "#########"
  1400. @echo "WARNING: The 'quantize' binary is deprecated. Please use 'llama-quantize' instead."
  1401. @echo " Remove the 'quantize' binary to remove this warning."
  1402. @echo "#########"
  1403. endif
  1404. perplexity: examples/deprecation-warning/deprecation-warning.o
  1405. ifneq (,$(wildcard perplexity))
  1406. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1407. @echo "#########"
  1408. @echo "WARNING: The 'perplexity' binary is deprecated. Please use 'llama-perplexity' instead."
  1409. @echo " Remove the 'perplexity' binary to remove this warning."
  1410. @echo "#########"
  1411. endif
  1412. embedding: examples/deprecation-warning/deprecation-warning.o
  1413. ifneq (,$(wildcard embedding))
  1414. $(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)
  1415. @echo "#########"
  1416. @echo "WARNING: The 'embedding' binary is deprecated. Please use 'llama-embedding' instead."
  1417. @echo " Remove the 'embedding' binary to remove this warning."
  1418. @echo "#########"
  1419. endif