Makefile 48 KB

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