Skip to content

Distribute and run LLMs with a single file.

Improve llamacpp update skill (#1027)

Davide Eynardf524de1b4f80
diff --git a/.llamafile_plugin/.claude-plugin/marketplace.json b/.llamafile_plugin/.claude-plugin/marketplace.json
index 0415c161..01c606bf 100644
--- a/.llamafile_plugin/.claude-plugin/marketplace.json
+++ b/.llamafile_plugin/.claude-plugin/marketplace.json
@@ -9,7 +9,7 @@
{
"name": "llamafile",
"description": "Build guidance and commands for the llamafile project",
- "version": "0.1.6",
+ "version": "0.1.7",
"author": {
"name": "Mozilla AI",
"email": "[email protected]"
diff --git a/.llamafile_plugin/.claude-plugin/plugin.json b/.llamafile_plugin/.claude-plugin/plugin.json
index a4725755..38dd3ce3 100644
--- a/.llamafile_plugin/.claude-plugin/plugin.json
+++ b/.llamafile_plugin/.claude-plugin/plugin.json
@@ -1,5 +1,5 @@
{
"name": "llamafile",
- "version": "0.1.6",
+ "version": "0.1.7",
"description": "Build guidance and commands for the llamafile project"
}
diff --git a/docs/commands/verify-clean.md b/docs/commands/verify-clean.md
index d9716a94..cf7a8756 100644
--- a/docs/commands/verify-clean.md
+++ b/docs/commands/verify-clean.md
@@ -18,6 +18,11 @@ stale objects. Always `make clean` first here — do not rebuild incrementally.
`make setup` both pulls submodules and applies patches, and it cannot pull
submodules onto a dirty tree — that is exactly why `reset-repo` runs first.
+Note: `reset-repo` is destructive (it `rm -rf`s the submodule dirs before
+restoring them), so a permission-gated environment may prompt or block it. Run
+it through the `$MAKE` (`.cosmocc/.../make`) form below rather than raw
+`git clean`/`reset`; allowlist `make reset-repo` if round-trips keep stalling.
+
```bash
# ensure the toolchain is available
if [ ! -d .cosmocc/4.0.2 ]; then
diff --git a/docs/skills/llamafile/building.md b/docs/skills/llamafile/building.md
index 360478bf..63a57c00 100644
--- a/docs/skills/llamafile/building.md
+++ b/docs/skills/llamafile/building.md
@@ -54,7 +54,7 @@ This command:
The `-j $(nproc)` flag enables parallel compilation (adjust based on CPU cores).
Adapt `nproc` to the OS where you are building, (e.g. `sysctl -n hw.physicalcpu` on mac)
-**Critical:** Always use `.cosmocc/4.0.2/bin/make`, not system make. The cosmocc toolchain includes its own make with Cosmopolitan-specific behavior.
+**Critical:** Always use `.cosmocc/4.0.2/bin/make`, not system make. The cosmocc toolchain includes its own make with Cosmopolitan-specific behavior. The only exceptions are `make setup` and `make reset-repo`, which use bare `make` — `setup` bootstraps cosmocc on a fresh clone, and both are exempt from the version check.
### Clean Build
diff --git a/docs/skills/llamafile/development.md b/docs/skills/llamafile/development.md
index 6a60b599..9246db8f 100644
--- a/docs/skills/llamafile/development.md
+++ b/docs/skills/llamafile/development.md
@@ -72,12 +72,13 @@ vim src/llama.cpp
#### Step 2: Generate Patches
Patches are usually generated after the code has been thoroughly tested and is
-ready to commit. To avoid manual errors, use the script `tools/generate-patches.sh`
-which automatically saves all new files and patches in the specified output directory.
+ready to commit. To avoid manual errors, use the script `tools/generate_patches.sh`
+(or the `llamafile:generate-patches` skill that wraps it), which automatically
+saves all new files and patches in the specified output directory.
```sh
-cd llama.cpp
-../tools/generate-patches.sh --output-dir ../llama.cpp.patches
+# echo y answers the prompt; the subshell restores the cwd even on failure
+( cd llama.cpp && echo y | ../tools/generate_patches.sh --output-dir ../llama.cpp.patches )
```
After this operation, one can double check which files have been modified / added
diff --git a/docs/skills/llamafile/update_llamacpp.md b/docs/skills/llamafile/update_llamacpp.md
index c2d21de6..199bbad8 100644
--- a/docs/skills/llamafile/update_llamacpp.md
+++ b/docs/skills/llamafile/update_llamacpp.md
@@ -13,7 +13,8 @@ This document is the canonical procedure for a llama.cpp bump. It is built by
|------|---------|----------|
| `make reset-repo` | Clean slate: drop all local changes, reset submodules | repo root |
| `make setup` | Pull submodules **and** apply patches (+ fetch UI assets). Needs a clean tree (can't pull onto a dirty one). Doubles as the patch-**application** test. | repo root |
-| `tools/check_patches.sh` | Triage **only**: of the pre-existing patches, which still apply to the freshly-bumped submodule (line-number fuzz tolerated) and which need hand-work. | repo root |
+| `tools/check_patches.sh` | Triage **only**: of the pre-existing patches, which still apply to the freshly-bumped submodule (line-number fuzz tolerated) and which need hand-work. Prints the conflicting patch's name and file:line. | repo root |
+| `apply-patches.sh --tolerant` | Reconcile-apply during a bump: apply every patch that fits, leave one `.rej` per drifted hunk (instead of aborting like strict `setup`), and do the non-patch steps (copy `llamafile-files/`, remove Makefile, fetch UI). | repo root |
| `llamafile:generate-patches` | Regenerate **all** patches from in-place submodule edits. The only sanctioned way to produce patches. | (wraps the `cd`) |
| `llamafile:verify-clean` | Clean round-trip: `reset-repo` → `setup` → clean build → `check`. The post-generate verification. | repo root |
| `llamafile:build` / `llamafile:check` | Build all targets / run unit tests. | repo root |
@@ -33,8 +34,9 @@ This document is the canonical procedure for a llama.cpp bump. It is built by
- **DO** use `git diff $OLD_ID..$COMMIT_ID` for **upstream-drift recon only**
(seeing what changed upstream to drive BUILD.mk / integration work). That is
the one legitimate ad-hoc `git diff` use.
-- **DO** use `git apply --reject <patch>` to *reconcile* a drifted patch (Step 3):
- it applies every hunk that still fits and drops only the stragglers as `.rej`
+- **DO** reconcile with `apply-patches.sh --tolerant` for the whole set at once,
+ or `git apply --reject <patch>` for a single drifted patch (Step 3):
+ each applies every hunk that still fits and drops only the stragglers as `.rej`
files, so a 50-hunk mechanical patch (e.g. the `GGML_CALL` ones) collapses to
hand-editing the 1–2 hunks that actually moved. This is distinct from — and
doesn't violate — the DON'Ts above: `check_patches.sh` triages (file-level,
@@ -44,6 +46,29 @@ This document is the canonical procedure for a llama.cpp bump. It is built by
## The procedure
+### Step 0 — Starting state (fresh clone vs. already-set-up worktree)
+
+The steps below assume a **clean** tree: submodules at their pinned commits with
+no patches applied. A fresh clone is clean; a worktree you (or a previous
+session) already ran `make setup` on is **not** — the applied patches live as
+uncommitted working-tree changes inside the submodules (`git status` shows
+`m llama.cpp`, `m whisper.cpp`, …), and Step 1's `git submodule update` will not
+run over them.
+
+If the tree is already set up, reset to a clean slate first — this is the common
+case when resuming a bump in an existing worktree:
+
+```bash
+make reset-repo # drops applied patches, resets all submodules
+```
+
+`reset-repo` and `setup` use **bare** `make` — both are exempt from the cosmocc
+version check, and `setup` must run bare on a fresh clone since it downloads
+cosmocc (every *other* target uses `.cosmocc/4.0.2/bin/make`). `reset-repo` is
+destructive (it `rm -rf`s each submodule dir before restoring it), so a
+permission-gated environment may prompt or block it; if bare `make` is blocked,
+the equivalent `.cosmocc/4.0.2/bin/make reset-repo` usually goes through.
+
### Step 1 — Bump the submodule
Creates a new branch with the submodule at its latest commit. The tree is now
@@ -64,6 +89,22 @@ git add llama.cpp
git commit -m "Update llama.cpp submodule to $COMMIT_ID"
```
+**To bump to a specific tag/release** (e.g. syncing to `b10083`) instead of the
+latest master, resolve the tag to a commit for `$COMMIT_ID` — replace the
+`fetch`/`checkout` lines above with:
+
+```bash
+git -C llama.cpp fetch origin --tags
+COMMIT_ID=`git -C llama.cpp rev-list -n1 b10083` # <tag> -> commit sha
+git -C llama.cpp checkout $COMMIT_ID
+```
+
+Branch/commit naming: the repo convention is release-based, not raw-SHA — branch
+`llamacpp-bNNNN` and commit `Update llama.cpp to bNNNN (<short-sha>)` (e.g.
+`llamacpp-b10083`, `Update llama.cpp to b10083 (846e991)`). Use that in place of
+the `llamacpp_$COMMIT_ID` form above when bumping to a tagged release; the branch
+may already exist, in which case just switch to it instead of `checkout -b`.
+
Keep `$OLD_ID` and `$COMMIT_ID` — you need them for drift recon below.
### Step 2 — Triage the existing patches
@@ -73,6 +114,11 @@ patch, whether it still applies to the bumped submodule. This is *triage only*:
it tells you which patches are free and which need reconciliation. A patch may
be accepted despite line shifts — that fuzz is fine and welcome.
+`check_patches.sh` uses `git apply --check`, which tolerates line **offset** but
+not changed context — stricter than the actual apply (`patch -p1`, via `setup` or
+`--tolerant`, which applies with fuzz). So triage tends to *over*-report: a patch
+flagged here may still apply cleanly under `--tolerant` and leave no `.rej`.
+
A patch that **fails** triage has three possible fates, not one — decide which
before editing:
- **Reconcile** — upstream moved the code; reproduce the patch's intent against
@@ -95,7 +141,9 @@ Make the submodule build and work against the new upstream, editing files
`git apply --reject` to isolate just the drifted hunks — see DO/DON'T).
- **BUILD.mk source lists:** add new upstream sources, drop deleted ones, fix
renames. Drive this from drift recon:
- `cd llama.cpp && git diff --stat --summary $OLD_ID..$COMMIT_ID -- src/ common/ ggml/ tools/`
+ `cd llama.cpp && git diff --stat --summary $OLD_ID..$COMMIT_ID -- src/ common/ ggml/ tools/ ':(exclude)tools/ui'`
+ (exclude `tools/ui` — the web UI ships via `fetch-ui-assets.sh`, not patches or
+ BUILD.mk, and its churn otherwise dominates the diff)
and cross-check against `llama.cpp/CMakeLists.txt` (the `src/models/` and
`tools/mtmd/models/` dirs are globbed there, so *every* new `.cpp` is a real
TU). A new source usually has to be listed in **more than one place** — see the
@@ -114,9 +162,37 @@ Make the submodule build and work against the new upstream, editing files
removes the upstream `Makefile`, and fetches the UI. If you reconcile by hand
(applying patches directly rather than via `setup`, e.g. because some patches
don't yet apply), replicate those steps or the build fails on a missing
- `BUILD.mk`/`license.cpp`. Also: a full `make` needs **all three** submodules
- initialized (`reset-repo` deinits whisper/sd); to prove *just* llama.cpp,
- `make o/$(MODE)/llama.cpp`.
+ `BUILD.mk`/`license.cpp`. Also: a full build needs **all** submodules
+ initialized (`reset-repo` deinits them all); to prove *just* llama.cpp,
+ `.cosmocc/4.0.2/bin/make o/$(MODE)/llama.cpp`.
+
+Reconcile with the pre-built tools — don't hand-roll `git apply` loops. After
+the bump the tree is pristine upstream with **none** of llamafile's changes;
+re-applying the patch set *replays* those changes onto the new base (a rebase),
+and Step 5 regenerates the patches from the result. Run on the clean, freshly
+bumped tree:
+
+- **Triage** already named (Step 2) the conflicting patches and their file:line —
+ `check_patches.sh` prints the `git apply --check` diagnostic under each failing
+ patch's name.
+- **No conflicts (the common case):** just run `make setup`. It applies every
+ patch and does the non-patch steps (copies `llamafile-files/`, removes the
+ Makefile, fetches the UI) deterministically.
+- **Conflicts:** strict `make setup`/`apply-patches.sh` is fail-early (aborts on
+ the first reject), so use the tolerant variant, which applies every hunk that
+ fits, does the same non-patch steps, and leaves one `*.rej` per drifted hunk
+ (listing them at the end):
+
+ ```bash
+ ./llama.cpp.patches/apply-patches.sh --tolerant
+ ```
+
+ Edit each `*.rej` into place — the one irreducibly-manual step (see the
+ `git apply --reject` note in DO/DON'T) — then delete them:
+
+ ```bash
+ find llama.cpp -name '*.rej' -delete
+ ```
### Step 4 — Prove the reconciliation works (on the dirty tree)
@@ -125,6 +201,11 @@ Before generating any patches, prove the in-place edits actually work:
- Clean build (`llamafile:clean` then `llamafile:build`) and `llamafile:check`.
- Run llamafile as expected — ideally the integration tests. Runtime / GPU /
platform validation is your hardware's job (see handoff checklist).
+- **Capturing build output:** a full clean build takes minutes — run it
+ backgrounded. Never write logs or relevant artifacts under `o/`: the clean
+ step does `rm -rf o`, so a redirect to `o/build.log` fails *before* the build
+ starts and reads like a build failure (and any other artifact left there is
+ wiped). Keep them in a scratch dir outside `o/`.
Only proceed past this gate once the build is green and llamafile runs. Patches
generated from unproven edits bake in breakage.
@@ -172,6 +253,13 @@ hardware/platforms and report them in the PR:
assets return **415**, and expect `Content-Encoding: gzip` on the response.
- [ ] Long-run stability (the `cv.wait` / futex class only shows after hours)
+**Reconciled a GPU-only patch?** The host build compiles none of the GPU backends
+— CUDA, Vulkan, and Metal DSOs build at runtime on the target — so any hunk you
+hand-edited in `ggml-cuda/*`, `ggml-vulkan/*`, or `ggml-metal/*` is **not**
+exercised by `verify-clean`; a green round-trip only proves the CPU-path patches.
+Call such reconciliations out in the PR and make sure the matching GPU smoke test
+above is run before merge.
+
## Recurring breakage (check these proactively)
Distilled from PRs #941, #951, #983 — these recur almost every bump and are the
diff --git a/llama.cpp.patches/README.md b/llama.cpp.patches/README.md
index b3faff29..4cd5b656 100644
--- a/llama.cpp.patches/README.md
+++ b/llama.cpp.patches/README.md
@@ -213,8 +213,8 @@ Files in `llama.cpp` are usually modified in-place for development and testing.
Once they are ready to be committed, you can update all files in the `llama.cpp.patches` directory by running the following:
```sh
-cd llama.cpp
-../tools/generate-patches.sh --output-dir ../llama.cpp.patches
+# echo y answers the prompt; the subshell restores the cwd even on failure
+( cd llama.cpp && echo y | ../tools/generate_patches.sh --output-dir ../llama.cpp.patches )
```
Patch filenames will automatically reflect the file path with underscores replacing slashes (e.g., `common_arg.cpp.patch` for `common/arg.cpp`).
diff --git a/llama.cpp.patches/apply-patches.sh b/llama.cpp.patches/apply-patches.sh
index e50d6c63..2ddcd43a 100755
--- a/llama.cpp.patches/apply-patches.sh
+++ b/llama.cpp.patches/apply-patches.sh
@@ -3,6 +3,20 @@
set -e
+# Usage: apply-patches.sh [--tolerant]
+# (default) strict — abort on the first patch that does not apply cleanly.
+# This is what `make setup` uses, so the common case fails early.
+# --tolerant apply every patch, leaving *.rej files for hunks that do not fit
+# and continuing instead of aborting; used during a llama.cpp bump
+# to reconcile drifted hunks by hand. Exits non-zero if any rejects.
+TOLERANT=""
+for arg in "$@"; do
+ case "$arg" in
+ --tolerant) TOLERANT=1 ;;
+ *) echo "Unknown option: $arg"; echo "Usage: $0 [--tolerant]"; exit 1 ;;
+ esac
+done
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LLAMA_DIR="$SCRIPT_DIR/../llama.cpp"
PATCHES_DIR="$SCRIPT_DIR/patches"
@@ -30,10 +44,17 @@ rm Makefile
cd ..
echo "Applying modifications to upstream files..."
+FAILED_PATCHES=()
for patch_file in "$PATCHES_DIR"/*.patch; do
if [ -f "$patch_file" ]; then
echo "Applying $(basename "$patch_file")..."
- patch -p1 < "$patch_file"
+ if [ -n "$TOLERANT" ]; then
+ # Apply the hunks that fit, leave *.rej for the rest, and keep going.
+ patch -p1 < "$patch_file" || FAILED_PATCHES+=("$(basename "$patch_file")")
+ else
+ # Strict (default): abort on the first patch that does not apply.
+ patch -p1 < "$patch_file"
+ fi
fi
done
@@ -41,6 +62,19 @@ done
"$SCRIPT_DIR/fetch-ui-assets.sh"
echo ""
+if [ ${#FAILED_PATCHES[@]} -gt 0 ]; then
+ # patch leaves a *.orig backup beside each reject; drop them (the pristine
+ # original is recoverable from git) so only the actionable *.rej remain and
+ # generate-patches doesn't pick the backups up as new files.
+ find llama.cpp -name '*.orig' -delete
+ echo "Applied with rejects (tolerant mode): ${#FAILED_PATCHES[@]} patch(es) did not apply cleanly:"
+ for p in "${FAILED_PATCHES[@]}"; do echo " - $p"; done
+ echo "Reconcile the drifted hunks in these reject files:"
+ find llama.cpp -name '*.rej' | sed 's/^/ /'
+ echo " (edit each *.rej into place, then: find llama.cpp -name '*.rej' -delete)"
+ exit 1
+fi
+
echo "Patches applied successfully!"
echo "Note: These changes are not committed to the submodule."
echo "To reset the submodule to its clean state, run:"