kv streaming: drop the residency maps when the view returns to the ring

A staged pass copies whole layers into the pool at block-id identity: block b
of layer l goes to base + b * block_bytes. The ring addresses its slots at
base + l * slots * block_bytes + s * block_bytes, so the stage row of a later
layer is the ring row of an earlier one. The copy never touches `res` or
`slot_block`, so every block the map still calls resident names bytes that some
later layer has since overwritten.

Block ids are recycled between requests, so this is not theoretical. The second
request's prefill writes its mirror and -- gated on `res`, which is still valid
from the first request -- its ring slots; the rest of the layers' stage rows
then overwrite those slots. The first decode trusts `res`, skips the resolve,
and reads another layer's, or another request's, KV. Fluent, no fault.

Measured with --kv-resident 32768 on 2x16 GB gfx1201, exact compose flags:
  ring off:  an arithmetic probe answers 15:55 on every round;
  ring on (before): 15:55, then 16:40 for rounds 2-4, on spec 0 and spec 3;
  ring on (after):  15:55 on every round.
The same sequence previously answered a 3k system passphrase as Q97/Q&A and an
unrelated weather question with the previous request's filler text.

Clear `res`, `slot_block` and `slot_state` when the binding leaves the stage
view for the ring, for streams whose ring is smaller than their id space. The
first ring pass then resolves every live block out of the mirror -- the cold-map
path the first request already takes -- and the ring is self-consistent for the
decode steps that follow. One memset per prefill, not per step. A stream whose
ring covers its id space is left alone: there the staged bytes and the ring's
bytes are the same bytes at the same offsets.

Tests: kv_window_test 8/8, p4b_kv_ring_test 3/4 (one skipped: no in-tree test
kernels in this build), sched_test 142 cases green. arch_test has 15
pre-existing graph-pin failures on this tree, identical with and without this
change. The 47-case streaming battery is clean on both ring arms (spec 0 and 3).
This commit is contained in:
C_Tamas committed 2026-10-25 19:06:06 +07:00
1 parent ac8b3a5017
commit 1f3154d366
1 file changed
+36
+36
View File
@@ -63,7 +63,43 @@ void kvstream_enter(Ctx* c, const RadBatch* b) {
KVPoolBinding* kb = c->kv_binding(b->kv[i].group);
if (!kb || !kb->has_stage) continue;
/* A pass selects by phase and row shape (bind.h). */
const bool was_stage = kb->depth == 1;
kv_binding_enter(*kb, kv_pass_view(b->phase, b->n_tok, b->n_seq, b->n_spec));
/* Coming back to the ring from a staged pass: the ring's bytes are not what the residency
* map says. A staged pass copies whole layers to the pool at block-id identity (offset
* block * block_bytes), and that range overlaps the slots the ring uses for its layers --
* the stage row of layer N writes over the ring row of layer 0, and so on. The map is not
* touched by that copy, so every block it still calls resident names bytes some later
* layer has overwritten. On a ring smaller than its id space the ids are not addresses, so
* the map cannot even notice: a decode that trusts it reads another layer's (or another
* request's) KV, fluently and without a fault.
*
* Drop the map when the view returns to the ring. The first ring pass then resolves every
* live block out of the mirror -- exactly the cold-map behaviour the first request gets --
* and the ring is self-consistent again for the decode steps that follow. A stream whose
* ring covers its id space is the identity case and is left alone: there the staged bytes
* and the ring's bytes are the same bytes at the same offsets.
*
* The alternative -- capping the stage copy so it cannot reach a resident slot -- cannot
* work: the stage must hold the whole history one layer reads (stage.h, VOLUME), and the
* ring is a window of that same layer. */
if (was_stage && kb->depth == 0) {
const RadKVStream* st = b->kv[i].stream;
const int64_t bb = st->block_bytes;
const int64_t blocks = bb > 0 ? st->host_layer_stride / bb : 0;
const int64_t layers = st->host_layer_stride > 0
? st->host_bytes / st->host_layer_stride : 0;
if (blocks > 0 && layers > 0 && st->slots > 0 && st->slots < blocks) {
(void)rad_memset_async(st->res, 0xff,
layers * blocks * (int64_t)sizeof(int32_t), c->stream());
(void)rad_memset_async(st->slot_block, 0xff,
layers * st->slots * (int64_t)sizeof(int32_t),
c->stream());
(void)rad_memset_async(st->slot_state, 0,
layers * st->slots * 2 * (int64_t)sizeof(int32_t),
c->stream());
}
}
/* The history one layer of this pass can read: every block up to the longest sequence's
* end, at the same block size the resolve uses. That is the useful part of the stage row
* the draw below copies in full. */