kv: every rank carves the least rank's blocks

One block table serves every rank, and Engine::serve refuses ranks whose
paged groups hold different block counts. Under the even split that was
a check that never fired: the ranks' pools had the same geometry and the
same bytes. Under the attention world they do not. A rank with 6 of 16
delta-net heads holds more recurrent state than one with 5 and has less
pool left for pages; the attention-zero rank has no attention weights
and has more. Three ranks over Flash-Next carved 136256, 142352 and
more, and the serve was refused.

KVManager::paged_split is the carve's paged arithmetic with nothing
carved -- the token count the paged groups exhaust at together for a
pool of so many bytes, a windowed group capped at its window -- and
carve_into spends what it says. paged_tokens asks it; cap_paged_tokens
sets a ceiling it keeps under. Engine::configure_kv plans every rank
first, asks each what it could page, and caps every rank at the least
before any carves; a rank that could have paged more says so at info.
At an even split the cap is what every rank would have carved.

mem_test: kv_ranks_with_unequal_pools_carve_the_least_ranks_blocks_under_a_cap.
This commit is contained in:
baKon authored and StillDeadcode committed 2026-10-06 11:23:06 +02:00
1 parent d72b00bfdb
commit 9fc7e5f1ff
4 files changed
+113 -31

No files matched your search

