Makefile.sync 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Helpers for managing our vendored llama.cpp repo and patch set
  2. REPO_ROOT:=$(dir $(patsubst %/,%,$(dir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))))
  3. DST_DIR:=$(dir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
  4. include $(REPO_ROOT)llama/vendoring
  5. LLAMACPP_REPO := $(REPO_ROOT)llama/vendor/
  6. LLAMACPP_PATCH_DIR := $(DST_DIR)patches/
  7. help-sync:
  8. @echo "The following make targets will help you update llama.cpp to a new base commit, or work on new features/fixes"
  9. @echo ""
  10. @echo "\tmake apply-patches # Establish the tracking repo if not already present, reset to the base commit, and apply our patch set"
  11. @echo "\tmake sync # Vendor llama.cpp and ggml from the tracking repo working tree"
  12. @echo "\tmake create-patches # Generate the patch set based on the current commits in the tracking repo since the base commit"
  13. @echo ""
  14. @echo "For more details on the workflow, see the Vendoring section in ../docs/development.md"
  15. apply-patches: $(LLAMACPP_REPO)
  16. @if ! git -C $(LLAMACPP_REPO) --no-pager diff --exit-code ; then \
  17. echo "ERROR: Your llama.cpp repo is dirty. The apply-patches target requires a clean working tree"; \
  18. echo "To clobber: git -C $(LLAMACPP_REPO) reset --hard HEAD" ; \
  19. exit 1; \
  20. fi
  21. @echo "Checking out $(LLAMACPP_BASE_COMMIT)"
  22. @git -C $(LLAMACPP_REPO) checkout -q $(LLAMACPP_BASE_COMMIT) || \
  23. git -C $(LLAMACPP_REPO) fetch --all && git -C $(LLAMACPP_REPO) checkout -q $(LLAMACPP_BASE_COMMIT)
  24. @echo "Applying ollama patches..."
  25. @git -c 'user.name=nobody' -c 'user.email=<>' -C $(LLAMACPP_REPO) am -3 $(LLAMACPP_PATCH_DIR)/*.patch || \
  26. echo "Please resolve the conflicts in $(LLAMACPP_REPO), and run 'git am --continue' to continue applying subsequent patches"
  27. @echo ""
  28. @echo "The tracking repo $(LLAMACPP_REPO) is now in a detached state with all patches applied."
  29. @echo "Don't forget to commit any changes you make and run 'make create-patches' "
  30. $(LLAMACPP_REPO):
  31. @echo "Cloning llama.cpp to $(LLAMACPP_REPO)"
  32. git clone https://github.com/ggerganov/llama.cpp.git $@
  33. create-patches: $(LLAMACPP_REPO)
  34. @if ! git -C $(LLAMACPP_REPO) --no-pager diff --exit-code ; then \
  35. echo "ERROR: Your llama.cpp repo is dirty. You must commit any pending changes for format-patch to generate patches"; \
  36. exit 1; \
  37. fi
  38. git -C $(LLAMACPP_REPO) format-patch --no-signature --no-numbered --zero-commit -o $(LLAMACPP_PATCH_DIR) $(LLAMACPP_BASE_COMMIT)
  39. # Vendoring template logic
  40. EXCLUDED_FILES=sgemm.cpp sgemm.h sampling_ext.cpp sampling_ext.h stb_image.h json.hpp llama_darwin.c base64.hpp
  41. OLLAMA_NATIVE_FILES=mllama.cpp mllama.h llama_darwin.c sampling_ext.cpp sampling_ext.h
  42. define vendor_file
  43. $(strip $(addprefix $(2),$(notdir $1))) : $(addprefix $(LLAMACPP_REPO),$(1))
  44. ifneq ($$(filter-out $(EXCLUDED_FILES),$(notdir $1)),)
  45. @echo "vendoring $1"; \
  46. mkdir -p $$(dir $$@) && \
  47. echo "/**" > $$@ && \
  48. echo " * llama.cpp - commit $$(LLAMACPP_BASE_COMMIT) - do not edit this file" >> $$@ && \
  49. echo " *" >> $$@ && \
  50. sed 's/^/ * /' <$(LLAMACPP_REPO)/LICENSE | sed 's/ *$$$$//' >> $$@ && \
  51. echo " */" >> $$@ && \
  52. echo "" >> $$@ && \
  53. cat $$< >> $$@
  54. else
  55. @echo "vendoring $1"; \
  56. mkdir -p $$(dir $$@) && \
  57. cat $$< > $$@
  58. endif
  59. VENDORED_FILES += $(strip $(addprefix $(2),$(notdir $1)))
  60. endef
  61. # llama.cpp files -> llama/
  62. LLAMACPP_FILES=\
  63. src/unicode.cpp \
  64. src/unicode.h \
  65. src/unicode-data.cpp \
  66. src/unicode-data.h \
  67. src/llama.cpp \
  68. src/llama-impl.h \
  69. src/llama-vocab.cpp \
  70. src/llama-vocab.h \
  71. src/llama-grammar.cpp \
  72. src/llama-grammar.h \
  73. src/llama-sampling.cpp \
  74. src/llama-sampling.h \
  75. include/llama.h \
  76. ggml/src/llamafile/sgemm.cpp \
  77. ggml/src/llamafile/sgemm.h
  78. $(foreach name,$(LLAMACPP_FILES),$(eval $(call vendor_file,$(name),$(DST_DIR))))
  79. # llama.cpp files -> llama/llamafile
  80. LLAMAFILE_FILES= \
  81. ggml/src/llamafile/sgemm.h
  82. $(foreach name,$(LLAMAFILE_FILES),$(eval $(call vendor_file,$(name),$(DST_DIR)llamafile/)))
  83. # ggml files -> llama/
  84. GGML_FILES= \
  85. ggml/src/ggml.c \
  86. ggml/include/ggml.h \
  87. ggml/src/ggml-quants.c \
  88. ggml/src/ggml-quants.h \
  89. ggml/src/ggml-metal.metal \
  90. ggml/include/ggml-metal.h \
  91. ggml/src/ggml-impl.h \
  92. ggml/include/ggml-cuda.h \
  93. ggml/src/ggml-cuda.cu \
  94. ggml/src/ggml-common.h \
  95. ggml/include/ggml-backend.h \
  96. ggml/src/ggml-backend.c \
  97. ggml/src/ggml-backend-impl.h \
  98. ggml/include/ggml-alloc.h \
  99. ggml/src/ggml-alloc.c \
  100. ggml/src/ggml-aarch64.h \
  101. ggml/src/ggml-aarch64.c \
  102. ggml/src/ggml-cpu-impl.h \
  103. ggml/include/ggml-blas.h \
  104. ggml/src/ggml-blas.cpp
  105. $(foreach name,$(GGML_FILES),$(eval $(call vendor_file,$(name),$(DST_DIR))))
  106. # TODO generalize renaming pattern if we have more of these
  107. $(DST_DIR)ggml-metal_darwin_arm64.m : $(LLAMACPP_REPO)ggml/src/ggml-metal.m
  108. @echo "vendoring $(subst $(LLAMACPP_REPO),,$<)"; \
  109. mkdir -p $(dir $@) && \
  110. echo "/**" > $@ && \
  111. echo " * llama.cpp - commit $(LLAMACPP_BASE_COMMIT) - do not edit this file" >> $@ && \
  112. echo " *" >> $@ && \
  113. sed 's/^/ * /' <$(LLAMACPP_REPO)/LICENSE | sed 's/ *$$//' >> $@ && \
  114. echo " */" >> $@ && \
  115. echo "" >> $@ && \
  116. cat $< >> $@
  117. VENDORED_FILES += $(DST_DIR)ggml-metal_darwin_arm64.m
  118. # ggml-cuda -> llama/ggml-cuda/
  119. GGML_CUDA_FILES= ggml/src/ggml-cuda/*.cu ggml/src/ggml-cuda/*.cuh
  120. GGML_CUDA_FILES_EXPANDED = $(addprefix ggml/src/ggml-cuda/,$(notdir $(wildcard $(addprefix $(LLAMACPP_REPO),$(GGML_CUDA_FILES)))))
  121. $(foreach name,$(GGML_CUDA_FILES_EXPANDED),$(eval $(call vendor_file,$(name),$(DST_DIR)ggml-cuda/)))
  122. GGML_TEMPLATE_FILES= ggml/src/ggml-cuda/template-instances/*.cu
  123. GGML_TEMPLATE_FILES_EXPANDED = $(addprefix ggml/src/ggml-cuda/template-instances/,$(notdir $(wildcard $(addprefix $(LLAMACPP_REPO),$(GGML_TEMPLATE_FILES)))))
  124. $(foreach name,$(GGML_TEMPLATE_FILES_EXPANDED),$(eval $(call vendor_file,$(name),$(DST_DIR)ggml-cuda/template-instances/)))
  125. GGML_VENDOR_FILES= ggml/src/ggml-cuda/vendors/*.h
  126. GGML_VENDOR_FILES_EXPANDED=$(addprefix ggml/src/ggml-cuda/vendors/,$(notdir $(wildcard $(addprefix $(LLAMACPP_REPO),$(GGML_VENDOR_FILES)))))
  127. $(foreach name,$(GGML_VENDOR_FILES_EXPANDED),$(eval $(call vendor_file,$(name),$(DST_DIR)ggml-cuda/vendors/)))
  128. # llava -> llama/
  129. LAVA_FILES= \
  130. examples/llava/clip.cpp \
  131. examples/llava/clip.h \
  132. examples/llava/llava.cpp \
  133. examples/llava/llava.h \
  134. common/log.h \
  135. common/log.cpp \
  136. common/stb_image.h
  137. # These files are mostly used by the llava code
  138. # and shouldn't be necessary once we use clip.cpp directly
  139. LAVA_FILES+= \
  140. common/common.cpp \
  141. common/common.h \
  142. common/sampling.cpp \
  143. common/sampling.h \
  144. common/json.hpp \
  145. common/json-schema-to-grammar.cpp \
  146. common/json-schema-to-grammar.h \
  147. common/base64.hpp
  148. $(foreach name,$(LAVA_FILES),$(eval $(call vendor_file,$(name),$(DST_DIR))))
  149. $(DST_DIR)build-info.cpp:
  150. @echo "Generating $@"
  151. @echo "int LLAMA_BUILD_NUMBER = 0;" > $@
  152. @echo "char const *LLAMA_COMMIT = \"$(LLAMACPP_BASE_COMMIT)\";" >> $@
  153. @echo "char const *LLAMA_COMPILER = \"\";" >> $@
  154. @echo "char const *LLAMA_BUILD_TARGET = \"\";" >> $@
  155. VENDORED_FILES += $(DST_DIR)build-info.cpp
  156. sync: $(LLAMACPP_REPO) .WAIT $(VENDORED_FILES) .WAIT remove-stale-files
  157. PATS=*.c *.h *.cpp *.m *.metal *.cu *.cuh
  158. NATIVE_DIRS=$(DST_DIR) $(DST_DIR)llamafile/ $(DST_DIR)ggml-cuda/ $(DST_DIR)ggml-cuda/template-instances/ $(DST_DIR)ggml-cuda/vendors/
  159. ALL_NATIVE_FILES=$(foreach dir,$(NATIVE_DIRS),$(wildcard $(addprefix $(dir),$(PATS))))
  160. EXTRA_NATIVE_FILES=$(filter-out $(VENDORED_FILES) $(addprefix $(DST_DIR),$(OLLAMA_NATIVE_FILES)), $(ALL_NATIVE_FILES))
  161. remove-stale-files:
  162. @rm -f $(EXTRA_NATIVE_FILES)
  163. .PHONY: help-sync apply-patches sync create-patches remove-stale-fails .WAIT
  164. # Handy debugging for make variables
  165. print-%:
  166. @echo '$*=$($*)'