Makefile 9.3 KB

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