Makefile 38 KB

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