Skip to content

[Native] Serve many sequences from one ggml engine KV cache - #25007

Draft
metascroy wants to merge 1 commit into
offgraph-kv-ggml-kv-poolfrom
offgraph-kv-ggml-cell-cache
Draft

metascroy wants to merge 1 commit into
offgraph-kv-ggml-kv-poolfrom
offgraph-kv-ggml-cell-cache

Conversation

@metascroy

@metascroy metascroy commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Stack

Off-graph KV cache for the native ggml engine, through serving Muse Glimmer on it. Review bottom-up; each PR builds and passes its tests on its own.

Summary

The ggml engine's off-graph cache held one sequence, so a batching runtime couldn't put several sessions in one forward, clone a prefix, or share a pool between sessions.

  • Cache: GgmlCellCache is the neutral CellCache over a GgmlKvPool whose rows are its cells, registered for kind::kBatched and kind::kBatchedCell. It offers BatchControl to the control plane.
  • Engine: a context accepts either cache through llm_cache_registry_key. With a cell cache, the executor:
    • places the forward's tokens at their positions before building the graph (the control plane declared their sequences);
    • grows storage to the step's read extent;
    • fills each window's cache rows and mask straight from the cache's step: rows are the step's cells, and the mask keeps each query to its own sequence's earlier cells within its window.
  • Windows: a windowed layer keeps every cell, so a rewind or clone never needs history the cache dropped.
  • Step lifecycle: CellCache gains end_step() and abandon_step(), which the executor calls after each forward. A forward that succeeds ends its step, so running it again without a new declaration is refused rather than re-served. One that fails after placement gives its cells back: each of the step's sequences drops to its first position in the step, so they continue where they were. Other backends don't call these, so they're unaffected.

Test plan

New engine tests against a multi-sequence reference (naive attention per sequence):

  • two sequences prefilling separately, then decoding in one forward, with and without graph caching, on fp32 and fp16 storage, starting from one row so storage regrows;
  • a clone sharing its source's prefix as both continue, plus rewinds and closing the clone;
  • sliding windows, including inside a prefill wider than the window;
  • undeclared steps and positions that don't continue a sequence are refused;
  • a sliding and a full layer served from the same cells in one two-layer forward;
  • a full pool refusing a step until a rewind frees cells;
  • a forward failing after placement (a host-side row check) gives its cells back; this fails without the undo;
  • a forward that ran can't be replayed without a declaration, so a failing replay can't undo it; this fails without end_step().

cache_test.cpp adds CellEndedStepIsNotReServed and CellAbandonedStepGivesBackItsCells.

All pass on CPU, and on Metal through a temporary local change. Making the executor ignore the cache's mask fails three of the four.


Authored with Claude Code.

@pytorch-bot

pytorch-bot Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25007

Note: Links to docs will display an error until the docs builds have been completed.

❌ 169 New Failures, 7 Unrelated Failures, 27 Unclassified Failures

As of commit 0d8ca8f with merge base c7f7931 (image):

NEW FAILURES - The following jobs have failed:

UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 13, 2026
This was referenced Nov 13, 2026
The ggml engine's off-graph cache held one sequence, so a batching runtime
could not put several sessions in one forward, clone a prefix, or share a
pool between sessions. GgmlCellCache is the neutral CellCache over a
GgmlKvPool whose rows are its cells, registered for kind::kBatched and
kind::kBatchedCell; it offers BatchControl to the control plane.

A context accepts either cache. With a cell cache, the executor places the
forward's tokens at their positions before building the graph (the control
plane declared their sequences), grows storage to the step's read extent,
and fills each window's cache rows and mask from the cache's step: rows are
the step's cells, and the mask keeps each query to its own sequence's
earlier cells within its window. A windowed layer keeps every cell, so a
rewind or clone never needs history the cache dropped. A forward that fails
after placement leaves its cells claimed, as a failed MLX forward does.

This branch had an error being deployed

1 failed deployment
cadence — 0d8ca8fe Deployed Nov 14, 2026 by metascroy via vision-build / vision #38347
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant