Skip to content

Refresh llama.cpp upstream patch queue - #1099

Merged
i386 merged 1 commit into
mainfrom
agent/refresh-llama-patch-queue
Jul 28, 2026
Merged

Refresh llama.cpp upstream patch queue#1099
i386 merged 1 commit into
mainfrom
agent/refresh-llama-patch-queue

Conversation

@i386

@i386 i386 commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Rebase all 47 carried llama.cpp patches onto upstream 9a3bf2b84923a85583b4ee8177b0cca13824bb03.
  • Update the pinned upstream revision and regenerate the durable patch queue.
  • Resolve upstream drift in staged OpenAI-MoE filtering, GLM-DSA Metal integration, and architecture-test metadata.

Root cause

The upstream canary failed before compilation because patch 0002 referenced an obsolete common/CMakeLists.txt preimage. Later upstream Metal refactors required a full queue refresh rather than a one-hunk patch fix.

Validation

  • Clean checkout: LLAMA_WORKDIR= scripts/prepare-llama.sh pinned
  • just build (patched Metal ABI built successfully; mesh-llm binary produced)
  • ./target/debug/mesh-llm --version

Summary by CodeRabbit

  • New Features

    • Added an expanded runtime API for staged and multimodal model execution.
    • Added configurable sampling, chat templates, tool-aware responses, reasoning formats, generation signals, and external media decoding.
    • Added support for model parts, resident prefixes, session state transfer, MTP draft generation, and broader architecture coverage.
    • Added GLM-DSA sparse execution and optimized Metal backend paths.
  • Bug Fixes

    • Improved activation handling, KV-page edge cases, dispatch safety, and model loading reliability.
  • Tests

    • Expanded coverage for chat parsing, KV-page handling, GLM-DSA execution, and backend performance.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The llama.cpp patch queue adds and evolves the Skippy C ABI, staged execution, sampling, resident-prefix and MTP support, GLM-DSA graph and Metal execution, KV-page handling, chat parsing, and extensive backend test coverage.

Changes

Skippy staged runtime

Layer / File(s) Summary
ABI and staged session foundations
third_party/llama.cpp/patches/0001-*.patch0019-*.patch
Adds public Skippy model/session APIs, staged graph filtering, activation sidebands, sampling, chat templating, state handling, resident prefixes, external MTP attachment, and related ABI revisions.
GLM-DSA graph and MTP execution
third_party/llama.cpp/patches/0021-*.patch0026-*.patch, 0040-*.patch, 0043-*.patch
Adds GLM-DSA sparse graph primitives, staged IndexShare execution, fused KV_B handling, and a dedicated GLM-DSA native MTP graph.
Metal kernels and backend tests
third_party/llama.cpp/patches/0027-*.patch0045-*.patch
Updates Metal routing, sparse attention, MoE dispatch, active-expert kernels, batch-invariant matmul, diagnostics, and GLM-DSA test suites.
KV edge cases and test isolation
third_party/llama.cpp/patches/0046-*.patch, 0047-*.patch, upstream.txt
Adds empty transposed-V KV-page round-trip coverage, scopes GLM-DSA test environment variables, and updates the recorded upstream revision.

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

Possibly related PRs

Suggested reviewers: michaelneale, ndizazzo

🚥 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 clearly summarizes the main change: refreshing the carried llama.cpp patch queue.
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
  • Commit unit tests in branch agent/refresh-llama-patch-queue

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.

@i386
i386 marked this pull request as ready for review July 28, 2026 08:28
@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.

@github-actions
github-actions Bot requested a review from ndizazzo July 28, 2026 08:29
@i386

i386 commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator Author

Rebase-conflict rationale

This is a queue rebase, not a feature change. The canary failed because patch 0002 carried an obsolete common/CMakeLists.txt preimage; upstream had reordered the common source list, so git am --3way could no longer construct a useful fake ancestor. Rebasing the full 47-commit series onto upstream revision 9a3bf2b8 was safer than hand-editing that first mail patch and deferring the later conflicts.

Is ours in conflict with theirs?

At the patch-text level, yes: both upstream and the carried queue changed the same files and, in several cases, the same control-flow or declaration anchors. At the behavior level, no mutually exclusive intent was identified. Each resolution keeps the upstream behavior and the staged-runtime behavior, selecting the appropriate path by architecture or execution mode. The highest residual risk is the Metal encoder refactor, which is why this remains a draft pending CI canary coverage.

1. Staged OpenAI-MoE filtering

The carried patch makes a stage run layers il_start through il_end, starts from an activation input for a nonzero il_start, and returns an activation boundary when include_output is false. New upstream behavior added two independent output paths:

  • embeddings_nextn_masked controls whether unused-token row selection happens inside the final layer.
  • t_h_nextn plus a later row selection supports the complementary next-token embedding path.

The rebase makes stage-local selection require all three conditions: this is the final layer of the selected stage, output IDs exist, and embeddings_nextn_masked is enabled. The stage-boundary return remains before t_h_nextn and the output head, so an activation-only stage neither constructs classifier output nor accidentally fills a full-model next-token result. With filtering disabled, control flow remains upstream behavior.

2. GLM-DSA Metal integration

Current upstream substantially refactored the Metal encoder path: a formerly monolithic encode implementation is now split into individual operation helpers and updated fusion/concurrency handling. The carried GLM-DSA patch adds sparse-attention, selected-row, routing, weighted-reduction, and GLM-MoE Metal machinery across the operator registry, pipeline lookup, argument structs, C++ dispatch, and Metal shaders. Replacing upstream sections wholesale would have discarded current upstream behavior.

