Skip to content

Refresh llama.cpp upstream patch queue - #1085

Merged
i386 merged 4 commits into
Mesh-LLM:mainfrom
i386:agent/fix-llama-upstream-canary
Jul 26, 2026
Merged

Refresh llama.cpp upstream patch queue#1085
i386 merged 4 commits into
Mesh-LLM:mainfrom
i386:agent/fix-llama-upstream-canary

Conversation

@i386

@i386 i386 commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • advance the pinned llama.cpp revision from 76f46ad2 to ff067f76
  • rebase the 41-patch Skippy queue over current upstream loader, chat-parser, and GLM-DSA graph changes
  • drop the GLM-DSA KV-cache patch now subsumed upstream
  • adapt Skippy model loading to llama.cpp's new llama_load_mode API

Why

The llama.cpp Upstream Canary stopped while applying the existing queue. Upstream changed the model-loader API and incorporated overlapping GLM-DSA / chat-parser work, so the old patches no longer applied cleanly.

The refreshed queue preserves the staged-runtime behavior while incorporating those upstream changes. GLM-DSA stage boundaries now reject a stage that starts on a shared-indexer layer, because the required top-k indices are produced by the preceding full-indexer layer and are not part of the activation sideband.

Fixes the failure in https://github.com/Mesh-LLM/mesh-llm/actions/runs/30146939161.

Validation

  • clean-clone scripts/prepare-llama.sh pinned replay; resulting tree matched the built checkout
  • MESH_LLM_AUTO_GENERATE_CODESIGN=0 just build
  • canary crate check:
    cargo check -p skippy-ffi -p skippy-runtime -p skippy-server -p skippy-model-package -p skippy-correctness -p llama-spec-bench
  • static runtime unit tests: 65 passed
  • dynamic binding unit tests: 3 passed
  • scripts/skippy-ci-smoke.sh (dense two-stage binary execution, recurrent state handoff, OpenAI surface, exact-prefix reuse)

Summary by CodeRabbit

  • New Features
    • Added/expanded a versioned public C ABI for staged execution, including slice/part GGUF output and model/session lifecycle helpers.
    • Expanded runtime/session controls (lane-based execution, checkpoints/reset/trim, external decode, sampled verify, frame sampling with positions), plus chat templates (JSON tools/grammar, reasoning/thinking support) and runtime progress events/device discovery.
    • Introduced MTP draft generation support and added GLM-DSA staged-runtime controls and generation-side signals.
  • Bug Fixes
    • Hardened staged-session and sideband bookkeeping, improved activation/safety when tensors are absent, and corrected staged graph contracts; improved grammar parsing and Metal dispatch safety.
  • Tests
    • Added coverage for GLM-DSA execution paths and parity/perf cases, with environment-scoped test behavior.

@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This change refreshes the embedded llama.cpp patch queue with Skippy staged-runtime APIs, GLM-DSA graph execution, native MTP support, runtime loading controls, Metal GLM-MoE kernels, and expanded backend tests.

Changes

Skippy staged runtime and GLM-DSA execution

Layer / File(s) Summary
ABI, staged loading, and part composition
third_party/llama.cpp/patches/0001-*, 0002-*, 0003-*, 0012-*, 0041-*, upstream.txt
Adds Skippy ABI and stage-filtered loading, ordered GGUF part loading, chat build wiring, runtime memory-load options, and updates the upstream revision.
Session lanes, media, chat, and MTP
third_party/llama.cpp/patches/0004-* ... 0020-*
Adds lane-aware sessions, activation sidebands, external decode and prefill variants, chat grammar and reasoning support, sampling and state APIs, generation signals, and native MTP proposal paths.
GLM-DSA graphs and staged controls
third_party/llama.cpp/patches/0021-* ... 0026-*, 0040-*, 0042-* ... 0043-*
Adds GLM-DSA IndexShare graphs, staged top-k sidebands, phase policies, tracing, KV integration, fused KV_B handling, native MTP graphs, and bookkeeping validation.
Metal GLM-MoE kernels and validation
third_party/llama.cpp/patches/0027-* ... 0039-*, 0044-* ... 0045-*
Updates Metal dispatch and active-expert handling, adds selector and fusion diagnostics, and expands GLM-DSA/GLM-MoE evaluation and performance coverage.

Estimated code review effort: 5 (Critical) | ~120 minutes

Possibly related PRs

Suggested labels: experimental

Suggested reviewers: michaelneale

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: refreshing the llama.cpp patch queue against upstream.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown
Contributor

This pull request is currently a draft. Reviews will not take place until the PR is marked as ready for review.

@i386
i386 marked this pull request as ready for review July 26, 2026 09:46
@github-actions
github-actions Bot requested a review from michaelneale July 26, 2026 09:46

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (15)
third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch (2)

6595-6607: 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

Thread-count clamp is a partial workaround for a documented correctness bug, not a fix.

The comment states the 256-thread sparse-attn kernel "can leave rows unwritten" on Apple Metal for large-top-k prefill shapes, and this function only clamps threads for n_batch > 1 && n_top_k >= 64 or n_top_k > 512. If the underlying kernel defect also manifests for shapes outside these two conditions (e.g. decode with very specific top_k boundaries not yet observed), rows would silently go unwritten with no detection. Given this is a data-correctness hazard silently masked by empirical shape-based clamping rather than fixed at the root (buffer/thread indexing bug in the 256-thread dsa_sparse_attn kernel), please confirm the coverage is exhaustive or file a tracked issue to fix the root cause.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch`
around lines 6595 - 6607, Replace the shape-based workaround in
ggml_metal_glm_dsa_sparse_attn_threads_for_shape with a root-cause fix in the
256-thread dsa_sparse_attn kernel’s buffer/thread indexing so no rows can remain
unwritten for any shape. If the kernel fix cannot be made here, document the
exhaustive coverage assumptions and create a tracked issue for the underlying
correctness defect rather than relying on these partial clamps.

32-37: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

RPC_PROTO_PATCH_VERSION not bumped despite the assert's own instruction.

GGML_OP_COUNT is bumped 101→107, but RPC_PROTO_PATCH_VERSION (line 32) stays at 3. The very static_assert being edited says to "update RPC_PROTO_PATCH_VERSION" whenever GGML_OP_COUNT changes — six new ops (DSA_SPARSE_MASK/ATTN/TOP1_ATTN, MOE_ROUTE_WEIGHTS/WEIGHTED_SUM/MUL_MAT_ID) were added without bumping the protocol version, so RPC clients/servers built against different op sets would report as protocol-compatible.

🔧 Proposed fix
-#define RPC_PROTO_PATCH_VERSION    3
+#define RPC_PROTO_PATCH_VERSION    4
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch`
around lines 32 - 37, Update RPC_PROTO_PATCH_VERSION alongside the GGML_OP_COUNT
change in the static assertion block, bumping it from 3 to the next protocol
version required for the six newly added operations. Keep the GGML_OP_COUNT
assertion at 107 and ensure the client/server compatibility marker reflects the
changed operation set.
third_party/llama.cpp/patches/0022-skippy-expose-GLM-DSA-staged-runtime-controls.patch (2)

