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

+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;