Two overlaps were resolved deliberately:

  • Upstream now declares the FWHT Metal argument struct beside the queue's GLM-MoE argument structs. The rebased patch retains the upstream typedef, then starts a separate typedef for the GLM route and weighted-sum structs. The patched Metal ABI compiled locally, which catches malformed declaration boundaries.
  • Upstream's Hadamard/FWHT fast path remains ahead of the carried GLM decode-block diagnostic and dispatch handling. The conditions are independent and both may return their specialized Metal encoder path.

This preserves upstream's new Metal support while keeping all carried GLM-DSA kernels and their registrations connected to the current encoder architecture.

3. Architecture-test metadata

The architecture test helper builds synthetic GGUF metadata. Upstream added MiniMax M3 / MSA requirements: its indexer uses one head per GQA/KV head, plus block-size and local-block metadata. The carried GLM-DSA tests require different metadata: one indexer head, a 128-wide key, fixture-controlled top-k, and IndexShare metadata for top-k frequency, skip offset, and full/shared indexer-type sequences.

The resolved helper selects the M3 head count only for MiniMax M3, preserves its block/local fields, and selects the GLM-DSA key/top-k and IndexShare fixtures only for GLM-DSA. Neither synthetic model consumes the other architecture's metadata.

Validation and remaining coverage

  • A fresh temporary llama.cpp checkout applied all 47 regenerated patches cleanly at the new pin.
  • The patched macOS Metal ABI compiled successfully through the repository build workflow.
  • The rebuilt debug binary responds to mesh-llm --version.

The scheduled canary remains the Linux/Rust and smoke-test validation for this exact upstream revision. No mesh wire-protocol change or intentional staged-runtime ABI change is included here.

@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 (16)
third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch (5)

2951-2956: 🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win

GGML_TYPE_Q2_0 was replaced by GGML_TYPE_TQ2_0 in the mul_mv/mul_mv_id type switches, removing Q2_0 dispatch.

Both switches previously had a case GGML_TYPE_Q2_0: arm setting N_SG_Q2_0/N_R0_Q2_0. This patch substitutes GGML_TYPE_TQ2_0 in place of it rather than adding it, so GGML_TYPE_Q2_0 now falls through to:

default:
    GGML_LOG_ERROR("Asserting on type %d\n", (int) tsrc0);
    GGML_ABORT("not implemented");

The kernel_mul_mv_q2_0_f32 kernel and the N_R0_Q2_0/N_SG_Q2_0 constants are still present in the patch, and ggml_metal_device_supports_op still reports GGML_OP_MUL_MAT as supported — so a Q2_0 weight tensor aborts the process at encode time instead of falling back. If the intent was to add TQ2_0, both cases need to be present.

🐛 Proposed fix — keep both quant types
+        case GGML_TYPE_Q2_0:
+            {
+                nsg = N_SG_Q2_0;
+                nr0 = N_R0_Q2_0;
+            } break;
         case GGML_TYPE_TQ2_0:
             {
                 nsg = N_SG_TQ2_0;
                 nr0 = N_R0_TQ2_0;
             } break;

Also applies to: 3199-3204

🤖 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 2951 - 2956, Restore the GGML_TYPE_Q2_0 dispatch arms in both
mul_mv and mul_mv_id type switches, preserving their N_SG_Q2_0 and N_R0_Q2_0
assignments. Keep the newly added GGML_TYPE_TQ2_0 cases alongside them so both
quantization types are supported.

3255-3282: 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

suffix aliases the name buffer that is overwritten a few lines later.

snprintf(name, sizeof(name), "_r%d", nr0); suffix = name; makes suffix point into name, which is then reused by snprintf(name, 256, "%s_nsg=%d", base, nsg);. It happens to work only because base is formatted from suffix first. Use a dedicated buffer so a future reordering cannot silently corrupt the kernel name.

♻️ Proposed fix
+    char suffix_buf[32];
...
-                } else if (nr0 != N_R0_Q2_K) {
-                    snprintf(name, sizeof(name), "_r%d", nr0);
-                    suffix = name;
-                }
+                } else if (nr0 != N_R0_Q2_K) {
+                    snprintf(suffix_buf, sizeof(suffix_buf), "_r%d", nr0);
+                    suffix = suffix_buf;
+                }

(and the equivalent change in the GGML_TYPE_Q3_K arm)

🤖 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 3255 - 3282, In the kernel-name construction logic, update the
suffix handling in the GGML_TYPE_Q2_K and GGML_TYPE_Q3_K cases so the generated
"_r%d" suffix is stored in a dedicated buffer rather than aliasing the reusable
name buffer. Keep suffix pointing to the dedicated storage before the later
"%s_nsg=%d" formatting step, while preserving the existing GLM-down suffix
behavior.

31371-31375: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Default-initialize indexer_types.

std::array<int32_t, LLAMA_MAX_LAYERS> indexer_types; is left indeterminate while the adjacent fields all have initializers. The comment documents -1 as "unknown", so make that the default rather than relying on every construction path to populate it before indexer_types_present is consulted.

🛡️ Proposed fix
-    std::array<int32_t, LLAMA_MAX_LAYERS> indexer_types; // -1 unknown, 0 shared, 1 full
+    std::array<int32_t, LLAMA_MAX_LAYERS> indexer_types = { }; // -1 unknown, 0 shared, 1 full

