Repository navigation
[Native] Serve kvcache::update_and_attend in the ggml engine - #24637
Merged
Merged
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/24637
Note: Links to docs will display an error until the docs builds have been completed. ⏳ 1 Pending, 2 Unrelated FailuresAs of commit 9a11bf5 with merge base 94313c9 ( BROKEN TRUNK - The following jobs failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This was referenced Nov 5, 2026
metascroy
force-pushed
the
offgraph-kv-ggml-engine
branch
from
November 5, 2026 22:29
2f88227 to
ab17a5f
Compare
metascroy
force-pushed
the
offgraph-kv-ggml-engine
branch
from
November 11, 2026 18:05
ab17a5f to
8128a44
Compare
Contributor
|
@claude /pr-review |
metascroy
force-pushed
the
offgraph-kv-ggml-engine
branch
from
November 17, 2026 23:02
c2076be to
b4658f1
Compare
The ggml engine now runs graphs exported with an off-graph KV cache. - GgmlSequenceCache is the ggml SequenceCache: the neutral sequence controller plus K/V storage per layer in the engine's backend buffer. Storage starts at the config's initial_capacity rows and doubles toward capacity, so short sequences do not pay for the full context. - register_ggml_cache_builders() registers it with the CacheFactory under backend id NativeGgml, kind single. - A context created with the llm_cache_registry_key option resolves the cache from the CacheRegistry. - update_and_attend lowers to ggml_set_rows into the cache and flash_attn_ext over the filled prefix, with per-step rows and causal mask uploads. Before each call the executor checks that the positions start at the sequence's length (re-running positions takes an explicit rewind), grows the cache, rebinds storage (dropping cached graphs on a regrow), and plans every layer; it commits after the call succeeds. Flat layers, batch 1, and fp32/fp16 storage only.
metascroy
force-pushed
the
offgraph-kv-ggml-engine
branch
from
November 18, 2026 00:06
b4658f1 to
9a11bf5
Compare
This branch was previously deployed
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.
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 native ggml engine can now run graphs exported with an off-graph KV cache (
kvcache::update_and_attend).Cache:
GgmlSequenceCache(new,GgmlCache.{h,cpp})SequenceCache, so it uses the neutral sequence controller (SequenceControl/SequencePlanner). It adds K/V storage per layer, laid out[head_dim, rows, n_kv_heads]in the engine backend's default buffer type.initial_capacityrows and doubles towardcapacity, copying existing rows across. Short sequences don't pay for the full context.Storage. An executable holds the storage its graphs point into until it rebinds, so a regrow by another executable never leaves dangling tensors behind.SequenceCache, it isn't thread-safe. Contexts bound to one cache must be serialized;create_context_with_optionsdocuments this.register_ggml_cache_builders()registers it with theCacheFactoryunder backend idNativeGgml, kindsingle. Registration is an explicit call rather than a static initializer, so a static link can't drop it.Engine
GgmlEngineHostimplementscreate_context_with_options. Thellm_cache_registry_keyoption resolves the cache from theCacheRegistry. A missing key or an unknown option throws.update_and_attendlowers toggml_set_rowsinto the layer's cache, a view of the filled prefix, andggml_flash_attn_extwith an F16 causal mask. The rows and mask are per-step uploads shared by all layers.How many cache rows to attend is part of the graph cache key. It's
max(position) + 1, rounded up to 256 and capped at the allocated rows.HostIntEvaluatorlearnsselect.intso it can evaluate HF'sposition_ids[0].Before each call, the executor reads the positions once on the host, then:
[T]or[T, 1]), consecutive, fitting the capacity, and starting at the sequence's length. One past the length would attend rows nothing wrote, and running earlier positions again takes an explicit rewind through the cache's control, as DFlash does;It commits only after the call succeeds. A step starts at the length, so whatever a failed call wrote lies past it, and the sequence is unchanged.
An F16 step written into an F32 cache is converted first:
ggml_set_rowsdoesn't convert F16 to F32 on every backend (CUDA aborts, Metal falls back to CPU).The cache builder reports a geometry or config the engine can't serve through
CacheFactory::build's error result rather than an exception.Limits: flat layers only (sliding-window layers are rejected), batch 1, fp32 or fp16 storage.
Test plan
New
test_ggml_engine.cpptests. Each runsupdate_and_attendagainst a naive softmax attention over everything the cache should hold:OffGraphAttentionMatchesReferenceAsTheCacheGrows:OffGraphAttentionReplaysFromEmptyAfterClear, including an executable whose binding went stale because another one regrew the cache.OffGraphAttentionContinuesFromARewind.OffGraphStepsStartAtTheLength: re-running positions without a rewind, or skipping past the length, is rejected; after a rewind they run again.FailedOffGraphStepLeavesTheSequence: a step that fails on a host-side row check leaves the sequence's length where it was.RejectsOffGraphStepsPastCapacityOrNotConsecutive.OffGraphAttentionNeedsAMatchingCache: no key, an unknown key, an unknown option, a layer out of range, and K/V shaped unlike the cache.Checked that the tests catch real bugs: skipping the row copy on regrow fails 3 of them, dropping the causal mask fails 4, and dropping the start-position check fails 2.
native_ggml_engine_test: 43 passed.GpuUnavailableOnCpuVariantfails as it does onmain, because this build has Metal enabled.