Skip to content

Add GLM DSA llama foundation patches - #1034

Merged
i386 merged 5 commits into
mainfrom
jd/glm-dsa-llama-foundation
Jul 23, 2026
Merged

Add GLM DSA llama foundation patches#1034
i386 merged 5 commits into
mainfrom
jd/glm-dsa-llama-foundation

Conversation

@i386

@i386 i386 commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • add the GLM-DSA llama.cpp foundation patch queue: sparse execution primitives, GLM-DSA staged controls, and native execution tests
  • add GLM-DSA fused KV_B tensor loading plus IndexShare graph wiring
  • route GLM-DSA through the DSA KV cache path

Why

This is the first clean split from the GLM-5.2 research branch. It keeps the native GLM-DSA llama.cpp foundation separate from quant tooling, package validation, correctness reporting, MoE micro-optimizations, and speculative/MTP experiments.

Validation

  • LLAMA_WORKDIR=/tmp/mesh-llm-llama.t1iPon scripts/prepare-llama.sh pinned
  • just build

Summary by CodeRabbit

  • New Features

    • Added native GLM-DSA execution with staged sparse-attention controls, top-k activation handling, and IndexShare graph support.
    • Added support for fused KV tensors and a dedicated DSA KV cache for GLM-DSA models.
    • Added configurable mmap and GLM-DSA policy settings.
  • Performance

    • Improved sparse attention, MoE decoding, compact flash, and graph execution paths.
  • Bug Fixes

    • Fixed Metal get_rows kernel placement.
    • Updated runtime compatibility requirements for the expanded ABI.

@coderabbitai

coderabbitai Bot commented Jul 21, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Failed to post review comments.

We encountered an issue with GitHub. Use @coderabbitai full review to retry the review.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3bf61313-04ca-4b2b-8241-c8d93f0253ef

📥 Commits

Reviewing files that changed from the base of the PR and between e7fc73e and 6af4462.

📒 Files selected for processing (10)
  • crates/skippy-ffi/src/lib.rs
  • crates/skippy-runtime/src/config.rs
  • third_party/llama.cpp/patches/0022-ggml-add-GLM-DSA-sparse-execution-primitives.patch
  • third_party/llama.cpp/patches/0023-skippy-expose-GLM-DSA-staged-runtime-controls.patch
  • third_party/llama.cpp/patches/0024-tests-cover-native-GLM-DSA-execution-paths.patch
  • third_party/llama.cpp/patches/0025-Support-GLM-DSA-fused-KV_B-tensors.patch
  • third_party/llama.cpp/patches/0026-Run-GLM-DSA-through-IndexShare-graph.patch
  • third_party/llama.cpp/patches/0027-Use-DSA-KV-cache-for-GLM-DSA.patch
  • third_party/llama.cpp/patches/0028-Bump-Skippy-ABI-for-GLM-DSA-runtime-config.patch
  • third_party/llama.cpp/patches/0029-Fix-GLM-DSA-Metal-get_rows-placement.patch
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/skippy-runtime/src/config.rs
⏰ Context from checks skipped due to timeout. (18)
  • GitHub Check: rust-clippy (1)
  • GitHub Check: rust-clippy (2)
  • GitHub Check: Windows ROCm
  • GitHub Check: HuggingFace download smoke / HuggingFace download smoke
  • GitHub Check: Windows CPU
  • GitHub Check: Windows CUDA
  • GitHub Check: Rust crate tests (1)
  • GitHub Check: Windows Vulkan
  • GitHub Check: Rust crate tests (0)
  • GitHub Check: Rust crate tests (2)
  • GitHub Check: Rust crate tests (3)
  • GitHub Check: macOS CPU
  • GitHub Check: Linux CUDA slim
  • GitHub Check: Linux ROCm slim
  • GitHub Check: Linux Vulkan
  • GitHub Check: Runner image / ARC arm64
  • GitHub Check: Linux CPU
  • GitHub Check: Runner image / GitHub-hosted public
🧰 Additional context used
📓 Path-based instructions (4)
**/*

📄 CodeRabbit inference engine (AGENTS.md)

**/*: Never commit credentials or private machine connection details to tracked files; keep them outside the repository.
Do not add the rejected api_key_token feature or an external llama-server/rpc-server runtime lane.
Do not leave compiler warnings in touched code and do not use #[allow(...)] to silence them without a clear reason and developer approval.
Do not commit until formatting and the basic validation appropriate to the change type have been run successfully; do not report a command complete until it exits with code 0.