Note { } value-initializes to 0; if -1 is the intended sentinel, prefer an explicit fill in the constructor or ggml-style reset helper.

🤖 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 31371 - 31375, Default-initialize the indexer_types member used by
the surrounding indexer state, ensuring every element starts at -1 to represent
“unknown.” Add this initialization in the relevant constructor or ggml-style
reset helper rather than using empty-brace array initialization, which would
produce zeros; preserve indexer_types_present and the existing adjacent field
defaults.

1101-1125: 🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win

Unconditional 9 MiB of fusion scratch is allocated for every Metal context.

GGML_METAL_FUSION_SCRATCH_SIZE (1 MiB) is allocated for all GGML_METAL_MAX_COMMAND_BUFFERS + 1 lanes at ggml_metal_init time, even though the private-resource paths that consume it are gated behind opt-in env flags (GGML_METAL_EXPERIMENTAL_GLM_MOE_PRIVATE_SCRATCH, ..._TWO_PHASE, ..._DUAL_LANE). Every process that touches the Metal backend pays ~9 MiB of wired device memory it will never use.

Consider allocating lazily on first use, or gating the allocation on the same predicate the consumers use.

🤖 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 1101 - 1125, Remove the unconditional fusion-scratch allocation
from ggml_metal_init and avoid freeing uninitialized lanes in ggml_metal_free.
Allocate fusion_scratch lazily on first use, or gate initialization with the
same opt-in predicate used by the private-resource consumers
(GGML_METAL_EXPERIMENTAL_GLM_MOE_PRIVATE_SCRATCH, TWO_PHASE, and DUAL_LANE),
while preserving allocation-failure handling for enabled paths.

1129-1137: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

n_cb_active narrows the command-buffer status sweep across graphs.

ggml_metal_synchronize now iterates cb_idx <= ctx->n_cb_active. When a graph sets n_cb_active = 0 (because n_nodes_1 == 0), command buffers retained from a previous, wider graph at indices 1..n_cb are no longer status-checked or reported on failure. Please confirm those buffers are already released/reset elsewhere before the next synchronize.

#!/bin/bash
# Check how cmd_bufs entries are reset/released between graphs.
rg -nP -C8 'cmd_bufs\[' -g '**/ggml-metal-context.m'

Also applies to: 1307-1312

🤖 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 1129 - 1137, Update ggml_metal_synchronize to status-check every
allocated command buffer through ctx->n_cb, not only the graph’s
ctx->n_cb_active count, so buffers retained from wider graphs are still
reported. Verify the cmd_bufs reset/release lifecycle before synchronization and
preserve the same full-range behavior at the other affected occurrence.
third_party/llama.cpp/patches/0023-tests-cover-native-GLM-DSA-execution-paths.patch (1)

220-221: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Fail closed when determining phase-gate success.

The summary treats any occurrence of "OK" as a passed gate. A successful command can still emit a failure summary alongside another OK line, producing a false-positive report. Parse an exact success marker or propagate an explicit gate result instead.

🤖 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 220 - 221, Update the phase-gate result handling around phase_text
and the appended summary line to fail closed: do not treat arbitrary occurrences
of “OK” as success. Parse the command’s exact success marker or propagate an
explicit exit/result status, and report the gate as passed only when that
definitive success condition is met; otherwise retain the failure/inspection
message.
third_party/llama.cpp/patches/0025-Run-GLM-DSA-through-IndexShare-graph.patch (2)

18-33: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Validate the complete staged layer range before indexing.

il_start is used without bounds validation, while only il_end is clamped. A negative or oversized layer_start can index model.layers[il]; an empty range later emits stage_boundary with il_end - 1. Reject or normalize 0 <= il_start < il_end <= n_layer before the validation loop.

Also applies to: 68-76

🤖 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/0025-Run-GLM-DSA-through-IndexShare-graph.patch`
around lines 18 - 33, Validate the staged range immediately after computing
il_start and il_end, requiring 0 <= il_start < il_end <= effective_n_layers
before any layer indexing or stage-boundary use. Reject invalid or empty ranges
consistently, then keep the existing full-indexer and split K_B/V_B validation
for the validated range.

29-33: 🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Do not reject the fused KV_B format introduced by patch 0024.

third_party/llama.cpp/patches/0024-Support-GLM-DSA-fused-KV_B-tensors.patch makes wk_b/wv_b optional and populates wkv_b for fused files, but this loop throws whenever either split tensor is absent. Fused GLM-DSA GGUFs therefore fail before graph construction. Select the split or fused graph path based on the tensors actually loaded.

🤖 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/0025-Run-GLM-DSA-through-IndexShare-graph.patch`
around lines 29 - 33, Update the GLM-DSA validation and graph construction
around the loop checking model.layers[il].wk_b and wv_b to accept either split
K_B/V_B tensors or the fused wkv_b tensor. Select the split or fused graph path
based on which tensors are loaded, and only reject a layer when neither valid
representation is available.
third_party/llama.cpp/patches/0046-Fix-empty-transposed-KV-page-import-and-export.patch (1)

144-176: 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

Make the KV-page import test non-vacuous.

The payload is imported back into the same cache that produced it, and the cached K data is never cleared or changed. An import implementation that ignores the payload could therefore still pass the final byte comparison. Import into a fresh cache, or clear/overwrite the destination page before importing, then verify the restored payload.

