Makefile 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. # Define the default target now so that it is always the first target
  2. BUILD_TARGETS = \
  3. main quantize quantize-stats perplexity imatrix embedding vdot q8dot train-text-from-scratch convert-llama2c-to-ggml \
  4. simple batched batched-bench save-load-state server gguf gguf-split eval-callback llama-bench libllava.a llava-cli baby-llama beam-search \
  5. retrieval speculative infill tokenize benchmark-matmult parallel finetune export-lora lookahead lookup passkey gritlm tests/test-c.o
  6. # Binaries only useful for tests
  7. TEST_TARGETS = \
  8. tests/test-autorelease \
  9. tests/test-backend-ops \
  10. tests/test-double-float \
  11. tests/test-grad0 \
  12. tests/test-grammar-integration \
  13. tests/test-grammar-parser \
  14. tests/test-json-schema-to-grammar \
  15. tests/test-llama-grammar \
  16. tests/test-model-load-cancel \
  17. tests/test-opt \
  18. tests/test-quantize-fns \
  19. tests/test-quantize-perf \
  20. tests/test-rope \
  21. tests/test-sampling \
  22. tests/test-tokenizer-0 \
  23. tests/test-tokenizer-1-bpe \
  24. tests/test-tokenizer-1-spm
  25. # Code coverage output files
  26. COV_TARGETS = *.gcno tests/*.gcno *.gcda tests/*.gcda *.gcov tests/*.gcov lcov-report gcovr-report
  27. ifndef UNAME_S
  28. UNAME_S := $(shell uname -s)
  29. endif
  30. ifndef UNAME_P
  31. UNAME_P := $(shell uname -p)
  32. endif
  33. ifndef UNAME_M
  34. UNAME_M := $(shell uname -m)
  35. endif
  36. # In GNU make default CXX is g++ instead of c++. Let's fix that so that users
  37. # of non-gcc compilers don't have to provide g++ alias or wrapper.
  38. DEFCC := cc
  39. DEFCXX := c++
  40. ifeq ($(origin CC),default)
  41. CC := $(DEFCC)
  42. endif
  43. ifeq ($(origin CXX),default)
  44. CXX := $(DEFCXX)
  45. endif
  46. # Mac OS + Arm can report x86_64
  47. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  48. ifeq ($(UNAME_S),Darwin)
  49. ifndef LLAMA_NO_METAL
  50. LLAMA_METAL := 1
  51. endif
  52. LLAMA_NO_OPENMP := 1
  53. ifneq ($(UNAME_P),arm)
  54. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  55. ifeq ($(SYSCTL_M),1)
  56. # UNAME_P := arm
  57. # UNAME_M := arm64
  58. 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)
  59. endif
  60. endif
  61. endif
  62. ifdef LLAMA_RPC
  63. BUILD_TARGETS += rpc-server
  64. endif
  65. default: $(BUILD_TARGETS)
  66. test: $(TEST_TARGETS)
  67. @failures=0; \
  68. for test_target in $(TEST_TARGETS); do \
  69. if [ "$$test_target" = "tests/test-tokenizer-0" ]; then \
  70. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-spm.gguf; \
  71. ./$$test_target $(CURDIR)/models/ggml-vocab-llama-bpe.gguf; \
  72. ./$$test_target $(CURDIR)/models/ggml-vocab-phi-3.gguf; \
  73. ./$$test_target $(CURDIR)/models/ggml-vocab-falcon.gguf; \
  74. ./$$test_target $(CURDIR)/models/ggml-vocab-bert-bge.gguf; \
  75. ./$$test_target $(CURDIR)/models/ggml-vocab-starcoder.gguf; \
  76. ./$$test_target $(CURDIR)/models/ggml-vocab-gpt-2.gguf; \
  77. ./$$test_target $(CURDIR)/models/ggml-vocab-refact.gguf; \
  78. elif [ "$$test_target" = "tests/test-tokenizer-1-spm" ]; then \
  79. continue; \
  80. elif [ "$$test_target" = "tests/test-tokenizer-1-bpe" ]; then \
  81. continue; \
  82. else \
  83. echo "Running test $$test_target..."; \
  84. ./$$test_target; \
  85. fi; \
  86. if [ $$? -ne 0 ]; then \
  87. printf 'Test %s FAILED!\n\n' $$test_target; \
  88. failures=$$(( failures + 1 )); \
  89. else \
  90. printf 'Test %s passed.\n\n' $$test_target; \
  91. fi; \
  92. done; \
  93. if [ $$failures -gt 0 ]; then \
  94. printf '\n%s tests failed.\n' $$failures; \
  95. exit 1; \
  96. fi
  97. @echo 'All tests passed.'
  98. all: $(BUILD_TARGETS) $(TEST_TARGETS)
  99. coverage: ## Run code coverage
  100. gcov -pb tests/*.cpp
  101. lcov-report: coverage ## Generate lcov report
  102. mkdir -p lcov-report
  103. lcov --capture --directory . --output-file lcov-report/coverage.info
  104. genhtml lcov-report/coverage.info --output-directory lcov-report
  105. gcovr-report: coverage ## Generate gcovr report
  106. mkdir -p gcovr-report
  107. gcovr --root . --html --html-details --output gcovr-report/coverage.html
  108. ifdef RISCV_CROSS_COMPILE
  109. CC := riscv64-unknown-linux-gnu-gcc
  110. CXX := riscv64-unknown-linux-gnu-g++
  111. endif
  112. #
  113. # Compile flags
  114. #
  115. # keep standard at C11 and C++11
  116. MK_CPPFLAGS = -I. -Icommon
  117. MK_CFLAGS = -std=c11 -fPIC
  118. MK_CXXFLAGS = -std=c++11 -fPIC
  119. MK_NVCCFLAGS = -std=c++11
  120. # -Ofast tends to produce faster code, but may not be available for some compilers.
  121. ifdef LLAMA_FAST
  122. MK_CFLAGS += -Ofast
  123. HOST_CXXFLAGS += -Ofast
  124. ifndef LLAMA_DEBUG
  125. MK_NVCCFLAGS += -O3
  126. endif # LLAMA_DEBUG
  127. else
  128. MK_CFLAGS += -O3
  129. MK_CXXFLAGS += -O3
  130. ifndef LLAMA_DEBUG
  131. MK_NVCCFLAGS += -O3
  132. endif # LLAMA_DEBUG
  133. endif # LLAMA_FAST
  134. ifndef LLAMA_NO_CCACHE
  135. CCACHE := $(shell which ccache)
  136. ifdef CCACHE
  137. export CCACHE_SLOPPINESS = time_macros
  138. $(info I ccache found, compilation results will be cached. Disable with LLAMA_NO_CCACHE.)
  139. CC := $(CCACHE) $(CC)
  140. CXX := $(CCACHE) $(CXX)
  141. else
  142. $(info I ccache not found. Consider installing it for faster compilation.)
  143. endif # CCACHE
  144. endif # LLAMA_NO_CCACHE
  145. # clock_gettime came in POSIX.1b (1993)
  146. # CLOCK_MONOTONIC came in POSIX.1-2001 / SUSv3 as optional
  147. # posix_memalign came in POSIX.1-2001 / SUSv3
  148. # M_PI is an XSI extension since POSIX.1-2001 / SUSv3, came in XPG1 (1985)
  149. MK_CPPFLAGS += -D_XOPEN_SOURCE=600
  150. # Somehow in OpenBSD whenever POSIX conformance is specified
  151. # some string functions rely on locale_t availability,
  152. # which was introduced in POSIX.1-2008, forcing us to go higher
  153. ifeq ($(UNAME_S),OpenBSD)
  154. MK_CPPFLAGS += -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700
  155. endif
  156. # Data types, macros and functions related to controlling CPU affinity and
  157. # some memory allocation are available on Linux through GNU extensions in libc
  158. ifeq ($(UNAME_S),Linux)
  159. MK_CPPFLAGS += -D_GNU_SOURCE
  160. endif
  161. # RLIMIT_MEMLOCK came in BSD, is not specified in POSIX.1,
  162. # and on macOS its availability depends on enabling Darwin extensions
  163. # similarly on DragonFly, enabling BSD extensions is necessary
  164. ifeq ($(UNAME_S),Darwin)
  165. MK_CPPFLAGS += -D_DARWIN_C_SOURCE
  166. endif
  167. ifeq ($(UNAME_S),DragonFly)
  168. MK_CPPFLAGS += -D__BSD_VISIBLE
  169. endif
  170. # alloca is a non-standard interface that is not visible on BSDs when
  171. # POSIX conformance is specified, but not all of them provide a clean way
  172. # to enable it in such cases
  173. ifeq ($(UNAME_S),FreeBSD)
  174. MK_CPPFLAGS += -D__BSD_VISIBLE
  175. endif
  176. ifeq ($(UNAME_S),NetBSD)
  177. MK_CPPFLAGS += -D_NETBSD_SOURCE
  178. endif
  179. ifeq ($(UNAME_S),OpenBSD)
  180. MK_CPPFLAGS += -D_BSD_SOURCE
  181. endif
  182. ifdef LLAMA_SCHED_MAX_COPIES
  183. MK_CPPFLAGS += -DGGML_SCHED_MAX_COPIES=$(LLAMA_SCHED_MAX_COPIES)
  184. endif
  185. ifdef LLAMA_DEBUG
  186. MK_CFLAGS += -O0 -g
  187. MK_CXXFLAGS += -O0 -g
  188. MK_LDFLAGS += -g
  189. MK_NVCCFLAGS += -O0 -g
  190. ifeq ($(UNAME_S),Linux)
  191. MK_CPPFLAGS += -D_GLIBCXX_ASSERTIONS
  192. endif
  193. else
  194. MK_CPPFLAGS += -DNDEBUG
  195. endif
  196. ifdef LLAMA_SANITIZE_THREAD
  197. MK_CFLAGS += -fsanitize=thread -g
  198. MK_CXXFLAGS += -fsanitize=thread -g
  199. MK_LDFLAGS += -fsanitize=thread -g
  200. endif
  201. ifdef LLAMA_SANITIZE_ADDRESS
  202. MK_CFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  203. MK_CXXFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  204. MK_LDFLAGS += -fsanitize=address -fno-omit-frame-pointer -g
  205. endif
  206. ifdef LLAMA_SANITIZE_UNDEFINED
  207. MK_CFLAGS += -fsanitize=undefined -g
  208. MK_CXXFLAGS += -fsanitize=undefined -g
  209. MK_LDFLAGS += -fsanitize=undefined -g
  210. endif
  211. ifdef LLAMA_SERVER_VERBOSE
  212. MK_CPPFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
  213. endif
  214. ifdef LLAMA_SERVER_SSL
  215. MK_CPPFLAGS += -DCPPHTTPLIB_OPENSSL_SUPPORT
  216. MK_LDFLAGS += -lssl -lcrypto
  217. endif
  218. ifdef LLAMA_CODE_COVERAGE
  219. MK_CXXFLAGS += -fprofile-arcs -ftest-coverage -dumpbase ''
  220. endif
  221. ifdef LLAMA_DISABLE_LOGS
  222. MK_CPPFLAGS += -DLOG_DISABLE_LOGS
  223. endif # LLAMA_DISABLE_LOGS
  224. # warnings
  225. WARN_FLAGS = -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
  226. MK_CFLAGS += $(WARN_FLAGS) -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int \
  227. -Werror=implicit-function-declaration
  228. MK_CXXFLAGS += $(WARN_FLAGS) -Wmissing-declarations -Wmissing-noreturn
  229. ifeq ($(LLAMA_FATAL_WARNINGS),1)
  230. MK_CFLAGS += -Werror
  231. MK_CXXFLAGS += -Werror
  232. endif
  233. # this version of Apple ld64 is buggy
  234. ifneq '' '$(findstring dyld-1015.7,$(shell $(CC) $(LDFLAGS) -Wl,-v 2>&1))'
  235. MK_CPPFLAGS += -DHAVE_BUGGY_APPLE_LINKER
  236. endif
  237. # OS specific
  238. # TODO: support Windows
  239. ifneq '' '$(filter $(UNAME_S),Linux Darwin FreeBSD NetBSD OpenBSD Haiku)'
  240. MK_CFLAGS += -pthread
  241. MK_CXXFLAGS += -pthread
  242. endif
  243. # detect Windows
  244. ifneq ($(findstring _NT,$(UNAME_S)),)
  245. _WIN32 := 1
  246. endif
  247. # library name prefix
  248. ifneq ($(_WIN32),1)
  249. LIB_PRE := lib
  250. endif
  251. # Dynamic Shared Object extension
  252. ifneq ($(_WIN32),1)
  253. DSO_EXT := .so
  254. else
  255. DSO_EXT := .dll
  256. endif
  257. # Windows Sockets 2 (Winsock) for network-capable apps
  258. ifeq ($(_WIN32),1)
  259. LWINSOCK2 := -lws2_32
  260. endif
  261. ifdef LLAMA_GPROF
  262. MK_CFLAGS += -pg
  263. MK_CXXFLAGS += -pg
  264. endif
  265. ifdef LLAMA_PERF
  266. MK_CPPFLAGS += -DGGML_PERF
  267. endif
  268. # Architecture specific
  269. # TODO: probably these flags need to be tweaked on some architectures
  270. # feel free to update the Makefile for your architecture and send a pull request or issue
  271. ifndef RISCV
  272. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
  273. # Use all CPU extensions that are available:
  274. MK_CFLAGS += -march=native -mtune=native
  275. HOST_CXXFLAGS += -march=native -mtune=native
  276. # Usage AVX-only
  277. #MK_CFLAGS += -mfma -mf16c -mavx
  278. #MK_CXXFLAGS += -mfma -mf16c -mavx
  279. # Usage SSSE3-only (Not is SSE3!)
  280. #MK_CFLAGS += -mssse3
  281. #MK_CXXFLAGS += -mssse3
  282. endif
  283. ifneq '' '$(findstring mingw,$(shell $(CC) -dumpmachine))'
  284. # The stack is only 16-byte aligned on Windows, so don't let gcc emit aligned moves.
  285. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
  286. # https://github.com/ggerganov/llama.cpp/issues/2922
  287. MK_CFLAGS += -Xassembler -muse-unaligned-vector-move
  288. MK_CXXFLAGS += -Xassembler -muse-unaligned-vector-move
  289. # Target Windows 8 for PrefetchVirtualMemory
  290. MK_CPPFLAGS += -D_WIN32_WINNT=0x602
  291. endif
  292. ifneq ($(filter aarch64%,$(UNAME_M)),)
  293. # Apple M1, M2, etc.
  294. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  295. # Nvidia Jetson
  296. MK_CFLAGS += -mcpu=native
  297. MK_CXXFLAGS += -mcpu=native
  298. JETSON_RELEASE_INFO = $(shell jetson_release)
  299. ifdef JETSON_RELEASE_INFO
  300. ifneq ($(filter TX2%,$(JETSON_RELEASE_INFO)),)
  301. JETSON_EOL_MODULE_DETECT = 1
  302. CC = aarch64-unknown-linux-gnu-gcc
  303. cxx = aarch64-unknown-linux-gnu-g++
  304. endif
  305. endif
  306. endif
  307. ifneq ($(filter armv6%,$(UNAME_M)),)
  308. # Raspberry Pi 1, Zero
  309. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  310. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  311. endif
  312. ifneq ($(filter armv7%,$(UNAME_M)),)
  313. # Raspberry Pi 2
  314. MK_CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  315. MK_CXXFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  316. endif
  317. ifneq ($(filter armv8%,$(UNAME_M)),)
  318. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  319. MK_CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  320. MK_CXXFLAGS += -mfp16-format=ieee -mno-unaligned-access
  321. endif
  322. ifneq ($(filter ppc64%,$(UNAME_M)),)
  323. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  324. ifneq (,$(findstring POWER9,$(POWER9_M)))
  325. MK_CFLAGS += -mcpu=power9
  326. MK_CXXFLAGS += -mcpu=power9
  327. endif
  328. endif
  329. ifneq ($(filter ppc64le%,$(UNAME_M)),)
  330. MK_CFLAGS += -mcpu=powerpc64le
  331. MK_CXXFLAGS += -mcpu=powerpc64le
  332. CUDA_POWER_ARCH = 1
  333. endif
  334. ifneq ($(filter loongarch64%,$(UNAME_M)),)
  335. MK_CFLAGS += -mlasx
  336. MK_CXXFLAGS += -mlasx
  337. endif
  338. else
  339. MK_CFLAGS += -march=rv64gcv -mabi=lp64d
  340. MK_CXXFLAGS += -march=rv64gcv -mabi=lp64d
  341. endif
  342. ifndef LLAMA_NO_ACCELERATE
  343. # Mac OS - include Accelerate framework.
  344. # `-framework Accelerate` works both with Apple Silicon and Mac Intel
  345. ifeq ($(UNAME_S),Darwin)
  346. MK_CPPFLAGS += -DGGML_USE_ACCELERATE
  347. MK_CPPFLAGS += -DACCELERATE_NEW_LAPACK
  348. MK_CPPFLAGS += -DACCELERATE_LAPACK_ILP64
  349. MK_LDFLAGS += -framework Accelerate
  350. endif
  351. endif # LLAMA_NO_ACCELERATE
  352. ifndef LLAMA_NO_OPENMP
  353. MK_CPPFLAGS += -DGGML_USE_OPENMP
  354. MK_CFLAGS += -fopenmp
  355. MK_CXXFLAGS += -fopenmp
  356. endif # LLAMA_NO_OPENMP
  357. ifdef LLAMA_OPENBLAS
  358. MK_CPPFLAGS += -DGGML_USE_OPENBLAS $(shell pkg-config --cflags-only-I openblas)
  359. MK_CFLAGS += $(shell pkg-config --cflags-only-other openblas)
  360. MK_LDFLAGS += $(shell pkg-config --libs openblas)
  361. endif # LLAMA_OPENBLAS
  362. ifndef LLAMA_NO_LLAMAFILE
  363. MK_CPPFLAGS += -DGGML_USE_LLAMAFILE
  364. OBJS += sgemm.o
  365. endif
  366. ifdef LLAMA_BLIS
  367. MK_CPPFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
  368. MK_LDFLAGS += -lblis -L/usr/local/lib
  369. endif # LLAMA_BLIS
  370. ifdef LLAMA_RPC
  371. MK_CPPFLAGS += -DGGML_USE_RPC
  372. OBJS += ggml-rpc.o
  373. endif # LLAMA_RPC
  374. ifdef LLAMA_CUBLAS
  375. # LLAMA_CUBLAS is deprecated and will be removed in the future
  376. LLAMA_CUDA := 1
  377. endif
  378. OBJS_CUDA_TEMP_INST = $(patsubst %.cu,%.o,$(wildcard ggml-cuda/template-instances/fattn-wmma*.cu))
  379. ifdef LLAMA_CUDA_FA_ALL_QUANTS
  380. OBJS_CUDA_TEMP_INST += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/template-instances/fattn-vec*.cu))
  381. else
  382. OBJS_CUDA_TEMP_INST += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/template-instances/fattn-vec*q4_0-q4_0.cu))
  383. OBJS_CUDA_TEMP_INST += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/template-instances/fattn-vec*q8_0-q8_0.cu))
  384. OBJS_CUDA_TEMP_INST += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/template-instances/fattn-vec*f16-f16.cu))
  385. endif # LLAMA_CUDA_FA_ALL_QUANTS
  386. ifdef LLAMA_CUDA
  387. ifneq ('', '$(wildcard /opt/cuda)')
  388. CUDA_PATH ?= /opt/cuda
  389. else
  390. CUDA_PATH ?= /usr/local/cuda
  391. endif
  392. MK_CPPFLAGS += -DGGML_USE_CUDA -I$(CUDA_PATH)/include -I$(CUDA_PATH)/targets/$(UNAME_M)-linux/include -DGGML_CUDA_USE_GRAPHS
  393. 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/usr/lib/wsl/lib
  394. OBJS += ggml-cuda.o
  395. OBJS += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/*.cu))
  396. OBJS += $(OBJS_CUDA_TEMP_INST)
  397. MK_NVCCFLAGS += -use_fast_math
  398. ifdef LLAMA_FATAL_WARNINGS
  399. MK_NVCCFLAGS += -Werror all-warnings
  400. endif # LLAMA_FATAL_WARNINGS
  401. ifndef JETSON_EOL_MODULE_DETECT
  402. MK_NVCCFLAGS += --forward-unknown-to-host-compiler
  403. endif # JETSON_EOL_MODULE_DETECT
  404. ifdef LLAMA_DEBUG
  405. MK_NVCCFLAGS += -lineinfo
  406. endif # LLAMA_DEBUG
  407. ifdef LLAMA_CUDA_DEBUG
  408. MK_NVCCFLAGS += --device-debug
  409. endif # LLAMA_CUDA_DEBUG
  410. ifdef LLAMA_CUDA_NVCC
  411. NVCC = $(CCACHE) $(LLAMA_CUDA_NVCC)
  412. else
  413. NVCC = $(CCACHE) nvcc
  414. endif #LLAMA_CUDA_NVCC
  415. ifdef CUDA_DOCKER_ARCH
  416. MK_NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  417. else ifndef CUDA_POWER_ARCH
  418. MK_NVCCFLAGS += -arch=native
  419. endif # CUDA_DOCKER_ARCH
  420. ifdef LLAMA_CUDA_FORCE_DMMV
  421. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  422. endif # LLAMA_CUDA_FORCE_DMMV
  423. ifdef LLAMA_CUDA_FORCE_MMQ
  424. MK_NVCCFLAGS += -DGGML_CUDA_FORCE_MMQ
  425. endif # LLAMA_CUDA_FORCE_MMQ
  426. ifdef LLAMA_CUDA_DMMV_X
  427. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  428. else
  429. MK_NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  430. endif # LLAMA_CUDA_DMMV_X
  431. ifdef LLAMA_CUDA_MMV_Y
  432. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  433. else ifdef LLAMA_CUDA_DMMV_Y
  434. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_DMMV_Y) # for backwards compatibility
  435. else
  436. MK_NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  437. endif # LLAMA_CUDA_MMV_Y
  438. ifdef LLAMA_CUDA_F16
  439. MK_NVCCFLAGS += -DGGML_CUDA_F16
  440. endif # LLAMA_CUDA_F16
  441. ifdef LLAMA_CUDA_DMMV_F16
  442. MK_NVCCFLAGS += -DGGML_CUDA_F16
  443. endif # LLAMA_CUDA_DMMV_F16
  444. ifdef LLAMA_CUDA_KQUANTS_ITER
  445. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  446. else
  447. MK_NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  448. endif
  449. ifdef LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  450. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=$(LLAMA_CUDA_PEER_MAX_BATCH_SIZE)
  451. else
  452. MK_NVCCFLAGS += -DGGML_CUDA_PEER_MAX_BATCH_SIZE=128
  453. endif # LLAMA_CUDA_PEER_MAX_BATCH_SIZE
  454. ifdef LLAMA_CUDA_NO_PEER_COPY
  455. MK_NVCCFLAGS += -DGGML_CUDA_NO_PEER_COPY
  456. endif # LLAMA_CUDA_NO_PEER_COPY
  457. ifdef LLAMA_CUDA_CCBIN
  458. MK_NVCCFLAGS += -ccbin $(LLAMA_CUDA_CCBIN)
  459. endif # LLAMA_CUDA_CCBIN
  460. ifdef LLAMA_CUDA_FA_ALL_QUANTS
  461. MK_NVCCFLAGS += -DGGML_CUDA_FA_ALL_QUANTS
  462. endif # LLAMA_CUDA_FA_ALL_QUANTS
  463. ifdef JETSON_EOL_MODULE_DETECT
  464. define NVCC_COMPILE
  465. $(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 $@
  466. endef # NVCC_COMPILE
  467. else
  468. define NVCC_COMPILE
  469. $(NVCC) $(NVCCFLAGS) $(CPPFLAGS) -Xcompiler "$(CUDA_CXXFLAGS)" -c $< -o $@
  470. endef # NVCC_COMPILE
  471. endif # JETSON_EOL_MODULE_DETECT
  472. ggml-cuda/%.o: ggml-cuda/%.cu ggml.h ggml-common.h ggml-cuda/common.cuh
  473. $(NVCC_COMPILE)
  474. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h ggml.h ggml-backend.h ggml-backend-impl.h ggml-common.h $(wildcard ggml-cuda/*.cuh)
  475. $(NVCC_COMPILE)
  476. endif # LLAMA_CUDA
  477. ifdef LLAMA_CLBLAST
  478. MK_CPPFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags-only-I clblast OpenCL)
  479. MK_CFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  480. MK_CXXFLAGS += $(shell pkg-config --cflags-only-other clblast OpenCL)
  481. # Mac provides OpenCL as a framework
  482. ifeq ($(UNAME_S),Darwin)
  483. MK_LDFLAGS += -lclblast -framework OpenCL
  484. else
  485. MK_LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
  486. endif
  487. OBJS += ggml-opencl.o
  488. ggml-opencl.o: ggml-opencl.cpp ggml-opencl.h
  489. $(CXX) $(CXXFLAGS) -c $< -o $@
  490. endif # LLAMA_CLBLAST
  491. ifdef LLAMA_VULKAN
  492. MK_CPPFLAGS += -DGGML_USE_VULKAN
  493. MK_LDFLAGS += -lvulkan
  494. OBJS += ggml-vulkan.o
  495. ifdef LLAMA_VULKAN_CHECK_RESULTS
  496. MK_CPPFLAGS += -DGGML_VULKAN_CHECK_RESULTS
  497. endif
  498. ifdef LLAMA_VULKAN_DEBUG
  499. MK_CPPFLAGS += -DGGML_VULKAN_DEBUG
  500. endif
  501. ifdef LLAMA_VULKAN_VALIDATE
  502. MK_CPPFLAGS += -DGGML_VULKAN_VALIDATE
  503. endif
  504. ifdef LLAMA_VULKAN_RUN_TESTS
  505. MK_CPPFLAGS += -DGGML_VULKAN_RUN_TESTS
  506. endif
  507. ggml-vulkan.o: ggml-vulkan.cpp ggml-vulkan.h
  508. $(CXX) $(CXXFLAGS) -c $< -o $@
  509. endif # LLAMA_VULKAN
  510. ifdef LLAMA_HIPBLAS
  511. ifeq ($(wildcard /opt/rocm),)
  512. ROCM_PATH ?= /usr
  513. AMDGPU_TARGETS ?= $(shell $(shell which amdgpu-arch))
  514. else
  515. ROCM_PATH ?= /opt/rocm
  516. AMDGPU_TARGETS ?= $(shell $(ROCM_PATH)/llvm/bin/amdgpu-arch)
  517. endif
  518. HIPCC ?= $(CCACHE) $(ROCM_PATH)/bin/hipcc
  519. LLAMA_CUDA_DMMV_X ?= 32
  520. LLAMA_CUDA_MMV_Y ?= 1
  521. LLAMA_CUDA_KQUANTS_ITER ?= 2
  522. MK_CPPFLAGS += -DGGML_USE_HIPBLAS -DGGML_USE_CUDA
  523. ifdef LLAMA_HIP_UMA
  524. MK_CPPFLAGS += -DGGML_HIP_UMA
  525. endif # LLAMA_HIP_UMA
  526. MK_LDFLAGS += -L$(ROCM_PATH)/lib -Wl,-rpath=$(ROCM_PATH)/lib
  527. MK_LDFLAGS += -L$(ROCM_PATH)/lib64 -Wl,-rpath=$(ROCM_PATH)/lib64
  528. MK_LDFLAGS += -lhipblas -lamdhip64 -lrocblas
  529. HIPFLAGS += $(addprefix --offload-arch=,$(AMDGPU_TARGETS))
  530. HIPFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  531. HIPFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  532. HIPFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  533. ifdef LLAMA_CUDA_FORCE_DMMV
  534. HIPFLAGS += -DGGML_CUDA_FORCE_DMMV
  535. endif # LLAMA_CUDA_FORCE_DMMV
  536. ifdef LLAMA_CUDA_NO_PEER_COPY
  537. HIPFLAGS += -DGGML_CUDA_NO_PEER_COPY
  538. endif # LLAMA_CUDA_NO_PEER_COPY
  539. OBJS += ggml-cuda.o
  540. OBJS += $(patsubst %.cu,%.o,$(wildcard ggml-cuda/*.cu))
  541. OBJS += $(OBJS_CUDA_TEMP_INST)
  542. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h ggml.h ggml-backend.h ggml-backend-impl.h ggml-common.h $(wildcard ggml-cuda/*.cuh)
  543. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  544. ggml-cuda/%.o: ggml-cuda/%.cu ggml.h ggml-common.h ggml-cuda/common.cuh
  545. $(HIPCC) $(CXXFLAGS) $(HIPFLAGS) -x hip -c -o $@ $<
  546. endif # LLAMA_HIPBLAS
  547. ifdef LLAMA_METAL
  548. MK_CPPFLAGS += -DGGML_USE_METAL
  549. MK_LDFLAGS += -framework Foundation -framework Metal -framework MetalKit
  550. OBJS += ggml-metal.o
  551. ifdef LLAMA_METAL_NDEBUG
  552. MK_CPPFLAGS += -DGGML_METAL_NDEBUG
  553. endif
  554. ifdef LLAMA_METAL_EMBED_LIBRARY
  555. MK_CPPFLAGS += -DGGML_METAL_EMBED_LIBRARY
  556. OBJS += ggml-metal-embed.o
  557. endif
  558. endif # LLAMA_METAL
  559. ifdef LLAMA_METAL
  560. ggml-metal.o: ggml-metal.m ggml-metal.h ggml.h
  561. $(CC) $(CFLAGS) -c $< -o $@
  562. ifdef LLAMA_METAL_EMBED_LIBRARY
  563. ggml-metal-embed.o: ggml-metal.metal ggml-common.h
  564. @echo "Embedding Metal library"
  565. @sed -e '/#include "ggml-common.h"/r ggml-common.h' -e '/#include "ggml-common.h"/d' < ggml-metal.metal > ggml-metal-embed.metal
  566. $(eval TEMP_ASSEMBLY=$(shell mktemp))
  567. @echo ".section __DATA, __ggml_metallib" > $(TEMP_ASSEMBLY)
  568. @echo ".globl _ggml_metallib_start" >> $(TEMP_ASSEMBLY)
  569. @echo "_ggml_metallib_start:" >> $(TEMP_ASSEMBLY)
  570. @echo ".incbin \"ggml-metal-embed.metal\"" >> $(TEMP_ASSEMBLY)
  571. @echo ".globl _ggml_metallib_end" >> $(TEMP_ASSEMBLY)
  572. @echo "_ggml_metallib_end:" >> $(TEMP_ASSEMBLY)
  573. @$(AS) $(TEMP_ASSEMBLY) -o $@
  574. @rm -f ${TEMP_ASSEMBLY}
  575. endif
  576. endif # LLAMA_METAL
  577. OBJS += ggml-alloc.o ggml-backend.o ggml-quants.o unicode.o unicode-data.o
  578. COMMON_H_DEPS = common/common.h common/sampling.h common/log.h llama.h
  579. COMMON_DEPS = common.o sampling.o grammar-parser.o build-info.o json-schema-to-grammar.o
  580. ifndef LLAMA_NO_LLAMAFILE
  581. sgemm.o: sgemm.cpp sgemm.h ggml.h
  582. $(CXX) $(CXXFLAGS) -c $< -o $@
  583. endif
  584. ifdef LLAMA_RPC
  585. ggml-rpc.o: ggml-rpc.cpp ggml-rpc.h
  586. $(CXX) $(CXXFLAGS) -c $< -o $@
  587. rpc-server.o: examples/rpc/rpc-server.cpp ggml-rpc.h
  588. $(CXX) $(CXXFLAGS) -c $< -o $@
  589. rpc-server: rpc-server.o ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  590. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  591. endif # LLAMA_RPC
  592. GF_CC := $(CC)
  593. include scripts/get-flags.mk
  594. # combine build flags with cmdline overrides
  595. override CPPFLAGS := $(MK_CPPFLAGS) $(CPPFLAGS)
  596. override CFLAGS := $(CPPFLAGS) $(MK_CFLAGS) $(GF_CFLAGS) $(CFLAGS)
  597. BASE_CXXFLAGS := $(MK_CXXFLAGS) $(CXXFLAGS)
  598. override CXXFLAGS := $(BASE_CXXFLAGS) $(HOST_CXXFLAGS) $(GF_CXXFLAGS) $(CPPFLAGS)
  599. override NVCCFLAGS := $(MK_NVCCFLAGS) $(NVCCFLAGS)
  600. override LDFLAGS := $(MK_LDFLAGS) $(LDFLAGS)
  601. # identify CUDA host compiler
  602. ifdef LLAMA_CUDA
  603. GF_CC := $(NVCC) $(NVCCFLAGS) 2>/dev/null .c -Xcompiler
  604. include scripts/get-flags.mk
  605. CUDA_CXXFLAGS := $(BASE_CXXFLAGS) $(GF_CXXFLAGS) -Wno-pedantic
  606. endif
  607. ifdef LLAMA_CURL
  608. override CXXFLAGS := $(CXXFLAGS) -DLLAMA_USE_CURL
  609. override LDFLAGS := $(LDFLAGS) -lcurl
  610. endif
  611. #
  612. # Print build information
  613. #
  614. $(info I llama.cpp build info: )
  615. $(info I UNAME_S: $(UNAME_S))
  616. $(info I UNAME_P: $(UNAME_P))
  617. $(info I UNAME_M: $(UNAME_M))
  618. $(info I CFLAGS: $(CFLAGS))
  619. $(info I CXXFLAGS: $(CXXFLAGS))
  620. $(info I NVCCFLAGS: $(NVCCFLAGS))
  621. $(info I LDFLAGS: $(LDFLAGS))
  622. $(info I CC: $(shell $(CC) --version | head -n 1))
  623. $(info I CXX: $(shell $(CXX) --version | head -n 1))
  624. ifdef LLAMA_CUDA
  625. $(info I NVCC: $(shell $(NVCC) --version | tail -n 1))
  626. CUDA_VERSION := $(shell $(NVCC) --version | grep -oP 'release (\K[0-9]+\.[0-9])')
  627. ifeq ($(shell awk -v "v=$(CUDA_VERSION)" 'BEGIN { print (v < 11.7) }'),1)
  628. ifndef CUDA_DOCKER_ARCH
  629. ifndef CUDA_POWER_ARCH
  630. $(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 )
  631. endif # CUDA_POWER_ARCH
  632. endif # CUDA_DOCKER_ARCH
  633. endif # eq ($(shell echo "$(CUDA_VERSION) < 11.7" | bc),1)
  634. endif # LLAMA_CUDA
  635. $(info )
  636. ifdef LLAMA_CUBLAS
  637. $(info !!!!)
  638. $(info LLAMA_CUBLAS is deprecated and will be removed in the future. Use LLAMA_CUDA instead.)
  639. $(info !!!!)
  640. $(info )
  641. endif
  642. #
  643. # Build library
  644. #
  645. ggml.o: ggml.c ggml.h ggml-cuda.h
  646. $(CC) $(CFLAGS) -c $< -o $@
  647. ggml-alloc.o: ggml-alloc.c ggml.h ggml-alloc.h
  648. $(CC) $(CFLAGS) -c $< -o $@
  649. ggml-backend.o: ggml-backend.c ggml.h ggml-backend.h
  650. $(CC) $(CFLAGS) -c $< -o $@
  651. ggml-quants.o: ggml-quants.c ggml.h ggml-quants.h ggml-common.h
  652. $(CC) $(CFLAGS) -c $< -o $@
  653. unicode.o: unicode.cpp unicode.h
  654. $(CXX) $(CXXFLAGS) -c $< -o $@
  655. unicode-data.o: unicode-data.cpp unicode-data.h
  656. $(CXX) $(CXXFLAGS) -c $< -o $@
  657. llama.o: llama.cpp unicode.h ggml.h ggml-alloc.h ggml-backend.h ggml-cuda.h ggml-metal.h llama.h
  658. $(CXX) $(CXXFLAGS) -c $< -o $@
  659. common.o: common/common.cpp $(COMMON_H_DEPS)
  660. $(CXX) $(CXXFLAGS) -c $< -o $@
  661. sampling.o: common/sampling.cpp $(COMMON_H_DEPS)
  662. $(CXX) $(CXXFLAGS) -c $< -o $@
  663. console.o: common/console.cpp common/console.h
  664. $(CXX) $(CXXFLAGS) -c $< -o $@
  665. grammar-parser.o: common/grammar-parser.cpp common/grammar-parser.h
  666. $(CXX) $(CXXFLAGS) -c $< -o $@
  667. json-schema-to-grammar.o: common/json-schema-to-grammar.cpp common/json-schema-to-grammar.h
  668. $(CXX) $(CXXFLAGS) -c $< -o $@
  669. train.o: common/train.cpp common/train.h
  670. $(CXX) $(CXXFLAGS) -c $< -o $@
  671. ngram-cache.o: common/ngram-cache.cpp common/ngram-cache.h
  672. $(CXX) $(CXXFLAGS) -c $< -o $@
  673. libllama.so: llama.o ggml.o $(OBJS)
  674. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  675. libllama.a: llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  676. ar rcs libllama.a llama.o ggml.o $(OBJS) $(COMMON_DEPS)
  677. clean:
  678. rm -vrf *.o tests/*.o *.so *.a *.dll benchmark-matmult lookup-create lookup-merge lookup-stats common/build-info.cpp *.dot $(COV_TARGETS) $(BUILD_TARGETS) $(TEST_TARGETS)
  679. rm -vrf ggml-cuda/*.o
  680. rm -vrf ggml-cuda/template-instances/*.o
  681. find examples pocs -type f -name "*.o" -delete
  682. #
  683. # Examples
  684. #
  685. # $< is the first prerequisite, i.e. the source file.
  686. # Explicitly compile this to an object file so that it can be cached with ccache.
  687. # The source file is then filtered out from $^ (the list of all prerequisites) and the object file is added instead.
  688. # Helper function that replaces .c, .cpp, and .cu file endings with .o:
  689. GET_OBJ_FILE = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(patsubst %.cu,%.o,$(1))))
  690. main: examples/main/main.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  691. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  692. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  693. @echo
  694. @echo '==== Run ./main -h for help. ===='
  695. @echo
  696. infill: examples/infill/infill.cpp ggml.o llama.o $(COMMON_DEPS) console.o grammar-parser.o $(OBJS)
  697. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  698. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  699. simple: examples/simple/simple.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  700. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  701. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  702. tokenize: examples/tokenize/tokenize.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  703. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  704. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  705. batched: examples/batched/batched.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  706. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  707. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  708. batched-bench: examples/batched-bench/batched-bench.cpp build-info.o ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  709. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  710. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  711. quantize: examples/quantize/quantize.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  712. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  713. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  714. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.o ggml.o llama.o $(OBJS)
  715. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  716. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  717. perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  718. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  719. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  720. imatrix: examples/imatrix/imatrix.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  721. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  722. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  723. embedding: examples/embedding/embedding.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  724. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  725. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  726. gritlm: examples/gritlm/gritlm.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  727. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  728. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  729. save-load-state: examples/save-load-state/save-load-state.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  730. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  731. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  732. server: examples/server/server.cpp examples/server/utils.hpp examples/server/httplib.h common/json.hpp examples/server/colorthemes.css.hpp examples/server/style.css.hpp examples/server/theme-beeninorder.css.hpp examples/server/theme-ketivah.css.hpp examples/server/theme-mangotango.css.hpp examples/server/theme-playground.css.hpp examples/server/theme-polarnight.css.hpp examples/server/theme-snowstorm.css.hpp examples/server/index.html.hpp examples/server/index-new.html.hpp examples/server/index.js.hpp examples/server/completion.js.hpp examples/server/system-prompts.js.hpp examples/server/prompt-formats.js.hpp examples/server/json-schema-to-grammar.mjs.hpp common/stb_image.h ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  733. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  734. $(CXX) $(CXXFLAGS) $(filter-out %.h %.hpp $<,$^) -Iexamples/server $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS) $(LWINSOCK2)
  735. # Portable equivalent of `cd examples/server/public && xxd -i $(notdir $<) ../$(notdir $<).hpp`:
  736. examples/server/%.hpp: examples/server/public/% Makefile
  737. @( export NAME=$(subst .,_,$(subst -,_,$(notdir $<))) && \
  738. echo "unsigned char $${NAME}[] = {" && \
  739. cat $< | od -v -t x1 -An | sed -E 's/([0-9a-fA-F]+)/0x\1, /g' && \
  740. echo "};" && \
  741. echo "unsigned int $${NAME}_len = $(shell cat $< | wc -c );" \
  742. ) > $@
  743. gguf: examples/gguf/gguf.cpp ggml.o $(OBJS)
  744. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  745. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  746. gguf-split: examples/gguf-split/gguf-split.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  747. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  748. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  749. eval-callback: examples/eval-callback/eval-callback.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  750. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  751. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  752. train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  753. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  754. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  755. convert-llama2c-to-ggml: examples/convert-llama2c-to-ggml/convert-llama2c-to-ggml.cpp ggml.o llama.o $(OBJS)
  756. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  757. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  758. llama-bench: examples/llama-bench/llama-bench.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  759. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  760. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  761. libllava.a: examples/llava/llava.cpp examples/llava/llava.h examples/llava/clip.cpp examples/llava/clip.h common/stb_image.h common/base64.hpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  762. $(CXX) $(CXXFLAGS) -static -fPIC -c $< -o $@ -Wno-cast-qual
  763. llava-cli: examples/llava/llava-cli.cpp examples/llava/clip.h examples/llava/clip.cpp examples/llava/llava.h examples/llava/llava.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  764. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  765. $(CXX) $(CXXFLAGS) -c examples/llava/clip.cpp -o $(call GET_OBJ_FILE, examples/llava/clip.cpp) -Wno-cast-qual
  766. $(CXX) $(CXXFLAGS) -c examples/llava/llava.cpp -o $(call GET_OBJ_FILE, examples/llava/llava.cpp)
  767. $(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)
  768. baby-llama: examples/baby-llama/baby-llama.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  769. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  770. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  771. beam-search: examples/beam-search/beam-search.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  772. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  773. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  774. finetune: examples/finetune/finetune.cpp ggml.o llama.o $(COMMON_DEPS) train.o $(OBJS)
  775. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  776. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  777. export-lora: examples/export-lora/export-lora.cpp ggml.o common/common.h $(OBJS)
  778. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  779. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  780. retrieval: examples/retrieval/retrieval.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  781. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  782. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  783. speculative: examples/speculative/speculative.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  784. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  785. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  786. parallel: examples/parallel/parallel.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  787. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  788. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  789. lookahead: examples/lookahead/lookahead.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  790. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  791. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  792. lookup: examples/lookup/lookup.cpp ggml.o llama.o ngram-cache.o $(COMMON_DEPS) $(OBJS)
  793. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  794. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  795. $(CXX) $(CXXFLAGS) -c examples/lookup/lookup-create.cpp -o $(call GET_OBJ_FILE, examples/lookup/lookup-create.cpp)
  796. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, examples/lookup/lookup-create.cpp) -o lookup-create $(LDFLAGS)
  797. $(CXX) $(CXXFLAGS) -c examples/lookup/lookup-merge.cpp -o $(call GET_OBJ_FILE, examples/lookup/lookup-merge.cpp)
  798. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, examples/lookup/lookup-merge.cpp) -o lookup-merge $(LDFLAGS)
  799. $(CXX) $(CXXFLAGS) -c examples/lookup/lookup-stats.cpp -o $(call GET_OBJ_FILE, examples/lookup/lookup-stats.cpp)
  800. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, examples/lookup/lookup-stats.cpp) -o lookup-stats $(LDFLAGS)
  801. passkey: examples/passkey/passkey.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  802. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  803. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  804. gbnf-validator: examples/gbnf-validator/gbnf-validator.cpp ggml.o llama.o $(COMMON_DEPS) grammar-parser.o $(OBJS)
  805. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  806. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  807. ifeq ($(UNAME_S),Darwin)
  808. swift: examples/batched.swift
  809. (cd examples/batched.swift; make build)
  810. endif
  811. common/build-info.cpp: $(wildcard .git/index) scripts/build-info.sh
  812. @sh scripts/build-info.sh "$(CC)" > $@.tmp
  813. @if ! cmp -s $@.tmp $@; then \
  814. mv $@.tmp $@; \
  815. else \
  816. rm $@.tmp; \
  817. fi
  818. build-info.o: common/build-info.cpp
  819. $(CXX) $(CXXFLAGS) -c $(filter-out %.h,$^) -o $@
  820. #
  821. # Tests
  822. #
  823. tests: $(TEST_TARGETS)
  824. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.o ggml.o $(OBJS)
  825. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  826. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  827. run-benchmark-matmult: benchmark-matmult
  828. ./$@
  829. .PHONY: run-benchmark-matmult swift
  830. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  831. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  832. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  833. q8dot: pocs/vdot/q8dot.cpp ggml.o $(OBJS)
  834. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  835. $(CXX) $(CXXFLAGS) $(filter-out $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  836. tests/test-llama-grammar: tests/test-llama-grammar.cpp ggml.o grammar-parser.o $(OBJS)
  837. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  838. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  839. tests/test-grammar-parser: tests/test-grammar-parser.cpp ggml.o llama.o grammar-parser.o $(OBJS)
  840. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  841. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  842. tests/test-grammar-integration: tests/test-grammar-integration.cpp ggml.o llama.o grammar-parser.o $(OBJS)
  843. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  844. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  845. tests/test-double-float: tests/test-double-float.cpp ggml.o $(OBJS)
  846. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  847. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  848. tests/test-json-schema-to-grammar: tests/test-json-schema-to-grammar.cpp json-schema-to-grammar.o ggml.o llama.o grammar-parser.o $(OBJS)
  849. $(CXX) $(CXXFLAGS) -Iexamples/server -c $< -o $(call GET_OBJ_FILE, $<)
  850. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  851. tests/test-grad0: tests/test-grad0.cpp ggml.o $(OBJS)
  852. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  853. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  854. tests/test-opt: tests/test-opt.cpp ggml.o $(OBJS)
  855. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  856. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  857. tests/test-quantize-fns: tests/test-quantize-fns.cpp ggml.o $(OBJS)
  858. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  859. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  860. tests/test-quantize-perf: tests/test-quantize-perf.cpp ggml.o $(OBJS)
  861. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  862. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  863. tests/test-sampling: tests/test-sampling.cpp ggml.o llama.o $(OBJS)
  864. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  865. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  866. tests/test-tokenizer-0: tests/test-tokenizer-0.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  867. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  868. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  869. tests/test-tokenizer-1-bpe: tests/test-tokenizer-1-bpe.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  870. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  871. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  872. tests/test-tokenizer-1-spm: tests/test-tokenizer-1-spm.cpp ggml.o llama.o $(COMMON_DEPS) console.o $(OBJS)
  873. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  874. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  875. tests/test-rope: tests/test-rope.cpp ggml.o $(OBJS)
  876. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  877. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  878. tests/test-c.o: tests/test-c.c llama.h
  879. $(CC) $(CFLAGS) -c $(filter-out %.h,$^) -o $@
  880. tests/test-backend-ops: tests/test-backend-ops.cpp ggml.o $(OBJS)
  881. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  882. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  883. tests/test-model-load-cancel: tests/test-model-load-cancel.cpp ggml.o llama.o tests/get-model.cpp $(COMMON_DEPS) $(OBJS)
  884. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  885. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  886. tests/test-autorelease: tests/test-autorelease.cpp ggml.o llama.o tests/get-model.cpp $(COMMON_DEPS) $(OBJS)
  887. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  888. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)
  889. tests/test-chat-template: tests/test-chat-template.cpp ggml.o llama.o $(COMMON_DEPS) $(OBJS)
  890. $(CXX) $(CXXFLAGS) -c $< -o $(call GET_OBJ_FILE, $<)
  891. $(CXX) $(CXXFLAGS) $(filter-out %.h $<,$^) $(call GET_OBJ_FILE, $<) -o $@ $(LDFLAGS)