+22 -1
View File
@@ -2499,10 +2499,31 @@ int Engine::configure_kv() {
/* Resolved once, for the cache below and for Scheduler::init later. Resolving it separately in
* each is how the two come to disagree, and the interval is the field where that shows. */
RAD_TRY(chunk_geometry_resolve(ranks_[0]->program, cfg_, &geo_));
/* ONE BLOCK TABLE SERVES EVERY RANK (serve() checks it), so every rank carves the SAME number
* of paged blocks. The ranks' pools are not the same size under an uneven tensor-parallel
* split -- a rank with more delta-net heads holds more recurrent state, a rank with zero
* attention weights has more pool -- so each is planned first, asked what it could carve, and
* every rank is capped at the least before any carves. At an even split the cap is what
* every rank would have carved anyway. */
int64_t least = -1;
for (auto& r : ranks_) {
LogRank lr(r->index);
RAD_TRY(r->kv.plan_groups(r->program.kv_groups, cfg_));
const int64_t t = r->kv.paged_tokens(r->pools.kv().avail());
if (least < 0 || t < least) least = t;
}
for (auto& r : ranks_) {
LogRank lr(r->index);
RAD_TRY(rad_dev_set(r->device)); /* allocates on the CURRENT device; see plan() */
RAD_TRY(r->kv.configure(r->program.kv_groups, cfg_, r->pools.kv()));
if (ranks_.size() > 1) {
const int64_t own = r->kv.paged_tokens(r->pools.kv().avail());
if (own > least)
RAD_INFO("kv: rank %d could page %lld tokens and carves %lld, the least rank's, so "
"that one block table serves every rank", r->index, (long long)own,
(long long)least);
r->kv.cap_paged_tokens(least);
}
RAD_TRY(r->kv.carve(r->pools.kv()));
/* THE ONE CASE WHERE THE CACHE CANNOT GIVE ANYTHING BACK, said once and named. Every
* caller is written against the elastic contract and runs unchanged here -- the pool
* simply holds its whole carve -- so nothing fails, and the only symptom is an expert
+43 -30
View File
@@ -362,6 +362,44 @@ void KVManager::forget() {
report_.clear();
}
int64_t KVManager::paged_split(int64_t budget_bytes, std::vector<int64_t>* blocks) const {
/* Not a policy: size the groups to exhaust at the same total token count. Any other split
* strands memory in whichever group outlasts the others, and a long-context server would then
* run out of full-attention blocks with a window pool still half empty. */
std::vector<int64_t> out(plans_.size(), 0);
int64_t remaining = budget_bytes - fixed_;
std::vector<int32_t> open;
for (const auto& p : plans_) if (p.paged()) open.push_back(p.index);
int64_t tokens = 0;
while (!open.empty() && remaining > 0) {
double cost = 0;
for (int32_t i : open)
cost += (double)plans_[i].bytes_per_block / (double)plans_[i].block_size;
if (cost <= 0) break;
tokens = (int64_t)((double)remaining / cost);
/* ONE BLOCK TABLE SERVES EVERY RANK (Engine::serve checks it), so a rank with more pool
* than its peers carves no more than the least of them. */
if (tokens_cap_ > 0 && tokens > tokens_cap_) tokens = tokens_cap_;
/* A windowed group cannot use more than window_blocks per sequence; anything past that
* is memory nobody will ever address. Cap it and give the bytes back to the others. */
int32_t capped = -1;
for (int32_t i : open) {
const auto& p = plans_[i];
if (p.kind != RAD_KV_WINDOW) continue;
if (tokens / p.block_size > max_seqs_ * p.window_blocks) { capped = i; break; }
}
if (capped < 0) break;
out[capped] = max_seqs_ * plans_[capped].window_blocks;
remaining -= out[capped] * plans_[capped].bytes_per_block;
open.erase(std::find(open.begin(), open.end(), capped));
}
for (int32_t i : open) out[i] = remaining > 0 ? tokens / plans_[i].block_size : 0;
if (blocks) *blocks = std::move(out);
return tokens;
}
int KVManager::carve_into(Pool& kv_pool) {
/* The pool is whatever the operator stated or the resolver derived; nothing here re-decides
* it. What this does decide is how the groups share it, which is arithmetic, not policy. */
@@ -390,36 +428,11 @@ int KVManager::carve_into(Pool& kv_pool) {
return RAD_E_NOMEM;
}
/* ---- the paged split. Not a policy: size the groups to exhaust at the same total token
* count. Any other split strands memory in whichever group outlasts the others, and a
* long-context server would then run out of full-attention blocks with a window pool still
* half empty. ---- */
int64_t remaining = budget_bytes - fixed;
std::vector<int32_t> open;
for (auto& p : plans_) if (p.paged()) open.push_back(p.index);
int64_t tokens = 0;
while (!open.empty()) {
double cost = 0;
for (int32_t i : open)
cost += (double)plans_[i].bytes_per_block / (double)plans_[i].block_size;
if (cost <= 0) break;
tokens = (int64_t)((double)remaining / cost);
/* A windowed group cannot use more than window_blocks per sequence; anything past that
* is memory nobody will ever address. Cap it and give the bytes back to the others. */
int32_t capped = -1;
for (int32_t i : open) {
const auto& p = plans_[i];
if (p.kind != RAD_KV_WINDOW) continue;
if (tokens / p.block_size > max_seqs_ * p.window_blocks) { capped = i; break; }
}
if (capped < 0) break;
plans_[capped].n_blocks = max_seqs_ * plans_[capped].window_blocks;
remaining -= plans_[capped].n_blocks * plans_[capped].bytes_per_block;
open.erase(std::find(open.begin(), open.end(), capped));
}
for (int32_t i : open) plans_[i].n_blocks = tokens / plans_[i].block_size;
/* ---- the paged split (paged_split below): every rank's share of it is the same arithmetic,
* and under a cap it is the least rank's. ---- */
std::vector<int64_t> blocks;
(void)paged_split(budget_bytes, &blocks);
for (auto& p : plans_) if (p.paged()) p.n_blocks = blocks[p.index];
for (auto& p : plans_) {
if (!p.paged()) continue;
+13
View File
@@ -168,6 +168,14 @@ public:
/* The part of every ceiling that is not context: recurrent and conv state for max_seqs
* sequences, the checkpoint slots and the padding. Set by plan_groups(). */
int64_t fixed_bytes() const { return fixed_; }
/* THE TOKEN COUNT THE PAGED GROUPS SETTLE ON for `budget_bytes` of pool -- carve()'s split
* with nothing carved. One block table serves every rank, so every rank's paged groups must
* hold the SAME number of blocks; under an uneven tensor-parallel split the ranks' pools are
* not the same size (a rank with more delta-net heads holds more state and has less left for
* pages, and a rank with no attention weights has more). The engine asks each rank this and
* cap_paged_tokens() the least on all of them before carve(). 0 is no cap. */
int64_t paged_tokens(int64_t budget_bytes) const { return paged_split(budget_bytes, nullptr); }
void cap_paged_tokens(int64_t tokens) { tokens_cap_ = tokens > 0 ? tokens : 0; }
int carve(Pool& kv_pool);
const std::vector<KVGroupPlan>& plans() const { return plans_; }
@@ -497,6 +505,10 @@ private:
* through cannot leave plans_ answering guards for block state that was never sized. forget()
* empties both together and is what plan_groups() starts from. */
int carve_into(Pool& kv_pool);
/* THE PAGED SPLIT, as arithmetic: the token count the paged groups exhaust at together for
* `budget_bytes` of pool, under tokens_cap_, and each group's block count in `blocks` (by group
* index; a windowed group capped at its window). carve_into() spends what this says. */
int64_t paged_split(int64_t budget_bytes, std::vector<int64_t>* blocks) const;
void forget();
/* One traversal serving both checkpoint directions; see checkpoint_save. */
@@ -550,6 +562,7 @@ private:
int64_t fixed_ = 0; /* the non-negotiable floor: states, conv slots, checkpoints, padding */
int64_t ck_total_ = 0; /* the checkpoint part of fixed_, named for the refusal message */
int64_t ckpts_ = 0; /* --checkpoint-slots, as plan_groups() resolved it */
int64_t tokens_cap_ = 0; /* cap_paged_tokens(): the least any rank can carve; 0 is none */
/* THE SLOT STRIDE AND WHERE THE SLOTS BEGIN. The stride is checkpoint_bytes_ rounded up to a
* whole number of commit granules, so a slot can be backed and released without touching a
* neighbour; the offset is where the eager startup commit stops, because everything from
+35
View File
@@ -473,6 +473,41 @@ TEST(kv_configure_reports_and_sizes) {
((1ll << 20) - 8 * 16384 - 4 * 16384 - 2 * RAD_ALIGN_UNIT) / 2048);
}
TEST(kv_ranks_with_unequal_pools_carve_the_least_ranks_blocks_under_a_cap) {
/* One block table serves every rank (Engine::serve), so every rank's paged groups must hold
* the same number of blocks. Under an uneven tensor-parallel split the pools are not equal --
* here a rank with 1 MiB and one with 2 MiB -- and Engine::configure_kv plans each, asks what
* it could page, and caps every rank at the least before any carves. */
Fixture a, b;
a.cfg.checkpoint_slots = b.cfg.checkpoint_slots = 0;
b.cfg.vram_kv_mib = 2;
CHECK_OK(a.pools.configure(a.cfg, 0, &rad_malloc_allocator(), &rad_malloc_allocator()));
CHECK_OK(b.pools.configure(b.cfg, 0, &rad_malloc_allocator(), &rad_malloc_allocator()));
CHECK_OK(a.kv.plan_groups(hybrid_groups(), a.cfg));
CHECK_OK(b.kv.plan_groups(hybrid_groups(), b.cfg));
const int64_t ta = a.kv.paged_tokens(a.pools.kv().avail());
const int64_t tb = b.kv.paged_tokens(b.pools.kv().avail());
CHECK(ta > 0);
CHECK(tb > ta); /* the larger pool pages more on its own */
const int64_t least = ta < tb ? ta : tb;
a.kv.cap_paged_tokens(least);
b.kv.cap_paged_tokens(least);
CHECK_OK(a.kv.carve(a.pools.kv()));
CHECK_OK(b.kv.carve(b.pools.kv()));
CHECK_EQ(a.kv.plan(G_FULL)->n_blocks, b.kv.plan(G_FULL)->n_blocks);
CHECK_EQ(a.kv.plan(G_FULL)->n_blocks, least / 16);
/* The cap does not touch what is not paged: each rank's own state count stays its own. */
CHECK_EQ(a.kv.plan(G_GDN)->n_states, b.kv.plan(G_GDN)->n_states);
/* paged_tokens() carved nothing: the uncapped figure is what carve() spends without a cap. */
Fixture c;
c.cfg.checkpoint_slots = 0;
CHECK_OK(c.pools.configure(c.cfg, 0, &rad_malloc_allocator(), &rad_malloc_allocator()));
CHECK_OK(c.kv.plan_groups(hybrid_groups(), c.cfg));
const int64_t tc = c.kv.paged_tokens(c.pools.kv().avail());
CHECK_OK(c.kv.carve(c.pools.kv()));
CHECK_EQ(c.kv.plan(G_FULL)->n_blocks, tc / 16);
}
TEST(kv_refuses_a_group_with_no_block_size) {
/* Block size comes off the resolved attention kernel. A zero here means the attention op did
* not resolve, and the manager refuses rather than inventing 16. */