Skip to content

Commit 70cbcba

Browse files
mxynggabe-l-hartdhiltgen
authored andcommitted
update vendored llama.cpp and ggml (ollama#11823)
* TEMPORARY: Update the llama.cpp upstream to my fork's Granite Four branch This will be redone once my branch is merged upstream in llama.cpp * feat: Update all patches There are a number that are no longer needed at all: - 0003-embeddings: Embeddings entirely overhauled on master - 0008-ensure-KV-cache-is-fully-defragmented: KV caching entirely overhauled on master - 0019-metal-add-mean-kernel-14267: Merged upstream - 0020-CUDA-add-mean-operation-14313: Merged upstream * feat: Sync llama.cpp and ggml * fix: Update rsync-filter for all moved/new/removed files * fix: Add files missing from sync * fix: Update ggml rsync-filter for new ggml-cpu/arch subdirs * fix: Add ggml files missing from sync * fix: Narrow llama.cpp rsync-filter to not include mtmd main tool cpp files * fix: Remove mtmd main cpp files * fix: Add missing include in sampling_ext.cpp * fix: Update llama.go to use mtmd instead of clip/llava * fix: Add patch for mtmd_input_text * chore: Ignore *.patched in the patch directory * fix: Fix support for arch-specific ggml-cpu source files with new arrangement In ggml-org/llama.cpp#13892, all arch-specific implementations were split out into a nested tree structure under ggml-cpu/arch. This conflicts with standard CGO layout where all arch-specific source files are expected to live in the same directory as the parent go module and use suffixes based on GOOS and GOARCH. As such, there were really two options for getting this to work: 1. Add a patch on top of the GGML sync to rearrange the files to match the GO layout convention 2. Use CGO directives to conditionally include the nested source files in the compilation units This commit does (2) in order to minimize the set of changes needed on top of the upstream file layout. To get this to work, there are two key things needed: 1. In cpu.go, #cgo directives are added to explicitly set __${GOARCH}__ in the preprocessor directives 2. In arch-impls.c|cpp, use an #ifdef | #elif defined | #endif chain to explicitly include the .c|.cpp files for the given architecture from the nested directory * fix: Use mtmd_helper to correctly load the bitmap for the image * fix: Apply patch for mtmd_text_input * fix: Add missing stb to llama.cpp rsync-filter * fix: Add sync'ed stb vendored header * fix: Use c++17 and include vendor for go wrapper modules * fix: Update patch 0015 for upstream implementation of uuid * feat: Bump to the latest tip of the branch * fix: Update patches for bump * feat: Bump back to the cenral repo and point at the latest master This includes granite 4 and a number of other model architectures! * fix: Revert changes to ggml export GPU UUID patch * fix: Add patch for GGML_VERSION and GGML_COMMIT constants * feat: Sync all patched code * build: Include cmake/common.cmake in ggml sync * build: Add top-level include for GNUINstallDirs in CMakeLists.txt This is used to populate CMAKE_INSTALL_BINDIR * fix: Add a patch to avoid power throttling API on non-msvc windows builds * fix: Sync patch changes for ggml-cpu.c * feat: Bump llama.cpp to 4a4f42 This picks up support for Kimi K2 and PLaMO-2 * feat: Sync llama.cpp * fix: Handle multi-chunk image encodings from mtmd * fix: Re-number patches after merge with `main` * feat: Bump to 41e78c in the makefile * fix: Fix Solar and argsort/copy patches after bump * fix: Remove Gemma3n CUDA Graphs patch It was implemented upstream: ggml-org/llama.cpp#14741 * feat: Sync llama.cpp / ggml after latest bump * build: Remove unnecessary CFLAGS definitions in cpu.go * fix: Remove unnecessary additions in the rsync-filter * fix: Remove unused vendored code for chat template parsing * Revert "fix: Remove Gemma3n CUDA Graphs patch" This reverts commit d724cac. * fix: Update 0020 CUDA Graphs for gemma3n to keep both llama.cpp and ollama fixes ollama#11195 (comment) * fix: Sync ggml-cuda.cu after keeping both style cuda graph fixes for gemma3n * unwind mxfp4 patch Prepare to bump ggml with their impl for mxfp4 * bump * fix windows build error * Convert tensors at load time Repack the mxfp4 tensors as ggmls kernels expect them to be. * convert mlp bf16 to f32 * buffer the conversion better * reshape earlier * openai swiglu * add ids * split qkv, gate_up * fix nested alt tags * fast attention * remove debug messages * fix lint * remove redundant test * remap values only if source/target are different * add back i32->i32 copy * refactor cpu quants * clean up vendor * update patch instructions * clean up patches * remove webgpu * update mem * also handle gpt-oss * revert convert changes --------- Signed-off-by: Gabe Goodhart <ghart@us.ibm.com> Co-authored-by: Gabe Goodhart <ghart@us.ibm.com> Co-authored-by: Daniel Hiltgen <daniel@ollama.com>
1 parent e3655bc commit 70cbcba

File tree

243 files changed

+156798
-48333
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

243 files changed

+156798
-48333
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.21)
33
project(Ollama C CXX)
44

