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

+11
View File
@@ -205,6 +205,11 @@ public:
int64_t block_size() const { return block_size_; }
int64_t checkpoint_interval() const { return interval_; }
const std::vector<int32_t>& cached_groups() const { return groups_; }
/* Whether the host tier can hold this cache's entries. False when any cached group streams: a
* streamed group's rows are addressed by the block id the sequence used, so they cannot be
* copied into a slot and their ids cannot be given up. With it false the cache is a VRAM cache
* and evicts whole entries, exactly as with no host tier (docs/KV-STREAMING.md §4.1). */
bool demotable() const { return demotable_; }
std::string report() const;
/* Overrides the policy configure() installed from Config::checkpoint_policy (custom policies,
@@ -461,6 +466,12 @@ private:
int64_t block_size_ = 0;
int64_t interval_ = 0;
std::vector<int32_t> groups_; /* paged groups this cache indexes */
/* groups_ less the streamed ones, in the same order, and where each sits in groups_. A tier
* job's block ids and a host slot are in this order; an entry that is not demotable_ never
* reaches the tiers at all. */
std::vector<int32_t> tier_groups_;
std::vector<size_t> tier_at_;
bool demotable_ = false; /* the host tier can hold every group in an entry */
bool has_linear_ = false;
mutable std::unordered_map<BlockHash, Entry, BlockHashHash> map_;