Makefile.sync 8.62 KB
Newer Older
1
2
# Helpers for managing our vendored llama.cpp repo and patch set

3
4
REPO_ROOT:=./
DEST_DIR:=./llama/
5

6
include $(DEST_DIR)vendoring
7

8
LLAMACPP_REPO := ./llama/vendor/
9

10
11
# Relative to the vendor dir
VENDOR_RELATIVE_PATCH_DIR := ../patches/
12
13
14
15
16


help-sync:
	@echo "The following make targets will help you update llama.cpp to a new base commit, or work on new features/fixes"
	@echo ""
17
18
19
20
	@echo "	make apply-patches	# Establish the tracking repo if not already present, reset to the base commit, and apply our patch set"
	@echo "	make sync		# Vendor llama.cpp and ggml from the tracking repo working tree"
	@echo "	make sync-clean		# Remove all vendored files"
	@echo "	make create-patches	# Generate the patch set based on the current commits in the tracking repo since the base commit"
21
	@echo ""
22
	@echo "For more details on the workflow, see the Vendoring section in 'docs/development.md'"
23
24
25
26
27
28
29
30
31
32
33

apply-patches: $(LLAMACPP_REPO)
	@if ! git -C $(LLAMACPP_REPO) --no-pager diff --exit-code ; then \
  		echo "ERROR: Your llama.cpp repo is dirty.  The apply-patches target requires a clean working tree"; \
		echo "To clobber: git -C $(LLAMACPP_REPO) reset --hard HEAD" ; \
  		exit 1; \
	fi
	@echo "Checking out $(LLAMACPP_BASE_COMMIT)"
	@git -C $(LLAMACPP_REPO) checkout -q $(LLAMACPP_BASE_COMMIT) || \
		git -C $(LLAMACPP_REPO) fetch --all && git -C $(LLAMACPP_REPO) checkout -q $(LLAMACPP_BASE_COMMIT)
	@echo "Applying ollama patches..."
34
	@cd $(LLAMACPP_REPO) && git -c 'user.name=nobody' -c 'user.email=<>' am -3 $(VENDOR_RELATIVE_PATCH_DIR)*.patch || \
35
36
37
38
39
40
41
42
43
44
45
46
47
48
		echo "Please resolve the conflicts in $(LLAMACPP_REPO), and run 'git am --continue' to continue applying subsequent patches"
	@echo ""
	@echo "The tracking repo $(LLAMACPP_REPO) is now in a detached state with all patches applied."
	@echo "Don't forget to commit any changes you make and run 'make create-patches' "
	
$(LLAMACPP_REPO):
	@echo "Cloning llama.cpp to $(LLAMACPP_REPO)"
	git clone https://github.com/ggerganov/llama.cpp.git $@

create-patches: $(LLAMACPP_REPO)
	@if ! git -C $(LLAMACPP_REPO) --no-pager diff --exit-code ; then \
  		echo "ERROR: Your llama.cpp repo is dirty.  You must commit any pending changes for format-patch to generate patches"; \
  		exit 1; \
	fi
49
	@cd $(LLAMACPP_REPO) && git format-patch --no-signature --no-numbered --zero-commit -o $(VENDOR_RELATIVE_PATCH_DIR) $(LLAMACPP_BASE_COMMIT)
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

# Vendoring template logic
EXCLUDED_FILES=sgemm.cpp sgemm.h sampling_ext.cpp sampling_ext.h stb_image.h json.hpp llama_darwin.c base64.hpp
OLLAMA_NATIVE_FILES=mllama.cpp mllama.h llama_darwin.c sampling_ext.cpp sampling_ext.h
define vendor_file
$(strip $(addprefix $(2),$(notdir $1))) : $(addprefix $(LLAMACPP_REPO),$(1))
ifneq ($$(filter-out $(EXCLUDED_FILES),$(notdir $1)),)
	@echo "vendoring $1"; \
		mkdir -p $$(dir $$@) && \
		echo "/**" > $$@ && \
		echo " * llama.cpp - commit $$(LLAMACPP_BASE_COMMIT) - do not edit this file" >> $$@ && \
		echo " *" >> $$@ && \
		sed 's/^/ * /' <$(LLAMACPP_REPO)/LICENSE | sed 's/ *$$$$//' >> $$@ && \
		echo " */" >> $$@ && \
		echo "" >> $$@ && \
		cat $$< >> $$@
else
	@echo "vendoring $1"; \
		mkdir -p $$(dir $$@) && \
		cat $$< > $$@
endif
VENDORED_FILES += $(strip $(addprefix $(2),$(notdir $1)))
endef

