Makefile 12 KB

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