2599-2602: 🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win

Unguarded get_gf_res_prev() dereference can crash KV page import.

Every other call site in this patch treats get_gf_res_prev() as nullable (res != nullptr ? res->get_skippy_glm_dsa_top_k() : nullptr at lines 2068-2069 and 2441-2442), but here it is dereferenced directly. skippy_import_kv_page can legitimately be called before any decode has built a graph result.

🛡️ Proposed guard
-    session->ctx->get_gf_res_prev()->reset();
+    if (llm_graph_result * res_prev = session->ctx->get_gf_res_prev()) {
+        res_prev->reset();
+    }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0022-skippy-expose-GLM-DSA-staged-runtime-controls.patch`
around lines 2599 - 2602, Guard the get_gf_res_prev() result in
skippy_import_kv_page before calling reset(), since it may be null before the
first decode builds a graph result. Preserve the reset behavior when a previous
graph result exists.

2437-2439: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Align GLM-DSA sideband storage with request_count in the batched frame path.

glm_dsa_top_k_storage/offsets/bytes_per_request are sized by request_count, the top-k tensor is validated as request-major request_count, and the per-request output copy uses request_idx == i in this path. Use request_count for the input sideband storage/scan or document/assert that request_count == n_tokens here.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0022-skippy-expose-GLM-DSA-staged-runtime-controls.patch`
around lines 2437 - 2439, Align the GLM-DSA sideband input storage and scan in
the batched frame path with request_count, matching glm_dsa_top_k_storage,
glm_dsa_top_k_offsets, glm_dsa_top_k_bytes_per_request, and the request-major
tensor validation. Ensure per-request processing uses the same request_count
indexing as the output copy, or add an explicit assertion/documented invariant
that request_count equals n_tokens if the input path must remain token-sized.
third_party/llama.cpp/patches/0026-Bump-Skippy-ABI-for-GLM-DSA-runtime-config.patch (1)

16-19: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Patch-level bump for a struct-layout change.

0022-skippy-expose-GLM-DSA-staged-runtime-controls.patch appends six new fields to struct skippy_runtime_config (glm_dsa_policy_profileglm_dsa_compact_flash_min_kv) plus use_mmap_prefetch/use_mmap_buffer, and adds a new activation flag. Any consumer compiled against patch 31 that passes a smaller struct by value/pointer will now be read out of bounds. Confirm that a minor bump (or an explicit size/versioned-struct handshake) isn't required here.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0026-Bump-Skippy-ABI-for-GLM-DSA-runtime-config.patch`
around lines 16 - 19, Update the ABI versioning around SKIPPY_ABI_VERSION_MAJOR,
SKIPPY_ABI_VERSION_MINOR, and SKIPPY_ABI_VERSION_PATCH to use the required minor
bump or an explicit struct-size/version handshake for the expanded
skippy_runtime_config and new activation flag; do not leave this struct-layout
change represented only by a patch-level increment.
third_party/llama.cpp/patches/0023-tests-cover-native-GLM-DSA-execution-paths.patch (1)

6382-6384: 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Unscoped set_test_env calls in build_graph leak global state into later test cases.

These fixtures set process-wide env vars from build_graph/initialize_tensors and never restore them, so every subsequent test case in the same run inherits e.g. SKIPPY_GLM_DSA_EXPERIMENTAL_SELECTED_ROW_FLASH=1, LLAMA_GLM_DSA_EXPERIMENTAL_SELECTED_ROW_FLASH_TILED=1 and the multihead/split tensor-name overrides. Results then depend on test ordering and filters. The patch already introduces scoped_test_env and uses it correctly in eval/eval_perf overrides elsewhere — the same pattern should apply here (e.g. hold the scoped guards as fixture members, or set them in eval/eval_perf rather than in graph construction).

Also applies to: 6483-6487, 6614-6617, 6703-6703, 6779-6780

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0023-tests-cover-native-GLM-DSA-execution-paths.patch`
around lines 6382 - 6384, Scope every environment override created by
build_graph/initialize_tensors, including the listed GLM DSA selected-row,
tiled, tensor-name, multihead, and split overrides, using the existing
scoped_test_env pattern. Store guards as fixture members or move setup into
eval/eval_perf, ensuring variables are restored before subsequent test cases
while preserving each test’s intended configuration.
third_party/llama.cpp/patches/0024-Support-GLM-DSA-fused-KV_B-tensors.patch (1)

77-83: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Fused-KV_B GLM-DSA checkpoints load successfully but can never execute. The loader now accepts models without split wk_b/wv_b and falls back to a fused wkv_b, while the GLM-DSA graph constructor unconditionally rejects exactly that layout — so the failure surfaces as a runtime exception on the first decode instead of a clear load-time error.

  • third_party/llama.cpp/patches/0024-Support-GLM-DSA-fused-KV_B-tensors.patch#L77-L83: either keep wk_b/wv_b required for GLM-DSA, or split wkv_b into wk_b/wv_b at load so the graph precondition holds.
  • third_party/llama.cpp/patches/0025-Run-GLM-DSA-through-IndexShare-graph.patch#L29-L33: if the fused layout is meant to be supported, replace the throw with a fused-wkv_b code path; otherwise drop this check once the loader enforces the requirement.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@third_party/llama.cpp/patches/0024-Support-GLM-DSA-fused-KV_B-tensors.patch`
around lines 77 - 83, Make fused KV_B handling consistent across both patches:
in third_party/llama.cpp/patches/0024-Support-GLM-DSA-fused-KV_B-tensors.patch
lines 77-83, either require split wk_b/wv_b for GLM-DSA or split wkv_b during
loading; in
third_party/llama.cpp/patches/0025-Run-GLM-DSA-through-IndexShare-graph.patch
lines 29-33, if fused tensors remain supported, replace the graph constructor’s
rejection with fused-wkv_b handling, otherwise remove the check after enforcing
split tensors at load time.
third_party/llama.cpp/patches/0040-Fix-GLM-DSA-native-MTP-execution.patch (1)

176-181: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Use the rope width for MTP nope-view byte offsets.