55
include(CheckLanguage)
6+
include(GNUInstallDirs)
67

78
find_package(Threads REQUIRED)
89

@@ -51,7 +52,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ml/backend/ggml/ggml/src/include
5152
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ml/backend/ggml/ggml/src/ggml-cpu)
5253
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ml/backend/ggml/ggml/src/ggml-cpu/amx)
5354

54-
add_compile_definitions(NDEBUG)
55+
add_compile_definitions(NDEBUG GGML_VERSION=0x0 GGML_COMMIT=0x0)
5556

5657
set(GGML_CPU ON)
5758
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/ml/backend/ggml/ggml/src)

Makefile.sync

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
UPSTREAM=https://github.com/ggerganov/llama.cpp.git
1+
UPSTREAM=https://github.com/ggml-org/llama.cpp.git
22
WORKDIR=llama/vendor
3-
FETCH_HEAD=de4c07f93783a1a96456a44dc16b9db538ee1618
3+
FETCH_HEAD=e54d41befcc1575f4c898c5ff4ef43970cead75f
44

55
.PHONY: help
66
help:
@@ -12,7 +12,7 @@ help:
1212
@echo " clean Clean local repository"
1313
@echo
1414
@echo "Example:"
15-
@echo " make -f $(lastword $(MAKEFILE_LIST)) clean sync"
15+
@echo " make -f $(lastword $(MAKEFILE_LIST)) clean apply-patches sync"
1616

1717
.PHONY: sync
1818
sync: llama/build-info.cpp ml/backend/ggml/ggml/src/ggml-metal/ggml-metal-embed.metal
@@ -24,12 +24,12 @@ ml/backend/ggml/ggml/src/ggml-metal/ggml-metal-embed.metal: ml/backend/ggml/ggml
2424
go generate ./$(@D)
2525

2626
.PHONY: llama/llama.cpp
27-
llama/llama.cpp: llama/vendor/
28-
rsync -arvzc -f "merge $@/.rsync-filter" $< $@
27+
llama/llama.cpp: llama/vendor
28+
rsync -arvzc --delete -f "include LICENSE" -f "merge $@/.rsync-filter" $(addprefix $<,/LICENSE /) $@
2929

3030
.PHONY: ml/backend/ggml/ggml
31-
ml/backend/ggml/ggml: llama/vendor/ggml/
32-
rsync -arvzc -f "merge $@/.rsync-filter" $< $@
31+
ml/backend/ggml/ggml: llama/vendor
32+
rsync -arvzc --delete -f "include LICENSE" -f "merge $@/.rsync-filter" $(addprefix $<,/LICENSE /ggml/) $@
3333

