Makefile 7.1 KB

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