In third_party/llama.cpp/patches/0040-Fix-GLM-DSA-native-MTP-execution.patch:176-181 and :203-208, ggml_view_3d(..., ..., ..., 0) already selects the pe slab. The offset argument should therefore be ggml_row_size(*->type, n_embd_indexer_head_rope), not n_embd_indexer_head_nope; otherwise Q/K nope slices start past the intended boundary when the rope size differs from the nope size.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@third_party/llama.cpp/patches/0040-Fix-GLM-DSA-native-MTP-execution.patch`
around lines 176 - 181, The MTP nope views use the nope width for their byte
offset instead of the rope width. Update the offset argument in the ggml_view_3d
calls for indexer_q_nope and the corresponding K-nope view to use ggml_row_size
of the tensor type with n_embd_indexer_head_rope, while preserving the existing
pe-slab selection and other dimensions.
third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch (2)

184-195: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Route every active-count branch through the matching active-specific factory.

Both sites introduce reduced active counts but still select the ordinary eight-slot pipeline, so active counts 2/4/6 have no runtime effect.

  • third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch#L184-L195: call the _active6, _active4, and _active2 factories for the corresponding branches.
  • third_party/llama.cpp/patches/0034-ggml-use-GLM-max-active-policy-for-Q3-down-kernels.patch#L75-L83: make the q3_active_count branches select those same active-specific factories.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch`
around lines 184 - 195, Route each reduced active-count branch through its
matching active-specific pipeline factory. In
third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch
lines 184-195, update the active6, active4, and active2 branches to call the
corresponding _active6, _active4, and _active2 factory symbols; apply the same
q3_active_count selection in
third_party/llama.cpp/patches/0034-ggml-use-GLM-max-active-policy-for-Q3-down-kernels.patch
lines 75-83. Leave the ordinary eight-slot factory only for the non-reduced
active-count path.

22-49: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Make the active paths compile the exact Metal kernels.

The active factory builds pipelines from kernel_mul_mv_id_q3_K_weighted_reduce_slots_sg_r8_nb8_w0..., while the active Metal entry points are kernel_mul_mv_id_q3_K_wr_slots_r8_a6, ...a4, and ...a2. No aliases are added for these names, so active6/active4/active2 dispatches resolve the wrong kernel; use matching names, add [[host_name]] aliases, or rewrite the active dispatch to call the new entry points. Also applies to lines 252-298.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch`
around lines 22 - 49, Update
ggml_metal_library_get_pipeline_mul_mv_id_q3_weighted_reduce_slots_sg_r8_nb8_w0
and the corresponding active4/active2 factories so active6/active4/active2
dispatch resolves the exact Metal entry points
kernel_mul_mv_id_q3_K_wr_slots_r8_a6, kernel_mul_mv_id_q3_K_wr_slots_r8_a4, and
kernel_mul_mv_id_q3_K_wr_slots_r8_a2. Align the generated pipeline names, add
matching host-name aliases, or route dispatch directly to those entry points;
apply the same correction to the related factories in the later diff section.
third_party/llama.cpp/patches/0003-Add-staged-sampling-checkpoints-and-part-loading.patch (2)

1178-1226: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Trim committed token history with the session.

skippy_trim_session removes the KV suffix and lowers n_past, but leaves token_history intact. Subsequent sampled decoding applies repetition penalties to tokens that were trimmed away. Resize history when it exceeds token_count.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0003-Add-staged-sampling-checkpoints-and-part-loading.patch`
around lines 1178 - 1226, Update skippy_trim_session to resize
session->token_history when it contains more than token_count entries,
truncating it to the retained token count alongside the KV cache and n_past
updates. Preserve existing validation and memory-trimming behavior.

491-530: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Keep sampler history synchronized after token verification.

Both verification paths commit tokens to the session but skip skippy_record_tokens, so later sampled decoding uses incomplete repetition-penalty history.

  • third_party/llama.cpp/patches/0003-Add-staged-sampling-checkpoints-and-part-loading.patch#L491-L530: record token_ids after successful token-batch verification.
  • third_party/llama.cpp/patches/0003-Add-staged-sampling-checkpoints-and-part-loading.patch#L607-L638: accept token IDs in the activation-frame helper and record them after successful verification.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0003-Add-staged-sampling-checkpoints-and-part-loading.patch`
around lines 491 - 530, Keep sampler history synchronized in both verification
paths: update skippy_verify_token_batch to call skippy_record_tokens(session,
token_ids, token_count) after skippy_decode_batch succeeds, and update the
activation-frame helper at
third_party/llama.cpp/patches/0003-Add-staged-sampling-checkpoints-and-part-loading.patch
lines 607-638 to accept token IDs and record them after successful verification.
third_party/llama.cpp/patches/0002-Add-early-staged-model-family-and-chat-support.patch (2)

1258-1267: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Do not rescale activation input in non-first Gemma4 stages.

A preceding stage already emits the scaled hidden state. When il_start > 0, build_inp_embd(nullptr) consumes that activation, then Line 1267 applies sqrt(n_embd) again. Gate this scaling to the first stage, as the Gemma/Gemma2/Gemma3 implementations do.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0002-Add-early-staged-model-family-and-chat-support.patch`
around lines 1258 - 1267, Update the activation scaling in the staged input path
near build_inp_embd so sqrtf(n_embd) is applied only when processing the first
stage (il_start == 0). Preserve the existing token-dependent scaling for
non-staged and first-stage inputs, while leaving preceding-stage hidden states
unscaled.

1857-1866: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Advertise the implemented chat-template capability.

skippy_apply_chat_template is public in this patch, but skippy_abi_features() omits SKIPPY_FEATURE_CHAT_TEMPLATE. Clients following the ABI’s feature-probe contract will incorrectly disable chat templating.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0002-Add-early-staged-model-family-and-chat-support.patch`
around lines 1857 - 1866, The feature mask returned by skippy_abi_features must
advertise the implemented chat-template capability. Add
SKIPPY_FEATURE_CHAT_TEMPLATE to the returned feature flags alongside the
existing capabilities, preserving all currently advertised features.
third_party/llama.cpp/patches/0001-Add-Skippy-ABI-and-package-writer-foundation.patch (1)

1854-1860: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Populate element_count instead of always returning zero.

