Repository navigation
feat(metal): boundary-event schedule for paged decode - #5
Merged
Merged
Conversation
Add a public C-API accessor returning a context's ggml_backend_sched_t, so downstream callers can enumerate per-backend compute buffer sizes via the existing public ggml_backend_sched_* API (memory observability). Thin wrapper over the existing llama_context::get_sched(); no behavior change. Carried as a titanium-chicken fork patch on top of b9828.
Returns the scheduler's current compute graph (the one split during ggml_backend_sched_reserve, which llama runs at context construction). Its nodes are in execution order with per-node backend assignments queryable via ggml_backend_sched_get_tensor_backend, enabling read-only compute-order/placement inspection without running a forward pass. Sibling to the existing llama_get_sched() patch (titanium-chicken fork). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ode (ggml-org#226) Add a persistent, pointer-keyed boundary-event schedule to the Metal backend so an engine can drive async event-fenced KV residency without a per-boundary GPU host drain (the cb_eval ggml_backend_synchronize that dominates paged decode). - ggml_metal_set_boundary_schedule(): register cut nodes (each optionally signalling an event after its command buffer) and wait nodes (each waiting on an event before its segment), keyed by ggml_tensor* POINTER. PERSISTENT: it applies to every graph_compute until cleared (n_cuts==0), so it covers all of ggml_backend_sched's splits within one llama_decode and the reused graph of later tokens; pointer matching is robust to split-vs-full-graph index skew. n_cuts==0 leaves the stock n_cb encode path untouched. - ggml_metal_graph_compute_paged(): sequential per-boundary-committed encode (no dispatch_apply) that, for whichever schedule nodes appear in the graph it is handed, emits encodeSignalEvent after each boundary's command buffer and encodeWaitForEvent before an admitted region's first-reader segment. Reuses ggml_metal_op per segment (fusion preserved). A graph with no schedule nodes encodes as one segment. - Explicit-value event primitives: ggml_metal_event_encode_{signal,wait}_value and host-side ggml_metal_event_host_{signal,wait,value} so an engine reclaim/prefetch thread can coordinate an MTLSharedEvent across threads. - ggml_metal_get_device(): the backend's own device (ggml_metal_device_get() returns a fresh, unrelated device each call, whose events never reach the queue) -- callers must init boundary events on this device. Verified by a standalone probe over a real ggml-metal graph: the segmented encode is bit-identical to the stock encode, every per-boundary signal fires progressively, and an admit wait fences real GPU work until a concurrent host signal from a long-lived thread releases it. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01PR4CoRiTG8d5YfpjumYJSS
…#226) Adds ggml_metal_event_signal_on_complete: a host reclaim thread waiting the boundary event must see coherent shared-storage KV bytes, so signal from the completion handler rather than mid-stream. Separate WIP commit on top of the seam (542d159) to preserve; squash when formalizing the fork PR. Had zero effect on the paging bit-identity bug (keep-or-drop). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01PR4CoRiTG8d5YfpjumYJSS
Exposes ggml_backend_metal_set_boundary_schedule plus the ggml_backend_metal_event_* primitives so an engine can drive fusion-preserving, event-fenced KV paging instead of a whole-graph host synchronize at each cut. Claude-Session: https://claude.ai/code/session_01GWUngsbi1CQNi4cBrmNFCG Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
wywen
marked this pull request as ready for review
July 12, 2026 22:26
wywen
commented
Jul 12, 2026
wywen
left a comment
Owner
Author
There was a problem hiding this comment.
Remove comments on top of the forward declarations like the rest of the files please, also do not mention github issue numbers or other repositiories.
Comment on lines
+331
to
+337
| // Get the scheduler's current compute graph (the last one split/reserved). | ||
| // Its nodes are in execution order; each node's assigned backend is | ||
| // queryable via ggml_backend_sched_get_tensor_backend. Valid after | ||
| // ggml_backend_sched_reserve (which llama runs at context construction); | ||
| // before any graph is split the returned graph has n_nodes == 0. The | ||
| // scheduler owns the graph — do not free or mutate it. Never NULL for a | ||
| // valid sched. (titanium-chicken: memory/scheduler observability.) |
Remove the multi-line block comments above the newly-added forward declarations to match the terse style of the surrounding headers, and strip references to external trackers/repositories from the remaining in-body comments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ggml_metal_set_boundary_schedule) and the per-command-buffer completion event machinery inggml-metal-context/ggml-metal-device.ggml_backend_metal_set_boundary_scheduleplusggml_backend_metal_event_init/free/host_signal/host_wait/host_value, so an engine outside ggml can install cut points and fence them with GPU events instead of a whole-graph host synchronize.Test plan
l_outboundary, fencing paging admits/evicts with these events. Armed (paging) decode is bit-identical to the unarmed control with Metal op fusion left enabled.