Add the off-graph KV-cache cell layout to the neutral C++ layer - #21902
Open
kiymetakdemir wants to merge 1 commit into
Open
Add the off-graph KV-cache cell layout to the neutral C++ layer#21902kiymetakdemir wants to merge 1 commit into
kiymetakdemir wants to merge 1 commit into
Conversation
kiymetakdemir
requested review from
larryliu0820 and
mergennachin
as code owners
August 17, 2026 22:53
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/21902
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 12796fa with merge base c461421 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
kiymetakdemir
force-pushed
the
kvcache-cell-faces
branch
from
August 17, 2026 22:59
b2df2c2 to
12796fa
Compare
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
Adds the cell layout to the neutral C++ cache. Cells are addressed absolutely: each holds a position and a bitset of the sequences that own it, so a sequence need not be contiguous, a fork sets a second bit instead of copying K/V, and a cell frees when its bitset empties.
plan()places a step's tokens once and every later layer of that forward reuses the placement. It classifies: a single sequence appending a contiguous run at the tail of a window it owns outright gets a fused kind and a run write; anything else— multi-sequence, forks, holes from removals — gets each token's cell plus a dense mask. Layers may window differently, so the mask is memoized per policy.
A step is declared before the forward through
begin_step, which is also the admission gate: it reports whether the tokens fit before any compute is spent. A step may only extend its sequences, and one that would rewrite a position a sequence still holds is refused rather than stored twice.Nothing constructs a
CellCacheyet — this is the neutral layer only. The existingSequenceCacheis untouched behaviourally.Files
extension/llm/cache/cache.h—CacheControlfactored out ofSequenceControlto holdcan_extend/capacity/clear; newBatchControl(the sequence verbs) andCellPlannerfaces;MaskKind; the fouras_*accessors now default tonullptrso a cache exposes only the faces it has.extension/llm/cache/cell_cache.h— new.CellStepPlan,CellPlanner,CellCache: placement, the seq verbs, classification, and the per-policy mask.extension/llm/cache/test/cache_test.cpp— 9 cell tests.Testing
cmake --build cmake-out --target extension_llm_cache_test && ctest --test-dir cmake-out -R extension_llm_cache --output-on-failureCovers the fused path, a second sequence forcing an explicit mask, one plan shared
across layers, a fork sharing cells and the refcounted free, ranged removal, holes
giving up the fused path, a rejected rewrite, layers sharing a window sharing a
plan, and the step protocol.