The public tensor-info API reports 0 for every tensor, despite parsed dimensions being available in info->tensors. Compute the product of meta.ne with overflow checking.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0001-Add-Skippy-ABI-and-package-writer-foundation.patch`
around lines 1854 - 1860, Update the tensor-info construction block that assigns
out_tensor->element_count to compute the product of the parsed dimensions in
info->tensors instead of always returning zero. Multiply meta.ne values with
overflow checking, and preserve the existing success/error flow by reporting
overflow through the established out_error mechanism.
🧹 Nitpick comments (2)
third_party/llama.cpp/patches/0022-skippy-expose-GLM-DSA-staged-runtime-controls.patch (1)

617-639: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Both branches of skippy_glm_dsa_sideband_top_k_count_is_valid return the same expression.

The token_count > 1 branch and the fall-through decode branch are identical (actual_top_k <= expected_top_k), so the split only adds dead code. Either collapse it to a single check or make the decode branch actually stricter if that was the intent.

♻️ Collapse to one check
-    // Prefill sidebands can be chunk-local: llama.cpp emits the actual
-    // per-chunk top-k width, while the receiving staged session may have a
-    // larger accumulated KV prefix.
-    if (token_count > 1) {
-        return actual_top_k <= expected_top_k;
-    }
-
-    // Decode producers export the actual top-k tensor width from their graph.
-    // The receiving session can have a larger accumulated KV prefix than the
-    // producer's current sideband width, especially when an OpenAI generation
-    // loop advances through cached or fused decode steps. The sideband is still
-    // valid as long as it is non-empty, token-major, and does not claim more
-    // rows than the receiver can make visible.
-    return actual_top_k <= expected_top_k;
+    // Prefill and decode sidebands are both chunk-local: llama.cpp emits the
+    // actual per-chunk/per-step top-k width, while the receiving staged session
+    // may have a larger accumulated KV prefix. The sideband is valid as long as
+    // it does not claim more rows than the receiver can make visible.
+    GGML_UNUSED(token_count);
+    return actual_top_k <= expected_top_k;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0022-skippy-expose-GLM-DSA-staged-runtime-controls.patch`
around lines 617 - 639, simplify skippy_glm_dsa_sideband_top_k_count_is_valid by
retaining the nonzero checks and replacing the redundant token_count branches
with a single actual_top_k <= expected_top_k validation; remove the
now-unnecessary comments and conditional split.
third_party/llama.cpp/patches/0038-ggml-shrink-GLM-Q2-gate-up-active-dispatch.patch (1)

19-25: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

_pad0 is being repurposed as the active-expert count.

Host and kernel independently duplicate args._pad0 > 0 && args._pad0 < neX ? args._pad0 : neX. Reusing a field literally named _pad0 for semantic data makes the host/device contract easy to break the next time the args struct is extended. Consider renaming the field (e.g. n_active_slots) and factoring the clamp into one shared helper so the two sides cannot drift.

Also applies to: 37-38

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0038-ggml-shrink-GLM-Q2-gate-up-active-dispatch.patch`
around lines 19 - 25, The active-expert count currently reuses the opaque _pad0
field and duplicates its clamping logic across host and kernel code. Rename the
field to a semantic name such as n_active_slots in the shared argument contract
and all affected patches (including 37–38), then centralize the >0 and <neX
fallback calculation in a shared helper used by both sides.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@third_party/llama.cpp/patches/0025-Run-GLM-DSA-through-IndexShare-graph.patch`:
- Around line 79-82: Move the h_nextn capture in the main graph before the final
build_norm call: assign and label res->t_h_nextn from the raw post-FFN cur, then
apply model.output_norm to cur. Keep graph_mtp’s ordering consistent so
nextn.hnorm receives the pre-output_norm hidden state.

---

Outside diff comments:
In
`@third_party/llama.cpp/patches/0001-Add-Skippy-ABI-and-package-writer-foundation.patch`:
- Around line 1854-1860: Update the tensor-info construction block that assigns
out_tensor->element_count to compute the product of the parsed dimensions in
info->tensors instead of always returning zero. Multiply meta.ne values with
overflow checking, and preserve the existing success/error flow by reporting
overflow through the established out_error mechanism.

In
`@third_party/llama.cpp/patches/0002-Add-early-staged-model-family-and-chat-support.patch`:
- Around line 1258-1267: Update the activation scaling in the staged input path
near build_inp_embd so sqrtf(n_embd) is applied only when processing the first
stage (il_start == 0). Preserve the existing token-dependent scaling for
non-staged and first-stage inputs, while leaving preceding-stage hidden states
unscaled.
- Around line 1857-1866: The feature mask returned by skippy_abi_features must
advertise the implemented chat-template capability. Add
SKIPPY_FEATURE_CHAT_TEMPLATE to the returned feature flags alongside the
existing capabilities, preserving all currently advertised features.

In
`@third_party/llama.cpp/patches/0003-Add-staged-sampling-checkpoints-and-part-loading.patch`:
- Around line 1178-1226: Update skippy_trim_session to resize
session->token_history when it contains more than token_count entries,
truncating it to the retained token count alongside the KV cache and n_past
updates. Preserve existing validation and memory-trimming behavior.
- Around line 491-530: Keep sampler history synchronized in both verification
paths: update skippy_verify_token_batch to call skippy_record_tokens(session,
token_ids, token_count) after skippy_decode_batch succeeds, and update the
activation-frame helper at
third_party/llama.cpp/patches/0003-Add-staged-sampling-checkpoints-and-part-loading.patch
lines 607-638 to accept token IDs and record them after successful verification.

In
`@third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch`:
- Around line 6595-6607: Replace the shape-based workaround in
ggml_metal_glm_dsa_sparse_attn_threads_for_shape with a root-cause fix in the
256-thread dsa_sparse_attn kernel’s buffer/thread indexing so no rows can remain
unwritten for any shape. If the kernel fix cannot be made here, document the
exhaustive coverage assumptions and create a tracked issue for the underlying
correctness defect rather than relying on these partial clamps.
- Around line 32-37: Update RPC_PROTO_PATCH_VERSION alongside the GGML_OP_COUNT
change in the static assertion block, bumping it from 3 to the next protocol
version required for the six newly added operations. Keep the GGML_OP_COUNT
assertion at 107 and ensure the client/server compatibility marker reflects the
changed operation set.

In
`@third_party/llama.cpp/patches/0022-skippy-expose-GLM-DSA-staged-runtime-controls.patch`:
- Around line 2599-2602: Guard the get_gf_res_prev() result in
skippy_import_kv_page before calling reset(), since it may be null before the
first decode builds a graph result. Preserve the reset behavior when a previous
graph result exists.
- Around line 2437-2439: Align the GLM-DSA sideband input storage and scan in
the batched frame path with request_count, matching glm_dsa_top_k_storage,
glm_dsa_top_k_offsets, glm_dsa_top_k_bytes_per_request, and the request-major
tensor validation. Ensure per-request processing uses the same request_count
indexing as the output copy, or add an explicit assertion/documented invariant
that request_count equals n_tokens if the input path must remain token-sized.

In
`@third_party/llama.cpp/patches/0023-tests-cover-native-GLM-DSA-execution-paths.patch`:
- Around line 6382-6384: Scope every environment override created by
build_graph/initialize_tensors, including the listed GLM DSA selected-row,
tiled, tensor-name, multihead, and split overrides, using the existing
scoped_test_env pattern. Store guards as fixture members or move setup into
eval/eval_perf, ensuring variables are restored before subsequent test cases
while preserving each test’s intended configuration.

