Makefile 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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)
  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. ifeq ($(UNAME_M),amd64)
  145. CFLAGS += -mavx -mavx2 -mfma -mf16c
  146. endif
  147. ifneq ($(filter ppc64%,$(UNAME_M)),)
  148. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  149. ifneq (,$(findstring POWER9,$(POWER9_M)))
  150. CFLAGS += -mpower9-vector
  151. endif
  152. # Require c++23's std::byteswap for big-endian support.
  153. ifeq ($(UNAME_M),ppc64)
  154. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  155. endif
  156. endif
  157. ifndef LLAMA_NO_ACCELERATE
  158. # Mac M1 - include Accelerate framework
  159. ifeq ($(UNAME_S),Darwin)
  160. CFLAGS += -DGGML_USE_ACCELERATE
  161. LDFLAGS += -framework Accelerate
  162. endif
  163. endif
  164. ifdef LLAMA_OPENBLAS
  165. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas
  166. LDFLAGS += -lopenblas
  167. endif
  168. ifdef LLAMA_GPROF
  169. CFLAGS += -pg
  170. CXXFLAGS += -pg
  171. endif
  172. ifneq ($(filter aarch64%,$(UNAME_M)),)
  173. CFLAGS += -mcpu=native
  174. CXXFLAGS += -mcpu=native
  175. endif
  176. ifneq ($(filter armv6%,$(UNAME_M)),)
  177. # Raspberry Pi 1, 2, 3
  178. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  179. endif
  180. ifneq ($(filter armv7%,$(UNAME_M)),)
  181. # Raspberry Pi 4
  182. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  183. endif
  184. ifneq ($(filter armv8%,$(UNAME_M)),)
  185. # Raspberry Pi 4
  186. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  187. endif
  188. #
  189. # Print build information
  190. #
  191. $(info I llama.cpp build info: )
  192. $(info I UNAME_S: $(UNAME_S))
  193. $(info I UNAME_P: $(UNAME_P))
  194. $(info I UNAME_M: $(UNAME_M))
  195. $(info I CFLAGS: $(CFLAGS))
  196. $(info I CXXFLAGS: $(CXXFLAGS))
  197. $(info I LDFLAGS: $(LDFLAGS))
  198. $(info I CC: $(CCV))
  199. $(info I CXX: $(CXXV))
  200. $(info )
  201. default: main quantize
  202. #
  203. # Build library
  204. #
  205. ggml.o: ggml.c ggml.h
  206. $(CC) $(CFLAGS) -c ggml.c -o ggml.o
  207. utils.o: utils.cpp utils.h
  208. $(CXX) $(CXXFLAGS) -c utils.cpp -o utils.o
  209. clean:
  210. rm -f *.o main quantize
  211. main: main.cpp ggml.o utils.o
  212. $(CXX) $(CXXFLAGS) main.cpp ggml.o utils.o -o main $(LDFLAGS)
  213. ./main -h
  214. quantize: quantize.cpp ggml.o utils.o
  215. $(CXX) $(CXXFLAGS) quantize.cpp ggml.o utils.o -o quantize $(LDFLAGS)
  216. #
  217. # Tests
  218. #
  219. .PHONY: tests
  220. tests:
  221. bash ./tests/run-tests.sh