Makefile 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. # warnings
  32. CFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith -Wno-unused-function
  33. CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function
  34. # OS specific
  35. # TODO: support Windows
  36. ifeq ($(UNAME_S),Linux)
  37. CFLAGS += -pthread
  38. CXXFLAGS += -pthread
  39. endif
  40. ifeq ($(UNAME_S),Darwin)
  41. CFLAGS += -pthread
  42. CXXFLAGS += -pthread
  43. endif
  44. ifeq ($(UNAME_S),FreeBSD)
  45. CFLAGS += -pthread
  46. CXXFLAGS += -pthread
  47. endif
  48. ifeq ($(UNAME_S),NetBSD)
  49. CFLAGS += -pthread
  50. CXXFLAGS += -pthread
  51. endif
  52. ifeq ($(UNAME_S),OpenBSD)
  53. CFLAGS += -pthread
  54. CXXFLAGS += -pthread
  55. endif
  56. ifeq ($(UNAME_S),Haiku)
  57. CFLAGS += -pthread
  58. CXXFLAGS += -pthread
  59. endif
  60. # Architecture specific
  61. # TODO: probably these flags need to be tweaked on some architectures
  62. # feel free to update the Makefile for your architecture and send a pull request or issue
  63. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686))
  64. # Use all CPU extensions that are available:
  65. CFLAGS += -march=native -mtune=native
  66. CXXFLAGS += -march=native -mtune=native
  67. endif
  68. ifneq ($(filter ppc64%,$(UNAME_M)),)
  69. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  70. ifneq (,$(findstring POWER9,$(POWER9_M)))
  71. CFLAGS += -mcpu=power9
  72. CXXFLAGS += -mcpu=power9
  73. endif
  74. # Require c++23's std::byteswap for big-endian support.
  75. ifeq ($(UNAME_M),ppc64)
  76. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  77. endif
  78. endif
  79. ifndef LLAMA_NO_ACCELERATE
  80. # Mac M1 - include Accelerate framework.
  81. # `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
  82. ifeq ($(UNAME_S),Darwin)
  83. CFLAGS += -DGGML_USE_ACCELERATE
  84. LDFLAGS += -framework Accelerate
  85. endif
  86. endif
  87. ifdef LLAMA_OPENBLAS
  88. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas
  89. LDFLAGS += -lopenblas
  90. endif
  91. ifdef LLAMA_GPROF
  92. CFLAGS += -pg
  93. CXXFLAGS += -pg
  94. endif
  95. ifneq ($(filter aarch64%,$(UNAME_M)),)
  96. CFLAGS += -mcpu=native
  97. CXXFLAGS += -mcpu=native
  98. endif
  99. ifneq ($(filter armv6%,$(UNAME_M)),)
  100. # Raspberry Pi 1, 2, 3
  101. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  102. endif
  103. ifneq ($(filter armv7%,$(UNAME_M)),)
  104. # Raspberry Pi 4
  105. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  106. endif
  107. ifneq ($(filter armv8%,$(UNAME_M)),)
  108. # Raspberry Pi 4
  109. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  110. endif
  111. #
  112. # Print build information
  113. #
  114. $(info I llama.cpp build info: )
  115. $(info I UNAME_S: $(UNAME_S))
  116. $(info I UNAME_P: $(UNAME_P))
  117. $(info I UNAME_M: $(UNAME_M))
  118. $(info I CFLAGS: $(CFLAGS))
  119. $(info I CXXFLAGS: $(CXXFLAGS))
  120. $(info I LDFLAGS: $(LDFLAGS))
  121. $(info I CC: $(CCV))
  122. $(info I CXX: $(CXXV))
  123. $(info )
  124. default: main quantize perplexity embedding
  125. #
  126. # Build library
  127. #
  128. ggml.o: ggml.c ggml.h
  129. $(CC) $(CFLAGS) -c ggml.c -o ggml.o
  130. llama.o: llama.cpp llama.h
  131. $(CXX) $(CXXFLAGS) -c llama.cpp -o llama.o
  132. common.o: examples/common.cpp examples/common.h
  133. $(CXX) $(CXXFLAGS) -c examples/common.cpp -o common.o
  134. clean:
  135. rm -vf *.o main quantize quantize-stats perplexity embedding
  136. main: examples/main/main.cpp ggml.o llama.o common.o
  137. $(CXX) $(CXXFLAGS) examples/main/main.cpp ggml.o llama.o common.o -o main $(LDFLAGS)
  138. @echo
  139. @echo '==== Run ./main -h for help. ===='
  140. @echo
  141. quantize: examples/quantize/quantize.cpp ggml.o llama.o
  142. $(CXX) $(CXXFLAGS) examples/quantize/quantize.cpp ggml.o llama.o -o quantize $(LDFLAGS)
  143. quantize-stats: examples/quantize-stats/quantize-stats.cpp ggml.o llama.o
  144. $(CXX) $(CXXFLAGS) examples/quantize-stats/quantize-stats.cpp ggml.o llama.o -o quantize-stats $(LDFLAGS)
  145. perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o common.o
  146. $(CXX) $(CXXFLAGS) examples/perplexity/perplexity.cpp ggml.o llama.o common.o -o perplexity $(LDFLAGS)
  147. embedding: examples/embedding/embedding.cpp ggml.o llama.o common.o
  148. $(CXX) $(CXXFLAGS) examples/embedding/embedding.cpp ggml.o llama.o common.o -o embedding $(LDFLAGS)
  149. libllama.so: llama.o ggml.o
  150. $(CXX) $(CXXFLAGS) -shared -fPIC -o libllama.so llama.o ggml.o $(LDFLAGS)
  151. #
  152. # Tests
  153. #
  154. .PHONY: tests
  155. tests:
  156. bash ./tests/run-tests.sh