In `@third_party/llama.cpp/patches/0024-Support-GLM-DSA-fused-KV_B-tensors.patch`:
- Around line 77-83: Make fused KV_B handling consistent across both patches: in
third_party/llama.cpp/patches/0024-Support-GLM-DSA-fused-KV_B-tensors.patch
lines 77-83, either require split wk_b/wv_b for GLM-DSA or split wkv_b during
loading; in
third_party/llama.cpp/patches/0025-Run-GLM-DSA-through-IndexShare-graph.patch
lines 29-33, if fused tensors remain supported, replace the graph constructor’s
rejection with fused-wkv_b handling, otherwise remove the check after enforcing
split tensors at load time.

In
`@third_party/llama.cpp/patches/0026-Bump-Skippy-ABI-for-GLM-DSA-runtime-config.patch`:
- Around line 16-19: Update the ABI versioning around SKIPPY_ABI_VERSION_MAJOR,
SKIPPY_ABI_VERSION_MINOR, and SKIPPY_ABI_VERSION_PATCH to use the required minor
bump or an explicit struct-size/version handshake for the expanded
skippy_runtime_config and new activation flag; do not leave this struct-layout
change represented only by a patch-level increment.

In
`@third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch`:
- Around line 184-195: Route each reduced active-count branch through its
matching active-specific pipeline factory. In
third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch
lines 184-195, update the active6, active4, and active2 branches to call the
corresponding _active6, _active4, and _active2 factory symbols; apply the same
q3_active_count selection in
third_party/llama.cpp/patches/0034-ggml-use-GLM-max-active-policy-for-Q3-down-kernels.patch
lines 75-83. Leave the ordinary eight-slot factory only for the non-reduced
active-count path.
- Around line 22-49: Update
ggml_metal_library_get_pipeline_mul_mv_id_q3_weighted_reduce_slots_sg_r8_nb8_w0
and the corresponding active4/active2 factories so active6/active4/active2
dispatch resolves the exact Metal entry points
kernel_mul_mv_id_q3_K_wr_slots_r8_a6, kernel_mul_mv_id_q3_K_wr_slots_r8_a4, and
kernel_mul_mv_id_q3_K_wr_slots_r8_a2. Align the generated pipeline names, add
matching host-name aliases, or route dispatch directly to those entry points;
apply the same correction to the related factories in the later diff section.

In `@third_party/llama.cpp/patches/0040-Fix-GLM-DSA-native-MTP-execution.patch`:
- Around line 176-181: The MTP nope views use the nope width for their byte
offset instead of the rope width. Update the offset argument in the ggml_view_3d
calls for indexer_q_nope and the corresponding K-nope view to use ggml_row_size
of the tensor type with n_embd_indexer_head_rope, while preserving the existing
pe-slab selection and other dimensions.

---

Nitpick comments:
In
`@third_party/llama.cpp/patches/0022-skippy-expose-GLM-DSA-staged-runtime-controls.patch`:
- Around line 617-639: simplify skippy_glm_dsa_sideband_top_k_count_is_valid by
retaining the nonzero checks and replacing the redundant token_count branches
with a single actual_top_k <= expected_top_k validation; remove the
now-unnecessary comments and conditional split.

In
`@third_party/llama.cpp/patches/0038-ggml-shrink-GLM-Q2-gate-up-active-dispatch.patch`:
- Around line 19-25: The active-expert count currently reuses the opaque _pad0
field and duplicates its clamping logic across host and kernel code. Rename the
field to a semantic name such as n_active_slots in the shared argument contract
and all affected patches (including 37–38), then centralize the >0 and <neX
fallback calculation in a shared helper used by both sides.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d8ab8897-98ba-4e66-a12e-649575374934

📥 Commits

Reviewing files that changed from the base of the PR and between 465cbc1 and 4b82641.

📒 Files selected for processing (44)
  • third_party/llama.cpp/patches/0001-Add-Skippy-ABI-and-package-writer-foundation.patch
  • third_party/llama.cpp/patches/0002-Add-early-staged-model-family-and-chat-support.patch
  • third_party/llama.cpp/patches/0003-Add-staged-sampling-checkpoints-and-part-loading.patch
  • third_party/llama.cpp/patches/0004-Add-lanes-external-media-and-chat-grammar-support.patch
  • third_party/llama.cpp/patches/0005-Add-resident-prefix-cache-and-session-refinements.patch
  • third_party/llama.cpp/patches/0006-Expand-staged-execution-across-dense-and-recurrent-f.patch
  • third_party/llama.cpp/patches/0007-Expand-staged-execution-across-VL-and-broad-model-fa.patch
  • third_party/llama.cpp/patches/0008-Add-external-decode-media-prefill-and-newer-family-s.patch
  • third_party/llama.cpp/patches/0009-Add-chat-grammar-device-enumeration-and-runtime-even.patch
  • third_party/llama.cpp/patches/0010-Add-MTP-execution-support-and-sampling-cleanup.patch
  • third_party/llama.cpp/patches/0011-Pass-reasoning-format-through-stage-chat-templates.patch
  • third_party/llama.cpp/patches/0012-Wire-mmap-and-mlock-runtime-load-options.patch
  • third_party/llama.cpp/patches/0013-Add-external-MTP-draft-sidecar-attachment.patch
  • third_party/llama.cpp/patches/0014-Add-non-frame-native-MTP-decode-ABI.patch
  • third_party/llama.cpp/patches/0015-Fix-stage-activation-graph-input-allocation.patch
  • third_party/llama.cpp/patches/0016-Recognize-thinking-field-in-chat-auto-parser.patch
  • third_party/llama.cpp/patches/0017-Expose-stateful-N-gram-cache-ABI.patch
  • third_party/llama.cpp/patches/0018-Remove-legacy-session-checkpoint-ABI.patch
  • third_party/llama.cpp/patches/0019-Re-prime-native-MTP-after-state-restoration.patch
  • third_party/llama.cpp/patches/0020-Fix-N-gram-confidence-threshold-indexing.patch
  • third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch
  • third_party/llama.cpp/patches/0022-skippy-expose-GLM-DSA-staged-runtime-controls.patch
  • third_party/llama.cpp/patches/0023-tests-cover-native-GLM-DSA-execution-paths.patch
  • third_party/llama.cpp/patches/0024-Support-GLM-DSA-fused-KV_B-tensors.patch
  • third_party/llama.cpp/patches/0025-Run-GLM-DSA-through-IndexShare-graph.patch
  • third_party/llama.cpp/patches/0026-Bump-Skippy-ABI-for-GLM-DSA-runtime-config.patch
  • third_party/llama.cpp/patches/0026-Run-GLM-DSA-through-IndexShare-graph.patch
  • third_party/llama.cpp/patches/0027-Fix-GLM-DSA-Metal-get_rows-placement.patch
  • third_party/llama.cpp/patches/0027-Use-DSA-KV-cache-for-GLM-DSA.patch
  • third_party/llama.cpp/patches/0028-ggml-default-GLM-MoE-two-phase-Metal-path.patch
  • third_party/llama.cpp/patches/0029-ggml-add-GLM-MoE-Metal-selector-diagnostics.patch
  • third_party/llama.cpp/patches/0030-ggml-skip-zero-weight-GLM-MoE-gate-up-slots.patch
  • third_party/llama.cpp/patches/0031-tests-cover-full-GLM-MoE-selected-chain.patch
  • third_party/llama.cpp/patches/0032-tests-add-GLM-Q2Q3-selected-weight-roofline.patch
  • third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch
  • third_party/llama.cpp/patches/0034-ggml-use-GLM-max-active-policy-for-Q3-down-kernels.patch
  • third_party/llama.cpp/patches/0035-ggml-honor-explicit-Q2-gate-up-Metal-variant-flags.patch
  • third_party/llama.cpp/patches/0036-ggml-make-GLM-MoE-roofline-honor-active-experts.patch
  • third_party/llama.cpp/patches/0037-ggml-avoid-GLM-Q3-fused-tail-under-active-policy.patch
  • third_party/llama.cpp/patches/0038-ggml-shrink-GLM-Q2-gate-up-active-dispatch.patch
  • third_party/llama.cpp/patches/0039-ggml-skip-inactive-GLM-Q3-down-slots.patch
  • third_party/llama.cpp/patches/0040-Fix-GLM-DSA-native-MTP-execution.patch
  • third_party/llama.cpp/patches/0041-Adapt-Skippy-model-loading-to-load-modes.patch
  • third_party/llama.cpp/upstream.txt
