prefix cache: a snapshot at a head other prompts share

A model with snapshots (recurrent state, or a sliding window the cache does not index) resumes a
hit only at a snapshot, and those are taken on the checkpoint interval and at each prompt's tail.
A head every request starts with -- the same system prompt or instructions in front of a different
text, as a translation service sends its pieces -- is usually shorter than an interval, and each
tail is its own, so the head was computed again by every request.

The cache now counts the blocks published right after each block (Entry::n_children) and reports
where a lookup leaves a head that at least two cached prompts already left differently
(CacheHit::branch_at; one is not enough, the next prompt could be the same conversation going on).
When nothing resumes within kSharedHeadMin (256) tokens of that point, admission sets the request's
own checkpoint there (Request::checkpoint_at, which n > 1 already uses), and the requests after it
skip the head.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-27 18:17:21 +01:00
1 parent 731a23a65a
commit 74e701ea17
8 files changed
+117 -1

No files matched your search

+8
View File
@@ -88,6 +88,11 @@ struct CacheHit {
int32_t n_linear_tokens = 0; /* linear state reused; <= n_attn_tokens, a checkpoint multiple */
int32_t checkpoint_slot = -1; /* the snapshot holding that state, -1 if none */
int32_t given_back = 0; /* reusable tokens a HitCap took off the whole hit */
/* Where the prompt leaves a head at least two cached prompts already left differently (the
* same instructions in front of different texts), or 0. One cached prompt past it is not
* enough: the next could be the same conversation going on. A snapshot there serves every
* request that starts with that head (Scheduler::admit_one). Uncapped by a HitCap. */
int32_t branch_at = 0;
};
/* Ends a hit early on purpose: at or below `tokens`, on a block and (with recurrent state) on the
@@ -405,6 +410,9 @@ private:
* off-device entry owns no block, so eviction must not reach it. */
bool in_lru = false;
bool drop_wait = false; /* in drop_wait_: clean, but a sequence still reads it */
/* Blocks ever published right after this one, saturating: the ways prompts went on from
* this head (CacheHit::branch_at). An evicted child still counts. */
uint8_t n_children = 0;
/* Last hit or publish, monotonic clock. Every path that moves an entry to the LRU's back
* stamps it, so the list is in this order and expiry stops at the first entry it keeps. */
uint64_t used_ns = 0;