Makefile 49 KB

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