💤 Files with no reviewable changes (2)
  • third_party/llama.cpp/patches/0027-Use-DSA-KV-cache-for-GLM-DSA.patch
  • third_party/llama.cpp/patches/0026-Run-GLM-DSA-through-IndexShare-graph.patch
👮 Files not reviewed due to content moderation or server errors (11)
  • third_party/llama.cpp/patches/0004-Add-lanes-external-media-and-chat-grammar-support.patch
  • third_party/llama.cpp/patches/0005-Add-resident-prefix-cache-and-session-refinements.patch
  • third_party/llama.cpp/patches/0006-Expand-staged-execution-across-dense-and-recurrent-f.patch
  • third_party/llama.cpp/patches/0007-Expand-staged-execution-across-VL-and-broad-model-fa.patch
  • third_party/llama.cpp/patches/0008-Add-external-decode-media-prefill-and-newer-family-s.patch
  • third_party/llama.cpp/patches/0009-Add-chat-grammar-device-enumeration-and-runtime-even.patch
  • third_party/llama.cpp/patches/0010-Add-MTP-execution-support-and-sampling-cleanup.patch
  • third_party/llama.cpp/patches/0011-Pass-reasoning-format-through-stage-chat-templates.patch
  • third_party/llama.cpp/patches/0013-Add-external-MTP-draft-sidecar-attachment.patch
  • third_party/llama.cpp/patches/0014-Add-non-frame-native-MTP-decode-ABI.patch
  • third_party/llama.cpp/patches/0015-Fix-stage-activation-graph-input-allocation.patch

@i386

i386 commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed review feedback in b71dd13.

Fixed in follow-up patches 0042–0045:

  • capture GLM-DSA h_nextn before output norm; correct RoPE/nope offsets in main + MTP graphs; require split K/V tensors at load time
  • record verified token history, guard nullable prior graphs, align batched GLM-DSA sidebands to request count, simplify top-k validation, and bump the ggml RPC patch version
  • route active-count Q3 dispatch to the actual a6/a4/a2 Metal kernels; cap all default sparse-attention shapes to the parity-certified 32-thread path; give active-slot state a semantic field and single host-side clamp
  • scope GLM-DSA backend-test environment overrides to each test evaluation

No additional change was needed for the element-count overflow check, first-stage Gemma scaling, chat-template feature bit, or history trimming: later patches in the queue already provide those final-tree behaviors. I also kept Skippy ABI at 0.1.32 because that is already the contract on main; this PR does not change the public ABI layout relative to main.

Validation: clean 45-patch replay with matching source tree, just build, cargo fmt --all -- --check, cargo check -p mesh-llm, cargo test -p skippy-ffi --lib (3 passed), cargo test -p skippy-runtime --lib (65 passed), and scripts/skippy-ci-smoke.sh (passed).

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
third_party/llama.cpp/patches/0005-Add-resident-prefix-cache-and-session-refinements.patch (1)

252-253: 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

Bound KV-page token counts before allocating.

Both public ABI paths allocate from caller-controlled token_count before checking cache capacity. A malformed request can exhaust memory or throw through the C ABI. Reject counts above cells.size() before allocation; also guard n_tokens * n_pos.

Proposed fix
+    if (token_count > cells.size()) {
+        error = "token count exceeds KV cache capacity";
+        return false;
+    }
     std::vector<uint32_t> cell_idxs(static_cast<size_t>(token_count), std::numeric_limits<uint32_t>::max());
+    if (desc.token_count > v_cells[strm].size() ||
+            n_pos != 0 && n_tokens > std::numeric_limits<size_t>::max()/n_pos) {
+        error = "native KV page exceeds KV cache capacity";
+        return false;
+    }
     udata->token.resize(n_tokens);

