Makefile 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # Define the default target now so that it is always the first target
  2. BUILD_TARGETS = main quantize quantize-stats perplexity embedding vdot
  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. CFLAGS = -I. -O3 -std=c11 -fPIC
  35. CXXFLAGS = -I. -I./examples -O3 -std=c++11 -fPIC
  36. LDFLAGS =
  37. ifndef LLAMA_DEBUG
  38. CFLAGS += -DNDEBUG
  39. CXXFLAGS += -DNDEBUG
  40. endif
  41. # warnings
  42. CFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith
  43. CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wno-multichar
  44. # OS specific
  45. # TODO: support Windows
  46. ifeq ($(UNAME_S),Linux)
  47. CFLAGS += -pthread
  48. CXXFLAGS += -pthread
  49. endif
  50. ifeq ($(UNAME_S),Darwin)
  51. CFLAGS += -pthread
  52. CXXFLAGS += -pthread
  53. endif
  54. ifeq ($(UNAME_S),FreeBSD)
  55. CFLAGS += -pthread
  56. CXXFLAGS += -pthread
  57. endif
  58. ifeq ($(UNAME_S),NetBSD)
  59. CFLAGS += -pthread
  60. CXXFLAGS += -pthread
  61. endif
  62. ifeq ($(UNAME_S),OpenBSD)
  63. CFLAGS += -pthread
  64. CXXFLAGS += -pthread
  65. endif
  66. ifeq ($(UNAME_S),Haiku)
  67. CFLAGS += -pthread
  68. CXXFLAGS += -pthread
  69. endif
  70. ifdef LLAMA_GPROF
  71. CFLAGS += -pg
  72. CXXFLAGS += -pg
  73. endif
  74. ifdef LLAMA_PERF
  75. CFLAGS += -DGGML_PERF
  76. CXXFLAGS += -DGGML_PERF
  77. endif
  78. # Architecture specific
  79. # TODO: probably these flags need to be tweaked on some architectures
  80. # feel free to update the Makefile for your architecture and send a pull request or issue
  81. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686))
  82. # Use all CPU extensions that are available:
  83. CFLAGS += -march=native -mtune=native
  84. CXXFLAGS += -march=native -mtune=native
  85. # Usage AVX-only
  86. #CFLAGS += -mfma -mf16c -mavx
  87. #CXXFLAGS += -mfma -mf16c -mavx
  88. endif
  89. ifneq ($(filter ppc64%,$(UNAME_M)),)
  90. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  91. ifneq (,$(findstring POWER9,$(POWER9_M)))
  92. CFLAGS += -mcpu=power9
  93. CXXFLAGS += -mcpu=power9
  94. endif
  95. # Require c++23's std::byteswap for big-endian support.
  96. ifeq ($(UNAME_M),ppc64)
  97. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  98. endif
  99. endif
  100. ifndef LLAMA_NO_ACCELERATE
  101. # Mac M1 - include Accelerate framework.
  102. # `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
  103. ifeq ($(UNAME_S),Darwin)
  104. CFLAGS += -DGGML_USE_ACCELERATE
  105. LDFLAGS += -framework Accelerate
  106. endif
  107. endif
  108. ifdef LLAMA_OPENBLAS
  109. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas -I/usr/include/openblas
  110. ifneq ($(shell grep -e "Arch Linux" -e "ID_LIKE=arch" /etc/os-release 2>/dev/null),)
  111. LDFLAGS += -lopenblas -lcblas
  112. else
  113. LDFLAGS += -lopenblas
  114. endif
  115. endif
  116. ifdef LLAMA_BLIS
  117. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
  118. LDFLAGS += -lblis -L/usr/local/lib
  119. endif
  120. ifdef LLAMA_CUBLAS
  121. CFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  122. CXXFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  123. 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
  124. OBJS += ggml-cuda.o
  125. NVCC = nvcc
  126. NVCCFLAGS = --forward-unknown-to-host-compiler -arch=native
  127. ifdef LLAMA_CUDA_DMMV_X
  128. NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  129. else
  130. NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  131. endif # LLAMA_CUDA_DMMV_X
  132. ifdef LLAMA_CUDA_DMMV_Y
  133. NVCCFLAGS += -DGGML_CUDA_DMMV_Y=$(LLAMA_CUDA_DMMV_Y)
  134. else
  135. NVCCFLAGS += -DGGML_CUDA_DMMV_Y=1
  136. endif # LLAMA_CUDA_DMMV_Y
  137. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  138. $(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -Wno-pedantic -c $< -o $@
  139. endif # LLAMA_CUBLAS
  140. ifdef LLAMA_CLBLAST
  141. CFLAGS += -DGGML_USE_CLBLAST
  142. CXXFLAGS += -DGGML_USE_CLBLAST
  143. # Mac provides OpenCL as a framework
  144. ifeq ($(UNAME_S),Darwin)
  145. LDFLAGS += -lclblast -framework OpenCL
  146. else
  147. LDFLAGS += -lclblast -lOpenCL
  148. endif
  149. OBJS += ggml-opencl.o
  150. ggml-opencl.o: ggml-opencl.cpp ggml-opencl.h
  151. $(CXX) $(CXXFLAGS) -c $< -o $@
  152. endif
  153. ifneq ($(filter aarch64%,$(UNAME_M)),)
  154. # Apple M1, M2, etc.
  155. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  156. CFLAGS += -mcpu=native
  157. CXXFLAGS += -mcpu=native
  158. endif
  159. ifneq ($(filter armv6%,$(UNAME_M)),)
  160. # Raspberry Pi 1, Zero
  161. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  162. endif
  163. ifneq ($(filter armv7%,$(UNAME_M)),)
  164. # Raspberry Pi 2
  165. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  166. endif
  167. ifneq ($(filter armv8%,$(UNAME_M)),)
  168. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  169. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  170. endif
  171. #
  172. # Print build information
  173. #
  174. $(info I llama.cpp build info: )
  175. $(info I UNAME_S: $(UNAME_S))
  176. $(info I UNAME_P: $(UNAME_P))
  177. $(info I UNAME_M: $(UNAME_M))
  178. $(info I CFLAGS: $(CFLAGS))
  179. $(info I CXXFLAGS: $(CXXFLAGS))
  180. $(info I LDFLAGS: $(LDFLAGS))
  181. $(info I CC: $(CCV))
  182. $(info I CXX: $(CXXV))
  183. $(info )
  184. #
  185. # Build library
  186. #
  187. ggml.o: ggml.c ggml.h ggml-cuda.h
  188. $(CC) $(CFLAGS) -c $< -o $@
  189. llama.o: llama.cpp ggml.h ggml-cuda.h llama.h llama-util.h
  190. $(CXX) $(CXXFLAGS) -c $< -o $@
  191. common.o: examples/common.cpp examples/common.h
  192. $(CXX) $(CXXFLAGS) -c $< -o $@
  193. libllama.so: llama.o ggml.o $(OBJS)
  194. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  195. clean:
  196. rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-matmult save-load-state server vdot build-info.h
  197. #
  198. # Examples
  199. #
  200. main: examples/main/main.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  201. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  202. @echo
  203. @echo '==== Run ./main -h for help. ===='
  204. @echo
  205. quantize: examples/quantize/quantize.cpp build-info.h ggml.o llama.o $(OBJS)
  206. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  207. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.h ggml.o llama.o $(OBJS)
  208. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  209. perplexity: examples/perplexity/perplexity.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  210. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  211. embedding: examples/embedding/embedding.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  212. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  213. save-load-state: examples/save-load-state/save-load-state.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  214. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  215. server: examples/server/server.cpp examples/server/httplib.h examples/server/json.hpp build-info.h ggml.o llama.o common.o $(OBJS)
  216. $(CXX) $(CXXFLAGS) -Iexamples/server $(filter-out %.h,$(filter-out %.hpp,$^)) -o $@ $(LDFLAGS)
  217. build-info.h: $(wildcard .git/index) scripts/build-info.sh
  218. @sh scripts/build-info.sh > $@.tmp
  219. @if ! cmp -s $@.tmp $@; then \
  220. mv $@.tmp $@; \
  221. else \
  222. rm $@.tmp; \
  223. fi
  224. #
  225. # Tests
  226. #
  227. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.h ggml.o $(OBJS)
  228. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  229. ./$@
  230. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  231. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  232. .PHONY: tests clean
  233. tests:
  234. bash ./tests/run-tests.sh