Skip to content

feat(metal): boundary-event schedule for paged decode - #5

Merged
wywen merged 7 commits into
masterfrom
feat/metal-boundary-schedule
Jul 12, 2026
Merged

wywen merged 7 commits into
masterfrom
feat/metal-boundary-schedule

Conversation

@wywen

@wywen wywen commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Summary

  • Two WIP commits already on this branch add the persistent, pointer-keyed boundary schedule (ggml_metal_set_boundary_schedule) and the per-command-buffer completion event machinery in ggml-metal-context/ggml-metal-device.
  • This PR's own commit exposes the public API: ggml_backend_metal_set_boundary_schedule plus ggml_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

  • Consumed by titanium-chicken to segment a quantized-KV decode graph uniformly at every l_out boundary, fencing paging admits/evicts with these events. Armed (paging) decode is bit-identical to the unarmed control with Metal op fusion left enabled.

Yi Wen Wong and others added 5 commits June 27, 2026 18:21
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
wywen marked this pull request as ready for review July 12, 2026 22:26

@wywen wywen left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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 thread ggml/include/ggml-backend.h Outdated
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.)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

remove comment

wywen and others added 2 commits July 12, 2026 15:33
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.
@wywen
wywen merged commit 3591a3c into master Jul 12, 2026
13 of 30 checks passed
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.

1 participant