prefix cache: the host tier cannot hold a streamed entry, so it is off

The prefix cache's host tier and --kv-resident are exclusive, and the engine
used to find that out at the first restore. An entry covers every cached
group; a streamed group's rows are addressed by the block id its sequence
used, in that group's own pinned mirror, so a slot cannot stand in for them
and their ids cannot be given up in exchange for one.

Three defects came from ignoring that:

  * the write-back gathered the resident sibling with the streamed group's
    block id, so the host slot held another group's row (TierExec indexes
    its block vector by gi_, the entry's ids are in groups_ order);
  * demotion released the streamed group's ids too (PrefixCache::
    release_blocks walks every group), handing the mirror rows the entry
    lives in back to the allocator;
  * a restore reserved one group's ids against an entry that has two, so
    rebind returned RAD_E_SHAPE and the record was dropped -- and
    engine.cpp's failure path then released those ids against the wrong
    pools.

Measured on 2x16GB tp2, three 44220-token conversations and a replay of the
first after the third displaced it, streaming on: 20247 entries demoted,
8588 restored, replay 0 cached tokens / 16.5s against streaming off's
44160 cached / 0.4s. The copies were pure waste.

Two rules now:

  * PrefixCache::demotable() is false when any cached group streams: the
    tiers are never told about such an entry (insert), and trim_sparing no
    longer skips it as though its copy were in flight, so it is evicted
    whole with every group's ids.
  * kvstream_prefix_host_off() turns --prefix-cache-mib off at bring-up,
    before anything is sized from it, when cfg.kv_streams is set -- the
    flag asked, the architecture streams and the ring is under the context.
    A flag with no streaming behind it (0, a ring that covers the context,
    a model with no kv_resolve) keeps the tier exactly as configured.

On the cards: --kv-resident 32768 reports "no host cache ... so it is off"
and 0 (off); --kv-resident 131072 keeps 2.00 GiB / 38836 entries. The tests
live in the mem test file's own commit; the docs in the docs commit.
This commit is contained in:
C_Tamas committed 2026-10-25 07:03:03 +07:00
1 parent 8a765db488
commit 2bf1096131
6 files changed
+82 -7

No files matched your search

+34 -4
View File
@@ -1,6 +1,7 @@
/* prefix.cpp -- see prefix.h. */
#include "mem/prefix.h"
#include "kvstream/plan.h"
#include "mem/kvtier.h"
#include <algorithm>
@@ -186,6 +187,25 @@ int PrefixCache::configure(const Config& cfg, KVManager* kv, int64_t checkpoint_
p.name.c_str(), (long long)p.block_size, (long long)block_size_);
}
/* An entry is only demotable when the host tier can hold every group in it. A streamed group
* cannot be gathered: its VRAM pool is one layer of its context, so a copy per layer would move
* the last staged layer N times, and its pinned host mirror is already its cold tier
* (docs/KV-STREAMING.md §4.1). Its rows are addressed by the block id the sequence used, so the
* entry cannot hand the mirror to a slot and drop its ids either: the ids are the bytes. Such an
* entry stays a VRAM entry and is evicted whole, exactly as with no host tier.
*
* The subset is also the block-id order of a tier job and of TierExec's slot, which is what
* rebind() takes; see "the host tier cannot hold a streamed group" in prefix.h. */
tier_groups_.clear();
tier_at_.clear();
for (size_t g = 0; g < groups_.size(); ++g) {
const KVGroupPlan* p = kv->plan(groups_[g]);
if (p && !kv_tier_cached(*p)) continue;
tier_at_.push_back(g);
tier_groups_.push_back(groups_[g]);
}
demotable_ = tier_at_.size() == groups_.size();
interval_ = checkpoint_interval;
/* A window snapshot is state too: a hit is reusable only at a checkpoint, whose slot carries
* the window behind it (KVManager::has_window_snapshot). */
@@ -217,6 +237,12 @@ int PrefixCache::configure(const Config& cfg, KVManager* kv, int64_t checkpoint_
(long long)kv->checkpoint_slots(), humanb(kv->checkpoint_bytes()).c_str(),
policy_->name());
report_ += skipped;
if (!demotable_) {
/* A fact about the model, not about the tiers, so it is stated here whether or not a host
* tier is configured: with one, these entries are still never demoted. */
report_ += " a streamed group is cached, so no entry can be demoted to the host tier: a "
"streamed group's rows are its pinned mirror, addressed by block id\n";
}
return RAD_OK;
}
@@ -431,14 +457,16 @@ int PrefixCache::insert(uint64_t session,
* The ids come from the map, not from `blocks`. For an entry that already existed the loop
* above kept the cache's blocks, and `blocks` belongs to a sequence about to be freed; the
* tiers would copy from blocks reassigned to another sequence. */
if (tiers_) {
if (tiers_ && demotable_) {
for (int64_t b = 0; b < n_full; ++b) {
auto it = map_.find(hs[(size_t)b]);
if (it == map_.end()) continue;
tiers_->note_used(hs[(size_t)b], it->second.blocks, now, b);
}
tiers_->note_session(session, hs, n_tokens, now, label);
}
/* The session index names no blocks: it is what /v1/sessions release cuts, and it stays useful
* with or without a host copy. */
if (tiers_) tiers_->note_session(session, hs, n_tokens, now, label);
take_over(session, hs, n_full * block_size_);
return RAD_OK;
@@ -635,11 +663,13 @@ int64_t PrefixCache::trim_sparing(int64_t n_blocks,
it = next;
continue;
}
if (keep && !tiers_->droppable(h)) { ++it; continue; }
/* Skip a copy still in flight, but never an entry this tier cannot hold: that one never
* gets a copy, so waiting for one would keep it out of reach of the trim forever. */
if (keep && demotable_ && !tiers_->droppable(h)) { ++it; continue; }
it = lru_.erase(it);
e.in_lru = false;
release_blocks(e);
if (keep) {
if (keep && demotable_) {
tiers_->drop_commit(h);
++stats_.drops;
} else {