Makefile 48 KB

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