1
0

Makefile 7.2 KB

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