radiance 1.13.0

New models:
- Tencent Hy-Memory (HYV3) from its BF16, FP8 and GGUF checkpoints, with its native tool calls
- M2XFP4: metadata-augmented MXFP4 weights for Qwen3.8-27B and its DFlash2 drafter (top-1 89.1% ->
  92.4% against plain MXFP4), its quantiser and recipe, and ParoQuant's M2XFP4 checkpoints
- Gemma 4 E2B/E4B audio input; NVIDIA-Nemotron-3.5-Bolt-E4B in int8 and 4-bit, on two cards by
  default; Ornith-1.5-122B compose files

Speed:
- Weight reads beside the decode all-reduces (qwen35_fp8, gemma3; --weight-prefetch, on by default):
  the 27B MXFP4 with DFlash2 61.2 -> 67.9 tok/s at c1, TranslateGemma 27B +13%
- Head-256 split-KV decode attention at 256 workgroups and without bank conflicts (27B: +3-5% a
  step at 25-41K context), and the prefill KV split for small launches (a short turn over a long
  context decodes the other sequences 27% faster in that step)
- MXFP4 decode gate_up, activation and fp8 quantiser in one launch (27B, Gemma 3 up to 64 rows:
  TranslateGemma +2-5% at 2-8 streams)
- ParoQuant int4/int5 tiled GEMMs 6-9% faster
- Speculation: a step's tokens are sent before the drafter's passes end, no step waits behind one
  that ends its requests, and the MTP head's first draft no longer collapses after short chats in
  the prefix cache

Features:
- --kv-resident N: KV streaming for a context bigger than VRAM (qwen4exp_fp8, GLM-5.3)
- MiniMax-H3: reference media and keyframe conditioning with the VAE encoders
- Krea 2 rebalance presets balanced, detail and subtle
- The dashboard's request history; a stall watch that reports a frozen step's thread and stack
- rad-tune at the served configuration, collectives measured as the exchange; Gemma 4 E2B/E4B's
  per-layer embeddings read ahead off the step path
- A GPTQ calibration tap and recipe for Gemma 4 31B's w4a8 container

Fixes:
- The two-rank all-reduce checks that a peer's push has landed before reducing it
- z-lab's DFlash drafter on Gemma 4 31B keeps drafting after a prefix-cache hit
- MiniMax-H3's modulation rows

Changes: Plugin ABI 31: out-of-tree plugins must be rebuilt. RADIANCE_PREFETCH_BESIDE is
--weight-prefetch now; RADIANCE_NO_HUGE_PAGES, RADIANCE_IDS_EARLY, RADIANCE_ATTN_BAND_SPLIT,
R4D_PREFILL_SPLITS, RAD_PLE_TIMING and RAD_PLE_CACHE_ROWS are gone.
This commit is contained in:
Deadcode committed 2026-10-27 16:47:35 +01:00
198 files changed
+20904 -3240

No files matched your search

