Makefile 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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
  3. ifdef LLAMA_BUILD_SERVER
  4. BUILD_TARGETS += server
  5. LLAMA_SERVER_VERBOSE ?= 1
  6. server: private CXXFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
  7. endif
  8. default: $(BUILD_TARGETS)
  9. ifndef UNAME_S
  10. UNAME_S := $(shell uname -s)
  11. endif
  12. ifndef UNAME_P
  13. UNAME_P := $(shell uname -p)
  14. endif
  15. ifndef UNAME_M
  16. UNAME_M := $(shell uname -m)
  17. endif
  18. CCV := $(shell $(CC) --version | head -n 1)
  19. CXXV := $(shell $(CXX) --version | head -n 1)
  20. # Mac OS + Arm can report x86_64
  21. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  22. ifeq ($(UNAME_S),Darwin)
  23. ifneq ($(UNAME_P),arm)
  24. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  25. ifeq ($(SYSCTL_M),1)
  26. # UNAME_P := arm
  27. # UNAME_M := arm64
  28. 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)
  29. endif
  30. endif
  31. endif
  32. #
  33. # Compile flags
  34. #
  35. # keep standard at C11 and C++11
  36. # -Ofast tends to produce faster code, but may not be available for some compilers.
  37. ifdef LLAMA_FAST
  38. OPT = -Ofast
  39. else
  40. OPT = -O3
  41. endif
  42. CFLAGS = -I. $(OPT) -std=c11 -fPIC
  43. CXXFLAGS = -I. -I./examples $(OPT) -std=c++11 -fPIC
  44. LDFLAGS =
  45. ifdef LLAMA_DEBUG
  46. CFLAGS += -O0 -g
  47. CXXFLAGS += -O0 -g
  48. LDFLAGS += -g
  49. else
  50. CFLAGS += -DNDEBUG
  51. CXXFLAGS += -DNDEBUG
  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. ifdef LLAMA_GPROF
  83. CFLAGS += -pg
  84. CXXFLAGS += -pg
  85. endif
  86. ifdef LLAMA_PERF
  87. CFLAGS += -DGGML_PERF
  88. CXXFLAGS += -DGGML_PERF
  89. endif
  90. # Architecture specific
  91. # TODO: probably these flags need to be tweaked on some architectures
  92. # feel free to update the Makefile for your architecture and send a pull request or issue
  93. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686))
  94. # Use all CPU extensions that are available:
  95. CFLAGS += -march=native -mtune=native
  96. CXXFLAGS += -march=native -mtune=native
  97. # Usage AVX-only
  98. #CFLAGS += -mfma -mf16c -mavx
  99. #CXXFLAGS += -mfma -mf16c -mavx
  100. # Usage SSSE3-only (Not is SSE3!)
  101. #CFLAGS += -mssse3
  102. #CXXFLAGS += -mssse3
  103. endif
  104. ifneq ($(filter ppc64%,$(UNAME_M)),)
  105. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  106. ifneq (,$(findstring POWER9,$(POWER9_M)))
  107. CFLAGS += -mcpu=power9
  108. CXXFLAGS += -mcpu=power9
  109. endif
  110. # Require c++23's std::byteswap for big-endian support.
  111. ifeq ($(UNAME_M),ppc64)
  112. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  113. endif
  114. endif
  115. ifndef LLAMA_NO_K_QUANTS
  116. CFLAGS += -DGGML_USE_K_QUANTS
  117. CXXFLAGS += -DGGML_USE_K_QUANTS
  118. OBJS += k_quants.o
  119. ifdef LLAMA_QKK_64
  120. CFLAGS += -DGGML_QKK_64
  121. CXXFLAGS += -DGGML_QKK_64
  122. endif
  123. endif
  124. ifndef LLAMA_NO_ACCELERATE
  125. # Mac M1 - include Accelerate framework.
  126. # `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
  127. ifeq ($(UNAME_S),Darwin)
  128. CFLAGS += -DGGML_USE_ACCELERATE
  129. LDFLAGS += -framework Accelerate
  130. endif
  131. endif # LLAMA_NO_ACCELERATE
  132. ifdef LLAMA_OPENBLAS
  133. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas -I/usr/include/openblas
  134. LDFLAGS += -lopenblas
  135. endif # LLAMA_OPENBLAS
  136. ifdef LLAMA_BLIS
  137. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
  138. LDFLAGS += -lblis -L/usr/local/lib
  139. endif # LLAMA_BLIS
  140. ifdef LLAMA_CUBLAS
  141. CFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  142. CXXFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  143. 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
  144. OBJS += ggml-cuda.o
  145. NVCC = nvcc
  146. NVCCFLAGS = --forward-unknown-to-host-compiler -arch=native
  147. ifdef LLAMA_CUDA_DMMV_X
  148. NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  149. else
  150. NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  151. endif # LLAMA_CUDA_DMMV_X
  152. ifdef LLAMA_CUDA_DMMV_Y
  153. NVCCFLAGS += -DGGML_CUDA_DMMV_Y=$(LLAMA_CUDA_DMMV_Y)
  154. else
  155. NVCCFLAGS += -DGGML_CUDA_DMMV_Y=1
  156. endif # LLAMA_CUDA_DMMV_Y
  157. ifdef LLAMA_CUDA_DMMV_F16
  158. NVCCFLAGS += -DGGML_CUDA_DMMV_F16
  159. endif # LLAMA_CUDA_DMMV_F16
  160. ifdef LLAMA_CUDA_KQUANTS_ITER
  161. NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  162. else
  163. NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  164. endif
  165. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  166. $(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -Wno-pedantic -c $< -o $@
  167. endif # LLAMA_CUBLAS
  168. ifdef LLAMA_CLBLAST
  169. CFLAGS += -DGGML_USE_CLBLAST
  170. CXXFLAGS += -DGGML_USE_CLBLAST
  171. # Mac provides OpenCL as a framework
  172. ifeq ($(UNAME_S),Darwin)
  173. LDFLAGS += -lclblast -framework OpenCL
  174. else
  175. LDFLAGS += -lclblast -lOpenCL
  176. endif
  177. OBJS += ggml-opencl.o
  178. ggml-opencl.o: ggml-opencl.cpp ggml-opencl.h
  179. $(CXX) $(CXXFLAGS) -c $< -o $@
  180. endif # LLAMA_CLBLAST
  181. ifdef LLAMA_METAL
  182. CFLAGS += -DGGML_USE_METAL -DGGML_METAL_NDEBUG
  183. CXXFLAGS += -DGGML_USE_METAL
  184. LDFLAGS += -framework Foundation -framework Metal -framework MetalKit -framework MetalPerformanceShaders
  185. OBJS += ggml-metal.o
  186. ggml-metal.o: ggml-metal.m ggml-metal.h
  187. $(CC) $(CFLAGS) -c $< -o $@
  188. endif # LLAMA_METAL
  189. ifneq ($(filter aarch64%,$(UNAME_M)),)
  190. # Apple M1, M2, etc.
  191. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  192. CFLAGS += -mcpu=native
  193. CXXFLAGS += -mcpu=native
  194. endif
  195. ifneq ($(filter armv6%,$(UNAME_M)),)
  196. # Raspberry Pi 1, Zero
  197. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  198. endif
  199. ifneq ($(filter armv7%,$(UNAME_M)),)
  200. # Raspberry Pi 2
  201. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  202. endif
  203. ifneq ($(filter armv8%,$(UNAME_M)),)
  204. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  205. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  206. endif
  207. ifdef LLAMA_NO_K_QUANTS
  208. k_quants.o: k_quants.c k_quants.h
  209. $(CC) $(CFLAGS) -c $< -o $@
  210. endif # LLAMA_NO_K_QUANTS
  211. #
  212. # Print build information
  213. #
  214. $(info I llama.cpp build info: )
  215. $(info I UNAME_S: $(UNAME_S))
  216. $(info I UNAME_P: $(UNAME_P))
  217. $(info I UNAME_M: $(UNAME_M))
  218. $(info I CFLAGS: $(CFLAGS))
  219. $(info I CXXFLAGS: $(CXXFLAGS))
  220. $(info I LDFLAGS: $(LDFLAGS))
  221. $(info I CC: $(CCV))
  222. $(info I CXX: $(CXXV))
  223. $(info )
  224. #
  225. # Build library
  226. #
  227. ggml.o: ggml.c ggml.h ggml-cuda.h
  228. $(CC) $(CFLAGS) -c $< -o $@
  229. llama.o: llama.cpp ggml.h ggml-cuda.h ggml-metal.h llama.h llama-util.h
  230. $(CXX) $(CXXFLAGS) -c $< -o $@
  231. common.o: examples/common.cpp examples/common.h
  232. $(CXX) $(CXXFLAGS) -c $< -o $@
  233. libllama.so: llama.o ggml.o $(OBJS)
  234. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  235. clean:
  236. rm -vf *.o *.so main quantize quantize-stats perplexity embedding benchmark-matmult save-load-state server vdot train-text-from-scratch build-info.h
  237. #
  238. # Examples
  239. #
  240. main: examples/main/main.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  241. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  242. @echo
  243. @echo '==== Run ./main -h for help. ===='
  244. @echo
  245. simple: examples/simple/simple.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  246. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  247. quantize: examples/quantize/quantize.cpp build-info.h ggml.o llama.o $(OBJS)
  248. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  249. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.h ggml.o llama.o $(OBJS)
  250. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  251. perplexity: examples/perplexity/perplexity.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  252. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  253. embedding: examples/embedding/embedding.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  254. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  255. save-load-state: examples/save-load-state/save-load-state.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  256. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  257. server: examples/server/server.cpp examples/server/httplib.h examples/server/json.hpp build-info.h ggml.o llama.o common.o $(OBJS)
  258. $(CXX) $(CXXFLAGS) -Iexamples/server $(filter-out %.h,$(filter-out %.hpp,$^)) -o $@ $(LDFLAGS)
  259. train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp build-info.h ggml.o llama.o $(OBJS)
  260. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  261. build-info.h: $(wildcard .git/index) scripts/build-info.sh
  262. @sh scripts/build-info.sh > $@.tmp
  263. @if ! cmp -s $@.tmp $@; then \
  264. mv $@.tmp $@; \
  265. else \
  266. rm $@.tmp; \
  267. fi
  268. #
  269. # Tests
  270. #
  271. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.h ggml.o $(OBJS)
  272. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  273. ./$@
  274. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  275. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  276. .PHONY: tests clean
  277. tests:
  278. bash ./tests/run-tests.sh