Skip to content

Add off-graph KV-cache ring policy - #21447

Open
kiymetakdemir wants to merge 2 commits into
pytorch:mainfrom
kiymetakdemir:kv-cache-ring
Open

Add off-graph KV-cache ring policy#21447
kiymetakdemir wants to merge 2 commits into
pytorch:mainfrom
kiymetakdemir:kv-cache-ring

Conversation

@kiymetakdemir

Copy link
Copy Markdown
Contributor

Summary

Adds the sliding-window (ring) layout policy. RingPolicy keeps a layer's last window tokens in a fixed ring of window + max_write − 1 slots — oversized so a multi-token prefill step fits without clobbering context earlier queries in the step still need — and evicts the oldest. SequenceCache dispatches windowed layers to it, so a mixed model (gemma4's alternating full/sliding-window layers) shares one logical length. Backend- and tensor-free: it emits integer write/read runs; the per-query window mask stays in the backend.

Files

  • extension/llm/cache/cache.h — LayerPolicy::Kind::Ring; max_write on CacheConfig (max tokens/step; default 1 = decode-only ring).
  • extension/llm/cache/sequence_cache.h — split_ring_runs (wrap into ≤2 runs), RingPolicy over the oversized ring (retained_from still window-based), the
    make_policy ring branch, and a ring-specific T ≤ max_write guard.
  • extension/llm/cache/test/cache_test.cpp — ring wrap/evict, mixed flat/ring shared length, rewind bounded by the window, and step-over-max_write rejected.

Testing

C++ unit tests for the ring path (wrap, eviction, mixed flat/ring, window-bounded rewind, max_write guard). Built and ran via the standard flow, all pass:

cmake -B cmake-out -DEXECUTORCH_BUILD_EXTENSION_LLM=ON -DEXECUTORCH_BUILD_TESTS=ON
cmake --build cmake-out --target extension_llm_cache_test -j
ctest --test-dir cmake-out -R extension_llm_cache --output-on-failure

@pytorch-bot

pytorch-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 2 New Failures

As of commit 984f7b2 with merge base 8001512 (image):

NEW FAILURES - The following jobs have failed:

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 Jul 28, 2026
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Comment thread extension/llm/cache/cache.h Outdated
int n_layers;
std::vector<LayerConfig> layers;
int initial_capacity = 512;
int max_write = 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should probably default to something bigger for prefill

@kiymetakdemir kiymetakdemir Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is window size in MLX (the field is optional; unset defaults to window). I can change to that or let me know if you have any other suggestion.

Comment thread extension/llm/cache/sequence_cache.h Outdated
// Splits a logical run [start, start+len) into up to two physical runs in a
// ring of `ring_size` slots (wrapping at the end). Precondition: len <=
// ring_size. Returns the run count (1 or 2).
inline int split_ring_runs(int start, int len, int ring_size, Run out[2]) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Private method on policy?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, changing it.

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.

2 participants