🤖 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/0046-Fix-empty-transposed-KV-page-import-and-export.patch`
around lines 144 - 176, Make the K-only roundtrip test around
stage_import_kv_page non-vacuous by importing the exported payload into a fresh
cache or after clearing/overwriting the destination page. Ensure the subsequent
stage_export_kv_page comparison verifies that import restored the data from
payload rather than returning unchanged cached contents.
third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch (2)

22-48: 🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

Route active-count Q3 kernels through the specialized pipelines.

The active-count branches currently return the generic ...r8_nb8_w0 pipeline, whose nsg remains 8. Consequently, the new a6/a4/a2 kernels are not dispatched and the max-active policy has no runtime effect.

  • third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch#L22-L48: compile the active-specific kernel_mul_mv_id_q3_K_wr_slots_r8_a{6,4,2} entry point instead of the generic kernel.
  • third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch#L184-L195: call the active6/active4/active2 pipeline getters.
  • third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch#L252-L297: ensure the newly added active entry points are the symbols used by the pipeline builder.
  • third_party/llama.cpp/patches/0034-ggml-use-GLM-max-active-policy-for-Q3-down-kernels.patch#L75-L83: select the corresponding active getter for each q3_active_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/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch`
around lines 22 - 48, Route active-count Q3 kernels through the specialized
entry points instead of the generic nsg=8 pipeline. In
third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch
lines 22-48, update
ggml_metal_library_get_pipeline_mul_mv_id_q3_weighted_reduce_slots_sg_r8_nb8_w0_active
to compile kernel_mul_mv_id_q3_K_wr_slots_r8_a6, a4, or a2 based on
active_slots; lines 184-195 must call the corresponding active6/active4/active2
pipeline getters, and lines 252-297 must register those active entry-point
symbols with the pipeline builder. In
third_party/llama.cpp/patches/0034-ggml-use-GLM-max-active-policy-for-Q3-down-kernels.patch
lines 75-83, select the matching active getter for each q3_active_count value.

397-408: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Activate the active-count paths in these test cases.

The static-motif and glb_moe_down_reduce entries only encode active_experts values; without GGML_METAL_ENABLE_Q3_DOWN_SLOT_PARALLEL_REDUCE_ACTIVE6/4/2, those cases hit the fallback Q3 reduce path. Scope those env vars around the added cases in both eval and perf registrations, and avoid suppressing the max-active policy cases when coverage is meant to exercise it.

🤖 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 397 - 408, Scope
GGML_METAL_ENABLE_Q3_DOWN_SLOT_PARALLEL_REDUCE_ACTIVE6/4/2 around the added
active-count test registrations for test_glm_moe_static_motif and
test_glm_moe_down_reduce in
third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch
ranges 397-408 and 416-426, covering both eval and perf cases. In
third_party/llama.cpp/patches/0034-ggml-use-GLM-max-active-policy-for-Q3-down-kernels.patch
range 91-103, preserve the max-active policy cases without disabling or
overriding the environment settings needed to exercise that coverage.
third_party/llama.cpp/patches/0032-tests-add-GLM-Q2Q3-selected-weight-roofline.patch (1)

32-38: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Apply the roofline environment settings during evaluation too.

eval_perf() sets the reference, chunk, and bypass variables, but these variants are also registered in make_test_cases_eval() via a base class that does not provide an inherited eval() with the same setup. Add an overridden eval() with the same scoped_test_env setup or remove these cases from the evaluation list.

🤖 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/0032-tests-add-GLM-Q2Q3-selected-weight-roofline.patch`
around lines 32 - 38, Ensure the GLM roofline variants registered by
make_test_cases_eval() apply the same
GGML_METAL_ENABLE_GLM_MOE_DECODE_MOTIF_REFERENCE,
GGML_METAL_EXPERIMENTAL_GLM_MOE_Q2_WEIGHT_ROOFLINE_CHUNKS, and bypass-route
scoped_test_env settings during evaluation. Add an eval() override alongside
eval_perf() that preserves the bypass_route value and delegates to the base test
evaluation, or remove these variants from make_test_cases_eval().
third_party/llama.cpp/patches/0018-Remove-legacy-session-checkpoint-ABI.patch (1)

20-28: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Bump the Skippy ABI version for the removed exports.

This patch removes public functions and a public feature flag, but does not update SKIPPY_ABI_VERSION_PATCH. Clients compiled against the previous ABI can still observe the old version while those symbols are unavailable. Increment the ABI version, or document and enforce a deliberate compatibility policy.

Also applies to: 37-42

🤖 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/0018-Remove-legacy-session-checkpoint-ABI.patch`
around lines 20 - 28, Update SKIPPY_ABI_VERSION_PATCH in the same patch to
reflect removal of the skippy_checkpoint_session and
skippy_restore_session_checkpoint exports and the associated public feature
flag. Ensure clients can distinguish this ABI from the previous version; if
compatibility is intentionally preserved instead, document and enforce that
policy consistently.
third_party/llama.cpp/patches/0042-Harden-staged-session-and-sideband-bookkeeping.patch (1)

93-95: 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Validate request_count before narrowing it to int32_t.

GGML_ASSERT is not a release-build guard. A value above INT32_MAX can narrow to an invalid or negative n_tokens, causing incorrect batch allocation and iteration. Return SKIPPY_STATUS_INVALID_ARGUMENT before the cast.

