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

+13
View File
@@ -1613,6 +1613,19 @@ int Engine::plan() {
* It prices the ring raised to a decode step's selection, before any weight is
* loaded. */
RAD_TRY(kvstream_cap_probe(probe, cfg_, r->program, ctx));
/* A streamed group's rows are addressed by block id, so the prefix cache cannot
* demote an entry that contains one (PrefixCache::demotable): its mirror is the only
* cold copy that group has. The host tier would only be pinned and never used, so it
* is turned off here -- once, before the tables and the checkpoint store are sized,
* so its memory is not reserved either. Only when streaming is really on: the flag
* asks, the architecture serves it and the ring is under the context (kv_streams).
*/
if (const int64_t off = kvstream_prefix_host_off(cfg_)) {
RAD_INFO("a streamed KV group has no host cache: --prefix-cache-mib %lld would "
"hold nothing (its entries could never be demoted), so it is off; the "
"%lld MiB go to the host pool and the page cache instead",
(long long)off, (long long)off);
}
f.kv_ceiling = probe.ceiling(ctx);
f.kv_fixed = probe.fixed_bytes();
/* One --max-model-len sequence's pool, as the inverse of the real plan: its