Makefile 49 KB

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