Makefile 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  124. $(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -Wno-pedantic -c $< -o $@
  125. endif
  126. ifdef LLAMA_CLBLAST
  127. CFLAGS += -DGGML_USE_CLBLAST
  128. # Mac provides OpenCL as a framework
  129. ifeq ($(UNAME_S),Darwin)
  130. LDFLAGS += -lclblast -framework OpenCL
  131. else
  132. LDFLAGS += -lclblast -lOpenCL
  133. endif
  134. OBJS += ggml-opencl.o
  135. ggml-opencl.o: ggml-opencl.c ggml-opencl.h
  136. $(CC) $(CFLAGS) -c $< -o $@
  137. endif
  138. ifneq ($(filter aarch64%,$(UNAME_M)),)
  139. # Apple M1, M2, etc.
  140. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  141. CFLAGS += -mcpu=native
  142. CXXFLAGS += -mcpu=native
  143. endif
  144. ifneq ($(filter armv6%,$(UNAME_M)),)
  145. # Raspberry Pi 1, Zero
  146. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  147. endif
  148. ifneq ($(filter armv7%,$(UNAME_M)),)
  149. # Raspberry Pi 2
  150. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  151. endif
  152. ifneq ($(filter armv8%,$(UNAME_M)),)
  153. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  154. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  155. endif
  156. #
  157. # Print build information
  158. #
  159. $(info I llama.cpp build info: )
  160. $(info I UNAME_S: $(UNAME_S))
  161. $(info I UNAME_P: $(UNAME_P))
  162. $(info I UNAME_M: $(UNAME_M))
  163. $(info I CFLAGS: $(CFLAGS))
  164. $(info I CXXFLAGS: $(CXXFLAGS))
  165. $(info I LDFLAGS: $(LDFLAGS))
  166. $(info I CC: $(CCV))
  167. $(info I CXX: $(CXXV))
  168. $(info )
  169. #
  170. # Build library
  171. #
  172. ggml.o: ggml.c ggml.h ggml-cuda.h
  173. $(CC) $(CFLAGS) -c $< -o $@
  174. llama.o: llama.cpp ggml.h ggml-cuda.h llama.h llama-util.h
  175. $(CXX) $(CXXFLAGS) -c $< -o $@
  176. common.o: examples/common.cpp examples/common.h
  177. $(CXX) $(CXXFLAGS) -c $< -o $@
  178. libllama.so: llama.o ggml.o $(OBJS)
  179. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  180. clean:
  181. rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-matmult save-load-state build-info.h
  182. #
  183. # Examples
  184. #
  185. main: examples/main/main.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  186. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  187. @echo
  188. @echo '==== Run ./main -h for help. ===='
  189. @echo
  190. quantize: examples/quantize/quantize.cpp build-info.h ggml.o llama.o $(OBJS)
  191. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  192. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.h ggml.o llama.o $(OBJS)
  193. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  194. perplexity: examples/perplexity/perplexity.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  195. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  196. embedding: examples/embedding/embedding.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  197. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  198. save-load-state: examples/save-load-state/save-load-state.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  199. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  200. build-info.h: $(wildcard .git/index) scripts/build-info.sh
  201. @sh scripts/build-info.sh > $@.tmp
  202. @if ! cmp -s $@.tmp $@; then \
  203. mv $@.tmp $@; \
  204. else \
  205. rm $@.tmp; \
  206. fi
  207. #
  208. # Tests
  209. #
  210. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.h ggml.o $(OBJS)
  211. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  212. ./$@
  213. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  214. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  215. .PHONY: tests
  216. tests:
  217. bash ./tests/run-tests.sh