Makefile 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. else ifeq ($(UNAME_S),Haiku)
  91. AVX1_M := $(shell sysinfo -cpu | grep "AVX ")
  92. ifneq (,$(findstring avx,$(AVX1_M)))
  93. CFLAGS += -mavx
  94. endif
  95. AVX2_M := $(shell sysinfo -cpu | grep "AVX2 ")
  96. ifneq (,$(findstring avx2,$(AVX2_M)))
  97. CFLAGS += -mavx2
  98. endif
  99. FMA_M := $(shell sysinfo -cpu | grep "FMA ")
  100. ifneq (,$(findstring fma,$(FMA_M)))
  101. CFLAGS += -mfma
  102. endif
  103. F16C_M := $(shell sysinfo -cpu | grep "F16C ")
  104. ifneq (,$(findstring f16c,$(F16C_M)))
  105. CFLAGS += -mf16c
  106. endif
  107. else
  108. CFLAGS += -mfma -mf16c -mavx -mavx2
  109. endif
  110. endif
  111. ifeq ($(UNAME_M),amd64)
  112. CFLAGS += -mavx -mavx2 -mfma -mf16c
  113. endif
  114. ifneq ($(filter ppc64%,$(UNAME_M)),)
  115. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  116. ifneq (,$(findstring POWER9,$(POWER9_M)))
  117. CFLAGS += -mpower9-vector
  118. endif
  119. # Require c++23's std::byteswap for big-endian support.
  120. ifeq ($(UNAME_M),ppc64)
  121. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  122. endif
  123. endif
  124. ifndef LLAMA_NO_ACCELERATE
  125. # Mac M1 - include Accelerate framework
  126. ifeq ($(UNAME_S),Darwin)
  127. CFLAGS += -DGGML_USE_ACCELERATE
  128. LDFLAGS += -framework Accelerate
  129. endif
  130. endif
  131. ifdef LLAMA_OPENBLAS
  132. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas
  133. LDFLAGS += -lopenblas
  134. endif
  135. ifdef LLAMA_GPROF
  136. CFLAGS += -pg
  137. CXXFLAGS += -pg
  138. endif
  139. ifneq ($(filter aarch64%,$(UNAME_M)),)
  140. CFLAGS += -mcpu=native
  141. CXXFLAGS += -mcpu=native
  142. endif
  143. ifneq ($(filter armv6%,$(UNAME_M)),)
  144. # Raspberry Pi 1, 2, 3
  145. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  146. endif
  147. ifneq ($(filter armv7%,$(UNAME_M)),)
  148. # Raspberry Pi 4
  149. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  150. endif
  151. ifneq ($(filter armv8%,$(UNAME_M)),)
  152. # Raspberry Pi 4
  153. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  154. endif
  155. #
  156. # Print build information
  157. #
  158. $(info I llama.cpp build info: )
  159. $(info I UNAME_S: $(UNAME_S))
  160. $(info I UNAME_P: $(UNAME_P))
  161. $(info I UNAME_M: $(UNAME_M))
  162. $(info I CFLAGS: $(CFLAGS))
  163. $(info I CXXFLAGS: $(CXXFLAGS))
  164. $(info I LDFLAGS: $(LDFLAGS))
  165. $(info I CC: $(CCV))
  166. $(info I CXX: $(CXXV))
  167. $(info )
  168. default: main quantize
  169. #
  170. # Build library
  171. #
  172. ggml.o: ggml.c ggml.h
  173. $(CC) $(CFLAGS) -c ggml.c -o ggml.o
  174. utils.o: utils.cpp utils.h
  175. $(CXX) $(CXXFLAGS) -c utils.cpp -o utils.o
  176. clean:
  177. rm -f *.o main quantize
  178. main: main.cpp ggml.o utils.o
  179. $(CXX) $(CXXFLAGS) main.cpp ggml.o utils.o -o main $(LDFLAGS)
  180. ./main -h
  181. quantize: quantize.cpp ggml.o utils.o
  182. $(CXX) $(CXXFLAGS) quantize.cpp ggml.o utils.o -o quantize $(LDFLAGS)
  183. #
  184. # Tests
  185. #
  186. .PHONY: tests
  187. tests:
  188. bash ./tests/run-tests.sh