+898
View File
@@ -7,6 +7,9 @@
#include "rad_test.h"
#include "mem/kv.h"
#include "kvstream/bringup.h"
#include "kvstream/bind.h"
#include "kvstream/plan.h"
#include "mem/pools.h"
#include "mem/prefix.h"
#include "mem/vram_budget.h"
@@ -116,6 +119,45 @@ struct Fixture {
}
};
/* The streamed path a plugin declares for `g`, as the builder records it: a kv_resolve stating the
* group's per-layer block pitch and selection pitch, and a kv_store_streamed stating its page.
* `with_resolve`/`with_store` build a model that declares one or neither (as the DSA-index models
* do). `store_resolved` false is the packed-cache shape: the store declared with no kernel row, so
* the builder keeps it with one band and both domains null. */
static KernelRow& a_resolved_row() {
/* Static, as a plugin table's rows are: a Resolved points at one. Nothing in this test issues
* the op, so the row carries no kernel. */
static KernelRow row;
return row;
}
static Program stream_program(const KVGroupInfo& g, int64_t sel_pitch, bool with_resolve = true,
bool with_store = true, bool store_resolved = true) {
Program p;
p.meta.arch_id = "test-arch";
const int64_t n_bound = g.layers.empty() ? 1 : (int64_t)g.layers.size();
const int64_t layer_bytes = g.bytes_per_block > 0 ? g.bytes_per_block / n_bound : 0;
if (with_resolve) {
OpInfo oi;
oi.op = "kv_resolve";
oi.bands.resize(1); /* resolved: the builder records one band per domain */
oi.bands[0].dom[RAD_DOMAIN_DEVICE].row = &a_resolved_row();
oi.base.set_i("block_bytes", layer_bytes);
oi.base.set_i("sel_pitch", sel_pitch);
p.ops.push_back(std::move(oi));
}
if (with_store) {
OpInfo oi;
oi.op = "kv_store_streamed";
oi.bands.resize(1);
if (store_resolved) oi.bands[0].dom[RAD_DOMAIN_DEVICE].row = &a_resolved_row();
else oi.bands[0].miss[RAD_DOMAIN_DEVICE] = "kv_dtype in {fp8_e4m3}, have fp8_v4";
oi.base.set_i("block_size", g.block_size);
p.ops.push_back(std::move(oi));
}
return p;
}
static std::vector<int32_t> tok_run(int32_t base, int n) {
std::vector<int32_t> v;
for (int i = 0; i < n; ++i) v.push_back(base + i);
@@ -776,6 +818,54 @@ static KVGroupInfo paged_group(const char* name, int kind, int64_t heads, int64_
return k;
}
/* The streamed group is the widest full group with a declared streamed path, never a dense-read
* one. GLM-5.3's two full groups show the difference: kv_latent (49 152 B a block) streams;
* kv_dsa_index (1 632 B) is read over the whole history every step and stays resident. */
TEST(kv_stream_group_picks_a_wired_full_group_not_merely_the_widest) {
std::vector<KVGroupInfo> g;
g.push_back(null_group());
const std::vector<int32_t> full_layers = { 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 45 };
KVGroupInfo latent = paged_group("kv_latent", RAD_KV_FULL, 1, 4, full_layers);
latent.decl.dtype = RAD_BF16;
latent.decl.head_dim = 256;
latent.bytes_per_block = 49152; /* 12 layers x 4 tokens x 256 x bf16 */
KVGroupInfo index = paged_group("kv_dsa_index", RAD_KV_FULL, 1, 4, full_layers);
index.decl.dtype = RAD_U8;
index.decl.head_dim = 17;
index.bytes_per_block = 1632; /* 12 layers x 4 tokens x 17 x u8 */
g.push_back(latent);
g.push_back(index);
auto check_pick = [&](const Program& p, int32_t want) {
Program prog = p;
prog.kv_groups = g; /* index-aligned, as Engine's program is */
CHECK_EQ(kv_stream_group(g, &prog), want);
/* The cap probe and the carve must agree, so the plans overload takes the same rule. */
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 4096;
f.cfg.max_seqs = 2;
CHECK_OK(f.kv.plan_groups(g, f.cfg));
CHECK_EQ(kv_stream_group(f.kv, &prog), want);
};
/* Wired latent, narrower: it is picked. */
check_pick(stream_program(g[1], 513), 1);
/* The index group made wider is still not picked: it is not wired. This is the width
* coincidence the rule removes. */
g[2].bytes_per_block = 65536;
check_pick(stream_program(g[1], 513), 1);
g[2].bytes_per_block = 1632;
/* A resolve that states the index group's pitch (136 B a layer) is the only wired path, so
* the narrower group is the one that streams. */
check_pick(stream_program(g[2], 513), 2);
/* A dense-read group is never a candidate, even when its own path is declared. Nothing else
* is wired here, so nothing streams. */
g[2].dense_read = true;
check_pick(stream_program(g[2], 513), -1);
g[2].dense_read = false;
}
/* A windowed group is capped at its window on both ranks, and the cap shifts the freed bytes to
* the other groups, a kink in paged_split the inverse must cross exactly. The dear rank's count is
* past the window, so the window group holds max_seqs x window_blocks on both ranks. */
@@ -846,6 +936,668 @@ TEST(an_uneven_worlds_cheaper_rank_gives_its_saved_kv_to_its_weights_up_to_all_r
CHECK_EQ(s.kv_mib, (int64_t)800);
}
TEST(a_streamed_group_is_refused_at_carve_without_its_host_copy) {
/* A streamed plan is refused unless something holds the blocks the pool does not back: a store
* for block 40 of a four-block pool would write past the layer stride. Each variant removes
* one of the three pieces the carve requires. */
for (int variant = 0; variant < 3; ++variant) {
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 1024; /* the context the group will address, in host RAM */
CHECK_OK(configure_pools(f.pools, f.cfg));
f.kv.cap_resident_tokens(G_FULL, 64);
CHECK_OK(f.kv.plan_groups(hybrid_groups(), f.cfg));
/* How many blocks the group has is decided at carve; the host copy has to be there first,
* so size it from the context the carve will compute: every sequence at full length. */
const int64_t ctx_blocks = f.cfg.max_seqs * (f.cfg.max_ctx / 16);
const int64_t page = f.kv.plan(G_FULL)->bytes_per_block;
const int64_t need = ctx_blocks * page;
std::vector<char> host((size_t)need, 0);
std::vector<int32_t> res((size_t)ctx_blocks, -1);
KVManager::StreamMap m;
m.host = variant == 0 ? nullptr : (uint8_t*)host.data(); /* 0: no copy at all */
m.host_bytes = variant == 1 ? need - 1 : need; /* 1: a copy too small */
m.res = variant == 2 ? nullptr : res.data(); /* 2: no map */
f.kv.set_stream_map(G_FULL, m);
CHECK_EQ(f.kv.carve(f.pools.kv()), RAD_E_INVAL);
}
}
TEST(a_streamed_group_addresses_its_context_but_backs_only_the_resident_window) {
/* A streamed group allocates block ids over its whole context while the pool backs only
* --kv-resident of them. This pins the places that multiply by one of the two numbers: the
* carve, the layer stride, the kernel's operand shape and the loan. A mistake there writes one
* layer's block into the next. The split is decided at carve, which knows the budget. */
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 1024; /* the context the group addresses */
CHECK_OK(configure_pools(f.pools, f.cfg));
f.kv.cap_resident_tokens(G_FULL, 64); /* four blocks of 16 tokens */
CHECK_OK(f.kv.plan_groups(hybrid_groups(), f.cfg));
/* Both numbers come from the carve: the context from --max-model-len and --max-num-seqs, the
* window from --kv-resident. The host copy must already cover the context. */
const int64_t ctx_blocks = f.cfg.max_seqs * (f.cfg.max_ctx / 16);
const int64_t page = f.kv.plan(G_FULL)->bytes_per_block;
CHECK(ctx_blocks > 4); /* the pool holds more than the window */
std::vector<char> host((size_t)(ctx_blocks * page), 0);
std::vector<int32_t> res((size_t)ctx_blocks, -1);
KVManager::StreamMap sm_in;
sm_in.host = (uint8_t*)host.data();
sm_in.res = res.data();
sm_in.host_bytes = (int64_t)host.size();
f.kv.set_stream_map(G_FULL, sm_in);
const KVManager::StreamMap* sm = f.kv.stream_map(G_FULL);
CHECK(sm != nullptr && sm->host == (uint8_t*)host.data() && sm->res == res.data() &&
sm->host_bytes >= ctx_blocks * page);
CHECK_OK(f.kv.carve(f.pools.kv()));
const KVGroupPlan* p = f.kv.plan(G_FULL);
CHECK_EQ(p->n_blocks, ctx_blocks);
CHECK_EQ(p->n_slots, 4);
CHECK(p->streamed());
CHECK_EQ(p->pool_blocks(), 4);
CHECK_EQ(p->layer_stride(), 4 * p->layer_bytes_per_block());
/* The carve is one reservation of max(ring, stage): the ring's four blocks of every layer, or
* the whole context one layer deep. Here the context's one layer is the larger, so the pool is
* a stage and the ring fits inside it (docs/KV-STREAMING.md §4.1). */
CHECK_EQ(f.kv.pool_bytes_carved(G_FULL),
std::max(4 * page, ctx_blocks * p->layer_bytes_per_block()));
CHECK_EQ(rad::kv_ceiling_bytes(*p, ctx_blocks, 4), ctx_blocks * p->layer_bytes_per_block());
/* The startup report must not read as a pool that holds the whole context. */
CHECK(f.kv.report().find("streaming:") != std::string::npos);
/* Not lendable: the window is what a residency map reads, and a block lent to the expert slab
* is one the attention has nowhere to put. */
CHECK(!f.kv.lendable(G_FULL));
/* The linear group next to it is untouched. */
CHECK_EQ(f.kv.plan(G_GDN)->n_slots, 0);
CHECK(!f.kv.plan(G_GDN)->streamed());
/* Block ids still run over the context. A sequence longer than the window asks for more ids
* than the pool backs and gets them, because its table names logical blocks -- which is exactly
* why a streamed plan may not be carved without somewhere for them to live. */
CHECK_OK(f.kv.add_sequence(1));
CHECK_OK(f.kv.ensure(1, 16 * 8)); /* eight blocks, twice the window */
const std::vector<int32_t>& bt = f.kv.block_table(1, G_FULL);
CHECK(bt.size() >= 8);
int32_t hi = 0;
for (int32_t b : bt) hi = std::max(hi, b);
CHECK(hi >= 4); /* an id past the window */
/* And a server that does not stream carves exactly what it did before the split: nothing
* moves for a group with no cap_resident_tokens(). */
Fixture plain;
plain.cfg.checkpoint_slots = 0;
CHECK_OK(plain.build());
const KVGroupPlan* q = plain.kv.plan(G_FULL);
CHECK(q->n_blocks > 0);
CHECK_EQ(q->n_slots, q->n_blocks);
CHECK(!q->streamed());
CHECK_EQ(plain.kv.pool_bytes_carved(G_FULL), q->n_blocks * q->bytes_per_block);
}
TEST(a_resident_window_that_does_not_fit_vram_kv_is_refused_by_its_own_flag) {
/* A window wider than the pool is refused by name; left to the split, it would leave the other
* groups zero blocks and point the operator at --host-pool-mib. */
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 1024;
CHECK_OK(configure_pools(f.pools, f.cfg));
f.kv.cap_resident_tokens(G_FULL, 100000); /* 6250 blocks against a 1 MiB pool */
CHECK_OK(f.kv.plan_groups(hybrid_groups(), f.cfg));
/* The host copy is the right size for the context; the window is what does not fit. */
const int64_t ctx_blocks = f.cfg.max_seqs * (f.cfg.max_ctx / 16);
std::vector<char> host((size_t)(ctx_blocks * f.kv.plan(G_FULL)->bytes_per_block), 0);
std::vector<int32_t> res((size_t)ctx_blocks, -1);
KVManager::StreamMap sm_in;
sm_in.host = (uint8_t*)host.data();
sm_in.res = res.data();
sm_in.host_bytes = (int64_t)host.size();
f.kv.set_stream_map(G_FULL, sm_in);
CHECK_EQ(f.kv.carve(f.pools.kv()), RAD_E_NOMEM);
}
TEST(a_streamed_window_group_is_refused_by_name) {
/* A window group's ids are its window's, not its context's, so it cannot stream. */
auto groups = hybrid_groups();
groups[G_FULL].decl.kind = RAD_KV_WINDOW;
groups[G_FULL].decl.window = 128; /* window_blocks = 128/16 + 1 */
Fixture f;
f.cfg.checkpoint_slots = 0;
CHECK_OK(configure_pools(f.pools, f.cfg));
f.kv.cap_resident_tokens(G_FULL, 64);
CHECK_OK(f.kv.plan_groups(groups, f.cfg));
CHECK_EQ(f.kv.carve(f.pools.kv()), RAD_E_INVAL);
}
/* The residency maps are device memory carved from the KV pool, which on a card is VRAM a host
* load faults on; so a check copies them back on a stream first, as the engine reads `ctl`. Counts:
* `res` is `n_layers * blocks`, `slot_block` `n_layers * slots`, `slot_state` twice that. */
struct StreamMapsHost {
std::vector<int32_t> res, slot_block, slot_state, ctl;
};
static int read_stream_maps(const KVManager::StreamMap& m, const KVGroupPlan& p, RadStream st,
StreamMapsHost* out) {
const int64_t layers = p.n_layers > 0 ? p.n_layers : 1;
out->res.assign((size_t)(layers * p.n_blocks), 0);
out->slot_block.assign((size_t)(layers * m.slots), 0);
out->slot_state.assign((size_t)(layers * m.slots * 2), 0);
out->ctl.assign(16, 0);
auto copy = [&](std::vector<int32_t>& dst, const int32_t* src) {
return rad_memcpy_async(dst.data(), src,
(int64_t)dst.size() * (int64_t)sizeof(int32_t), st);
};
int rc = copy(out->res, m.res);
if (rc == RAD_OK) rc = copy(out->slot_block, m.slot_block);
if (rc == RAD_OK) rc = copy(out->slot_state, m.slot_state);
if (rc == RAD_OK) rc = copy(out->ctl, m.ctl);
if (rc == RAD_OK) rc = rad_stream_sync(st);
return rc;
}
TEST(a_streamed_plan_allocates_reports_and_frees_its_mirror) {
/* The host mirror and the ring's maps, end to end: what stream_cost() promises is what
* alloc_stream() takes, the mirror is zeroed (a block nothing wrote yet must read as zeros, not
* as a page's leftovers), the maps come out of the KV pool and hold the reset state a resolve
* launch expects, the report shows both tiers, and free_stream() releases the mirror and is
* safe to call twice. */
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 1024;
CHECK_OK(configure_pools(f.pools, f.cfg));
f.kv.cap_resident_tokens(G_FULL, 64); /* four blocks of 16 tokens */
CHECK_OK(f.kv.plan_groups(hybrid_groups(), f.cfg));
const KVManager::StreamCost cost = f.kv.stream_cost();
CHECK_EQ(cost.groups, (int64_t)1);
CHECK_EQ(cost.host_bytes, f.cfg.max_seqs * (f.cfg.max_ctx / 16) *
f.kv.plan(G_FULL)->bytes_per_block);
CHECK(cost.map_bytes > 0);
const int64_t pool_used = f.pools.kv().used();
CHECK_OK(f.kv.alloc_stream(rad_malloc_allocator(), f.pools.kv()));
/* Every map byte is a reservation in the KV pool, so the paged split sees it. */
CHECK_EQ(f.pools.kv().used() - pool_used, cost.map_bytes);
const KVManager::StreamMap* m = f.kv.stream_map(G_FULL);
REQUIRE(m != nullptr);
CHECK(m->host != nullptr && m->res != nullptr && m->ctl != nullptr);
CHECK_EQ(m->host_bytes, cost.host_bytes);
CHECK_EQ(m->block_bytes, f.kv.plan(G_FULL)->layer_bytes_per_block());
CHECK_EQ(m->host_layer_stride, (f.cfg.max_seqs * (f.cfg.max_ctx / 16)) * m->block_bytes);
/* Zeroed at allocation: a miss copies these bytes to the ring before attention reads them. */
int64_t nonzero = 0;
for (int64_t i = 0; i < m->host_bytes; ++i) nonzero += m->host[i] != 0;
CHECK_EQ(nonzero, (int64_t)0);
CHECK_OK(f.kv.carve(f.pools.kv()));
RadStream st = nullptr;
REQUIRE_EQ(rad_stream_create(&st, 0), RAD_OK);
CHECK_OK(f.kv.init_stream(st));
CHECK_OK(rad_stream_sync(st));
/* The reset the reference contract states: both maps -1, state and control zero, in every
* layer's slice (the maps are device memory; read_stream_maps copies them back). */
const KVGroupPlan* p = f.kv.plan(G_FULL);
StreamMapsHost got;
CHECK_OK(read_stream_maps(*m, *p, st, &got));
rad_stream_destroy(st);
int64_t wrong = 0;
for (int64_t b = 0; b < (int64_t)got.res.size(); ++b) wrong += got.res[(size_t)b] != -1;
for (int64_t s = 0; s < (int64_t)got.slot_block.size(); ++s)
wrong += got.slot_block[(size_t)s] != -1;
for (int64_t i = 0; i < (int64_t)got.slot_state.size(); ++i)
wrong += got.slot_state[(size_t)i] != 0;
CHECK_EQ(wrong, (int64_t)0);
for (int i = 0; i < 16; ++i) CHECK_EQ(got.ctl[(size_t)i], 0);
CHECK_EQ(m->slots, p->pool_blocks());
CHECK(f.kv.stream_view(G_FULL) == m);
const KVManager::StreamStats stats = f.kv.stream_stats();
CHECK_EQ(stats.resident_blocks, p->pool_blocks());
/* Both tiers in one line, and the ring placed in the pool it was carved from. */
CHECK(f.kv.report().find("streaming:") != std::string::npos);
CHECK(f.kv.report().find("vram_kv") != std::string::npos);
CHECK(f.kv.report().find("pinned host memory") != std::string::npos);
f.kv.free_stream(rad_malloc_allocator());
CHECK(f.kv.stream_view(G_FULL) == nullptr);
CHECK(f.kv.stream_map(G_FULL) == nullptr || f.kv.stream_map(G_FULL)->host == nullptr);
f.kv.free_stream(rad_malloc_allocator()); /* teardown after a partial bring-up */
}
TEST(a_streamed_plans_maps_are_device_memory_read_back_on_its_stream) {
/* The maps come out of the KV pool, which on a card is VRAM: only the mirror may be read with a
* host load. Their reset contents are checked by
* `a_streamed_groups_maps_are_charged_before_the_pool_is_sized`. */
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 1024;
CHECK_OK(configure_pools(f.pools, f.cfg));
f.kv.cap_resident_tokens(G_FULL, 64);
CHECK_OK(f.kv.plan_groups(hybrid_groups(), f.cfg));
CHECK_OK(f.kv.alloc_stream(rad_malloc_allocator(), f.pools.kv()));
CHECK_OK(f.kv.carve(f.pools.kv()));
const KVManager::StreamMap* m = f.kv.stream_map(G_FULL);
REQUIRE(m != nullptr);
/* Every map array is a reservation of this pool, and the pool is what the card backs: the
* mirror is the only thing here the host may read with a load. */
const char* lo = (const char*)f.pools.kv().base();
const char* hi = lo + f.pools.kv().bytes();
CHECK((const char*)m->res >= lo && (const char*)m->res < hi);
CHECK(m->host < (const uint8_t*)lo || m->host >= (const uint8_t*)hi);
if (!device_is_host()) CHECK(rad_dev_host_ptr(m->res) == nullptr);
f.kv.free_stream(rad_malloc_allocator());
}
TEST(a_plan_that_does_not_stream_allocates_and_reports_nothing) {
/* The inert case every existing server runs: no cap_resident_tokens(), so stream_cost() is
* zero, alloc_stream() takes nothing, the ABI view is null (every group fully resident) and
* the report has no streaming line. */
Fixture f;
CHECK_OK(f.build());
const KVManager::StreamCost cost = f.kv.stream_cost();
CHECK_EQ(cost.groups, (int64_t)0);
CHECK_EQ(cost.host_bytes, (int64_t)0);
CHECK_EQ(cost.map_bytes, (int64_t)0);
const int64_t pool_used = f.pools.kv().used();
CHECK_OK(f.kv.alloc_stream(rad_malloc_allocator(), f.pools.kv()));
CHECK_EQ(f.pools.kv().used(), pool_used);
CHECK(f.kv.stream_view(G_FULL) == nullptr);
CHECK(f.kv.stream_map(G_FULL) == nullptr);
const KVManager::StreamStats stats = f.kv.stream_stats();
CHECK_EQ(stats.resident_blocks, (int64_t)0);
CHECK(f.kv.report().find("streaming:") == std::string::npos);
f.kv.free_stream(rad_malloc_allocator());
CHECK_EQ(f.pools.kv().used(), pool_used);
}
TEST(a_ring_that_covers_the_context_streams_nothing_and_the_engine_pays_nothing) {
/* --kv-resident absent or 0 streams nothing, and neither does a ring that covers the group's
* context: such a group keeps the resident path, with no mirror, maps or stage and a null
* RadKVStream, so no kv_resolve can be issued (docs/KV-STREAMING.md §3.1). */
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 1024;
f.cfg.max_seqs = 2; /* context: 2 sequences x 1024 tokens */
std::vector<KVGroupInfo> groups = hybrid_groups();
groups[G_FULL].bytes_per_block = 2048; /* 16 tokens x 128 B, as declare sets it */
const int64_t ctx_blocks = 2 * (1024 / 16); /* 128 blocks of 16 tokens */
/* A ring that covers the context. The group is bf16 here on purpose: a configuration that fits
* is never refused for a cache the streamed store has no row for, because nothing streams. */
f.cfg.kv_resident = 1024 * 2;
KVStreamSetup s;
CHECK_OK(kvstream_configure(f.cfg, 1024, groups, stream_program(groups[G_FULL], 513), &s));
CHECK_EQ(s.group, -1);
CHECK_EQ(s.asked_tokens, (int64_t)2048);
CHECK_EQ(s.ctx_blocks, ctx_blocks);
CHECK_EQ(s.ring_blocks, ctx_blocks);
CHECK_EQ(s.stage_blocks, (int64_t)0); /* no stage view is declared */
CHECK_EQ(s.group_name, std::string("full"));
/* And the engine applies that decision: configure_kv caps only a group that streams, so the
* manager is never told about a window, alloc_stream() takes nothing and the carve is the
* ordinary one-view pool. */
CHECK_OK(configure_pools(f.pools, f.cfg));
CHECK_OK(f.kv.plan_groups(groups, f.cfg));
const KVManager::StreamCost cost = f.kv.stream_cost();
CHECK_EQ(cost.groups, (int64_t)0);
CHECK_EQ(cost.host_bytes, (int64_t)0);
CHECK_EQ(cost.map_bytes, (int64_t)0);
const int64_t pool_used = f.pools.kv().used();
CHECK_OK(f.kv.alloc_stream(rad_malloc_allocator(), f.pools.kv()));
CHECK_EQ(f.pools.kv().used(), pool_used); /* no mirror and no maps were taken */
CHECK_OK(f.kv.carve(f.pools.kv()));
const KVGroupPlan* p = f.kv.plan(G_FULL);
CHECK_EQ(p->n_slots, p->n_blocks); /* every block has its own slot */
CHECK(!p->streamed());
CHECK_EQ(p->depth, (int64_t)0); /* the arch sees no stage view */
CHECK_EQ(f.kv.pool_bytes_carved(G_FULL), p->n_blocks * p->bytes_per_block);
CHECK(f.kv.stream_view(G_FULL) == nullptr); /* so no kv_resolve can be issued */
CHECK(f.kv.stream_map(G_FULL) == nullptr);
CHECK(f.kv.report().find("streaming:") == std::string::npos);
f.kv.free_stream(rad_malloc_allocator());
/* Absent (the parser's default) and 0 are the same: nothing is asked for, nothing is decided,
* nothing is capped. */
{
Fixture g;
g.cfg.checkpoint_slots = 0;
g.cfg.max_ctx = 1024;
g.cfg.max_seqs = 2;
g.cfg.kv_resident = 0;
KVStreamSetup o;
CHECK_OK(kvstream_configure(g.cfg, 1024, groups, stream_program(groups[G_FULL], 513), &o));
CHECK_EQ(o.group, -1);
CHECK_EQ(o.asked_tokens, (int64_t)0);
CHECK_EQ(o.ring_blocks, (int64_t)0);
CHECK_EQ(o.stage_blocks, (int64_t)0);
CHECK_OK(g.build_groups(groups));
CHECK_EQ(g.kv.stream_cost().groups, (int64_t)0);
CHECK(g.kv.stream_view(G_FULL) == nullptr);
CHECK(!g.kv.plan(G_FULL)->streamed());
CHECK_EQ(g.kv.plan(G_FULL)->depth, (int64_t)0);
}
}
/* The prefix cache's host tier is turned off when --kv-resident really streams, and only then.
* The three cases that must leave it on are the ones an operator hits without meaning to: the flag
* absent or 0, a ring that covers the group's context (nothing streams), and a flag on a model the
* tier does not stream. `cfg.kv_streams` is the one answer to all three (kvstream_cap_probe). */
TEST(the_prefix_cache_host_tier_is_off_only_when_streaming_is_really_on) {
std::vector<KVGroupInfo> groups = hybrid_groups();
groups[G_FULL].bytes_per_block = 2048; /* 16 tokens x 128 B, as declare sets it */
/* A pitch of 513 blocks is this fixture's resolve; the ceiling is max_seqs x (1 + n_spec) x
* pitch, so a ring under a 128-block context needs a small pitch: 8 x 2 = 16 blocks. */
const Program wide = stream_program(groups[G_FULL], 513);
const Program narrow = stream_program(groups[G_FULL], 8);
struct Case {
const char* what;
int64_t resident; /* --kv-resident */
bool streams;
int64_t tier_after; /* --prefix-cache-mib once bring-up has had its say */
};
const Case cases[] = {
{ "the flag absent", 0, false, 2048 },
{ "a ring covering the ctx", 2 * 65536, false, 2048 },
{ "a ring under the ctx", 256, true, 0 },
};
for (const Case& c : cases) {
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 65536; /* clears the 32 768-token floor */
f.cfg.max_seqs = 2;
f.cfg.kv_resident = c.resident;
f.cfg.prefix_cache_mib = 2048;
CHECK_OK(f.kv.plan_groups(groups, f.cfg));
CHECK_OK(kvstream_cap_probe(f.kv, f.cfg, c.streams ? narrow : wide, f.cfg.max_ctx));
CHECK_EQ(f.cfg.kv_streams, c.streams);
const int64_t off = kvstream_prefix_host_off(f.cfg);
CHECK_EQ(f.cfg.prefix_cache_mib, c.tier_after);
CHECK_EQ(off, c.streams ? 2048ll : 0ll);
CHECK_EQ(kvstream_prefix_host_off(f.cfg), 0ll); /* idempotent: a second rank says nothing */
f.kv.free_stream(rad_malloc_allocator());
}
}
TEST(a_decode_pass_wider_than_a_verify_stages_instead_of_resolving_the_ring) {
/* The serial drafter's history pass is RAD_PHASE_DECODE, but after a prefill chunk it carries
* the chunk's tokens of every sequence: more rows a sequence than a decode step can select for
* (1 + n_spec). Its selection is the union of the histories it spans, not one row a selection,
* so it reads the one-layer stage -- identity-addressed over the whole context -- and cannot
* overflow the ring. A verify or a history pass over the verified window stays on the ring:
* routing those to the stage would stage every decode step. */
using rad::KVView;
CHECK(kv_pass_view(RAD_PHASE_PREFILL, 2048, 4, 3) == KVView::Stage);
CHECK(kv_pass_view(RAD_PHASE_MIXED, 900, 3, 3) == KVView::Stage);
/* The ring's passes: a verify step (4 rows a sequence with n_spec 3), one row a sequence, and
* the history pass that redoes the verified positions. */
CHECK(kv_pass_view(RAD_PHASE_DECODE, 16, 4, 3) == KVView::Ring);
CHECK(kv_pass_view(RAD_PHASE_DECODE, 4, 4, 3) == KVView::Ring);
CHECK(kv_pass_view(RAD_PHASE_DECODE, 12, 3, 3) == KVView::Ring);
CHECK(kv_pass_view(RAD_PHASE_DECODE, 4, 4, 0) == KVView::Ring);
/* The history pass over a prefill chunk, and every prefill-shaped decode pass. */
CHECK(kv_pass_view(RAD_PHASE_DECODE, 2048, 4, 3) == KVView::Stage);
CHECK(kv_pass_view(RAD_PHASE_DECODE, 2039, 3, 3) == KVView::Stage);
CHECK(kv_pass_view(RAD_PHASE_DECODE, 2048, 4, 0) == KVView::Stage);
}
TEST(a_ring_below_one_decode_step_s_selection_is_raised_to_the_ceiling) {
/* A ring under one decode step's selection, max_seqs x (1 + n_spec) x sel_pitch blocks, would
* serve a table that no longer addresses the pool, so the flag is raised to it with one
* warning and the carve takes the raised ring. */
Fixture f;
f.cfg.checkpoint_slots = 0;
/* The context must clear the shipped floor (kStreamFloorTokens, 32 768 tokens) for the step
* ceiling to be the binding bound: 2 x 65 536 tokens = 8192 blocks of 16. */
f.cfg.max_ctx = 65536;
f.cfg.max_seqs = 2;
f.cfg.n_spec = 0;
f.cfg.vram_kv_mib = 64;
std::vector<KVGroupInfo> groups = hybrid_groups();
groups[G_FULL].decl.dtype = RAD_F8E4M3; /* the streamed store's one row */
groups[G_FULL].bytes_per_block = 2048; /* as declare sets it */
const int64_t sel_pitch = 2048; /* a synthetic indexer's topk + 1 */
const int64_t ctx_blocks = 2 * (65536 / 16); /* 8192 blocks of 16 tokens */
const Program prog = stream_program(groups[G_FULL], sel_pitch);
CHECK_OK(configure_pools(f.pools, f.cfg));
CHECK_OK(f.kv.plan_groups(groups, f.cfg));
/* The ceiling is 2 sequences x 1 row x sel_pitch = 4096 blocks = 65 536 tokens, above the
* shipped floor of 2048 blocks (32 768 tokens) and under the 8192-block context. */
f.cfg.kv_resident = 128; /* 8 blocks: far under it */
KVStreamSetup s;
const std::string log = stderr_of([&] {
/* The budget probe prices the ring the carve will take, both of them raised. */
CHECK_OK(kvstream_cap_probe(f.kv, f.cfg, prog, 65536));
CHECK_OK(kvstream_configure(f.cfg, 65536, groups, prog, &s));
});
CHECK(has(log, "--kv-resident 128 raised to 65536 tokens"));
CHECK(has(log, "one decode step can select 4096 blocks of 16 tokens"));
CHECK(has(log, "(--max-num-seqs 2 x 1 rows x 2048)"));
/* Raised, not refused: the flag is as the operator left it and the plan is the ceiling. */
CHECK_EQ(f.cfg.kv_resident, (int64_t)128);
CHECK_EQ(s.group, (int32_t)G_FULL);
CHECK_EQ(s.asked_tokens, (int64_t)128);
CHECK_EQ(s.raised_tokens, (int64_t)65536);
CHECK_EQ(s.ring_blocks, (int64_t)4096);
CHECK_EQ(s.ring_tokens, (int64_t)65536);
CHECK_EQ(s.ctx_blocks, ctx_blocks);
CHECK_EQ(s.stage_blocks, ctx_blocks); /* one layer, the whole context */
/* The carve takes the ceiling, not the flag's 8 blocks: cap_resident_tokens is handed the
* raised ring, and the residency map, the pool and the resolve's slot ceiling are that depth. */
CHECK_OK(f.kv.alloc_stream(rad_malloc_allocator(), f.pools.kv()));
CHECK_OK(f.kv.carve(f.pools.kv()));
const KVGroupPlan* p = f.kv.plan(G_FULL);
CHECK(p->streamed());
CHECK_EQ(p->n_blocks, ctx_blocks);
CHECK_EQ(p->n_slots, (int64_t)4096); /* the ceiling */
CHECK_EQ(p->n_slots, s.ring_blocks);
CHECK_EQ(f.kv.stream_map(G_FULL)->slots, (int64_t)4096);
CHECK_EQ(f.kv.stream_stats().resident_blocks, (int64_t)4096);
/* And the startup report says so in the ring's own line, not only in the warning: the ring is
* no longer what the operator typed, and the report is what people read afterwards. */
KVStreamHostUse use;
kvstream_host_add(&use, f.kv);
const std::string rep = stderr_of([&] { kvstream_report(s, use, 1); });
CHECK(has(rep, "--kv-resident 128 became 65536 tokens"));
CHECK(has(rep, "(8 -> 4096 blocks of 16 tokens)"));
CHECK(has(rep, "ring 4096 blocks")); /* the staging line quotes the ring */
f.kv.free_stream(rad_malloc_allocator());
/* Under speculation a verify step runs the drafts beside the accepted position, so the rows --
* and the ceiling -- scale by 1 + n_spec: 2 x 4 x 512 = 4096 blocks = 65 536 tokens. */
{
const Program spec = stream_program(groups[G_FULL], 512);
Fixture g;
g.cfg.checkpoint_slots = 0;
g.cfg.max_ctx = 65536;
g.cfg.max_seqs = 2;
g.cfg.n_spec = 3;
g.cfg.vram_kv_mib = 64;
g.cfg.kv_resident = 512; /* 32 blocks: far under the ceiling */
KVStreamSetup o;
const std::string l = stderr_of([&] {
CHECK_OK(kvstream_configure(g.cfg, 65536, groups, spec, &o));
});
CHECK(has(l, "(--max-num-seqs 2 x 4 rows x 512)"));
CHECK_EQ(o.group, (int32_t)G_FULL);
CHECK_EQ(o.ring_blocks, (int64_t)4096);
CHECK_EQ(o.raised_tokens, (int64_t)65536);
}
/* At or above the ceiling nothing is raised and the ring is the flag's own number. */
{
Fixture g;
g.cfg.checkpoint_slots = 0;
g.cfg.max_ctx = 65536;
g.cfg.max_seqs = 2;
g.cfg.n_spec = 0;
g.cfg.kv_resident = 65536; /* exactly 2 x 1 x 2048 blocks of 16 */
KVStreamSetup o;
const std::string l = stderr_of([&] {
CHECK_OK(kvstream_configure(g.cfg, 65536, groups, prog, &o));
});
CHECK(!has(l, "raised"));
CHECK_EQ(o.raised_tokens, (int64_t)0);
CHECK_EQ(o.ring_blocks, (int64_t)4096);
CHECK_EQ(o.ring_tokens, (int64_t)65536);
}
}
TEST(a_model_whose_architecture_declares_no_streamed_ops_ignores_the_flag) {
/* An architecture with no streamed path ignores the flag with one line (docs/KV-STREAMING.md
* §4.5): without the resolve the attention would read block ids past the ring, and without the
* streamed store a miss would read zeros. */
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 65536; /* above the 32 768-token ring floor */
f.cfg.max_seqs = 2;
f.cfg.kv_resident = 256; /* under the ring floor, so it is raised */
std::vector<KVGroupInfo> groups = hybrid_groups();
groups[G_FULL].bytes_per_block = 2048; /* 16 tokens x 2 kv heads x 8 x 2 B x 2 layers */
const Program none =
stream_program(groups[G_FULL], 8, /*with_resolve=*/false, /*with_store=*/false);
KVStreamSetup s;
int st = RAD_OK;
const std::string log = stderr_of([&] { st = kvstream_configure(f.cfg, 65536, groups, none, &s); });
CHECK_OK(st); /* ignored, not refused */
CHECK_EQ(s.group, -1);
CHECK_EQ(s.asked_tokens, (int64_t)0); /* nothing is claimed, so nothing is reported */
CHECK_EQ(s.ring_blocks, (int64_t)0);
CHECK_EQ(s.stage_blocks, (int64_t)0);
CHECK(has(log, "ignored"));
CHECK(has(log, "test-arch")); /* the architecture, not just the group */
CHECK(has(log, "kv_resolve")); /* the op that is missing, by name */
CHECK(!has(log, "raised to")); /* that is the other message: this model cannot stream at all */
/* Half a path is no path: the resolve with no streamed store would leave the mirror unwritten
* and the miss list meaningless, so the warning names that op instead. */
KVStreamSetup half;
const std::string log2 = stderr_of([&] {
CHECK_OK(kvstream_configure(f.cfg, 65536, groups,
stream_program(groups[G_FULL], 8, true, false), &half));
});
CHECK_EQ(half.group, -1);
CHECK(has(log2, "kv_store_streamed"));
/* And the fail-safe never turns a working architecture off: the same configuration with both
* ops declared streams the ring it was sized for. */
KVStreamSetup ok;
CHECK_OK(kvstream_configure(f.cfg, 65536, groups, stream_program(groups[G_FULL], 8), &ok));
CHECK_EQ(ok.group, (int32_t)G_FULL);
CHECK_EQ(ok.ring_blocks, (int64_t)2048); /* raised to the 32 768-token floor */
CHECK_EQ(ok.stage_blocks, ok.ctx_blocks);
}
TEST(a_declared_streamed_store_with_no_kernel_is_not_a_streamed_path) {
/* A declaration with no kernel is not a streamed path: the builder keeps the op with one band
* and no domain bound, and counting it would stream a group whose store never runs. */
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 65536; /* above the 32 768-token ring floor */
f.cfg.max_seqs = 2;
f.cfg.kv_resident = 256; /* under the ring floor, so it is raised */
std::vector<KVGroupInfo> groups = hybrid_groups();
groups[G_FULL].decl.dtype = RAD_F8E4M3;
groups[G_FULL].bytes_per_block = 2048;
/* The resolve resolves; the store is declared with no row (both domains null). */
const Program unresolved = stream_program(groups[G_FULL], 8, true, true, /*store_resolved=*/false);
REQUIRE_EQ(unresolved.ops.size(), (size_t)2);
REQUIRE_EQ(unresolved.ops[1].bands.size(), (size_t)1);
CHECK(unresolved.ops[1].bands[0].dom[RAD_DOMAIN_DEVICE].row == nullptr);
const KVStreamOps ops = kvstream_ops(unresolved, &groups[G_FULL]);
CHECK(ops.resolve);
CHECK(!ops.store);
CHECK(!ops.folded);
CHECK(!ops.streams()); /* no writer, so no streamed path */
KVStreamSetup s;
int st = RAD_OK;
const std::string log =
stderr_of([&] { st = kvstream_configure(f.cfg, 65536, groups, unresolved, &s); });
CHECK_OK(st); /* ignored, not carved */
CHECK_EQ(s.group, -1);
CHECK_EQ(s.asked_tokens, (int64_t)0);
CHECK_EQ(s.ring_blocks, (int64_t)0);
CHECK_EQ(s.stage_blocks, (int64_t)0);
CHECK(has(log, "ignored"));
CHECK(has(log, "kv_store_streamed")); /* the op that is missing, by name */
}
TEST(a_packed_cache_warns_that_the_layout_has_no_writer_and_serves_non_streamed) {
/* --kv-cache-dtype fp8_v4 / k6_v4: the architecture declares the resolve and no streamed store,
* because libr4d's streamed rows are E4M3 and bf16 and a packed cache has base-store rows only
* (arch/common/rad_block_attn_gated_fp8.h). The group is declared to the core as plain E4M3, so
* no dtype gate downstream can see the layout: the operator has to be told the *layout* has no
* writer, not that the architecture cannot stream -- and the group must run the base path.
* Before this the op was declared against a table with no row for it, which is a weightless
* device hole: startup refused with or without --kv-resident, naming a kernel band and never the
* flag. */
for (const char* dtype : { "fp8_v4", "k6_v4" }) {
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 65536; /* above the 32 768-token ring floor */
f.cfg.max_seqs = 2;
f.cfg.kv_resident = 256; /* under the ring floor, so it is raised */
f.cfg.kv_cache_dtype = dtype;
std::vector<KVGroupInfo> groups = hybrid_groups();
groups[G_FULL].decl.dtype = RAD_F8E4M3; /* what declare gives the core, packed or not */
groups[G_FULL].bytes_per_block = 2048;
/* What the gated plugin emits for a packed cache: the layout-blind resolve, no store. */
const Program packed = stream_program(groups[G_FULL], 8, true, /*with_store=*/false);
KVStreamSetup s;
int st = RAD_OK;
const std::string log = stderr_of([&] {
st = kvstream_configure(f.cfg, 65536, groups, packed, &s);
});
CHECK_OK(st); /* started, not refused */
CHECK_EQ(s.group, -1);
CHECK_EQ(s.asked_tokens, (int64_t)0);
CHECK_EQ(s.stage_blocks, (int64_t)0);
CHECK(has(log, "ignored"));
CHECK(has(log, std::string("group 'full' has a ") + dtype + " cache"));
CHECK(has(log, "has no writer for that layout (E4M3 and bf16 only)"));
CHECK(!has(log, "declares no")); /* not that sentence: the arch declared it */
CHECK(!has(log, "raised to")); /* and not the ring's */
}
}
TEST(a_resolve_declared_for_another_group_does_not_enable_streaming) {
/* The check is per group, not per program: the resolve states the layer-bytes pitch of the
* group it was emitted for, so one declared for a narrower full group cannot make streaming
* of the widest group look wired. That is the difference between "some group resolves" and
* "the group the plan streams resolves", and only the second is safe. */
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_ctx = 65536; /* above the 32 768-token ring floor */
f.cfg.max_seqs = 2;
f.cfg.kv_resident = 256;
std::vector<KVGroupInfo> groups = hybrid_groups();
groups[G_FULL].bytes_per_block = 2048; /* 1024 B a layer */
Program other = stream_program(groups[G_FULL], 8);
other.ops[0].base.set_i("block_bytes", 4096); /* a wider group's pitch, not this one's */
KVStreamSetup s;
int st = RAD_OK;
const std::string log = stderr_of([&] { st = kvstream_configure(f.cfg, 65536, groups, other, &s); });
CHECK_OK(st);
CHECK_EQ(s.group, -1);
CHECK(has(log, "kv_resolve"));
}
TEST(kv_refuses_a_group_with_no_block_size) {
/* Block size comes from the resolved attention kernel; zero means it did not resolve, and the
* manager refuses rather than inventing one. */
@@ -6669,4 +7421,150 @@ TEST(a_tier_transfer_gives_back_every_event_and_device_byte) {
CHECK_EQ(vram1, vram0);
}
/* The streamed group's residency maps are reserved from the KV pool before the carve, so the
* budget probe must charge them before it sizes the pool. Left out, the pool it names is short by
* their bytes and the group that stays resident is carved below one full-length conversation --
* the target shape's `kv_qsa_bkey` at 47 185 of the 51 200 blocks it needed (188 740 tokens
* against --max-model-len 204800). The probe has no pool and cannot reserve them; the carve has
* already taken them. paged_split() charges them and carve_into() adds back what was reserved, so
* both paths see the same bytes (kvstream/carve.h). */
TEST(a_streamed_groups_maps_are_charged_before_the_pool_is_sized) {
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_seqs = 4;
f.cfg.max_ctx = 65536; /* the maps are more than the MiB rounding */
std::vector<KVGroupInfo> groups = hybrid_groups();
groups.push_back(paged_group("bkey", RAD_KV_FULL, 2, 4, { 6, 7 }));
CHECK_OK(f.kv.plan_groups(groups, f.cfg));
f.kv.cap_resident_tokens(G_FULL, 64); /* four blocks of 16 tokens */
const int64_t need = f.kv.pool_mib_for_tokens(f.cfg.max_ctx, 256);
REQUIRE(need > 0);
f.cfg.vram_kv_mib = need;
CHECK_OK(configure_pools(f.pools, f.cfg));
CHECK_OK(f.kv.alloc_stream(rad_malloc_allocator(), f.pools.kv()));
CHECK_OK(f.kv.carve(f.pools.kv()));
/* The group that stays resident holds one whole conversation, not the pool's remainder. */
const KVGroupPlan* bkey = f.kv.plan(3);
REQUIRE(bkey != nullptr);
CHECK(!bkey->streamed());
CHECK(bkey->n_blocks * bkey->block_size >= f.cfg.max_ctx);
}
/* A dense-read group is read over its whole live history every decode step, so it cannot stream.
* While a sibling does, its carve is a floor of max_seqs full contexts: the host mirror next to it
* is sized for max_seqs sequences, and an indexer funded for one makes the advertised capacity a
* promise the engine cannot keep. The probe sizes the pool for the floor before the carve spends
* it (kvstream/carve.h). */
TEST(a_dense_read_groups_floor_holds_every_sequence_while_a_sibling_streams) {
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_seqs = 4;
f.cfg.max_ctx = 65536;
std::vector<KVGroupInfo> groups = hybrid_groups();
groups.push_back(paged_group("bkey", RAD_KV_FULL, 2, 4, { 6, 7 }));
groups.back().dense_read = true;
CHECK_OK(f.kv.plan_groups(groups, f.cfg));
f.kv.cap_resident_tokens(G_FULL, 64); /* four blocks of 16 tokens */
const int64_t need = f.kv.pool_mib_for_tokens(f.cfg.max_ctx, 512);
REQUIRE(need > 0);
f.cfg.vram_kv_mib = need;
CHECK_OK(configure_pools(f.pools, f.cfg));
CHECK_OK(f.kv.alloc_stream(rad_malloc_allocator(), f.pools.kv()));
CHECK_OK(f.kv.carve(f.pools.kv()));
const KVGroupPlan* bkey = f.kv.plan(3);
REQUIRE(bkey != nullptr);
CHECK(!bkey->streamed());
CHECK_EQ(bkey->n_blocks, f.cfg.max_seqs * ((f.cfg.max_ctx + 3) / 4));
CHECK_EQ(bkey->n_slots, bkey->n_blocks);
}
/* A prefix entry covers every cached group, the streamed one included: a hit re-adopts its block
* ids, and the mirror behind them is that group's tier. A streamed group's rows are addressed by
* the block id the sequence used, so its ids cannot be given up for a host slot: an entry that
* contains one stays in VRAM, the tiers are never told about it, and an eviction takes every
* group's ids with it. */
TEST(an_entry_with_a_streamed_group_is_never_demoted) {
if (!host_backend_or_skip("the tier restore")) return;
Fixture f;
f.cfg.checkpoint_slots = 0;
f.cfg.max_seqs = 1;
f.cfg.max_ctx = 1024;
std::vector<KVGroupInfo> groups = hybrid_groups();
groups.push_back(paged_group("bkey", RAD_KV_FULL, 2, 16, { 6, 7 }));
CHECK_OK(f.kv.plan_groups(groups, f.cfg));
f.kv.cap_resident_tokens(G_FULL, 64); /* four blocks of 16 tokens */
const int64_t need = f.kv.pool_mib_for_tokens(f.cfg.max_ctx, 256);
REQUIRE(need > 0);
f.cfg.vram_kv_mib = need;
CHECK_OK(configure_pools(f.pools, f.cfg));
CHECK_OK(f.kv.alloc_stream(rad_malloc_allocator(), f.pools.kv()));
CHECK_OK(f.kv.carve(f.pools.kv()));
const std::vector<int32_t>& cg = f.pc.cached_groups();
IdleTiers t;
REQUIRE_EQ(t.configure(tier_config(4096 * 1024, 4096 * 1024)), RAD_OK);
f.pc.set_tiers(&t);
REQUIRE_EQ(f.pc.configure(f.cfg, &f.kv, f.cfg.checkpoint_interval), RAD_OK);
REQUIRE_EQ(cg.size(), 2u);
CHECK(f.kv.plan(cg[0])->streamed());
/* The one fact the tier path depends on: a slot cannot hold this cache's entries, so no
* write-back is planned, nothing is queued, and nothing can be dropped to the host. */
CHECK(!f.pc.demotable());
const auto tokens = tok_run(5000, 64);
publish(f, 1, tokens, 64);
f.kv.free_sequence(1);
const std::vector<BlockHash> hs = chain_of(tokens);
REQUIRE_EQ(hs.size(), 4u);
std::vector<TierJob> wb;
t.plan_writeback(64, &wb);
CHECK_EQ(wb.size(), 0u); /* the streamed entry is never copied */
CHECK_EQ(f.pc.drop_clean(), 0ll); /* so it is never demoted */
CHECK(!t.droppable(hs[0]));
/* The trim still frees it: an entry the tier cannot hold must not be skipped as though its
* copy were in flight. All four blocks are at refcount one and the trim takes them. */
CHECK_EQ(f.pc.trim(4), 4ll);
CHECK_EQ(f.pc.size(), 0ll);
CHECK_EQ(f.kv.block_refcount(cg[0], 0), 0);
CHECK_EQ(f.kv.block_refcount(cg[1], 0), 0);
f.pc.set_tiers(nullptr);
}
RAD_TEST_MAIN()
/* A streamed-only op exists only when the architecture's capability table says it can stream AND
* the deployment asked for a ring (core/kvstream/bringup.cpp, consulted from rad_op_declared). One
* binary serves both runs, so this is the only place the two questions meet; no model file carries
* the flag, and an architecture that cannot stream keeps the graph it had before streaming existed. */
TEST(a_streamed_op_exists_only_for_a_capable_architecture_and_a_ring) {
RadBuildCtx ctx{};
ctx.kv_stream_ok = 1;
ctx.kv_resident_blocks = 32768;
/* capable and asked: the ops declare as they always did */
CHECK(!kvstream_op_gated(ctx, "kv_store_streamed"));
CHECK(!kvstream_op_gated(ctx, "qk_norm_rope_gate_kv_store_streamed"));
CHECK(!kvstream_op_gated(ctx, "kv_resolve"));
/* nothing that works without a ring is ever gated */
CHECK(!kvstream_op_gated(ctx, "kv_store"));
CHECK(!kvstream_op_gated(ctx, "qk_norm_rope_gate_kv_store"));
CHECK(!kvstream_op_gated(ctx, "attn_paged"));
CHECK(!kvstream_op_gated(ctx, "qsa_select"));
CHECK(!kvstream_op_gated(ctx, nullptr));
/* no flag: the ops that exist only for a ring are gone */
ctx.kv_resident_blocks = 0;
CHECK(kvstream_op_gated(ctx, "kv_store_streamed"));
CHECK(kvstream_op_gated(ctx, "kv_resolve"));
CHECK(!kvstream_op_gated(ctx, "kv_store"));
/* an architecture with no streamed path declares none even when the flag asks */
ctx.kv_resident_blocks = 32768;
ctx.kv_stream_ok = 0;
CHECK(kvstream_op_gated(ctx, "kv_store_streamed"));
CHECK(kvstream_op_gated(ctx, "qk_norm_rope_gate_kv_store_streamed"));
CHECK(kvstream_op_gated(ctx, "kv_resolve"));
CHECK(!kvstream_op_gated(ctx, "kv_store"));
CHECK(!kvstream_op_gated(ctx, "attn_paged"));
}