Makefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. # Define the default target now so that it is always the first target
  2. BUILD_TARGETS = main quantize quantize-stats perplexity embedding vdot train-text-from-scratch simple server embd-input-test
  3. # Binaries only useful for tests
  4. TEST_TARGETS = tests/test-double-float tests/test-grad0 tests/test-opt tests/test-quantize-fns tests/test-quantize-perf tests/test-sampling tests/test-tokenizer-0
  5. default: $(BUILD_TARGETS)
  6. ifndef UNAME_S
  7. UNAME_S := $(shell uname -s)
  8. endif
  9. ifndef UNAME_P
  10. UNAME_P := $(shell uname -p)
  11. endif
  12. ifndef UNAME_M
  13. UNAME_M := $(shell uname -m)
  14. endif
  15. CCV := $(shell $(CC) --version | head -n 1)
  16. CXXV := $(shell $(CXX) --version | head -n 1)
  17. # Mac OS + Arm can report x86_64
  18. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  19. ifeq ($(UNAME_S),Darwin)
  20. ifneq ($(UNAME_P),arm)
  21. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  22. ifeq ($(SYSCTL_M),1)
  23. # UNAME_P := arm
  24. # UNAME_M := arm64
  25. 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)
  26. endif
  27. endif
  28. endif
  29. #
  30. # Compile flags
  31. #
  32. # keep standard at C11 and C++11
  33. # -Ofast tends to produce faster code, but may not be available for some compilers.
  34. ifdef LLAMA_FAST
  35. OPT = -Ofast
  36. else
  37. OPT = -O3
  38. endif
  39. CFLAGS = -I. $(OPT) -std=c11 -fPIC
  40. CXXFLAGS = -I. -I./examples $(OPT) -std=c++11 -fPIC
  41. LDFLAGS =
  42. ifdef LLAMA_DEBUG
  43. CFLAGS += -O0 -g
  44. CXXFLAGS += -O0 -g
  45. LDFLAGS += -g
  46. else
  47. CFLAGS += -DNDEBUG
  48. CXXFLAGS += -DNDEBUG
  49. endif
  50. ifdef LLAMA_SERVER_VERBOSE
  51. CXXFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
  52. endif
  53. # warnings
  54. CFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith \
  55. -Wmissing-prototypes
  56. CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wno-multichar
  57. # OS specific
  58. # TODO: support Windows
  59. ifeq ($(UNAME_S),Linux)
  60. CFLAGS += -pthread
  61. CXXFLAGS += -pthread
  62. endif
  63. ifeq ($(UNAME_S),Darwin)
  64. CFLAGS += -pthread
  65. CXXFLAGS += -pthread
  66. endif
  67. ifeq ($(UNAME_S),FreeBSD)
  68. CFLAGS += -pthread
  69. CXXFLAGS += -pthread
  70. endif
  71. ifeq ($(UNAME_S),NetBSD)
  72. CFLAGS += -pthread
  73. CXXFLAGS += -pthread
  74. endif
  75. ifeq ($(UNAME_S),OpenBSD)
  76. CFLAGS += -pthread
  77. CXXFLAGS += -pthread
  78. endif
  79. ifeq ($(UNAME_S),Haiku)
  80. CFLAGS += -pthread
  81. CXXFLAGS += -pthread
  82. endif
  83. # detect Windows
  84. ifneq ($(findstring _NT,$(UNAME_S)),)
  85. _WIN32 := 1
  86. endif
  87. # library name prefix
  88. ifneq ($(_WIN32),1)
  89. LIB_PRE := lib
  90. endif
  91. # Dynamic Shared Object extension
  92. ifneq ($(_WIN32),1)
  93. DSO_EXT := .so
  94. else
  95. DSO_EXT := .dll
  96. endif
  97. # Windows Sockets 2 (Winsock) for network-capable apps
  98. ifeq ($(_WIN32),1)
  99. LWINSOCK2 := -lws2_32
  100. endif
  101. ifdef LLAMA_GPROF
  102. CFLAGS += -pg
  103. CXXFLAGS += -pg
  104. endif
  105. ifdef LLAMA_PERF
  106. CFLAGS += -DGGML_PERF
  107. CXXFLAGS += -DGGML_PERF
  108. endif
  109. # Architecture specific
  110. # TODO: probably these flags need to be tweaked on some architectures
  111. # feel free to update the Makefile for your architecture and send a pull request or issue
  112. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686 amd64))
  113. # Use all CPU extensions that are available:
  114. CFLAGS += -march=native -mtune=native
  115. CXXFLAGS += -march=native -mtune=native
  116. # Usage AVX-only
  117. #CFLAGS += -mfma -mf16c -mavx
  118. #CXXFLAGS += -mfma -mf16c -mavx
  119. # Usage SSSE3-only (Not is SSE3!)
  120. #CFLAGS += -mssse3
  121. #CXXFLAGS += -mssse3
  122. endif
  123. ifneq ($(filter ppc64%,$(UNAME_M)),)
  124. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  125. ifneq (,$(findstring POWER9,$(POWER9_M)))
  126. CFLAGS += -mcpu=power9
  127. CXXFLAGS += -mcpu=power9
  128. endif
  129. # Require c++23's std::byteswap for big-endian support.
  130. ifeq ($(UNAME_M),ppc64)
  131. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  132. endif
  133. endif
  134. ifndef LLAMA_NO_K_QUANTS
  135. CFLAGS += -DGGML_USE_K_QUANTS
  136. CXXFLAGS += -DGGML_USE_K_QUANTS
  137. OBJS += k_quants.o
  138. ifdef LLAMA_QKK_64
  139. CFLAGS += -DGGML_QKK_64
  140. CXXFLAGS += -DGGML_QKK_64
  141. endif
  142. endif
  143. ifndef LLAMA_NO_ACCELERATE
  144. # Mac M1 - include Accelerate framework.
  145. # `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
  146. ifeq ($(UNAME_S),Darwin)
  147. CFLAGS += -DGGML_USE_ACCELERATE
  148. LDFLAGS += -framework Accelerate
  149. endif
  150. endif # LLAMA_NO_ACCELERATE
  151. ifdef LLAMA_MPI
  152. CFLAGS += -DGGML_USE_MPI -Wno-cast-qual
  153. CXXFLAGS += -DGGML_USE_MPI -Wno-cast-qual
  154. OBJS += ggml-mpi.o
  155. endif # LLAMA_MPI
  156. ifdef LLAMA_OPENBLAS
  157. CFLAGS += -DGGML_USE_OPENBLAS $(shell pkg-config --cflags openblas)
  158. LDFLAGS += $(shell pkg-config --libs openblas)
  159. endif # LLAMA_OPENBLAS
  160. ifdef LLAMA_BLIS
  161. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
  162. LDFLAGS += -lblis -L/usr/local/lib
  163. endif # LLAMA_BLIS
  164. ifdef LLAMA_CUBLAS
  165. CFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  166. CXXFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  167. LDFLAGS += -lcublas -lculibos -lcudart -lcublasLt -lpthread -ldl -lrt -L/usr/local/cuda/lib64 -L/opt/cuda/lib64 -L$(CUDA_PATH)/targets/x86_64-linux/lib
  168. OBJS += ggml-cuda.o
  169. NVCCFLAGS = --forward-unknown-to-host-compiler
  170. ifdef LLAMA_CUDA_NVCC
  171. NVCC = $(LLAMA_CUDA_NVCC)
  172. else
  173. NVCC = nvcc
  174. endif #LLAMA_CUDA_NVCC
  175. ifdef CUDA_DOCKER_ARCH
  176. NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  177. else
  178. NVCCFLAGS += -arch=native
  179. endif # CUDA_DOCKER_ARCH
  180. ifdef LLAMA_CUDA_FORCE_DMMV
  181. NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  182. endif # LLAMA_CUDA_FORCE_DMMV
  183. ifdef LLAMA_CUDA_DMMV_X
  184. NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  185. else
  186. NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  187. endif # LLAMA_CUDA_DMMV_X
  188. ifdef LLAMA_CUDA_MMV_Y
  189. NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  190. else ifdef LLAMA_CUDA_DMMV_Y
  191. NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_DMMV_Y) # for backwards compatibility
  192. else
  193. NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  194. endif # LLAMA_CUDA_MMV_Y
  195. ifdef LLAMA_CUDA_DMMV_F16
  196. NVCCFLAGS += -DGGML_CUDA_DMMV_F16
  197. endif # LLAMA_CUDA_DMMV_F16
  198. ifdef LLAMA_CUDA_KQUANTS_ITER
  199. NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  200. else
  201. NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  202. endif
  203. ifdef LLAMA_CUDA_CCBIN
  204. NVCCFLAGS += -ccbin $(LLAMA_CUDA_CCBIN)
  205. endif
  206. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  207. $(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -Wno-pedantic -c $< -o $@
  208. endif # LLAMA_CUBLAS
  209. ifdef LLAMA_CLBLAST
  210. CFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags clblast OpenCL)
  211. CXXFLAGS += -DGGML_USE_CLBLAST $(shell pkg-config --cflags clblast OpenCL)
  212. # Mac provides OpenCL as a framework
  213. ifeq ($(UNAME_S),Darwin)
  214. LDFLAGS += -lclblast -framework OpenCL
  215. else
  216. LDFLAGS += $(shell pkg-config --libs clblast OpenCL)
  217. endif
  218. OBJS += ggml-opencl.o
  219. ggml-opencl.o: ggml-opencl.cpp ggml-opencl.h
  220. $(CXX) $(CXXFLAGS) -c $< -o $@
  221. endif # LLAMA_CLBLAST
  222. ifdef LLAMA_METAL
  223. CFLAGS += -DGGML_USE_METAL -DGGML_METAL_NDEBUG
  224. CXXFLAGS += -DGGML_USE_METAL
  225. LDFLAGS += -framework Foundation -framework Metal -framework MetalKit -framework MetalPerformanceShaders
  226. OBJS += ggml-metal.o
  227. endif # LLAMA_METAL
  228. ifneq ($(filter aarch64%,$(UNAME_M)),)
  229. # Apple M1, M2, etc.
  230. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  231. CFLAGS += -mcpu=native
  232. CXXFLAGS += -mcpu=native
  233. endif
  234. ifneq ($(filter armv6%,$(UNAME_M)),)
  235. # Raspberry Pi 1, Zero
  236. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  237. endif
  238. ifneq ($(filter armv7%,$(UNAME_M)),)
  239. # Raspberry Pi 2
  240. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  241. endif
  242. ifneq ($(filter armv8%,$(UNAME_M)),)
  243. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  244. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  245. endif
  246. ifdef LLAMA_METAL
  247. ggml-metal.o: ggml-metal.m ggml-metal.h
  248. $(CC) $(CFLAGS) -c $< -o $@
  249. endif # LLAMA_METAL
  250. ifdef LLAMA_MPI
  251. ggml-mpi.o: ggml-mpi.c ggml-mpi.h
  252. $(CC) $(CFLAGS) -c $< -o $@
  253. endif # LLAMA_MPI
  254. ifdef LLAMA_NO_K_QUANTS
  255. k_quants.o: k_quants.c k_quants.h
  256. $(CC) $(CFLAGS) -c $< -o $@
  257. endif # LLAMA_NO_K_QUANTS
  258. #
  259. # Print build information
  260. #
  261. $(info I llama.cpp build info: )
  262. $(info I UNAME_S: $(UNAME_S))
  263. $(info I UNAME_P: $(UNAME_P))
  264. $(info I UNAME_M: $(UNAME_M))
  265. $(info I CFLAGS: $(CFLAGS))
  266. $(info I CXXFLAGS: $(CXXFLAGS))
  267. $(info I LDFLAGS: $(LDFLAGS))
  268. $(info I CC: $(CCV))
  269. $(info I CXX: $(CXXV))
  270. $(info )
  271. #
  272. # Build library
  273. #
  274. ggml.o: ggml.c ggml.h ggml-cuda.h
  275. $(CC) $(CFLAGS) -c $< -o $@
  276. llama.o: llama.cpp ggml.h ggml-cuda.h ggml-metal.h llama.h llama-util.h
  277. $(CXX) $(CXXFLAGS) -c $< -o $@
  278. common.o: examples/common.cpp examples/common.h
  279. $(CXX) $(CXXFLAGS) -c $< -o $@
  280. grammar-parser.o: examples/grammar-parser.cpp examples/grammar-parser.h
  281. $(CXX) $(CXXFLAGS) -c $< -o $@
  282. libllama.so: llama.o ggml.o $(OBJS)
  283. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  284. clean:
  285. rm -vf *.o *.so *.dll main quantize quantize-stats perplexity embedding benchmark-matmult save-load-state server simple vdot train-text-from-scratch embd-input-test build-info.h $(TEST_TARGETS)
  286. #
  287. # Examples
  288. #
  289. main: examples/main/main.cpp build-info.h ggml.o llama.o common.o grammar-parser.o $(OBJS)
  290. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  291. @echo
  292. @echo '==== Run ./main -h for help. ===='
  293. @echo
  294. simple: examples/simple/simple.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  295. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  296. quantize: examples/quantize/quantize.cpp build-info.h ggml.o llama.o $(OBJS)
  297. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  298. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.h ggml.o llama.o $(OBJS)
  299. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  300. perplexity: examples/perplexity/perplexity.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  301. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  302. embedding: examples/embedding/embedding.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  303. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  304. save-load-state: examples/save-load-state/save-load-state.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  305. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  306. server: examples/server/server.cpp examples/server/httplib.h examples/server/json.hpp examples/server/index.html.hpp examples/server/index.js.hpp examples/server/completion.js.hpp build-info.h ggml.o llama.o common.o $(OBJS)
  307. $(CXX) $(CXXFLAGS) -Iexamples/server $(filter-out %.h,$(filter-out %.hpp,$^)) -o $@ $(LDFLAGS) $(LWINSOCK2)
  308. $(LIB_PRE)embdinput$(DSO_EXT): examples/embd-input/embd-input.h examples/embd-input/embd-input-lib.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  309. $(CXX) --shared $(CXXFLAGS) $(filter-out %.h,$(filter-out %.hpp,$^)) -o $@ $(LDFLAGS)
  310. embd-input-test: $(LIB_PRE)embdinput$(DSO_EXT) examples/embd-input/embd-input-test.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  311. $(CXX) $(CXXFLAGS) $(filter-out %$(DSO_EXT),$(filter-out %.h,$(filter-out %.hpp,$^))) -o $@ $(LDFLAGS) -L. -lembdinput
  312. train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp build-info.h ggml.o llama.o $(OBJS)
  313. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  314. build-info.h: $(wildcard .git/index) scripts/build-info.sh
  315. @sh scripts/build-info.sh > $@.tmp
  316. @if ! cmp -s $@.tmp $@; then \
  317. mv $@.tmp $@; \
  318. else \
  319. rm $@.tmp; \
  320. fi
  321. #
  322. # Tests
  323. #
  324. tests: $(TEST_TARGETS)
  325. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.h ggml.o $(OBJS)
  326. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  327. ./$@
  328. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  329. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  330. tests/test-double-float: tests/test-double-float.c build-info.h ggml.o llama.o common.o $(OBJS)
  331. $(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS)
  332. tests/test-grad0: tests/test-grad0.c build-info.h ggml.o llama.o common.o $(OBJS)
  333. $(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS)
  334. tests/test-opt: tests/test-opt.c build-info.h ggml.o llama.o common.o $(OBJS)
  335. $(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS)
  336. tests/test-quantize-fns: tests/test-quantize-fns.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  337. $(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS)
  338. tests/test-quantize-perf: tests/test-quantize-perf.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  339. $(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS)
  340. tests/test-sampling: tests/test-sampling.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  341. $(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS)
  342. tests/test-tokenizer-0: tests/test-tokenizer-0.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  343. $(CXX) $(CXXFLAGS) $(filter-out %.txt,$^) -o $@ $(LDFLAGS)