Add off-graph KV-cache ring policy - #21447
Conversation
🔗 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 FailuresAs of commit 984f7b2 with merge base 8001512 ( NEW FAILURES - The following jobs have failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
| int n_layers; | ||
| std::vector<LayerConfig> layers; | ||
| int initial_capacity = 512; | ||
| int max_write = 1; |
There was a problem hiding this comment.
This should probably default to something bigger for prefill
There was a problem hiding this comment.
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.
| // 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]) { |
There was a problem hiding this comment.
Private method on policy?
There was a problem hiding this comment.
Makes sense, changing it.
e8c3a3c to
f84f5f5
Compare
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
make_policy ring branch, and a ring-specific T ≤ max_write guard.
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