Files:

  • third_party/llama.cpp/patches/0022-ggml-add-GLM-DSA-sparse-execution-primitives.patch
  • third_party/llama.cpp/patches/0028-Bump-Skippy-ABI-for-GLM-DSA-runtime-config.patch
  • third_party/llama.cpp/patches/0029-Fix-GLM-DSA-Metal-get_rows-placement.patch
  • crates/skippy-ffi/src/lib.rs
  • third_party/llama.cpp/patches/0027-Use-DSA-KV-cache-for-GLM-DSA.patch
  • third_party/llama.cpp/patches/0025-Support-GLM-DSA-fused-KV_B-tensors.patch
  • third_party/llama.cpp/patches/0026-Run-GLM-DSA-through-IndexShare-graph.patch
  • third_party/llama.cpp/patches/0023-skippy-expose-GLM-DSA-staged-runtime-controls.patch
  • third_party/llama.cpp/patches/0024-tests-cover-native-GLM-DSA-execution-paths.patch
**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

**/*.rs: Use just for repository builds; do not build manually with cargo build or cargo check as a substitute.
Avoid generic module or directory names such as app, utils, misc, common, helpers, stuff, or vague logic; name modules after their semantic responsibility.
Prefer modules inside an existing crate; add a workspace crate only for genuinely cross-cutting responsibilities or compile-time/dependency isolation, and name it after its responsibility.
Do not add Rust methods or functions exceeding the configured Clippy line-count or cognitive-complexity limits; split them into semantically named helpers.
Do not add Rust source files over 2,000 lines; split approaching oversized files by responsibility.
When modifying a Rust file already over 1,000 lines, extract any separable responsibility into a named module, keep the new file under 1,000 lines, and move or add its tests with the extracted behavior.
Before committing Rust changes, run formatting checks, the relevant crate's cargo check, and warning-denying Clippy; run commands serially.

Files:

  • crates/skippy-ffi/src/lib.rs
crates/skippy-ffi/src/**/*.rs

📄 CodeRabbit inference engine (AGENTS.md)

Keep SKIPPY_ABI_VERSION_* constants synchronized with the corresponding ABI version in skippy/common.h whenever the staged-runtime ABI changes.

Files:

  • crates/skippy-ffi/src/lib.rs
{proto/**,crates/mesh-llm-host-runtime/src/{protocol,mesh,gossip.rs},crates/skippy-ffi/src/**/*.rs}

📄 CodeRabbit inference engine (AGENTS.md)

Protocol, gossip, serialization, and Skippy ABI changes require focused tests beyond build-only validation before committing.

Files:

  • crates/skippy-ffi/src/lib.rs
📝 Walkthrough

Walkthrough

The PR adds GLM-DSA runtime configuration, staged top-k activation transport, an IndexShare graph, fused KV_B and DSA KV-cache support, expanded backend coverage, graph contract tests, reporting scripts, and a Skippy ABI patch bump.

Changes

GLM-DSA runtime integration

Layer / File(s) Summary
ABI and runtime configuration
crates/skippy-ffi/src/lib.rs, crates/skippy-runtime/src/config.rs, third_party/llama.cpp/patches/0028-...
The ABI patch level is raised to 32, runtime configuration gains mmap and GLM-DSA fields, and disabled defaults are tested.
Staged activation plumbing
third_party/llama.cpp/patches/0023-...
GLM-DSA policy controls, phase hints, top-k sidebands, payload validation, tracing, batched decode handling, and DSA KV integration are added.
IndexShare GLM-DSA graph
third_party/llama.cpp/patches/0026-...
A dedicated graph builds IndexShare selection, sparse attention, staged FFN execution, residuals, and final outputs.
Fused KV_B and DSA KV cache
third_party/llama.cpp/patches/0025-..., third_party/llama.cpp/patches/0027-...
Fused or split KV_B tensors are supported, and GLM-DSA selects the DSA KV-cache path.
Execution-path validation
third_party/llama.cpp/patches/0024-..., third_party/llama.cpp/patches/0029-...
Backend fixtures, performance tooling, reporting scripts, graph contract tests, and Metal get-rows declarations are updated.

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 clearly summarizes the main change: adding GLM DSA foundation patches for llama.cpp.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch jd/glm-dsa-llama-foundation

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 21, 2026 05:47
@github-actions
github-actions Bot requested a review from michaelneale July 21, 2026 05:49

