1
0

Makefile 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. # Architecture specific
  67. # TODO: probably these flags need to be tweaked on some architectures
  68. # feel free to update the Makefile for your architecture and send a pull request or issue
  69. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686))
  70. # Use all CPU extensions that are available:
  71. CFLAGS += -march=native -mtune=native
  72. CXXFLAGS += -march=native -mtune=native
  73. # Usage AVX-only
  74. #CFLAGS += -mfma -mf16c -mavx
  75. #CXXFLAGS += -mfma -mf16c -mavx
  76. endif
  77. ifneq ($(filter ppc64%,$(UNAME_M)),)
  78. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  79. ifneq (,$(findstring POWER9,$(POWER9_M)))
  80. CFLAGS += -mcpu=power9
  81. CXXFLAGS += -mcpu=power9
  82. endif
  83. # Require c++23's std::byteswap for big-endian support.
  84. ifeq ($(UNAME_M),ppc64)
  85. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  86. endif
  87. endif
  88. ifndef LLAMA_NO_ACCELERATE
  89. # Mac M1 - include Accelerate framework.
  90. # `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
  91. ifeq ($(UNAME_S),Darwin)
  92. CFLAGS += -DGGML_USE_ACCELERATE
  93. LDFLAGS += -framework Accelerate
  94. endif
  95. endif
  96. ifdef LLAMA_OPENBLAS
  97. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas
  98. LDFLAGS += -lopenblas
  99. endif
  100. ifdef LLAMA_CUBLAS
  101. CFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  102. CXXFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  103. 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
  104. OBJS += ggml-cuda.o
  105. NVCC = nvcc
  106. NVCCFLAGS = --forward-unknown-to-host-compiler -arch=native
  107. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  108. $(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -Wno-pedantic -c $< -o $@
  109. endif
  110. ifdef LLAMA_CLBLAST
  111. CFLAGS += -DGGML_USE_CLBLAST
  112. LDFLAGS += -lclblast -lOpenCL
  113. OBJS += ggml-opencl.o
  114. ggml-opencl.o: ggml-opencl.c ggml-opencl.h
  115. $(CC) $(CFLAGS) -c $< -o $@
  116. endif
  117. ifdef LLAMA_GPROF
  118. CFLAGS += -pg
  119. CXXFLAGS += -pg
  120. endif
  121. ifdef LLAMA_PERF
  122. CFLAGS += -DGGML_PERF
  123. CXXFLAGS += -DGGML_PERF
  124. endif
  125. ifneq ($(filter aarch64%,$(UNAME_M)),)
  126. # Apple M1, M2, etc.
  127. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  128. CFLAGS += -mcpu=native
  129. CXXFLAGS += -mcpu=native
  130. endif
  131. ifneq ($(filter armv6%,$(UNAME_M)),)
  132. # Raspberry Pi 1, Zero
  133. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  134. endif
  135. ifneq ($(filter armv7%,$(UNAME_M)),)
  136. # Raspberry Pi 2
  137. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  138. endif
  139. ifneq ($(filter armv8%,$(UNAME_M)),)
  140. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  141. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  142. endif
  143. #
  144. # Print build information
  145. #
  146. $(info I llama.cpp build info: )
  147. $(info I UNAME_S: $(UNAME_S))
  148. $(info I UNAME_P: $(UNAME_P))
  149. $(info I UNAME_M: $(UNAME_M))
  150. $(info I CFLAGS: $(CFLAGS))
  151. $(info I CXXFLAGS: $(CXXFLAGS))
  152. $(info I LDFLAGS: $(LDFLAGS))
  153. $(info I CC: $(CCV))
  154. $(info I CXX: $(CXXV))
  155. $(info )
  156. #
  157. # Build library
  158. #
  159. ggml.o: ggml.c ggml.h ggml-cuda.h
  160. $(CC) $(CFLAGS) -c $< -o $@
  161. llama.o: llama.cpp ggml.h ggml-cuda.h llama.h llama-util.h
  162. $(CXX) $(CXXFLAGS) -c $< -o $@
  163. common.o: examples/common.cpp examples/common.h
  164. $(CXX) $(CXXFLAGS) -c $< -o $@
  165. libllama.so: llama.o ggml.o $(OBJS)
  166. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  167. clean:
  168. rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-matmult save-load-state build-info.h
  169. #
  170. # Examples
  171. #
  172. main: examples/main/main.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  173. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  174. @echo
  175. @echo '==== Run ./main -h for help. ===='
  176. @echo
  177. quantize: examples/quantize/quantize.cpp build-info.h ggml.o llama.o $(OBJS)
  178. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  179. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.h ggml.o llama.o $(OBJS)
  180. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  181. perplexity: examples/perplexity/perplexity.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  182. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  183. embedding: examples/embedding/embedding.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  184. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  185. save-load-state: examples/save-load-state/save-load-state.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  186. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  187. build-info.h: $(wildcard .git/index) scripts/build-info.sh
  188. @sh scripts/build-info.sh > $@.tmp
  189. @if ! cmp -s $@.tmp $@; then \
  190. mv $@.tmp $@; \
  191. else \
  192. rm $@.tmp; \
  193. fi
  194. #
  195. # Tests
  196. #
  197. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.h ggml.o $(OBJS)
  198. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  199. ./$@
  200. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  201. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  202. .PHONY: tests
  203. tests:
  204. bash ./tests/run-tests.sh