Also applies to: 483-488

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0005-Add-resident-prefix-cache-and-session-refinements.patch`
around lines 252 - 253, Validate caller-controlled token counts against cache
capacity before allocating cell_idxs in both public ABI paths, including the
corresponding path near the second allocation. Reject token_count values greater
than cells.size() and guard any n_tokens * n_pos multiplication against overflow
or capacity before allocation, returning the existing failure result without
throwing through the C ABI.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In
`@third_party/llama.cpp/patches/0005-Add-resident-prefix-cache-and-session-refinements.patch`:
- Around line 252-253: Validate caller-controlled token counts against cache
capacity before allocating cell_idxs in both public ABI paths, including the
corresponding path near the second allocation. Reject token_count values greater
than cells.size() and guard any n_tokens * n_pos multiplication against overflow
or capacity before allocation, returning the existing failure result without
throwing through the C ABI.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: decd822a-8c20-4caa-a295-a008831d0218

📥 Commits

Reviewing files that changed from the base of the PR and between 4b82641 and b71dd13.

📒 Files selected for processing (45)
  • third_party/llama.cpp/patches/0001-Add-Skippy-ABI-and-package-writer-foundation.patch
  • third_party/llama.cpp/patches/0002-Add-early-staged-model-family-and-chat-support.patch
  • third_party/llama.cpp/patches/0003-Add-staged-sampling-checkpoints-and-part-loading.patch
  • third_party/llama.cpp/patches/0004-Add-lanes-external-media-and-chat-grammar-support.patch
  • third_party/llama.cpp/patches/0005-Add-resident-prefix-cache-and-session-refinements.patch
  • third_party/llama.cpp/patches/0006-Expand-staged-execution-across-dense-and-recurrent-f.patch
  • third_party/llama.cpp/patches/0007-Expand-staged-execution-across-VL-and-broad-model-fa.patch
  • third_party/llama.cpp/patches/0008-Add-external-decode-media-prefill-and-newer-family-s.patch
  • third_party/llama.cpp/patches/0009-Add-chat-grammar-device-enumeration-and-runtime-even.patch
  • third_party/llama.cpp/patches/0010-Add-MTP-execution-support-and-sampling-cleanup.patch
  • third_party/llama.cpp/patches/0011-Pass-reasoning-format-through-stage-chat-templates.patch
  • third_party/llama.cpp/patches/0012-Wire-mmap-and-mlock-runtime-load-options.patch
  • third_party/llama.cpp/patches/0013-Add-external-MTP-draft-sidecar-attachment.patch
  • third_party/llama.cpp/patches/0014-Add-non-frame-native-MTP-decode-ABI.patch
  • third_party/llama.cpp/patches/0015-Fix-stage-activation-graph-input-allocation.patch
  • third_party/llama.cpp/patches/0016-Recognize-thinking-field-in-chat-auto-parser.patch
  • third_party/llama.cpp/patches/0017-Expose-stateful-N-gram-cache-ABI.patch
  • third_party/llama.cpp/patches/0018-Remove-legacy-session-checkpoint-ABI.patch
  • third_party/llama.cpp/patches/0019-Re-prime-native-MTP-after-state-restoration.patch
  • third_party/llama.cpp/patches/0020-Fix-N-gram-confidence-threshold-indexing.patch
  • third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch
  • third_party/llama.cpp/patches/0022-skippy-expose-GLM-DSA-staged-runtime-controls.patch
  • third_party/llama.cpp/patches/0023-tests-cover-native-GLM-DSA-execution-paths.patch
  • third_party/llama.cpp/patches/0024-Support-GLM-DSA-fused-KV_B-tensors.patch
  • third_party/llama.cpp/patches/0025-Run-GLM-DSA-through-IndexShare-graph.patch
  • third_party/llama.cpp/patches/0026-Bump-Skippy-ABI-for-GLM-DSA-runtime-config.patch
  • third_party/llama.cpp/patches/0027-Fix-GLM-DSA-Metal-get_rows-placement.patch
  • third_party/llama.cpp/patches/0028-ggml-default-GLM-MoE-two-phase-Metal-path.patch
  • third_party/llama.cpp/patches/0029-ggml-add-GLM-MoE-Metal-selector-diagnostics.patch
  • third_party/llama.cpp/patches/0030-ggml-skip-zero-weight-GLM-MoE-gate-up-slots.patch
  • third_party/llama.cpp/patches/0031-tests-cover-full-GLM-MoE-selected-chain.patch
  • third_party/llama.cpp/patches/0032-tests-add-GLM-Q2Q3-selected-weight-roofline.patch
  • third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch
  • third_party/llama.cpp/patches/0034-ggml-use-GLM-max-active-policy-for-Q3-down-kernels.patch
  • third_party/llama.cpp/patches/0035-ggml-honor-explicit-Q2-gate-up-Metal-variant-flags.patch
  • third_party/llama.cpp/patches/0036-ggml-make-GLM-MoE-roofline-honor-active-experts.patch
  • third_party/llama.cpp/patches/0037-ggml-avoid-GLM-Q3-fused-tail-under-active-policy.patch
  • third_party/llama.cpp/patches/0038-ggml-shrink-GLM-Q2-gate-up-active-dispatch.patch
  • third_party/llama.cpp/patches/0039-ggml-skip-inactive-GLM-Q3-down-slots.patch
  • third_party/llama.cpp/patches/0040-Fix-GLM-DSA-native-MTP-execution.patch
  • third_party/llama.cpp/patches/0041-Adapt-Skippy-model-loading-to-load-modes.patch
  • third_party/llama.cpp/patches/0042-Harden-staged-session-and-sideband-bookkeeping.patch
  • third_party/llama.cpp/patches/0043-Correct-GLM-DSA-staged-graph-contracts.patch
  • third_party/llama.cpp/patches/0044-Fix-GLM-DSA-Metal-dispatch-safety.patch
  • third_party/llama.cpp/patches/0045-Scope-GLM-DSA-backend-test-environment.patch
🚧 Files skipped from review as they are similar to previous changes (32)
  • third_party/llama.cpp/patches/0017-Expose-stateful-N-gram-cache-ABI.patch
  • third_party/llama.cpp/patches/0014-Add-non-frame-native-MTP-decode-ABI.patch
  • third_party/llama.cpp/patches/0031-tests-cover-full-GLM-MoE-selected-chain.patch
  • third_party/llama.cpp/patches/0012-Wire-mmap-and-mlock-runtime-load-options.patch
  • third_party/llama.cpp/patches/0036-ggml-make-GLM-MoE-roofline-honor-active-experts.patch
  • third_party/llama.cpp/patches/0013-Add-external-MTP-draft-sidecar-attachment.patch
  • third_party/llama.cpp/patches/0030-ggml-skip-zero-weight-GLM-MoE-gate-up-slots.patch
  • third_party/llama.cpp/patches/0006-Expand-staged-execution-across-dense-and-recurrent-f.patch
  • third_party/llama.cpp/patches/0020-Fix-N-gram-confidence-threshold-indexing.patch
  • third_party/llama.cpp/patches/0018-Remove-legacy-session-checkpoint-ABI.patch
  • third_party/llama.cpp/patches/0035-ggml-honor-explicit-Q2-gate-up-Metal-variant-flags.patch
  • third_party/llama.cpp/patches/0039-ggml-skip-inactive-GLM-Q3-down-slots.patch
  • third_party/llama.cpp/patches/0025-Run-GLM-DSA-through-IndexShare-graph.patch
  • third_party/llama.cpp/patches/0019-Re-prime-native-MTP-after-state-restoration.patch
  • third_party/llama.cpp/patches/0040-Fix-GLM-DSA-native-MTP-execution.patch
  • third_party/llama.cpp/patches/0032-tests-add-GLM-Q2Q3-selected-weight-roofline.patch
  • third_party/llama.cpp/patches/0027-Fix-GLM-DSA-Metal-get_rows-placement.patch
  • third_party/llama.cpp/patches/0016-Recognize-thinking-field-in-chat-auto-parser.patch
  • third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch
  • third_party/llama.cpp/patches/0011-Pass-reasoning-format-through-stage-chat-templates.patch
  • third_party/llama.cpp/patches/0034-ggml-use-GLM-max-active-policy-for-Q3-down-kernels.patch
  • third_party/llama.cpp/patches/0029-ggml-add-GLM-MoE-Metal-selector-diagnostics.patch
  • third_party/llama.cpp/patches/0023-tests-cover-native-GLM-DSA-execution-paths.patch
  • third_party/llama.cpp/patches/0009-Add-chat-grammar-device-enumeration-and-runtime-even.patch
  • third_party/llama.cpp/patches/0008-Add-external-decode-media-prefill-and-newer-family-s.patch
  • third_party/llama.cpp/patches/0007-Expand-staged-execution-across-VL-and-broad-model-fa.patch
  • third_party/llama.cpp/patches/0004-Add-lanes-external-media-and-chat-grammar-support.patch
  • third_party/llama.cpp/patches/0041-Adapt-Skippy-model-loading-to-load-modes.patch
  • third_party/llama.cpp/patches/0022-skippy-expose-GLM-DSA-staged-runtime-controls.patch
  • third_party/llama.cpp/patches/0003-Add-staged-sampling-checkpoints-and-part-loading.patch
  • third_party/llama.cpp/patches/0010-Add-MTP-execution-support-and-sampling-cleanup.patch
  • third_party/llama.cpp/patches/0002-Add-early-staged-model-family-and-chat-support.patch

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
third_party/llama.cpp/patches/0005-Add-resident-prefix-cache-and-session-refinements.patch (2)

896-910: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Keep Skippy session metadata consistent with imported KV state.

Both state/VK-page import APIs advance n_past, but they do not restore, invalidate, or block access to token_history and signal_history. Imported sessions can report a past position N while those histories are absent or too short, causing prefix save/restore, checkpoint, and signal APIs to fail or behave differently from the imported cache. Include the token/signal history in the transfer or require/initialize metadata before allowing imports to advance session state.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0005-Add-resident-prefix-cache-and-session-refinements.patch`
around lines 896 - 910, Update skippy_update_session_state_after_import and both
state/VK-page import paths so imported KV state cannot advance n_past without
corresponding token_history and signal_history metadata. Transfer those
histories with the import when available, or explicitly require and initialize
missing entries to the imported length before updating session state. Preserve
existing entries, truncate excess data, and keep checkpoint_valid invalidated
after import.