# llama.cpp files -> llama/
LLAMACPP_FILES=\
	src/unicode.cpp \
	src/unicode.h \
	src/unicode-data.cpp \
	src/unicode-data.h \
	src/llama.cpp \
	src/llama-impl.h \
	src/llama-vocab.cpp \
	src/llama-vocab.h \
	src/llama-grammar.cpp \
	src/llama-grammar.h \
	src/llama-sampling.cpp \
	src/llama-sampling.h \
	include/llama.h \
89
90
91
	ggml/include/ggml-cpu.h \
	ggml/src/ggml-cpu/llamafile/sgemm.cpp \
	ggml/src/ggml-cpu/llamafile/sgemm.h
92
$(foreach name,$(LLAMACPP_FILES),$(eval $(call vendor_file,$(name),$(DEST_DIR))))
93
94
95

# llama.cpp files -> llama/llamafile
LLAMAFILE_FILES= \
96
	ggml/src/ggml-cpu/llamafile/sgemm.h
97
$(foreach name,$(LLAMAFILE_FILES),$(eval $(call vendor_file,$(name),$(DEST_DIR)llamafile/)))
98
99
100
101
102
103
104

# ggml files -> llama/
GGML_FILES= \
	ggml/src/ggml.c \
	ggml/include/ggml.h \
	ggml/src/ggml-quants.c \
	ggml/src/ggml-quants.h \
105
	ggml/src/ggml-metal/ggml-metal.metal \
106
107
	ggml/include/ggml-metal.h \
	ggml/src/ggml-impl.h \
108
	ggml/src/ggml-threading.h \
109
	ggml/include/ggml-cuda.h \
110
111
	ggml/src/ggml-backend-reg.cpp \
	ggml/src/ggml-metal/ggml-metal-impl.h \
112
113
	ggml/src/ggml-common.h \
	ggml/include/ggml-backend.h \
114
	ggml/src/ggml-backend.cpp \
115
116
117
118
119
120
	ggml/src/ggml-backend-impl.h \
	ggml/include/ggml-alloc.h \
	ggml/src/ggml-alloc.c \
	ggml/src/ggml-aarch64.h \
	ggml/src/ggml-aarch64.c \
	ggml/include/ggml-blas.h \
121
122
123
124
125
126
127
128
129
130
131
132
133
134
	ggml/include/ggml-cpp.h \
	ggml/src/ggml-threading.cpp \
	ggml/src/ggml-blas/ggml-blas.cpp \
	ggml/src/ggml-cpu/ggml-cpu.c \
	ggml/src/ggml-cpu/ggml-cpu-aarch64.c \
	ggml/src/ggml-cpu/ggml-cpu.cpp \
	ggml/src/ggml-cpu/ggml-cpu-aarch64.h \
	ggml/src/ggml-cpu/ggml-cpu-quants.h \
	ggml/src/ggml-cpu/ggml-cpu-quants.c \
	ggml/src/ggml-cpu/ggml-cpu-impl.h \
	ggml/src/ggml-cpu/amx/amx.h \
	ggml/src/ggml-cpu/amx/amx.cpp \
	ggml/src/ggml-cpu/amx/mmq.cpp \
	ggml/src/ggml-cpu/amx/mmq.h
135
$(foreach name,$(GGML_FILES),$(eval $(call vendor_file,$(name),$(DEST_DIR))))
136

137
138
139
140
141
142
143
144
145
146
147
148
149
$(DEST_DIR)ggml-metal-embed.metal: $(DEST_DIR)ggml-common.h $(DEST_DIR)ggml-metal-impl.h
	@sed -e '/__embed_ggml-common.h__/r $(DEST_DIR)/ggml-common.h' \
	     -e '/__embed_ggml-common.h__/d' \
	     < $(DEST_DIR)/ggml-metal.metal \
	     > $(DEST_DIR)/ggml-metal-embed.metal.tmp
	@sed -e '/#include "ggml-metal-impl.h"/r $(DEST_DIR)/ggml-metal-impl.h' \
	     -e '/#include "ggml-metal-impl.h"/d' \
	     < $(DEST_DIR)/ggml-metal-embed.metal.tmp \
	     > $(DEST_DIR)/ggml-metal-embed.metal
	@rm $(DEST_DIR)/ggml-metal-embed.metal.tmp

VENDORED_FILES += $(DEST_DIR)ggml-metal-embed.metal

150
# TODO generalize renaming pattern if we have more of these
151
$(DEST_DIR)ggml-metal_darwin_arm64.m : $(LLAMACPP_REPO)ggml/src/ggml-metal/ggml-metal.m
152
153
154
155
156
157
158
159
160
	@echo "vendoring $(subst $(LLAMACPP_REPO),,$<)"; \
		mkdir -p $(dir $@) && \
		echo "/**" > $@ && \
		echo " * llama.cpp - commit $(LLAMACPP_BASE_COMMIT) - do not edit this file" >> $@ && \
		echo " *" >> $@ && \
		sed 's/^/ * /' <$(LLAMACPP_REPO)/LICENSE | sed 's/ *$$//' >> $@ && \
		echo " */" >> $@ && \
		echo "" >> $@ && \
		cat $< >> $@
