Makefile 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. # Define the default target now so that it is always the first target
  2. BUILD_TARGETS = main quantize quantize-stats perplexity embedding vdot train-text-from-scratch simple server libembdinput.so embd-input-test
  3. default: $(BUILD_TARGETS)
  4. ifndef UNAME_S
  5. UNAME_S := $(shell uname -s)
  6. endif
  7. ifndef UNAME_P
  8. UNAME_P := $(shell uname -p)
  9. endif
  10. ifndef UNAME_M
  11. UNAME_M := $(shell uname -m)
  12. endif
  13. CCV := $(shell $(CC) --version | head -n 1)
  14. CXXV := $(shell $(CXX) --version | head -n 1)
  15. # Mac OS + Arm can report x86_64
  16. # ref: https://github.com/ggerganov/whisper.cpp/issues/66#issuecomment-1282546789
  17. ifeq ($(UNAME_S),Darwin)
  18. ifneq ($(UNAME_P),arm)
  19. SYSCTL_M := $(shell sysctl -n hw.optional.arm64 2>/dev/null)
  20. ifeq ($(SYSCTL_M),1)
  21. # UNAME_P := arm
  22. # UNAME_M := arm64
  23. 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)
  24. endif
  25. endif
  26. endif
  27. #
  28. # Compile flags
  29. #
  30. # keep standard at C11 and C++11
  31. # -Ofast tends to produce faster code, but may not be available for some compilers.
  32. ifdef LLAMA_FAST
  33. OPT = -Ofast
  34. else
  35. OPT = -O3
  36. endif
  37. CFLAGS = -I. $(OPT) -std=c11 -fPIC
  38. CXXFLAGS = -I. -I./examples $(OPT) -std=c++11 -fPIC
  39. LDFLAGS =
  40. ifdef LLAMA_DEBUG
  41. CFLAGS += -O0 -g
  42. CXXFLAGS += -O0 -g
  43. LDFLAGS += -g
  44. else
  45. CFLAGS += -DNDEBUG
  46. CXXFLAGS += -DNDEBUG
  47. endif
  48. ifdef LLAMA_SERVER_VERBOSE
  49. CXXFLAGS += -DSERVER_VERBOSE=$(LLAMA_SERVER_VERBOSE)
  50. endif
  51. # warnings
  52. CFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wdouble-promotion -Wshadow -Wstrict-prototypes -Wpointer-arith
  53. CXXFLAGS += -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wno-multichar
  54. # OS specific
  55. # TODO: support Windows
  56. ifeq ($(UNAME_S),Linux)
  57. CFLAGS += -pthread
  58. CXXFLAGS += -pthread
  59. endif
  60. ifeq ($(UNAME_S),Darwin)
  61. CFLAGS += -pthread
  62. CXXFLAGS += -pthread
  63. endif
  64. ifeq ($(UNAME_S),FreeBSD)
  65. CFLAGS += -pthread
  66. CXXFLAGS += -pthread
  67. endif
  68. ifeq ($(UNAME_S),NetBSD)
  69. CFLAGS += -pthread
  70. CXXFLAGS += -pthread
  71. endif
  72. ifeq ($(UNAME_S),OpenBSD)
  73. CFLAGS += -pthread
  74. CXXFLAGS += -pthread
  75. endif
  76. ifeq ($(UNAME_S),Haiku)
  77. CFLAGS += -pthread
  78. CXXFLAGS += -pthread
  79. endif
  80. ifdef LLAMA_GPROF
  81. CFLAGS += -pg
  82. CXXFLAGS += -pg
  83. endif
  84. ifdef LLAMA_PERF
  85. CFLAGS += -DGGML_PERF
  86. CXXFLAGS += -DGGML_PERF
  87. endif
  88. # Architecture specific
  89. # TODO: probably these flags need to be tweaked on some architectures
  90. # feel free to update the Makefile for your architecture and send a pull request or issue
  91. ifeq ($(UNAME_M),$(filter $(UNAME_M),x86_64 i686))
  92. # Use all CPU extensions that are available:
  93. CFLAGS += -march=native -mtune=native
  94. CXXFLAGS += -march=native -mtune=native
  95. # Usage AVX-only
  96. #CFLAGS += -mfma -mf16c -mavx
  97. #CXXFLAGS += -mfma -mf16c -mavx
  98. # Usage SSSE3-only (Not is SSE3!)
  99. #CFLAGS += -mssse3
  100. #CXXFLAGS += -mssse3
  101. endif
  102. ifneq ($(filter ppc64%,$(UNAME_M)),)
  103. POWER9_M := $(shell grep "POWER9" /proc/cpuinfo)
  104. ifneq (,$(findstring POWER9,$(POWER9_M)))
  105. CFLAGS += -mcpu=power9
  106. CXXFLAGS += -mcpu=power9
  107. endif
  108. # Require c++23's std::byteswap for big-endian support.
  109. ifeq ($(UNAME_M),ppc64)
  110. CXXFLAGS += -std=c++23 -DGGML_BIG_ENDIAN
  111. endif
  112. endif
  113. ifndef LLAMA_NO_K_QUANTS
  114. CFLAGS += -DGGML_USE_K_QUANTS
  115. CXXFLAGS += -DGGML_USE_K_QUANTS
  116. OBJS += k_quants.o
  117. ifdef LLAMA_QKK_64
  118. CFLAGS += -DGGML_QKK_64
  119. CXXFLAGS += -DGGML_QKK_64
  120. endif
  121. endif
  122. ifndef LLAMA_NO_ACCELERATE
  123. # Mac M1 - include Accelerate framework.
  124. # `-framework Accelerate` works on Mac Intel as well, with negliable performance boost (as of the predict time).
  125. ifeq ($(UNAME_S),Darwin)
  126. CFLAGS += -DGGML_USE_ACCELERATE
  127. LDFLAGS += -framework Accelerate
  128. endif
  129. endif # LLAMA_NO_ACCELERATE
  130. ifdef LLAMA_OPENBLAS
  131. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/openblas -I/usr/include/openblas
  132. LDFLAGS += -lopenblas
  133. endif # LLAMA_OPENBLAS
  134. ifdef LLAMA_BLIS
  135. CFLAGS += -DGGML_USE_OPENBLAS -I/usr/local/include/blis -I/usr/include/blis
  136. LDFLAGS += -lblis -L/usr/local/lib
  137. endif # LLAMA_BLIS
  138. ifdef LLAMA_CUBLAS
  139. CFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  140. CXXFLAGS += -DGGML_USE_CUBLAS -I/usr/local/cuda/include -I/opt/cuda/include -I$(CUDA_PATH)/targets/x86_64-linux/include
  141. 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
  142. OBJS += ggml-cuda.o
  143. NVCC = nvcc
  144. NVCCFLAGS = --forward-unknown-to-host-compiler
  145. ifdef CUDA_DOCKER_ARCH
  146. NVCCFLAGS += -Wno-deprecated-gpu-targets -arch=$(CUDA_DOCKER_ARCH)
  147. else
  148. NVCCFLAGS += -arch=native
  149. endif # CUDA_DOCKER_ARCH
  150. ifdef LLAMA_CUDA_FORCE_DMMV
  151. NVCCFLAGS += -DGGML_CUDA_FORCE_DMMV
  152. endif # LLAMA_CUDA_FORCE_DMMV
  153. ifdef LLAMA_CUDA_DMMV_X
  154. NVCCFLAGS += -DGGML_CUDA_DMMV_X=$(LLAMA_CUDA_DMMV_X)
  155. else
  156. NVCCFLAGS += -DGGML_CUDA_DMMV_X=32
  157. endif # LLAMA_CUDA_DMMV_X
  158. ifdef LLAMA_CUDA_MMV_Y
  159. NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_MMV_Y)
  160. else ifdef LLAMA_CUDA_DMMV_Y
  161. NVCCFLAGS += -DGGML_CUDA_MMV_Y=$(LLAMA_CUDA_DMMV_Y) # for backwards compatibility
  162. else
  163. NVCCFLAGS += -DGGML_CUDA_MMV_Y=1
  164. endif # LLAMA_CUDA_MMV_Y
  165. ifdef LLAMA_CUDA_DMMV_F16
  166. NVCCFLAGS += -DGGML_CUDA_DMMV_F16
  167. endif # LLAMA_CUDA_DMMV_F16
  168. ifdef LLAMA_CUDA_KQUANTS_ITER
  169. NVCCFLAGS += -DK_QUANTS_PER_ITERATION=$(LLAMA_CUDA_KQUANTS_ITER)
  170. else
  171. NVCCFLAGS += -DK_QUANTS_PER_ITERATION=2
  172. endif
  173. ggml-cuda.o: ggml-cuda.cu ggml-cuda.h
  174. $(NVCC) $(NVCCFLAGS) $(CXXFLAGS) -Wno-pedantic -c $< -o $@
  175. endif # LLAMA_CUBLAS
  176. ifdef LLAMA_CLBLAST
  177. CFLAGS += -DGGML_USE_CLBLAST
  178. CXXFLAGS += -DGGML_USE_CLBLAST
  179. # Mac provides OpenCL as a framework
  180. ifeq ($(UNAME_S),Darwin)
  181. LDFLAGS += -lclblast -framework OpenCL
  182. else
  183. LDFLAGS += -lclblast -lOpenCL
  184. endif
  185. OBJS += ggml-opencl.o
  186. ggml-opencl.o: ggml-opencl.cpp ggml-opencl.h
  187. $(CXX) $(CXXFLAGS) -c $< -o $@
  188. endif # LLAMA_CLBLAST
  189. ifdef LLAMA_METAL
  190. CFLAGS += -DGGML_USE_METAL -DGGML_METAL_NDEBUG
  191. CXXFLAGS += -DGGML_USE_METAL
  192. LDFLAGS += -framework Foundation -framework Metal -framework MetalKit -framework MetalPerformanceShaders
  193. OBJS += ggml-metal.o
  194. ggml-metal.o: ggml-metal.m ggml-metal.h
  195. $(CC) $(CFLAGS) -c $< -o $@
  196. endif # LLAMA_METAL
  197. ifneq ($(filter aarch64%,$(UNAME_M)),)
  198. # Apple M1, M2, etc.
  199. # Raspberry Pi 3, 4, Zero 2 (64-bit)
  200. CFLAGS += -mcpu=native
  201. CXXFLAGS += -mcpu=native
  202. endif
  203. ifneq ($(filter armv6%,$(UNAME_M)),)
  204. # Raspberry Pi 1, Zero
  205. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access
  206. endif
  207. ifneq ($(filter armv7%,$(UNAME_M)),)
  208. # Raspberry Pi 2
  209. CFLAGS += -mfpu=neon-fp-armv8 -mfp16-format=ieee -mno-unaligned-access -funsafe-math-optimizations
  210. endif
  211. ifneq ($(filter armv8%,$(UNAME_M)),)
  212. # Raspberry Pi 3, 4, Zero 2 (32-bit)
  213. CFLAGS += -mfp16-format=ieee -mno-unaligned-access
  214. endif
  215. ifdef LLAMA_NO_K_QUANTS
  216. k_quants.o: k_quants.c k_quants.h
  217. $(CC) $(CFLAGS) -c $< -o $@
  218. endif # LLAMA_NO_K_QUANTS
  219. #
  220. # Print build information
  221. #
  222. $(info I llama.cpp build info: )
  223. $(info I UNAME_S: $(UNAME_S))
  224. $(info I UNAME_P: $(UNAME_P))
  225. $(info I UNAME_M: $(UNAME_M))
  226. $(info I CFLAGS: $(CFLAGS))
  227. $(info I CXXFLAGS: $(CXXFLAGS))
  228. $(info I LDFLAGS: $(LDFLAGS))
  229. $(info I CC: $(CCV))
  230. $(info I CXX: $(CXXV))
  231. $(info )
  232. #
  233. # Build library
  234. #
  235. ggml.o: ggml.c ggml.h ggml-cuda.h
  236. $(CC) $(CFLAGS) -c $< -o $@
  237. llama.o: llama.cpp ggml.h ggml-cuda.h ggml-metal.h llama.h llama-util.h
  238. $(CXX) $(CXXFLAGS) -c $< -o $@
  239. common.o: examples/common.cpp examples/common.h
  240. $(CXX) $(CXXFLAGS) -c $< -o $@
  241. libllama.so: llama.o ggml.o $(OBJS)
  242. $(CXX) $(CXXFLAGS) -shared -fPIC -o $@ $^ $(LDFLAGS)
  243. clean:
  244. rm -vf *.o *.so main quantize quantize-stats perplexity embedding benchmark-matmult save-load-state server simple vdot train-text-from-scratch embd-input-test build-info.h
  245. #
  246. # Examples
  247. #
  248. main: examples/main/main.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  249. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  250. @echo
  251. @echo '==== Run ./main -h for help. ===='
  252. @echo
  253. simple: examples/simple/simple.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  254. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  255. quantize: examples/quantize/quantize.cpp build-info.h ggml.o llama.o $(OBJS)
  256. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  257. quantize-stats: examples/quantize-stats/quantize-stats.cpp build-info.h ggml.o llama.o $(OBJS)
  258. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  259. perplexity: examples/perplexity/perplexity.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  260. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  261. embedding: examples/embedding/embedding.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  262. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  263. save-load-state: examples/save-load-state/save-load-state.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  264. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  265. server: examples/server/server.cpp examples/server/httplib.h examples/server/json.hpp build-info.h ggml.o llama.o common.o $(OBJS)
  266. $(CXX) $(CXXFLAGS) -Iexamples/server $(filter-out %.h,$(filter-out %.hpp,$^)) -o $@ $(LDFLAGS)
  267. libembdinput.so: examples/embd-input/embd-input.h examples/embd-input/embd-input-lib.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  268. $(CXX) --shared $(CXXFLAGS) $(filter-out %.h,$(filter-out %.hpp,$^)) -o $@ $(LDFLAGS)
  269. embd-input-test: libembdinput.so examples/embd-input/embd-input-test.cpp build-info.h ggml.o llama.o common.o $(OBJS)
  270. $(CXX) $(CXXFLAGS) $(filter-out %.so,$(filter-out %.h,$(filter-out %.hpp,$^))) -o $@ $(LDFLAGS) -L. -lembdinput
  271. train-text-from-scratch: examples/train-text-from-scratch/train-text-from-scratch.cpp build-info.h ggml.o llama.o $(OBJS)
  272. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  273. build-info.h: $(wildcard .git/index) scripts/build-info.sh
  274. @sh scripts/build-info.sh > $@.tmp
  275. @if ! cmp -s $@.tmp $@; then \
  276. mv $@.tmp $@; \
  277. else \
  278. rm $@.tmp; \
  279. fi
  280. #
  281. # Tests
  282. #
  283. benchmark-matmult: examples/benchmark/benchmark-matmult.cpp build-info.h ggml.o $(OBJS)
  284. $(CXX) $(CXXFLAGS) $(filter-out %.h,$^) -o $@ $(LDFLAGS)
  285. ./$@
  286. vdot: pocs/vdot/vdot.cpp ggml.o $(OBJS)
  287. $(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)
  288. .PHONY: tests clean
  289. tests:
  290. bash ./tests/run-tests.sh