Makefile 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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++11 -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),Haiku)
  45. CFLAGS += -pthread
  46. CXXFLAGS += -pthread
  47. endif
  48. # Architecture specific
  49. # TODO: probably these flags need to be tweaked on some architectures
  50. # feel free to update the Makefile for your architecture and send a pull request or issue
  51. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686))
  52. ifeq ($(UNAME_S),Darwin)
  53. CFLAGS += -mf16c
  54. AVX1_M := $(shell sysctl machdep.cpu.features)
  55. ifneq (,$(findstring FMA,$(AVX1_M)))
  56. CFLAGS += -mfma
  57. endif
  58. ifneq (,$(findstring AVX1.0,$(AVX1_M)))
  59. CFLAGS += -mavx
  60. endif
  61. AVX2_M := $(shell sysctl machdep.cpu.leaf7_features)
  62. ifneq (,$(findstring AVX2,$(AVX2_M)))
  63. CFLAGS += -mavx2
  64. endif
  65. else ifeq ($(UNAME_S),Linux)
  66. AVX1_M := $(shell grep "avx " /proc/cpuinfo)
  67. ifneq (,$(findstring avx,$(AVX1_M)))
  68. CFLAGS += -mavx
  69. endif
  70. AVX2_M := $(shell grep "avx2 " /proc/cpuinfo)
  71. ifneq (,$(findstring avx2,$(AVX2_M)))
  72. CFLAGS += -mavx2
  73. endif
  74. FMA_M := $(shell grep "fma " /proc/cpuinfo)
  75. ifneq (,$(findstring fma,$(FMA_M)))
  76. CFLAGS += -mfma
  77. endif
  78. F16C_M := $(shell grep "f16c " /proc/cpuinfo)
  79. ifneq (,$(findstring f16c,$(F16C_M)))
  80. CFLAGS += -mf16c
  81. endif
  82. SSE3_M := $(shell grep "sse3 " /proc/cpuinfo)
  83. ifneq (,$(findstring sse3,$(SSE3_M)))
  84. CFLAGS += -msse3
  85. endif
  86. else ifeq ($(UNAME_S),Haiku)
  87. AVX1_M := $(shell sysinfo -cpu | grep "AVX ")
  88. ifneq (,$(findstring avx,$(AVX1_M)))
  89. CFLAGS += -mavx
  90. endif
  91. AVX2_M := $(shell sysinfo -cpu | grep "AVX2 ")
  92. ifneq (,$(findstring avx2,$(AVX2_M)))
  93. CFLAGS += -mavx2
  94. endif
  95. FMA_M := $(shell sysinfo -cpu | grep "FMA ")
  96. ifneq (,$(findstring fma,$(FMA_M)))
  97. CFLAGS += -mfma
  98. endif
  99. F16C_M := $(shell sysinfo -cpu | grep "F16C ")
  100. ifneq (,$(findstring f16c,$(F16C_M)))
  101. CFLAGS += -mf16c
  102. endif
  103. else
  104. CFLAGS += -mfma -mf16c -mavx -mavx2
  105. endif
  106. endif
  107. ifeq ($(UNAME_M),amd64)
  108. CFLAGS += -mavx -mavx2 -mfma -mf16c
  109. endif
  110. ifneq ($(filter ppc64%,$(UNAME_M)),)
  111. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  112. ifneq (,$(findstring POWER9,$(POWER9_M)))
  113. CFLAGS += -mpower9-vector
  114. endif
  115. # Require c++23's std::byteswap for big-endian support.
  116. ifeq ($(UNAME_M),ppc64)
  117. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  118. endif
  119. endif
  120. ifndef LLAMA_NO_ACCELERATE
  121. # Mac M1 - include Accelerate framework
  122. ifeq ($(UNAME_S),Darwin)
  123. CFLAGS += -DGGML_USE_ACCELERATE
  124. LDFLAGS += -framework Accelerate
  125. endif
  126. endif
  127. ifdef LLAMA_OPENBLAS
  128. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas
  129. LDFLAGS += -lopenblas
  130. endif
  131. ifdef LLAMA_GPROF
  132. CFLAGS += -pg
  133. CXXFLAGS += -pg
  134. endif
  135. ifneq ($(filter aarch64%,$(UNAME_M)),)
  136. CFLAGS += -mcpu=native
  137. CXXFLAGS += -mcpu=native
  138. endif
  139. ifneq ($(filter armv6%,$(UNAME_M)),)
  140. # Raspberry Pi 1, 2, 3
  141. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  142. endif
  143. ifneq ($(filter armv7%,$(UNAME_M)),)
  144. # Raspberry Pi 4
  145. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  146. endif
  147. ifneq ($(filter armv8%,$(UNAME_M)),)
  148. # Raspberry Pi 4
  149. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  150. endif
  151. #
  152. # Print build information
  153. #
  154. $(info I llama.cpp build info: )
  155. $(info I UNAME_S: $(UNAME_S))
  156. $(info I UNAME_P: $(UNAME_P))
  157. $(info I UNAME_M: $(UNAME_M))
  158. $(info I CFLAGS: $(CFLAGS))
  159. $(info I CXXFLAGS: $(CXXFLAGS))
  160. $(info I LDFLAGS: $(LDFLAGS))
  161. $(info I CC: $(CCV))
  162. $(info I CXX: $(CXXV))
  163. $(info )
  164. default: main quantize
  165. #
  166. # Build library
  167. #
  168. ggml.o: ggml.c ggml.h
  169. $(CC) $(CFLAGS) -c ggml.c -o ggml.o
  170. utils.o: utils.cpp utils.h
  171. $(CXX) $(CXXFLAGS) -c utils.cpp -o utils.o
  172. clean:
  173. rm -f *.o main quantize
  174. main: main.cpp ggml.o utils.o
  175. $(CXX) $(CXXFLAGS) main.cpp ggml.o utils.o -o main $(LDFLAGS)
  176. ./main -h
  177. quantize: quantize.cpp ggml.o utils.o
  178. $(CXX) $(CXXFLAGS) quantize.cpp ggml.o utils.o -o quantize $(LDFLAGS)
  179. #
  180. # Tests
  181. #
  182. .PHONY: tests
  183. tests:
  184. bash ./tests/run-tests.sh