@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: 3

🧹 Nitpick comments (1)
third_party/llama.cpp/patches/0019-ggml-add-GLM-DSA-sparse-execution-primitives.patch (1)

1032-1118: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

"Known-broken" route-pack path is shipped, not just deferred.

The function's own comment states: "Route packing only changes graph reorder grouping; it does not itself emit the fused route kernel. Keep the known-broken pack path behind an explicit unsafe debug switch until route fusion is represented by a real backend op or the encoder can reliably consume the packed subgraph." It's correctly gated behind three chained env-var checks that all default to disabled, so runtime risk is negligible, but shipping self-documented "known-broken" logic (even dead-by-default) is a maintainability smell better tracked via an issue/TODO with a target milestone rather than left as permanent debug scaffolding.

🤖 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/0019-ggml-add-GLM-DSA-sparse-execution-primitives.patch`
around lines 1032 - 1118, Remove the shipped known-broken route-pack
implementation anchored at ggml_metal_topk_moe_route_pack_len, including its
unsafe debug gating and candidate logging, and replace it with a TODO or issue
reference that records the required target milestone. Leave route packing
disabled until a real backend op or reliable packed-subgraph encoder support
exists.
🤖 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/0020-skippy-expose-GLM-DSA-staged-runtime-controls.patch`:
- Around line 1999-2004: Remove the newly added duplicate status-guarded
skippy_mtp_sync_target_tokens call, leaving the pre-existing synchronization
block unchanged so target-token sync runs only once per decode.

In
`@third_party/llama.cpp/patches/0021-tests-cover-native-GLM-DSA-execution-paths.patch`:
- Around line 784-788: Remove the undefined ffn_q3_merged_shexp check from this
separate Python heredoc, or replace it with a locally available signal such as
combined_q3_us or q3. Ensure the routed whole-graph sanity probe no longer
raises NameError when generating decision_lines.
- Around line 3369-3377: Update the tensor initialization override in
initialize_tensors to avoid direct memset on t->data, since it may reference
non-CPU backend memory. Allocate or reuse a host buffer filled with 0xa5 for
glm_decode_block_byte_ceiling_weights and zero-filled for other tensors, then
write each buffer through ggml_backend_tensor_set in both branches.

---

Nitpick comments:
In
`@third_party/llama.cpp/patches/0019-ggml-add-GLM-DSA-sparse-execution-primitives.patch`:
- Around line 1032-1118: Remove the shipped known-broken route-pack
implementation anchored at ggml_metal_topk_moe_route_pack_len, including its
unsafe debug gating and candidate logging, and replace it with a TODO or issue
reference that records the required target milestone. Leave route packing
disabled until a real backend op or reliable packed-subgraph encoder support
exists.
🪄 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: a80d059f-9b84-41ad-8bc7-c6313f5c7cc9

📥 Commits

Reviewing files that changed from the base of the PR and between efcae3e and 44f6e4f.

📒 Files selected for processing (10)
  • crates/mesh-llm-system/src/autoupdate.rs
  • crates/skippy-ffi/src/lib.rs
  • crates/skippy-runtime/src/config.rs
  • third_party/llama.cpp/patches/0019-ggml-add-GLM-DSA-sparse-execution-primitives.patch
  • third_party/llama.cpp/patches/0020-skippy-expose-GLM-DSA-staged-runtime-controls.patch
  • third_party/llama.cpp/patches/0021-tests-cover-native-GLM-DSA-execution-paths.patch
  • third_party/llama.cpp/patches/0022-Support-GLM-DSA-fused-KV_B-tensors.patch
  • third_party/llama.cpp/patches/0023-Run-GLM-DSA-through-IndexShare-graph.patch
  • third_party/llama.cpp/patches/0024-Use-DSA-KV-cache-for-GLM-DSA.patch
  • third_party/llama.cpp/patches/0025-Bump-Skippy-ABI-for-GLM-DSA-runtime-config.patch

@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.

@i386 curious - what happens when llamacpp adds something upstream WRT GLM5.2? Do we slowly strip out and adapt any of our support work to integrate it?

It seems inevitable that support will come through the base inference provider at some point, so I'm not sure what the protocol for us should be for stuff we've bolted on

@i386

i386 commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator Author

@i386 curious - what happens when llamacpp adds something upstream WRT GLM5.2? Do we slowly strip out and adapt any of our support work to integrate it?

