Makefile 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. CXXFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  99. 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
  100. OBJS += ggml-cuda.o
  101. NVCC = nvcc
  102. NVCCFLAGS = --forward-unknown-to-host-compiler -arch=native
  103. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  104. $(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -Wno-pedantic -c $< -o $@
  105. endif
  106. ifdef LLAMA_CLBLAST
  107. CFLAGS += -DGGML_USE_CLBLAST
  108. LDFLAGS += -lclblast -lOpenCL
  109. OBJS += ggml-opencl.o
  110. ggml-opencl.o: ggml-opencl.c ggml-opencl.h
  111. $(CC) $(CFLAGS) -c $< -o $@
  112. endif
  113. ifdef LLAMA_GPROF
  114. CFLAGS += -pg
  115. CXXFLAGS += -pg
  116. endif
  117. ifdef LLAMA_PERF
  118. CFLAGS += -DGGML_PERF
  119. CXXFLAGS += -DGGML_PERF
  120. endif
  121. ifneq ($(filter aarch64%,$(UNAME_M)),)
  122. CFLAGS += -mcpu=native
  123. CXXFLAGS += -mcpu=native
  124. endif
  125. ifneq ($(filter armv6%,$(UNAME_M)),)
  126. # Raspberry Pi 1, 2, 3
  127. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  128. endif
  129. ifneq ($(filter armv7%,$(UNAME_M)),)
  130. # Raspberry Pi 4
  131. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  132. endif
  133. ifneq ($(filter armv8%,$(UNAME_M)),)
  134. # Raspberry Pi 4
  135. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  136. endif
  137. #
  138. # Print build information
  139. #
  140. $(info I llama.cpp build info: )
  141. $(info I UNAME_S: $(UNAME_S))
  142. $(info I UNAME_P: $(UNAME_P))
  143. $(info I UNAME_M: $(UNAME_M))
  144. $(info I CFLAGS: $(CFLAGS))
  145. $(info I CXXFLAGS: $(CXXFLAGS))
  146. $(info I LDFLAGS: $(LDFLAGS))
  147. $(info I CC: $(CCV))
  148. $(info I CXX: $(CXXV))
  149. $(info )
  150. #
  151. # Build library
  152. #
  153. ggml.o: ggml.c ggml.h ggml-cuda.h
  154. $(CC) $(CFLAGS) -c $< -o $@
  155. llama.o: llama.cpp ggml.h ggml-cuda.h llama.h llama_util.h
  156. $(CXX) $(CXXFLAGS) -c $< -o $@
  157. common.o: examples/common.cpp examples/common.h
  158. $(CXX) $(CXXFLAGS) -c $< -o $@
  159. clean:
  160. rm -vf *.o main quantize quantize-stats perplexity embedding benchmark-q4_0-matmult
  161. main: examples/main/main.cpp ggml.o llama.o common.o $(OBJS)
  162. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  163. @echo
  164. @echo '==== Run ./main -h for help. ===='
  165. @echo
  166. quantize: examples/quantize/quantize.cpp ggml.o llama.o $(OBJS)
  167. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  168. quantize-stats: examples/quantize-stats/quantize-stats.cpp ggml.o llama.o $(OBJS)
  169. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  170. perplexity: examples/perplexity/perplexity.cpp ggml.o llama.o common.o $(OBJS)
  171. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  172. embedding: examples/embedding/embedding.cpp ggml.o llama.o common.o $(OBJS)
  173. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  174. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  175. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  176. libllama.so: llama.o ggml.o $(OBJS)
  177. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  178. #
  179. # Tests
  180. #
  181. benchmark: examples/benchmark/benchmark-q4_0-matmult.c ggml.o $(OBJS)
  182. $(CXX) $(CXXFLAGS) $^ -o benchmark-q4_0-matmult $(LDFLAGS)
  183. ./benchmark-q4_0-matmult
  184. .PHONY: tests
  185. tests:
  186. bash ./tests/run-tests.sh