Proposed fix
-    const int32_t n_tokens = static_cast<int32_t>(request_count);
-    GGML_ASSERT(static_cast<size_t>(n_tokens) == request_count);
+    if (request_count > static_cast<size_t>(std::numeric_limits<int32_t>::max())) {
+        skippy_set_error(out_error, SKIPPY_STATUS_INVALID_ARGUMENT, "request_count is too large");
+        return SKIPPY_STATUS_INVALID_ARGUMENT;
+    }
+    const int32_t n_tokens = static_cast<int32_t>(request_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/0042-Harden-staged-session-and-sideband-bookkeeping.patch`
around lines 93 - 95, Validate request_count against INT32_MAX and return
SKIPPY_STATUS_INVALID_ARGUMENT before assigning it to the int32_t n_tokens
variable; remove the GGML_ASSERT-based narrowing check and preserve the existing
llama_batch initialization for valid counts.
third_party/llama.cpp/patches/0017-Expose-stateful-N-gram-cache-ABI.patch (1)

52-66: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Clear the retained N-gram cache layers on reset.

skippy_ngram_cache_reset() only clears history and context, while skippy_ngram_cache_draft() also uses dynamic and static_cache. Since this API is request-local and draft state can leak from a prior conversation, reset those layers too, or document that they are intentionally sticky/shared.

🤖 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/0017-Expose-stateful-N-gram-cache-ABI.patch`
around lines 52 - 66, Update skippy_ngram_cache_reset() to clear the dynamic and
static_cache layers in addition to history and context whenever reset is
requested, ensuring all request-local draft state is discarded together.
third_party/llama.cpp/patches/0010-Add-MTP-execution-support-and-sampling-cleanup.patch (1)

1981-1983: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Missing skippy_set_error before UNSUPPORTED return leaves out_error unset.

In skippy_decode_step_frame_batch_sampled, the Gemma3n AltUp branch returns SKIPPY_STATUS_UNSUPPORTED directly without calling skippy_set_error, unlike the sibling check a few lines earlier in the same function (skippy_output_activation_flags(...) != 0) which does call it. Callers relying on out_error for a message when status != OK will get a stale/null error pointer for this specific failure path.

🐛 Proposed fix
             if ((input_desc->flags & SKIPPY_ACTIVATION_FLAG_GEMMA3N_ALTUP) != 0) {
-                return SKIPPY_STATUS_UNSUPPORTED;
+                skippy_set_error(out_error, SKIPPY_STATUS_UNSUPPORTED, "batched activation decode does not support Gemma3n AltUp sidebands yet");
+                return SKIPPY_STATUS_UNSUPPORTED;
             } else if (n_embd_inp == n_embd) {
🤖 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/0010-Add-MTP-execution-support-and-sampling-cleanup.patch`
around lines 1981 - 1983, Update the Gemma3n AltUp branch in
skippy_decode_step_frame_batch_sampled to call skippy_set_error with an
appropriate unsupported-operation message before returning
SKIPPY_STATUS_UNSUPPORTED, matching the existing sibling failure path and
ensuring out_error is populated.
🧹 Nitpick comments (6)
third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch (5)

6902-6965: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Document the private-scratch ownership contract for fused_range_outputs.

The set_fused_range_outputs / tracks_fused_range_output mechanism deliberately withholds fused intermediates from ggml_mem_ranges, which is correct only as long as those intermediates never escape the fused span. That invariant is enforced today by can_fuse_filtered_subgraph's use-count and view_src checks, but the connection between the two is not stated anywhere. A short comment on set_fused_range_outputs naming the invariant ("callers must have validated via can_fuse* that untracked nodes have no consumers outside the span") would make a future regression much harder to introduce.

Also applies to: 11505-11517

🤖 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 6902 - 6965, Document the ownership invariant at
set_fused_range_outputs: callers must first validate through
can_fuse_filtered_subgraph or the relevant can_fuse* checks that untracked fused
intermediates have no consumers or view sources outside the fused span. Keep the
implementation unchanged and place the concise contract comment directly with
the fused-range output mechanism.

5766-6320: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚖️ Poor tradeoff

The env-flag surface for GLM-DSA Metal paths has grown past the point of being auditable.

There are now ~70 GGML_METAL_EXPERIMENTAL_* / SKIPPY_GLM_DSA_* predicates selecting among a dozen near-identical Q2/Q3 gate-up and down-projection kernels, several with overlapping enable/disable pairs and shape-conditional defaults. Two concrete consequences:

  • The effective kernel for a given build is not determinable without tracing the predicate chain, which makes performance regressions hard to bisect.
  • Several flags are mutually exclusive by construction but nothing enforces or reports that.

Since the dispatch logger already exists, consider emitting a one-time summary of the resolved configuration at first encode, and pruning the variants that lost their bake-off.

🤖 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 5766 - 6320, The GLM-DSA Metal dispatch configuration is too
difficult to audit because numerous overlapping environment predicates select
mutually exclusive kernel variants without visibility. Consolidate or remove
obsolete Q2/Q3 gate-up and down-projection flags and variants that lost their
bake-off, enforce mutually exclusive selections, and use the existing dispatch
logger to emit a one-time summary of the resolved configuration at first encode,
centered on the predicate helpers such as
ggml_metal_glm_dsa_q2_gate_up_swiglu_pair_sg_any_variant_enabled and the
relevant encode/dispatch path.

1298-1305: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Env override silently discards the caller's n_cb.

SKIPPY_GLM_DSA_METAL_PROFILE_N_CB replaces the value the backend requested with no validation and no log line. A stray or malformed value (e.g. 0, negative, non-numeric) becomes the scheduling width for the whole session with no diagnostic. At minimum clamp to [1, GGML_METAL_MAX_COMMAND_BUFFERS] and log when the override fires.

🤖 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 1298 - 1305, Validate the SKIPPY_GLM_DSA_METAL_PROFILE_N_CB
override in ggml_metal_set_n_cb before replacing the caller’s n_cb: parse it
strictly, clamp valid values to [1, GGML_METAL_MAX_COMMAND_BUFFERS], and retain
the caller-provided value for malformed or non-positive input. Log whenever the
environment override is applied, including the effective value.

137-218: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Cache the diagnostic env lookup and harden the snprintf offset arithmetic.

ggml_backend_sched_glm_route_diag_enabled() calls getenv twice on every ggml_backend_sched_split_graph invocation, and on truncation snprintf returns the would-be length, so offset can exceed sizeof(ops). The loop guard prevents an out-of-bounds write today, but the invariant is fragile.

♻️ Proposed hardening
-static bool ggml_backend_sched_glm_route_diag_enabled() {
-    const char * log = getenv("GGML_METAL_MOE_DISPATCH_LOG");
-    if (log && atoi(log) != 0) {
-        return true;
-    }
-    log = getenv("SKIPPY_GLM_DSA_LOG_METAL_DISPATCH");
-    return log && atoi(log) != 0;
-}
+static bool ggml_backend_sched_glm_route_diag_enabled() {
+    static const bool enabled = [] {
+        const char * log = getenv("GGML_METAL_MOE_DISPATCH_LOG");
+        if (log && atoi(log) != 0) {
+            return true;
+        }
+        log = getenv("SKIPPY_GLM_DSA_LOG_METAL_DISPATCH");
+        return log && atoi(log) != 0;
+    }();
+    return enabled;
+}
-            if (written < 0) {
+            if (written < 0 || (size_t) written >= sizeof(ops) - offset) {
                 break;
             }
🤖 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 137 - 218, Update ggml_backend_sched_glm_route_diag_enabled to
cache the diagnostic environment lookup result instead of calling getenv on
every invocation, while preserving both supported environment variables and
their precedence. In ggml_backend_sched_log_glm_route_splits, guard the snprintf
offset update against truncation by only advancing offset when the written
length fits the remaining buffer; otherwise terminate the operation-string
construction safely without allowing offset to exceed sizeof(ops).

1341-1540: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Environment-variable configuration is re-read on hot paths in both the Metal pipeline selector and the scheduler diagnostics. The shared root cause is that every *_enabled() / *_requested() helper calls getenv (often two or three times) on each invocation, even though the values cannot change during the process lifetime. Cache each helper's result in a function-local static const.

  • third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch#L1341-L1540: convert the ggml_metal_glm_dsa_*_requested() / *_enabled() helpers in ggml-metal-device.cpp to cache their parsed value, so pipeline-cache hits no longer pay getenv/atoi.
  • third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch#L137-L218: cache ggml_backend_sched_glm_route_diag_enabled() in ggml-backend.cpp so ggml_backend_sched_split_graph does not call getenv twice per split.
🤖 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 1341 - 1540, Cache the parsed environment-variable results in
function-local static const values for every ggml_metal_glm_dsa_*_requested()
and *_enabled() helper in the 1341-1540 range, preserving each helper’s current
fallback and validation behavior. Also update
ggml_backend_sched_glm_route_diag_enabled() in
third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch
lines 137-218 to cache its result, eliminating repeated getenv/atoi calls while
retaining existing configuration semantics.
third_party/llama.cpp/patches/0010-Add-MTP-execution-support-and-sampling-cleanup.patch (1)

4029-4046: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚖️ Poor tradeoff

MTP draft metadata smuggled into the llama_token output buffer.

skippy_verify_tokens_frame_sampled packs mtp_draft.token_count, mtp_draft.token_ids[0], and a truncated proposal_compute_us (cast into a llama_token/int32) into extra slots of output_tokens, relying on the caller reserving token_count + 3 capacity and knowing this undocumented convention. Every other MTP-producing entry point (skippy_decode_step_frame_sampled_mtp) returns the draft via the dedicated skippy_native_mtp_draft struct instead. This inconsistent, type-unsafe encoding is easy to misuse from FFI callers and loses precision on proposal_compute_us for larger values.

Please confirm intentional design vs. oversight — if intentional, consider adding an out_mtp_draft out-parameter (as used elsewhere) instead of overloading the token buffer.

🤖 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/0010-Add-MTP-execution-support-and-sampling-cleanup.patch`
around lines 4029 - 4046, Remove the MTP metadata packing from
skippy_verify_tokens_frame_sampled’s output_tokens buffer and expose the draft
through a dedicated skippy_native_mtp_draft out-parameter, matching
skippy_decode_step_frame_sampled_mtp. Update the function signature and all
callers to pass and consume this structured result, while keeping output_tokens
limited to actual verified tokens and preserving proposal_compute_us precision.
🤖 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/0010-Add-MTP-execution-support-and-sampling-cleanup.patch`:
- Around line 1981-1983: Update the Gemma3n AltUp branch in
skippy_decode_step_frame_batch_sampled to call skippy_set_error with an
appropriate unsupported-operation message before returning
SKIPPY_STATUS_UNSUPPORTED, matching the existing sibling failure path and
ensuring out_error is populated.

In `@third_party/llama.cpp/patches/0017-Expose-stateful-N-gram-cache-ABI.patch`:
- Around line 52-66: Update skippy_ngram_cache_reset() to clear the dynamic and
static_cache layers in addition to history and context whenever reset is
requested, ensuring all request-local draft state is discarded together.

In
`@third_party/llama.cpp/patches/0018-Remove-legacy-session-checkpoint-ABI.patch`:
- Around line 20-28: Update SKIPPY_ABI_VERSION_PATCH in the same patch to
reflect removal of the skippy_checkpoint_session and
skippy_restore_session_checkpoint exports and the associated public feature
flag. Ensure clients can distinguish this ABI from the previous version; if
compatibility is intentionally preserved instead, document and enforce that
policy consistently.

In
`@third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch`:
- Around line 2951-2956: Restore the GGML_TYPE_Q2_0 dispatch arms in both mul_mv
and mul_mv_id type switches, preserving their N_SG_Q2_0 and N_R0_Q2_0
assignments. Keep the newly added GGML_TYPE_TQ2_0 cases alongside them so both
quantization types are supported.
- Around line 3255-3282: In the kernel-name construction logic, update the
suffix handling in the GGML_TYPE_Q2_K and GGML_TYPE_Q3_K cases so the generated
"_r%d" suffix is stored in a dedicated buffer rather than aliasing the reusable
name buffer. Keep suffix pointing to the dedicated storage before the later
"%s_nsg=%d" formatting step, while preserving the existing GLM-down suffix
behavior.
- Around line 31371-31375: Default-initialize the indexer_types member used by
the surrounding indexer state, ensuring every element starts at -1 to represent
“unknown.” Add this initialization in the relevant constructor or ggml-style
reset helper rather than using empty-brace array initialization, which would
produce zeros; preserve indexer_types_present and the existing adjacent field
defaults.
- Around line 1101-1125: Remove the unconditional fusion-scratch allocation from
ggml_metal_init and avoid freeing uninitialized lanes in ggml_metal_free.
Allocate fusion_scratch lazily on first use, or gate initialization with the
same opt-in predicate used by the private-resource consumers
(GGML_METAL_EXPERIMENTAL_GLM_MOE_PRIVATE_SCRATCH, TWO_PHASE, and DUAL_LANE),
while preserving allocation-failure handling for enabled paths.
- Around line 1129-1137: Update ggml_metal_synchronize to status-check every
allocated command buffer through ctx->n_cb, not only the graph’s
ctx->n_cb_active count, so buffers retained from wider graphs are still
reported. Verify the cmd_bufs reset/release lifecycle before synchronization and
preserve the same full-range behavior at the other affected occurrence.

In
`@third_party/llama.cpp/patches/0023-tests-cover-native-GLM-DSA-execution-paths.patch`:
- Around line 220-221: Update the phase-gate result handling around phase_text
and the appended summary line to fail closed: do not treat arbitrary occurrences
of “OK” as success. Parse the command’s exact success marker or propagate an
explicit exit/result status, and report the gate as passed only when that
definitive success condition is met; otherwise retain the failure/inspection
message.

In
`@third_party/llama.cpp/patches/0025-Run-GLM-DSA-through-IndexShare-graph.patch`:
- Around line 18-33: Validate the staged range immediately after computing
il_start and il_end, requiring 0 <= il_start < il_end <= effective_n_layers
before any layer indexing or stage-boundary use. Reject invalid or empty ranges
consistently, then keep the existing full-indexer and split K_B/V_B validation
for the validated range.
- Around line 29-33: Update the GLM-DSA validation and graph construction around
the loop checking model.layers[il].wk_b and wv_b to accept either split K_B/V_B
tensors or the fused wkv_b tensor. Select the split or fused graph path based on
which tensors are loaded, and only reject a layer when neither valid
representation is available.

In
`@third_party/llama.cpp/patches/0032-tests-add-GLM-Q2Q3-selected-weight-roofline.patch`:
- Around line 32-38: Ensure the GLM roofline variants registered by
make_test_cases_eval() apply the same
GGML_METAL_ENABLE_GLM_MOE_DECODE_MOTIF_REFERENCE,
GGML_METAL_EXPERIMENTAL_GLM_MOE_Q2_WEIGHT_ROOFLINE_CHUNKS, and bypass-route
scoped_test_env settings during evaluation. Add an eval() override alongside
eval_perf() that preserves the bypass_route value and delegates to the base test
evaluation, or remove these variants from make_test_cases_eval().

In
`@third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch`:
- Around line 22-48: Route active-count Q3 kernels through the specialized entry
points instead of the generic nsg=8 pipeline. In
third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch
lines 22-48, update
ggml_metal_library_get_pipeline_mul_mv_id_q3_weighted_reduce_slots_sg_r8_nb8_w0_active
to compile kernel_mul_mv_id_q3_K_wr_slots_r8_a6, a4, or a2 based on
active_slots; lines 184-195 must call the corresponding active6/active4/active2
pipeline getters, and lines 252-297 must register those active entry-point
symbols with the pipeline builder. In
third_party/llama.cpp/patches/0034-ggml-use-GLM-max-active-policy-for-Q3-down-kernels.patch
lines 75-83, select the matching active getter for each q3_active_count value.
- Around line 397-408: Scope
GGML_METAL_ENABLE_Q3_DOWN_SLOT_PARALLEL_REDUCE_ACTIVE6/4/2 around the added
active-count test registrations for test_glm_moe_static_motif and
test_glm_moe_down_reduce in
third_party/llama.cpp/patches/0033-ggml-add-active-count-Q3-GLM-MoE-down-kernels.patch
ranges 397-408 and 416-426, covering both eval and perf cases. In
third_party/llama.cpp/patches/0034-ggml-use-GLM-max-active-policy-for-Q3-down-kernels.patch
range 91-103, preserve the max-active policy cases without disabling or
overriding the environment settings needed to exercise that coverage.

In
`@third_party/llama.cpp/patches/0042-Harden-staged-session-and-sideband-bookkeeping.patch`:
- Around line 93-95: Validate request_count against INT32_MAX and return
SKIPPY_STATUS_INVALID_ARGUMENT before assigning it to the int32_t n_tokens
variable; remove the GGML_ASSERT-based narrowing check and preserve the existing
llama_batch initialization for valid counts.

In
`@third_party/llama.cpp/patches/0046-Fix-empty-transposed-KV-page-import-and-export.patch`:
- Around line 144-176: Make the K-only roundtrip test around
stage_import_kv_page non-vacuous by importing the exported payload into a fresh
cache or after clearing/overwriting the destination page. Ensure the subsequent
stage_export_kv_page comparison verifies that import restored the data from
payload rather than returning unchanged cached contents.

---

Nitpick comments:
In
`@third_party/llama.cpp/patches/0010-Add-MTP-execution-support-and-sampling-cleanup.patch`:
- Around line 4029-4046: Remove the MTP metadata packing from
skippy_verify_tokens_frame_sampled’s output_tokens buffer and expose the draft
through a dedicated skippy_native_mtp_draft out-parameter, matching
skippy_decode_step_frame_sampled_mtp. Update the function signature and all
callers to pass and consume this structured result, while keeping output_tokens
limited to actual verified tokens and preserving proposal_compute_us precision.

In
`@third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch`:
- Around line 6902-6965: Document the ownership invariant at
set_fused_range_outputs: callers must first validate through
can_fuse_filtered_subgraph or the relevant can_fuse* checks that untracked fused
intermediates have no consumers or view sources outside the fused span. Keep the
implementation unchanged and place the concise contract comment directly with
the fused-range output mechanism.
- Around line 5766-6320: The GLM-DSA Metal dispatch configuration is too
difficult to audit because numerous overlapping environment predicates select
mutually exclusive kernel variants without visibility. Consolidate or remove
obsolete Q2/Q3 gate-up and down-projection flags and variants that lost their
bake-off, enforce mutually exclusive selections, and use the existing dispatch
logger to emit a one-time summary of the resolved configuration at first encode,
centered on the predicate helpers such as
ggml_metal_glm_dsa_q2_gate_up_swiglu_pair_sg_any_variant_enabled and the
relevant encode/dispatch path.
- Around line 1298-1305: Validate the SKIPPY_GLM_DSA_METAL_PROFILE_N_CB override
in ggml_metal_set_n_cb before replacing the caller’s n_cb: parse it strictly,
clamp valid values to [1, GGML_METAL_MAX_COMMAND_BUFFERS], and retain the
caller-provided value for malformed or non-positive input. Log whenever the
environment override is applied, including the effective value.
- Around line 137-218: Update ggml_backend_sched_glm_route_diag_enabled to cache
the diagnostic environment lookup result instead of calling getenv on every
invocation, while preserving both supported environment variables and their
precedence. In ggml_backend_sched_log_glm_route_splits, guard the snprintf
offset update against truncation by only advancing offset when the written
length fits the remaining buffer; otherwise terminate the operation-string
construction safely without allowing offset to exceed sizeof(ops).
- Around line 1341-1540: Cache the parsed environment-variable results in
function-local static const values for every ggml_metal_glm_dsa_*_requested()
and *_enabled() helper in the 1341-1540 range, preserving each helper’s current
fallback and validation behavior. Also update
ggml_backend_sched_glm_route_diag_enabled() in
third_party/llama.cpp/patches/0021-ggml-add-GLM-DSA-sparse-execution-primitives.patch
lines 137-218 to cache its result, eliminating repeated getenv/atoi calls while
retaining existing configuration semantics.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a609078d-a921-4209-a4a2-c4090aa28ef3

📥 Commits

Reviewing files that changed from the base of the PR and between a878919 and 7b89aa7.

📒 Files selected for processing (48)
  • 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-ggml-metal-make-small-batch-matmul-batch-invariant.patch
  • third_party/llama.cpp/patches/0045-Fix-GLM-DSA-Metal-dispatch-safety.patch
  • third_party/llama.cpp/patches/0046-Fix-empty-transposed-KV-page-import-and-export.patch
  • third_party/llama.cpp/patches/0047-Scope-GLM-DSA-backend-test-environment.patch
  • third_party/llama.cpp/upstream.txt

@ndizazzo ndizazzo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm good with this, but we should consider implementing the changes suggested from @CodeRabbit before merging. I have an agent working on this currently... please stand by

@i386
i386 merged commit face081 into main Jul 28, 2026
39 of 52 checks passed
@i386
i386 deleted the agent/refresh-llama-patch-queue branch July 28, 2026 21:05
ndizazzo added a commit that referenced this pull request Jul 28, 2026
@ndizazzo
ndizazzo restored the agent/refresh-llama-patch-queue branch July 28, 2026 21:06
michaelneale added a commit that referenced this pull request Jul 29, 2026
…-retention

* origin/main:
  Refresh llama.cpp upstream patch queue (#1105)
  Revert "Refresh llama.cpp upstream patch queue (#1099)"
  Refresh llama.cpp upstream patch queue (#1099)
  chore(code-quality): generalized code-quality refactoring (#1098)
  Document canonical Homebrew tap (#1102)
  Rename the Node SDK npm package scope (#1101)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants