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
@@ -2128,6 +2128,43 @@ TEST(prefix_cache_hits_the_shared_prefix_only) {
|
||||
CHECK_EQ(f.pc.lookup(p1, {}).n_attn_tokens, 48);
|
||||
}
|
||||
|
||||
/* Where a hit ends at a head two cached prompts already left differently (CacheHit::branch_at):
|
||||
* the same instructions in front of different texts. One cached prompt past it is not enough, the
|
||||
* next prompt could be the same conversation going on; a prompt that follows one of them further, or
|
||||
* is all cached, is at no branch. */
|
||||
TEST(a_hit_reports_the_head_two_cached_prompts_left_differently) {
|
||||
Fixture f;
|
||||
CHECK_OK(f.build(/*with_linear=*/true, /*ckpt_slots=*/4));
|
||||
const auto head = tok_run(10, 64); /* 4 blocks every prompt starts with */
|
||||
auto with_tail = [&](int32_t base) {
|
||||
auto p = head;
|
||||
for (int i = 0; i < 48; ++i) p.push_back(base + i);
|
||||
return p;
|
||||
};
|
||||
const auto a = with_tail(1000), b = with_tail(2000), c = with_tail(3000);
|
||||
publish(f, 1, a, 112);
|
||||
CacheHit h1 = f.pc.lookup(b, {});
|
||||
CHECK_EQ(h1.n_attn_tokens, 64);
|
||||
CHECK_EQ(h1.branch_at, 0);
|
||||
publish(f, 2, b, 112);
|
||||
CacheHit h2 = f.pc.lookup(c, {});
|
||||
CHECK_EQ(h2.n_attn_tokens, 64);
|
||||
CHECK_EQ(h2.branch_at, 64);
|
||||
|
||||
auto a2 = a;
|
||||
for (int i = 0; i < 32; ++i) a2.push_back(4000 + i);
|
||||
CHECK_EQ(f.pc.lookup(a2, {}).n_attn_tokens, 112);
|
||||
CHECK_EQ(f.pc.lookup(a2, {}).branch_at, 0);
|
||||
CHECK_EQ(f.pc.lookup(a, {}).branch_at, 0);
|
||||
/* Publishing the same prompt again adds no way on. */
|
||||
publish(f, 3, a, 112);
|
||||
auto d = head;
|
||||
for (int i = 0; i < 16; ++i) d.push_back(1000 + i);
|
||||
for (int i = 0; i < 32; ++i) d.push_back(5000 + i);
|
||||
CHECK_EQ(f.pc.lookup(d, {}).n_attn_tokens, 80);
|
||||
CHECK_EQ(f.pc.lookup(d, {}).branch_at, 0);
|
||||
}
|
||||
|
||||
TEST(prefix_cache_lru_eviction_order) {
|
||||
Fixture f;
|
||||
CHECK_OK(f.build(/*with_linear=*/false));
|
||||
|
||||
Reference in new issue
Block a user