Makefile 44 KB

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