Makefile 6.1 KB

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