Archived
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:
1 parent
731a23a65a
commit
74e701ea17
8 files changed
+117
-1
No files matched your search
+9
-1
@@ -292,9 +292,10 @@ CacheHit PrefixCache::lookup(const std::vector<int32_t>& tokens,
|
||||
std::vector<BlockHash> hs = chain_all(tokens, mm, n_full);
|
||||
|
||||
int64_t matched = 0;
|
||||
bool departed = false; /* the prompt goes on where nothing is cached */
|
||||
for (; matched < n_full; ++matched) {
|
||||
auto it = map_.find(hs[(size_t)matched]);
|
||||
if (it == map_.end()) break;
|
||||
if (it == map_.end()) { departed = true; break; }
|
||||
/* An off-device entry is not a hit: demotion released its blocks and its recorded ids now
|
||||
* belong to other sequences. The prefix stops here (also while a promotion is in flight);
|
||||
* the caller may promote and look up again (restorable_from). */
|
||||
@@ -302,6 +303,9 @@ CacheHit PrefixCache::lookup(const std::vector<int32_t>& tokens,
|
||||
if (!same_tokens(it->second, tokens.data() + matched * block_size_)) break;
|
||||
}
|
||||
|
||||
if (departed && matched > 0 && map_.find(hs[(size_t)matched - 1])->second.n_children >= 2)
|
||||
hit.branch_at = (int32_t)(matched * block_size_);
|
||||
|
||||
/* Back a full hit off by one block so one token remains to compute a logit from. */
|
||||
if (matched * block_size_ >= (int64_t)tokens.size() && matched > 0) --matched;
|
||||
|
||||
@@ -435,6 +439,10 @@ int PrefixCache::insert(uint64_t session,
|
||||
e.lru = std::prev(lru_.end());
|
||||
e.in_lru = true;
|
||||
map_.emplace(h, std::move(e));
|
||||
if (b > 0) {
|
||||
auto p = map_.find(hs[(size_t)b - 1]);
|
||||
if (p != map_.end() && p->second.n_children < 255) ++p->second.n_children;
|
||||
}
|
||||
}
|
||||
/* Deepest first, as in lookup: the first block ends most recent. */
|
||||
for (int64_t b = n_full - 1; b >= 0; --b) touch(hs[(size_t)b], now);
|
||||
|
||||
Reference in new issue
Block a user