Makefile 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. # Usage AVX-only
  70. #CFLAGS += -mfma -mf16c -mavx
  71. #CXXFLAGS += -mfma -mf16c -mavx
  72. endif
  73. ifneq ($(filter ppc64%,$(UNAME_M)),)
  74. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  75. ifneq (,$(findstring POWER9,$(POWER9_M)))
  76. CFLAGS += -mcpu=power9
  77. CXXFLAGS += -mcpu=power9
  78. endif
  79. # Require c++23's std::byteswap for big-endian support.
  80. ifeq ($(UNAME_M),ppc64)
  81. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  82. endif
  83. endif
  84. ifndef LLAMA_NO_ACCELERATE
  85. # Mac M1 - include Accelerate framework.
  86. # `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
  87. ifeq ($(UNAME_S),Darwin)
  88. CFLAGS += -DGGML_USE_ACCELERATE
  89. LDFLAGS += -framework Accelerate
  90. endif
  91. endif
  92. ifdef LLAMA_OPENBLAS
  93. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas
  94. LDFLAGS += -lopenblas
  95. endif
  96. ifdef LLAMA_CUBLAS
  97. CFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  98. LDFLAGS += -lcublas -lculibos -lcudart -lcublasLt -lpthread -ldl -lrt -L/usr/local/cuda/lib64 -L/opt/cuda/lib64 -L$(CUDA_PATH)/targets/x86_64-linux/lib
  99. OBJS += ggml-cuda.o
  100. NVCC = nvcc
  101. NVCCFLAGS = --forward-unknown-to-host-compiler -arch=native
  102. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  103. $(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -Wno-pedantic -c $< -o $@
  104. endif
  105. ifdef LLAMA_CLBLAST
  106. CFLAGS += -DGGML_USE_CLBLAST
  107. LDFLAGS += -lclblast -lOpenCL
  108. OBJS += ggml-opencl.o
  109. ggml-opencl.o: ggml-opencl.c ggml-opencl.h
  110. $(CC) $(CFLAGS) -c $< -o $@
  111. endif
  112. ifdef LLAMA_GPROF
  113. CFLAGS += -pg
  114. CXXFLAGS += -pg
  115. endif
  116. ifdef LLAMA_PERF
  117. CFLAGS += -DGGML_PERF
  118. CXXFLAGS += -DGGML_PERF
  119. endif
  120. ifneq ($(filter aarch64%,$(UNAME_M)),)
  121. CFLAGS += -mcpu=native
  122. CXXFLAGS += -mcpu=native
  123. endif
  124. ifneq ($(filter armv6%,$(UNAME_M)),)
  125. # Raspberry Pi 1, 2, 3
  126. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  127. endif
  128. ifneq ($(filter armv7%,$(UNAME_M)),)
  129. # Raspberry Pi 4
  130. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  131. endif
  132. ifneq ($(filter armv8%,$(UNAME_M)),)
  133. # Raspberry Pi 4
  134. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  135. endif
  136. #
  137. # Print build information
  138. #
  139. $(info I llama.cpp build info: )
  140. $(info I UNAME_S: $(UNAME_S))
  141. $(info I UNAME_P: $(UNAME_P))
  142. $(info I UNAME_M: $(UNAME_M))
  143. $(info I CFLAGS: $(CFLAGS))
  144. $(info I CXXFLAGS: $(CXXFLAGS))
  145. $(info I LDFLAGS: $(LDFLAGS))
  146. $(info I CC: $(CCV))
  147. $(info I CXX: $(CXXV))
  148. $(info )
  149. #
  150. # Build library
  151. #
  152. ggml.o: ggml.c ggml.h
  153. $(CC) $(CFLAGS) -c $< -o $@
  154. llama.o: llama.cpp ggml.h llama.h llama_util.h
  155. $(CXX) $(CXXFLAGS) -c $< -o $@
  156. common.o: examples/common.cpp examples/common.h
  157. $(CXX) $(CXXFLAGS) -c $< -o $@
  158. clean:
  159. rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-q4_0-matmult
  160. main: examples/main/main.cpp ggml.o llama.o common.o $(OBJS)
  161. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  162. @echo
  163. @echo '==== Run ./main -h for help. ===='
  164. @echo
  165. quantize: examples/quantize/quantize.cpp ggml.o llama.o $(OBJS)
  166. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  167. quantize-stats: examples/quantize-stats/quantize-stats.cpp ggml.o llama.o $(OBJS)
  168. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  169. perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o common.o $(OBJS)
  170. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  171. embedding: examples/embedding/embedding.cpp ggml.o llama.o common.o $(OBJS)
  172. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  173. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  174. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  175. libllama.so: llama.o ggml.o $(OBJS)
  176. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  177. #
  178. # Tests
  179. #
  180. benchmark: examples/benchmark/benchmark-q4_0-matmult.c ggml.o $(OBJS)
  181. $(CXX) $(CXXFLAGS) $^ -o benchmark-q4_0-matmult $(LDFLAGS)
  182. ./benchmark-q4_0-matmult
  183. .PHONY: tests
  184. tests:
  185. bash ./tests/run-tests.sh