450-477: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Reject imported KV pages with no selected layers.

expected_bytes stays 0 when selected is empty, so a descriptor with a non-overlapping layer range and layer_count == 0 can pass validation, allocate KV cells via find_slot()/apply_ubatch(), and then copy no K/V payload into those cells. Add the empty-selected guard before allocation.

Proposed fix
     if (selected.size() != desc.layer_count) {
         error = "native KV page layer count does not match runtime";
         return false;
     }
+    if (selected.empty()) {
+        error = "native KV page must contain at least one KV layer";
+        return false;
+    }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@third_party/llama.cpp/patches/0005-Add-resident-prefix-cache-and-session-refinements.patch`
around lines 450 - 477, Reject descriptors that produce no selected KV layers
before any allocation or cell application occurs. In the validation flow that
builds selected in the native KV page import path, add an empty-selected check
after layer-count validation and return false with an appropriate error; ensure
find_slot() and apply_ubatch() are not reached for this case.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In
`@third_party/llama.cpp/patches/0005-Add-resident-prefix-cache-and-session-refinements.patch`:
- Around line 896-910: Update skippy_update_session_state_after_import and both
state/VK-page import paths so imported KV state cannot advance n_past without
corresponding token_history and signal_history metadata. Transfer those
histories with the import when available, or explicitly require and initialize
missing entries to the imported length before updating session state. Preserve
existing entries, truncate excess data, and keep checkpoint_valid invalidated
after import.
- Around line 450-477: Reject descriptors that produce no selected KV layers
before any allocation or cell application occurs. In the validation flow that
builds selected in the native KV page import path, add an empty-selected check
after layer-count validation and return false with an appropriate error; ensure
find_slot() and apply_ubatch() are not reached for this case.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 81cb25c3-da1d-42b4-882b-bfe7a9ca3768

📥 Commits

Reviewing files that changed from the base of the PR and between b71dd13 and e70c18e.

📒 Files selected for processing (1)
  • third_party/llama.cpp/patches/0005-Add-resident-prefix-cache-and-session-refinements.patch

@i386
i386 merged commit f6e7e85 into Mesh-LLM:main Jul 26, 2026
30 checks passed
michaelneale added a commit that referenced this pull request Jul 27, 2026
The refreshed llama.cpp (#1085) enables mtmd video by default, which pulls in
tools/mtmd/mtmd-helper.cpp's ffmpeg subprocess path via sheredom/subprocess.h.
That header calls posix_spawn_file_actions_addchdir_np, which is unavailable on
iOS, so the Swift XCFramework's iOS slice fails to compile mtmd-helper.cpp.

mesh-llm does not use mtmd video. Set MTMD_VIDEO=OFF in build-llama.sh (covers
Linux, macOS, and the Apple XCFramework targets) and in build-windows.ps1 for
parity. Verified locally: the mtmd target now builds without subprocess.h.

Assisted-by: Claude Sonnet

Co-authored-by: Michael Neale <14976+michaelneale@users.noreply.github.com>
michaelneale added a commit that referenced this pull request Jul 27, 2026
* origin/main:
  Fix Metal small-batch matmul parity for GLM verification (#1078)
  Handle K-only transposed KV page import and export (#1084)
  Refresh llama.cpp upstream patch queue (#1085)
  chore: improve embedded native-runtime compatibility guidance (#1043)
  fix(console-ui): chat transcript snapping during live status updates (#1083)
  ci: bump Linux CUDA slim container to gha-convention base runner image
  fix: record activation cache prefix identities (#1041)
  fix: read-only model download caches (#1042)
  ci: disable sccache for Windows ROCm native runtime build (#1087)
  ci: fix v0.74 release GPU builds (sccache disk-only + force_hosted_runners) (#1086)
  Make release sccache failures non-fatal (#1079)
  Keep client-only nodes out of model election (#1074)

# Conflicts:
#	crates/mesh-llm-host-runtime/src/runtime/auto_join.rs
#	crates/mesh-llm-host-runtime/src/runtime/tests/auto_join.rs
#	third_party/llama.cpp/patches/0004-Add-lanes-external-media-and-chat-grammar-support.patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants