Makefile 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. ifneq ($(shell grep -e "Arch Linux" -e "ID_LIKE=arch" /etc/os-release 2>/dev/null),)
  99. LDFLAGS += -lopenblas -lcblas
  100. else
  101. LDFLAGS += -lopenblas
  102. endif
  103. endif
  104. ifdef LLAMA_CUBLAS
  105. CFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  106. CXXFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  107. 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
  108. OBJS += ggml-cuda.o
  109. NVCC = nvcc
  110. NVCCFLAGS = --forward-unknown-to-host-compiler -arch=native
  111. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  112. $(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -Wno-pedantic -c $< -o $@
  113. endif
  114. ifdef LLAMA_CLBLAST
  115. CFLAGS += -DGGML_USE_CLBLAST
  116. # Mac provides OpenCL as a framework
  117. ifeq ($(UNAME_S),Darwin)
  118. LDFLAGS += -lclblast -framework OpenCL
  119. else
  120. LDFLAGS += -lclblast -lOpenCL
  121. endif
  122. OBJS += ggml-opencl.o
  123. ggml-opencl.o: ggml-opencl.c ggml-opencl.h
  124. $(CC) $(CFLAGS) -c $< -o $@
  125. endif
  126. ifdef LLAMA_GPROF
  127. CFLAGS += -pg
  128. CXXFLAGS += -pg
  129. endif
  130. ifdef LLAMA_PERF
  131. CFLAGS += -DGGML_PERF
  132. CXXFLAGS += -DGGML_PERF
  133. endif
  134. ifneq ($(filter aarch64%,$(UNAME_M)),)
  135. # Apple M1, M2, etc.
  136. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  137. CFLAGS += -mcpu=native
  138. CXXFLAGS += -mcpu=native
  139. endif
  140. ifneq ($(filter armv6%,$(UNAME_M)),)
  141. # Raspberry Pi 1, Zero
  142. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  143. endif
  144. ifneq ($(filter armv7%,$(UNAME_M)),)
  145. # Raspberry Pi 2
  146. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  147. endif
  148. ifneq ($(filter armv8%,$(UNAME_M)),)
  149. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  150. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  151. endif
  152. #
  153. # Print build information
  154. #
  155. $(info I llama.cpp build info: )
  156. $(info I UNAME_S: $(UNAME_S))
  157. $(info I UNAME_P: $(UNAME_P))
  158. $(info I UNAME_M: $(UNAME_M))
  159. $(info I CFLAGS: $(CFLAGS))
  160. $(info I CXXFLAGS: $(CXXFLAGS))
  161. $(info I LDFLAGS: $(LDFLAGS))
  162. $(info I CC: $(CCV))
  163. $(info I CXX: $(CXXV))
  164. $(info )
  165. #
  166. # Build library
  167. #
  168. ggml.o: ggml.c ggml.h ggml-cuda.h
  169. $(CC) $(CFLAGS) -c $< -o $@
  170. llama.o: llama.cpp ggml.h ggml-cuda.h llama.h llama-util.h
  171. $(CXX) $(CXXFLAGS) -c $< -o $@
  172. common.o: examples/common.cpp examples/common.h
  173. $(CXX) $(CXXFLAGS) -c $< -o $@
  174. libllama.so: llama.o ggml.o $(OBJS)
  175. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  176. clean:
  177. rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-matmult save-load-state build-info.h
  178. #
  179. # Examples
  180. #
  181. main: examples/main/main.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  182. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  183. @echo
  184. @echo '==== Run ./main -h for help. ===='
  185. @echo
  186. quantize: examples/quantize/quantize.cpp build-info.h ggml.o llama.o $(OBJS)
  187. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  188. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.h ggml.o llama.o $(OBJS)
  189. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  190. perplexity: examples/perplexity/perplexity.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  191. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  192. embedding: examples/embedding/embedding.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  193. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  194. save-load-state: examples/save-load-state/save-load-state.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  195. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  196. build-info.h: $(wildcard .git/index) scripts/build-info.sh
  197. @sh scripts/build-info.sh > $@.tmp
  198. @if ! cmp -s $@.tmp $@; then \
  199. mv $@.tmp $@; \
  200. else \
  201. rm $@.tmp; \
  202. fi
  203. #
  204. # Tests
  205. #
  206. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.h ggml.o $(OBJS)
  207. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  208. ./$@
  209. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  210. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  211. .PHONY: tests
  212. tests:
  213. bash ./tests/run-tests.sh