It seems inevitable that support will come through the base inference provider at some point, so I'm not sure what the protocol for us should be for stuff we've bolted on

Yeah we slowly strip it out

@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/0019-ggml-add-GLM-DSA-sparse-execution-primitives.patch (1)

32-36: 🗄️ Data Integrity & Integration | 🔴 Critical | ⚡ Quick win

RPC_PROTO_PATCH_VERSION not bumped despite GGML_OP_COUNT change.

The static assert's own message says to update RPC_PROTO_PATCH_VERSION whenever GGML_OP_COUNT changes, and GGML_OP_COUNT moved from 101 to 107 here (6 new ops), but the diff only updates the assert's expected value — RPC_PROTO_PATCH_VERSION is left untouched. This defeats the version check's purpose: RPC clients/servers built against the old op set will silently interoperate with servers that support the new DSA/MoE ops instead of being flagged incompatible.

🐛 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/0019-ggml-add-GLM-DSA-sparse-execution-primitives.patch`
around lines 32 - 36, Update RPC_PROTO_PATCH_VERSION alongside the GGML_OP_COUNT
change in the version guard, incrementing it from 3 so RPC clients and servers
using the old operation set are rejected as incompatible.
🧹 Nitpick comments (1)
third_party/llama.cpp/patches/0019-ggml-add-GLM-DSA-sparse-execution-primitives.patch (1)

137-144: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

Repeated getenv() calls on the hot dispatch path.

Helpers such as ggml_backend_sched_glm_route_diag_enabled() (and the many analogous ggml_metal_glm_dsa_*_enabled()/*_requested() helpers throughout this file) call getenv() unconditionally on every graph split / node encode, i.e. once per token for hot ops. Since these values don't change at runtime, caching them once (e.g., static initialization) would avoid repeated syscall overhead on the decode hot path. Given the PR description flags this whole GLM-DSA lane as scaffolding to be pared down as upstream support lands, this is a minor/deferrable item rather than a blocker.

🤖 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/0019-ggml-add-GLM-DSA-sparse-execution-primitives.patch`
around lines 137 - 144, Cache environment-variable results in
ggml_backend_sched_glm_route_diag_enabled() and the analogous
ggml_metal_glm_dsa_*_enabled()/ *_requested() helpers using one-time static
initialization, so getenv() is not called on each dispatch. Preserve the
existing variable precedence and enabled-value semantics.
🤖 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/0019-ggml-add-GLM-DSA-sparse-execution-primitives.patch`:
- Around line 32-36: Update RPC_PROTO_PATCH_VERSION alongside the GGML_OP_COUNT
change in the version guard, incrementing it from 3 so RPC clients and servers
using the old operation set are rejected as incompatible.

---

Nitpick comments:
In
`@third_party/llama.cpp/patches/0019-ggml-add-GLM-DSA-sparse-execution-primitives.patch`:
- Around line 137-144: Cache environment-variable results in
ggml_backend_sched_glm_route_diag_enabled() and the analogous
ggml_metal_glm_dsa_*_enabled()/ *_requested() helpers using one-time static
initialization, so getenv() is not called on each dispatch. Preserve the
existing variable precedence and enabled-value semantics.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2678def4-2ed9-4a4c-af6f-0abe177aa1b7

📥 Commits

Reviewing files that changed from the base of the PR and between 82a140d and e7fc73e.

📒 Files selected for processing (4)
  • third_party/llama.cpp/patches/0019-ggml-add-GLM-DSA-sparse-execution-primitives.patch
  • third_party/llama.cpp/patches/0020-skippy-expose-GLM-DSA-staged-runtime-controls.patch
  • third_party/llama.cpp/patches/0021-tests-cover-native-GLM-DSA-execution-paths.patch
  • third_party/llama.cpp/patches/0026-Fix-GLM-DSA-Metal-get_rows-placement.patch
🚧 Files skipped from review as they are similar to previous changes (2)
  • third_party/llama.cpp/patches/0020-skippy-expose-GLM-DSA-staged-runtime-controls.patch
  • third_party/llama.cpp/patches/0021-tests-cover-native-GLM-DSA-execution-paths.patch

@i386
i386 force-pushed the jd/glm-dsa-llama-foundation branch from e7fc73e to 6af4462 Compare July 23, 2026 20:58
@i386
i386 merged commit e92422f into main Jul 23, 2026
33 checks passed
@i386
i386 deleted the jd/glm-dsa-llama-foundation branch July 23, 2026 21:26
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