diff --git a/core/mem/prefix.cpp b/core/mem/prefix.cpp index a4a450e7..da2a2c9a 100644 --- a/core/mem/prefix.cpp +++ b/core/mem/prefix.cpp @@ -292,9 +292,10 @@ CacheHit PrefixCache::lookup(const std::vector& tokens, std::vector 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& 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); diff --git a/core/mem/prefix.h b/core/mem/prefix.h index c3301bfc..b0a6016a 100644 --- a/core/mem/prefix.h +++ b/core/mem/prefix.h @@ -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; diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 9f6507c9..1664614f 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -1209,6 +1209,17 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) { } } + /* A head other prompts share (CacheHit::branch_at) that no snapshot lets this request + * skip: it takes one there on its way past (Request::checkpoint_at), and every later + * request with the same head skips it -- the same system prompt or instructions in front + * of a different text, as TranslateGemma's chunks are. The interval and tail snapshots + * miss it: the head is shorter than an interval, and each prompt's tail is its own. */ + if (geo_.checkpoints() && r.checkpoint_at == 0 && h.branch_at - reuse >= kSharedHeadMin) { + r.checkpoint_at = h.branch_at; + RAD_DEBUG("admit %llu: a head other prompts share ends at %lld; a snapshot there", + (unsigned long long)r.id, (long long)h.branch_at); + } + r.n_computed = reuse; r.n_cached = reuse; s.ckpt_src = (reuse > 0 && reuse == h.n_linear_tokens && diff --git a/core/sched/scheduler.h b/core/sched/scheduler.h index d30eafc3..5efb9ee7 100644 --- a/core/sched/scheduler.h +++ b/core/sched/scheduler.h @@ -427,6 +427,9 @@ public: /* plan_chunk's tail-checkpoint rule (see the definition). */ int64_t tail_checkpoint(int64_t target) const; + /* The least recompute a snapshot at a shared head (CacheHit::branch_at) must save each later + * request for the chunk split that takes it. */ + static constexpr int64_t kSharedHeadMin = 256; /* Visible for tests: the chunk the scheduler would give a request at `n_computed` under a * token budget, and the checkpoint position it will land on (-1 for none). `keep_at` is the * request's own checkpoint (Request::checkpoint_at), 0 for none. */ diff --git a/docs/GUIDE.md b/docs/GUIDE.md index 7b52a3a2..7beb4407 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -1162,6 +1162,12 @@ just retaining the tip afterward. This restores tail-only snapshot scheduling an avoids the extra copies, at the cost of full re-prefill if an edit precedes the tip. Recurrent-state models still checkpoint at intervals under `tip-only`; their existing scheduling is unchanged. +Both kinds also take a snapshot at a head other prompts share: when a prompt leaves a cached +head that at least two earlier prompts already left differently (the same system prompt or +instructions in front of a different text, as a translation service sends its pieces) and no +snapshot lets it skip at least 256 tokens of that head, the request takes one where the head +ends, at the cost of one chunk split, and the requests after it resume there. A conversation's +next turn has only its own earlier turn past that point, so it splits nothing extra. **One prefill sequence in two pieces.** At two ranks, on `qwen35_fp8` (and `qwen35_w8a8`, built from it, where its norms do not write rotated int8 codes) and on `gemma3`'s MXFP4 trunk with the diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index 159c18a8..41df9c91 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -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)); diff --git a/tests/sched_fakes.h b/tests/sched_fakes.h index 8dc3cce0..5ed703fb 100644 --- a/tests/sched_fakes.h +++ b/tests/sched_fakes.h @@ -362,6 +362,7 @@ struct FakeKV : IKVManager { struct FakePC : IPrefixCache { FakeKV* kv = nullptr; int32_t attn = 0, lin = 0, src = -1; + int32_t branch_at = 0; /* CacheHit::branch_at, as the real cache reports it */ int32_t next_slot = 0; int32_t reserved = 0; int64_t evicted = 0, ckpt_evicted = 0; @@ -407,6 +408,7 @@ struct FakePC : IPrefixCache { (*out)[i] = seeded[i]; } CacheHit h{ attn, lin, src }; + h.branch_at = branch_at; const int64_t whole = src >= 0 ? lin : 0; if (cap && attn > cap->tokens / 16 * 16 && whole > cap->tokens) { const int64_t a = cap->tokens / 16 * 16; diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index fdc88ce1..caead36d 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -434,6 +434,47 @@ TEST(tip_only_window_prefill_writes_only_the_tail_snapshot) { CHECK_EQ(f.sch.metrics().checkpoints_written, 1ll); } +/* A hit that ends at a head other prompts share (CacheHit::branch_at) and resumes nothing there: + * the request takes a snapshot at that point (Request::checkpoint_at), so the requests after it + * skip the head. TranslateGemma's chunks: the same instructions, then a different text each. Not + * when a snapshot already resumes within Scheduler::kSharedHeadMin of it, nor for an ordinary hit. */ +TEST(a_hit_at_a_shared_head_takes_a_snapshot_there) { + { /* a window model, tip-only: the head's snapshot comes in addition to the tail's */ + Fixture f; + f.build(4096, 64, 512, 4096, /*windowed=*/true); + f.prog.kv_groups.erase(f.prog.kv_groups.begin() + 1, f.prog.kv_groups.begin() + 3); + f.prog.ops.resize(1); + f.prog.buffers.resize(1); + f.kv.setup(f.prog.kv_groups, f.cfg.max_ctx, 4096, 64); + f.pc.groups = { 0 }; + f.cfg.checkpoint_policy = "tip-only"; + REQUIRE_EQ(f.start(), RAD_OK); + f.pc.seed(640); + f.pc.branch_at = 640; + const uint64_t a = f.submit(1100, 1); + CHECK(f.sch.step() != nullptr); + CHECK_EQ(f.sch.find(a)->n_computed, (int64_t)0); + CHECK_EQ(f.sch.find(a)->checkpoint_at, (int64_t)640); + f.commit_plain(); + for (int i = 0; i < 8 && f.sch.step(); ++i) f.commit_plain(); + CHECK_EQ(f.sch.metrics().checkpoints_written, 2ll); + const std::vector want = { 640, 1072 }; + CHECK(f.pc.committed == want); + } + for (int c = 0; c < 3; ++c) { /* a hybrid model, interval 256, quantum 64 */ + Fixture f; + f.build(); + CHECK_OK(f.start()); + f.pc.seed(640); + if (c == 1) { f.pc.lin = 512; f.pc.src = 3; } /* resumes 128 short: not worth a split */ + f.pc.branch_at = c == 2 ? 0 : 640; /* an ordinary hit: no branch */ + const uint64_t a = f.submit(1100, 1); + CHECK(f.sch.step() != nullptr); + CHECK_EQ(f.sch.find(a)->n_computed, (int64_t)(c == 1 ? 512 : 0)); + CHECK_EQ(f.sch.find(a)->checkpoint_at, (int64_t)(c == 0 ? 640 : 0)); + } +} + TEST(tip_only_does_not_disable_recurrent_interval_checkpoints) { Fixture f; f.build();