161
VENDORED_FILES += $(DEST_DIR)ggml-metal_darwin_arm64.m
162
163
164
165

# ggml-cuda -> llama/ggml-cuda/
GGML_CUDA_FILES= ggml/src/ggml-cuda/*.cu ggml/src/ggml-cuda/*.cuh
GGML_CUDA_FILES_EXPANDED = $(addprefix ggml/src/ggml-cuda/,$(notdir $(wildcard $(addprefix $(LLAMACPP_REPO),$(GGML_CUDA_FILES)))))
166
$(foreach name,$(GGML_CUDA_FILES_EXPANDED),$(eval $(call vendor_file,$(name),$(DEST_DIR)ggml-cuda/)))
167
168
169

GGML_TEMPLATE_FILES= ggml/src/ggml-cuda/template-instances/*.cu
GGML_TEMPLATE_FILES_EXPANDED = 	$(addprefix ggml/src/ggml-cuda/template-instances/,$(notdir $(wildcard $(addprefix $(LLAMACPP_REPO),$(GGML_TEMPLATE_FILES)))))
170
$(foreach name,$(GGML_TEMPLATE_FILES_EXPANDED),$(eval $(call vendor_file,$(name),$(DEST_DIR)ggml-cuda/template-instances/)))
171
172
173

GGML_VENDOR_FILES= ggml/src/ggml-cuda/vendors/*.h
GGML_VENDOR_FILES_EXPANDED=$(addprefix ggml/src/ggml-cuda/vendors/,$(notdir $(wildcard $(addprefix $(LLAMACPP_REPO),$(GGML_VENDOR_FILES)))))
174
$(foreach name,$(GGML_VENDOR_FILES_EXPANDED),$(eval $(call vendor_file,$(name),$(DEST_DIR)ggml-cuda/vendors/)))
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

# llava -> llama/
LAVA_FILES= \
	examples/llava/clip.cpp \
	examples/llava/clip.h \
	examples/llava/llava.cpp \
	examples/llava/llava.h \
	common/log.h \
	common/log.cpp \
	common/stb_image.h
# These files are mostly used by the llava code
# and shouldn't be necessary once we use clip.cpp directly
LAVA_FILES+= \
	common/common.cpp \
	common/common.h \
	common/sampling.cpp \
	common/sampling.h \
	common/json.hpp \
	common/json-schema-to-grammar.cpp \
	common/json-schema-to-grammar.h \
	common/base64.hpp
196
$(foreach name,$(LAVA_FILES),$(eval $(call vendor_file,$(name),$(DEST_DIR))))
197

198
$(DEST_DIR)build-info.cpp:
199
200
201
202
203
	@echo "Generating $@"
	@echo "int LLAMA_BUILD_NUMBER = 0;" > $@
	@echo "char const *LLAMA_COMMIT = \"$(LLAMACPP_BASE_COMMIT)\";" >> $@
	@echo "char const *LLAMA_COMPILER = \"\";" >> $@
	@echo "char const *LLAMA_BUILD_TARGET = \"\";" >> $@
204
VENDORED_FILES += $(DEST_DIR)build-info.cpp
205
206
207
208


sync: $(LLAMACPP_REPO) .WAIT $(VENDORED_FILES) .WAIT remove-stale-files

209
210
211
sync-clean:
	rm -f $(VENDORED_FILES) $(EXTRA_NATIVE_FILES)

212
PATS=*.c *.h *.cpp *.m *.metal *.cu *.cuh
213
NATIVE_DIRS=$(DEST_DIR) $(DEST_DIR)llamafile/ $(DEST_DIR)ggml-cuda/ $(DEST_DIR)ggml-cuda/template-instances/ $(DEST_DIR)ggml-cuda/vendors/
214
ALL_NATIVE_FILES=$(foreach dir,$(NATIVE_DIRS),$(wildcard $(addprefix $(dir),$(PATS))))
215
EXTRA_NATIVE_FILES=$(filter-out $(VENDORED_FILES) $(addprefix $(DEST_DIR),$(OLLAMA_NATIVE_FILES)), $(ALL_NATIVE_FILES))
216
217
218
remove-stale-files:
	@rm -f $(EXTRA_NATIVE_FILES)

219
.PHONY: help-sync apply-patches sync create-patches remove-stale-fails .WAIT
220
221
222
223
224


# Handy debugging for make variables
print-%:
	@echo '$*=$($*)'