Makefile 7.4 KB

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