Makefile 6.2 KB

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