Makefile.sync 7.5 KB

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