Makefile 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # Define the default target now so that it is always the first target
  2. default: main quantize quantize-stats perplexity embedding vdot
  3. ifndef UNAME_S
  4. UNAME_S := $(shell uname -s)
  5. endif
  6. ifndef UNAME_P
  7. UNAME_P := $(shell uname -p)
  8. endif
  9. ifndef UNAME_M
  10. UNAME_M := $(shell uname -m)
  11. endif
  12. CCV := $(shell $(CC) --version | head -n 1)
  13. CXXV := $(shell $(CXX) --version | head -n 1)
  14. # Mac OS + Arm can report x86_64
  15. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  16. ifeq ($(UNAME_S),Darwin)
  17. ifneq ($(UNAME_P),arm)
  18. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  19. ifeq ($(SYSCTL_M),1)
  20. # UNAME_P := arm
  21. # UNAME_M := arm64
  22. 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)
  23. endif
  24. endif
  25. endif
  26. #
  27. # Compile flags
  28. #
  29. # keep standard at C11 and C++11
  30. CFLAGS = -I. -O3 -DNDEBUG -std=c11 -fPIC
  31. CXXFLAGS = -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC
  32. LDFLAGS =
  33. # warnings
  34. CFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith
  35. CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wno-multichar
  36. # OS specific
  37. # TODO: support Windows
  38. ifeq ($(UNAME_S),Linux)
  39. CFLAGS += -pthread
  40. CXXFLAGS += -pthread
  41. endif
  42. ifeq ($(UNAME_S),Darwin)
  43. CFLAGS += -pthread
  44. CXXFLAGS += -pthread
  45. endif
  46. ifeq ($(UNAME_S),FreeBSD)
  47. CFLAGS += -pthread
  48. CXXFLAGS += -pthread
  49. endif
  50. ifeq ($(UNAME_S),NetBSD)
  51. CFLAGS += -pthread
  52. CXXFLAGS += -pthread
  53. endif
  54. ifeq ($(UNAME_S),OpenBSD)
  55. CFLAGS += -pthread
  56. CXXFLAGS += -pthread
  57. endif
  58. ifeq ($(UNAME_S),Haiku)
  59. CFLAGS += -pthread
  60. CXXFLAGS += -pthread
  61. endif
  62. # Architecture specific
  63. # TODO: probably these flags need to be tweaked on some architectures
  64. # feel free to update the Makefile for your architecture and send a pull request or issue
  65. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686))
  66. # Use all CPU extensions that are available:
  67. CFLAGS += -march=native -mtune=native
  68. CXXFLAGS += -march=native -mtune=native
  69. endif
  70. ifneq ($(filter ppc64%,$(UNAME_M)),)
  71. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  72. ifneq (,$(findstring POWER9,$(POWER9_M)))
  73. CFLAGS += -mcpu=power9
  74. CXXFLAGS += -mcpu=power9
  75. endif
  76. # Require c++23's std::byteswap for big-endian support.
  77. ifeq ($(UNAME_M),ppc64)
  78. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  79. endif
  80. endif
  81. ifndef LLAMA_NO_ACCELERATE
  82. # Mac M1 - include Accelerate framework.
  83. # `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
  84. ifeq ($(UNAME_S),Darwin)
  85. CFLAGS += -DGGML_USE_ACCELERATE
  86. LDFLAGS += -framework Accelerate
  87. endif
  88. endif
  89. ifdef LLAMA_OPENBLAS
  90. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas
  91. LDFLAGS += -lopenblas
  92. endif
  93. ifdef LLAMA_CUBLAS
  94. CFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include
  95. LDFLAGS += -lcublas -lculibos -lcudart -lcublasLt -lpthread -ldl -lrt -L/usr/local/cuda/lib64
  96. OBJS += ggml-cuda.o
  97. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  98. nvcc -arch=native -c -o $@ $<
  99. endif
  100. ifdef LLAMA_GPROF
  101. CFLAGS += -pg
  102. CXXFLAGS += -pg
  103. endif
  104. ifneq ($(filter aarch64%,$(UNAME_M)),)
  105. CFLAGS += -mcpu=native
  106. CXXFLAGS += -mcpu=native
  107. endif
  108. ifneq ($(filter armv6%,$(UNAME_M)),)
  109. # Raspberry Pi 1, 2, 3
  110. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  111. endif
  112. ifneq ($(filter armv7%,$(UNAME_M)),)
  113. # Raspberry Pi 4
  114. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  115. endif
  116. ifneq ($(filter armv8%,$(UNAME_M)),)
  117. # Raspberry Pi 4
  118. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  119. endif
  120. #
  121. # Print build information
  122. #
  123. $(info I llama.cpp build info: )
  124. $(info I UNAME_S: $(UNAME_S))
  125. $(info I UNAME_P: $(UNAME_P))
  126. $(info I UNAME_M: $(UNAME_M))
  127. $(info I CFLAGS: $(CFLAGS))
  128. $(info I CXXFLAGS: $(CXXFLAGS))
  129. $(info I LDFLAGS: $(LDFLAGS))
  130. $(info I CC: $(CCV))
  131. $(info I CXX: $(CXXV))
  132. $(info )
  133. #
  134. # Build library
  135. #
  136. ggml.o: ggml.c ggml.h
  137. $(CC) $(CFLAGS) -c $< -o $@
  138. llama.o: llama.cpp ggml.h llama.h llama_util.h
  139. $(CXX) $(CXXFLAGS) -c $< -o $@
  140. common.o: examples/common.cpp examples/common.h
  141. $(CXX) $(CXXFLAGS) -c $< -o $@
  142. clean:
  143. rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-q4_0-matmult
  144. main: examples/main/main.cpp ggml.o llama.o common.o $(OBJS)
  145. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  146. @echo
  147. @echo '==== Run ./main -h for help. ===='
  148. @echo
  149. quantize: examples/quantize/quantize.cpp ggml.o llama.o $(OBJS)
  150. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  151. quantize-stats: examples/quantize-stats/quantize-stats.cpp ggml.o llama.o $(OBJS)
  152. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  153. perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o common.o $(OBJS)
  154. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  155. embedding: examples/embedding/embedding.cpp ggml.o llama.o common.o $(OBJS)
  156. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  157. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  158. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  159. libllama.so: llama.o ggml.o $(OBJS)
  160. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  161. #
  162. # Tests
  163. #
  164. benchmark: examples/benchmark/benchmark-q4_0-matmult.c ggml.o $(OBJS)
  165. $(CXX) $(CXXFLAGS) $^ -o benchmark-q4_0-matmult $(LDFLAGS)
  166. ./benchmark-q4_0-matmult
  167. .PHONY: tests
  168. tests:
  169. bash ./tests/run-tests.sh