From f2c892761a7fbd7d71096506b4cd83f20e93f55d Mon Sep 17 00:00:00 2001 From: Constantin Date: Fri, 23 Oct 2026 09:55:58 -0400 Subject: [PATCH] mem: preserve snapshot-backed prefix KV across recomputes When a stateful model's cached prefix is host-resident and another request recomputes shared tokens without restoring that entry, PrefixCache::insert can replace KV still referenced by an older recurrent checkpoint. Preserve the first cached copy for snapshot-backed prefixes regardless of tier, while retaining rebinding behavior for non-stateful caches. Add a regression covering a recomputed shared prefix, the original host copy, and its recurrent snapshot. Tests: `ctest --test-dir build-gfx1201 -L '^fast$' --output-on-failure --parallel 24` (96/96 passed). --- core/mem/prefix.cpp | 5 ++++- tests/mem_test.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/core/mem/prefix.cpp b/core/mem/prefix.cpp index ccd86acf..a899f0ad 100644 --- a/core/mem/prefix.cpp +++ b/core/mem/prefix.cpp @@ -368,7 +368,10 @@ int PrefixCache::insert(uint64_t session, for (int64_t b = 0; b < n_full; ++b) { const BlockHash& h = hs[(size_t)b]; auto it = map_.find(h); - if (it != map_.end() && it->second.in_lru) continue; /* touched below */ + /* A snapshot-backed prefix keeps its first bytes even in the host tier: a short + * recompute can round the KV differently, while older conversations' recurrent snapshots + * still depend on the original attention KV. Non-stateful caches may rebind below. */ + if (it != map_.end() && (it->second.in_lru || has_linear_)) continue; /* touched below */ if (it != map_.end()) { /* An off-device entry this request recomputed (a restore that did not fit, or a chain * cut short). Its blocks become the entry's device copy and the tier copy goes diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index 6feb14ca..4a160a39 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -3358,6 +3358,48 @@ TEST(dropping_a_clean_entry_gives_its_vram_back_and_keeps_it_restorable) { CHECK_EQ(x.f.pc.drop_clean(), 0); /* nothing left to drop */ } +TEST(a_recomputed_shared_prefix_keeps_its_original_host_copy_and_snapshot) { + /* A short request shares less than one checkpoint with an off-device conversation, so it + * recomputes instead of adopting that prefix. Its different prefill shape can round the KV + * differently. Publishing it must not replace the original bytes underneath the longer + * conversation's still-valid recurrent snapshot. Device-resident entries already keep the + * first copy; host-resident entries must obey the same rule. */ + TierFixture x; + CHECK_OK(x.build(/*with_linear=*/true, /*ckpt_slots=*/4)); + const auto prompt = tok_run(7600, 336); + const auto keys = chain_of(prompt); + publish(x.f, 1, prompt, 320); + const int32_t snapshot = ckpt_at(x.f, 1, keys, 320); + x.f.kv.free_sequence(1); + + const auto copied = copy_now(x.t); + REQUIRE_EQ(copied.size(), 20u); + const int32_t host_slot = copied[0].host_slot; + REQUIRE(host_slot >= 0); + std::memset(x.t.arena().at(host_slot), 0x5a, 1024); + CHECK_EQ(x.f.pc.drop_clean(), 20); + CHECK_EQ(x.t.arena().used_slots(), 20ll); + + std::vector short_prompt(prompt.begin(), prompt.begin() + 32); + const auto tail = tok_run(9600, 32); + short_prompt.insert(short_prompt.end(), tail.begin(), tail.end()); + CHECK_EQ(x.f.pc.lookup(short_prompt, {}).n_linear_tokens, 0); + publish(x.f, 2, short_prompt, 64); + x.f.kv.free_sequence(2); + + CHECK(x.t.resident_off_device(keys[0])); + CHECK(x.t.resident_off_device(keys[1])); + CHECK_EQ(x.t.arena().used_slots(), 20ll); + CHECK_EQ(x.f.pc.restorable_from(prompt, {}).size(), 20u); + CHECK(x.f.kv.checkpoint_held(snapshot)); + const auto* bytes = static_cast(x.t.arena().at(host_slot)); + for (int i = 0; i < 1024; ++i) CHECK_EQ(bytes[i], 0x5a); + TierJob restore; + CHECK(x.t.plan_promote(keys[0], &restore)); + CHECK_EQ(restore.host_slot, host_slot); + x.t.report(restore, false); /* leave the unstarted restore in host memory */ +} + TEST(an_entry_a_running_sequence_reads_is_dropped_once_the_sequence_lets_go) { /* Dropping a block a sequence also holds would free nothing while the entry read off-device, * so the next lookup would restore a second copy beside it. */