Makefile 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. ifndef UNAME_S
  2. UNAME_S := $(shell uname -s)
  3. endif
  4. ifndef UNAME_P
  5. UNAME_P := $(shell uname -p)
  6. endif
  7. ifndef UNAME_M
  8. UNAME_M := $(shell uname -m)
  9. endif
  10. CCV := $(shell $(CC) --version | head -n 1)
  11. CXXV := $(shell $(CXX) --version | head -n 1)
  12. # Mac OS + Arm can report x86_64
  13. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  14. ifeq ($(UNAME_S),Darwin)
  15. ifneq ($(UNAME_P),arm)
  16. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  17. ifeq ($(SYSCTL_M),1)
  18. # UNAME_P := arm
  19. # UNAME_M := arm64
  20. 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)
  21. endif
  22. endif
  23. endif
  24. #
  25. # Compile flags
  26. #
  27. # keep standard at C11 and C++11
  28. CFLAGS = -I. -O3 -DNDEBUG -std=c11 -fPIC
  29. CXXFLAGS = -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC
  30. LDFLAGS =
  31. # OS specific
  32. # TODO: support Windows
  33. ifeq ($(UNAME_S),Linux)
  34. CFLAGS += -pthread
  35. CXXFLAGS += -pthread
  36. endif
  37. ifeq ($(UNAME_S),Darwin)
  38. CFLAGS += -pthread
  39. CXXFLAGS += -pthread
  40. endif
  41. ifeq ($(UNAME_S),FreeBSD)
  42. CFLAGS += -pthread
  43. CXXFLAGS += -pthread
  44. endif
  45. ifeq ($(UNAME_S),NetBSD)
  46. CFLAGS += -pthread
  47. CXXFLAGS += -pthread
  48. endif
  49. ifeq ($(UNAME_S),OpenBSD)
  50. CFLAGS += -pthread
  51. CXXFLAGS += -pthread
  52. endif
  53. ifeq ($(UNAME_S),Haiku)
  54. CFLAGS += -pthread
  55. CXXFLAGS += -pthread
  56. endif
  57. # Architecture specific
  58. # TODO: probably these flags need to be tweaked on some architectures
  59. # feel free to update the Makefile for your architecture and send a pull request or issue
  60. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686))
  61. ifeq ($(UNAME_S),Darwin)
  62. CFLAGS += -mf16c
  63. AVX1_M := $(shell sysctl machdep.cpu.features)
  64. ifneq (,$(findstring FMA,$(AVX1_M)))
  65. CFLAGS += -mfma
  66. endif
  67. ifneq (,$(findstring AVX1.0,$(AVX1_M)))
  68. CFLAGS += -mavx
  69. endif
  70. AVX2_M := $(shell sysctl machdep.cpu.leaf7_features)
  71. ifneq (,$(findstring AVX2,$(AVX2_M)))
  72. CFLAGS += -mavx2
  73. endif
  74. else ifeq ($(UNAME_S),Linux)
  75. AVX1_M := $(shell grep "avx " /proc/cpuinfo)
  76. ifneq (,$(findstring avx,$(AVX1_M)))
  77. CFLAGS += -mavx
  78. endif
  79. AVX2_M := $(shell grep "avx2 " /proc/cpuinfo)
  80. ifneq (,$(findstring avx2,$(AVX2_M)))
  81. CFLAGS += -mavx2
  82. endif
  83. FMA_M := $(shell grep "fma " /proc/cpuinfo)
  84. ifneq (,$(findstring fma,$(FMA_M)))
  85. CFLAGS += -mfma
  86. endif
  87. F16C_M := $(shell grep "f16c " /proc/cpuinfo)
  88. ifneq (,$(findstring f16c,$(F16C_M)))
  89. CFLAGS += -mf16c
  90. endif
  91. SSE3_M := $(shell grep "sse3 " /proc/cpuinfo)
  92. ifneq (,$(findstring sse3,$(SSE3_M)))
  93. CFLAGS += -msse3
  94. endif
  95. AVX512F_M := $(shell grep "avx512f " /proc/cpuinfo)
  96. ifneq (,$(findstring avx512f,$(AVX512F_M)))
  97. CFLAGS += -mavx512f
  98. endif
  99. AVX512BW_M := $(shell grep "avx512bw " /proc/cpuinfo)
  100. ifneq (,$(findstring avx512bw,$(AVX512BW_M)))
  101. CFLAGS += -mavx512bw
  102. endif
  103. AVX512DQ_M := $(shell grep "avx512dq " /proc/cpuinfo)
  104. ifneq (,$(findstring avx512dq,$(AVX512DQ_M)))
  105. CFLAGS += -mavx512dq
  106. endif
  107. AVX512VL_M := $(shell grep "avx512vl " /proc/cpuinfo)
  108. ifneq (,$(findstring avx512vl,$(AVX512VL_M)))
  109. CFLAGS += -mavx512vl
  110. endif
  111. AVX512CD_M := $(shell grep "avx512cd " /proc/cpuinfo)
  112. ifneq (,$(findstring avx512cd,$(AVX512CD_M)))
  113. CFLAGS += -mavx512cd
  114. endif
  115. AVX512ER_M := $(shell grep "avx512er " /proc/cpuinfo)
  116. ifneq (,$(findstring avx512er,$(AVX512ER_M)))
  117. CFLAGS += -mavx512er
  118. endif
  119. AVX512IFMA_M := $(shell grep "avx512ifma " /proc/cpuinfo)
  120. ifneq (,$(findstring avx512ifma,$(AVX512IFMA_M)))
  121. CFLAGS += -mavx512ifma
  122. endif
  123. AVX512PF_M := $(shell grep "avx512pf " /proc/cpuinfo)
  124. ifneq (,$(findstring avx512pf,$(AVX512PF_M)))
  125. CFLAGS += -mavx512pf
  126. endif
  127. else ifeq ($(UNAME_S),Haiku)
  128. AVX1_M := $(shell sysinfo -cpu | grep -w "AVX")
  129. ifneq (,$(findstring AVX,$(AVX1_M)))
  130. CFLAGS += -mavx
  131. endif
  132. AVX2_M := $(shell sysinfo -cpu | grep -w "AVX2")
  133. ifneq (,$(findstring AVX2,$(AVX2_M)))
  134. CFLAGS += -mavx2
  135. endif
  136. FMA_M := $(shell sysinfo -cpu | grep -w "FMA")
  137. ifneq (,$(findstring FMA,$(FMA_M)))
  138. CFLAGS += -mfma
  139. endif
  140. F16C_M := $(shell sysinfo -cpu | grep -w "F16C")
  141. ifneq (,$(findstring F16C,$(F16C_M)))
  142. CFLAGS += -mf16c
  143. endif
  144. else
  145. CFLAGS += -mfma -mf16c -mavx -mavx2
  146. endif
  147. endif
  148. ifneq ($(filter ppc64%,$(UNAME_M)),)
  149. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  150. ifneq (,$(findstring POWER9,$(POWER9_M)))
  151. CFLAGS += -mcpu=power9
  152. CXXFLAGS += -mcpu=power9
  153. endif
  154. # Require c++23's std::byteswap for big-endian support.
  155. ifeq ($(UNAME_M),ppc64)
  156. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  157. endif
  158. endif
  159. ifndef LLAMA_NO_ACCELERATE
  160. # Mac M1 - include Accelerate framework.
  161. # `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
  162. ifeq ($(UNAME_S),Darwin)
  163. CFLAGS += -DGGML_USE_ACCELERATE
  164. LDFLAGS += -framework Accelerate
  165. endif
  166. endif
  167. ifdef LLAMA_OPENBLAS
  168. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas
  169. LDFLAGS += -lopenblas
  170. endif
  171. ifdef LLAMA_GPROF
  172. CFLAGS += -pg
  173. CXXFLAGS += -pg
  174. endif
  175. ifneq ($(filter aarch64%,$(UNAME_M)),)
  176. CFLAGS += -mcpu=native
  177. CXXFLAGS += -mcpu=native
  178. endif
  179. ifneq ($(filter armv6%,$(UNAME_M)),)
  180. # Raspberry Pi 1, 2, 3
  181. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  182. endif
  183. ifneq ($(filter armv7%,$(UNAME_M)),)
  184. # Raspberry Pi 4
  185. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  186. endif
  187. ifneq ($(filter armv8%,$(UNAME_M)),)
  188. # Raspberry Pi 4
  189. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  190. endif
  191. #
  192. # Print build information
  193. #
  194. $(info I llama.cpp build info: )
  195. $(info I UNAME_S: $(UNAME_S))
  196. $(info I UNAME_P: $(UNAME_P))
  197. $(info I UNAME_M: $(UNAME_M))
  198. $(info I CFLAGS: $(CFLAGS))
  199. $(info I CXXFLAGS: $(CXXFLAGS))
  200. $(info I LDFLAGS: $(LDFLAGS))
  201. $(info I CC: $(CCV))
  202. $(info I CXX: $(CXXV))
  203. $(info )
  204. default: main quantize perplexity
  205. #
  206. # Build library
  207. #
  208. ggml.o: ggml.c ggml.h
  209. $(CC) $(CFLAGS) -c ggml.c -o ggml.o
  210. llama.o: llama.cpp llama.h
  211. $(CXX) $(CXXFLAGS) -c llama.cpp -o llama.o
  212. common.o: examples/common.cpp examples/common.h
  213. $(CXX) $(CXXFLAGS) -c examples/common.cpp -o common.o
  214. clean:
  215. rm -vf *.o main quantize perplexity
  216. main: examples/main/main.cpp ggml.o llama.o common.o
  217. $(CXX) $(CXXFLAGS) examples/main/main.cpp ggml.o llama.o common.o -o main $(LDFLAGS)
  218. @echo
  219. @echo '==== Run ./main -h for help. ===='
  220. @echo
  221. quantize: examples/quantize/quantize.cpp ggml.o llama.o
  222. $(CXX) $(CXXFLAGS) examples/quantize/quantize.cpp ggml.o llama.o -o quantize $(LDFLAGS)
  223. perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o common.o
  224. $(CXX) $(CXXFLAGS) examples/perplexity/perplexity.cpp ggml.o llama.o common.o -o perplexity $(LDFLAGS)
  225. #
  226. # Tests
  227. #
  228. .PHONY: tests
  229. tests:
  230. bash ./tests/run-tests.sh