3434
PATCHES=$(wildcard llama/patches/*.patch)
3535
PATCHED=$(join $(dir $(PATCHES)), $(addsuffix ed, $(addprefix ., $(notdir $(PATCHES)))))
@@ -39,7 +39,15 @@ PATCHED=$(join $(dir $(PATCHES)), $(addsuffix ed, $(addprefix ., $(notdir $(PATC
3939
apply-patches: $(PATCHED)
4040

4141
llama/patches/.%.patched: llama/patches/%.patch
42-
@if git -c user.name=nobody -c 'user.email=<>' -C $(WORKDIR) am -3 $(realpath $<); then touch $@; else git -C $(WORKDIR) am --abort; exit 1; fi
42+
@if git -c user.name=nobody -c 'user.email=<>' -C $(WORKDIR) am -3 $(realpath $<); then \
43+
touch $@; \
44+
else \
45+
echo "Patch failed. Resolve any conflicts then continue."; \
46+
echo "1. Run 'git -C $(WORKDIR) am --continue'"; \
47+
echo "2. Run 'make -f $(lastword $(MAKEFILE_LIST)) format-patches'"; \
48+
echo "3. Run 'make -f $(lastword $(MAKEFILE_LIST)) clean apply-patches'"; \
49+
exit 1; \
50+
fi
4351

4452
.PHONY: checkout
4553
checkout: $(WORKDIR)
@@ -60,4 +68,5 @@ format-patches: llama/patches
6068

6169
.PHONE: clean
6270
clean: checkout
71+
@git -C $(WORKDIR) am --abort || true
6372
$(RM) llama/patches/.*.patched

convert/reader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939

4040
func (t tensorBase) Kind() uint32 {
4141
if strings.HasSuffix(t.name, ".ffn_gate_inp.weight") ||
42+
strings.HasSuffix(t.name, ".bias") ||
4243
t.name == "token_types.weight" ||
4344
t.name == "v.positional_embedding_vlm" ||
4445
t.name == "v.tile_position_embd.weight" ||

fs/ggml/ggml.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (kv KV) OllamaEngineRequired() bool {
180180
"llama4",
181181
"mllama",
182182
"qwen25vl",
183-
"gptoss",
183+
"gptoss", "gpt-oss",
184184
}, kv.Architecture())
185185
}
186186

@@ -328,7 +328,7 @@ func (t TensorType) TypeSize() uint64 {
328328
return 2 + blockSize/2
329329
case TensorTypeQ4_1:
330330
return 2 + 2 + blockSize/2
331-
case TensorTypeMXFP4:
331+
case TensorTypeMXFP4, 39:
332332
return 1 + blockSize/2
333333
case TensorTypeQ5_0:
334334
return 2 + 4 + blockSize/2
@@ -665,7 +665,7 @@ func (f GGML) GraphSize(context, batch uint64, numParallel int, kvCacheType stri
665665
4*qkvBias.Shape[0],
666666
)
667667
}
668-
case "gptoss":
668+
case "gptoss", "gpt-oss":
669669
kv = make([]uint64, f.KV().BlockCount())
670670
for i := range kv {
671671
kv[i] = uint64(float64((embeddingHeadsK+embeddingHeadsV)*headsKV) * bytesPerElement)
@@ -675,8 +675,7 @@ func (f GGML) GraphSize(context, batch uint64, numParallel int, kvCacheType stri
675675
kv[i] *= context
676676
}
677677
}
678-
fullOffload = 4 * f.KV().HeadCountMax() / cmp.Or(f.KV().HeadCountKVMin(), 1) * kvTotal / 6
679-
partialOffload = fullOffload
678+
partialOffload = 2 * f.KV().HeadCountMax() / cmp.Or(f.KV().HeadCountKVMin(), 1) * kvTotal / 6
680679
}
681680

682681
return
@@ -761,10 +760,6 @@ func (f GGML) SupportsFlashAttention() bool {
761760
return false
762761
}
763762

764-
if f.KV().Architecture() == "gptoss" {
765-
return false
766-
}
767-
768763
// Check head counts match and are non-zero
769764
headCountK := f.KV().EmbeddingHeadCountK()
770765
headCountV := f.KV().EmbeddingHeadCountV()

llama/build-info.cpp

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

llama/llama.cpp/.rsync-filter

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
protect **/*.go
2-
include common/
3-
include common/base64.*
4-
include common/common.*
5-
include common/json-schema-to-grammar.*
6-
include common/json.*
7-
include common/log.*
8-
include common/sampling.*
9-
include common/stb_image.*
10-
include include/
11-
include include/llama.*
12-
include include/llama-*.*
13-
include tools/
14-
include tools/mtmd/
15-
include tools/mtmd/clip.*
16-
include tools/mtmd/clip-impl.*
17-
include tools/mtmd/llava.*
18-
include src/
19-
include src/llama.*
20-
include src/llama-*.*
21-
include src/unicode-data.*
22-
include src/unicode.*
23-
exclude *
1+
protect .rsync-filter
2+
protect *.go
3+
include /common/
4+
include /common/base64.*
5+
include /common/common.*
6+
include /common/json-schema-to-grammar.*
7+
include /common/json.*
8+
include /common/log.*
9+
include /common/sampling.*
10+
include /include/
11+
include /include/llama.*
12+
include /include/llama-*.*
13+
include /tools/
14+
include /tools/mtmd/
15+
include /tools/mtmd/*.h
16+
include /tools/mtmd/clip.cpp
17+
include /tools/mtmd/mtmd.cpp
18+
include /tools/mtmd/mtmd-audio.cpp
19+
include /tools/mtmd/mtmd-helper.cpp
20+
include /src/
21+
include /src/llama.*
22+
include /src/llama-*.*
23+
include /src/unicode-data.*
24+
include /src/unicode.*
25+
include /vendor/
26+
include /vendor/miniaudio/
27+
include /vendor/miniaudio/*.h
28+
include /vendor/nlohmann/
29+
include /vendor/nlohmann/*.hpp
30+
include /vendor/stb/
31+
include /vendor/stb/*.h
32+
hide *

0 commit comments

Comments
 (0)