kv: a group a rank keeps no cache for

rad_kv_group_zero(b, group): the group is declared as on every other rank --
the scheduler indexes groups by position and one block table serves every
rank -- and this rank carves nothing for it: its block count is the shared
table's, its costs are zero, its pool base is null, and issue refuses any op
on this rank that names it. For a rank the geometry gives no attention heads,
whose attention is a fill of zeros and never stores or reads a key. qwen4exp
declares its attention group so on such a rank. The arch test's contract
becomes the real one: same KV groups by position; ops are each rank's own.
This commit is contained in:
baKon authored and StillDeadcode committed 2026-10-06 11:23:06 +02:00
1 parent 7be92ec8f4
commit 3e629e7a27
9 files changed
+95 -18

No files matched your search

+8
View File
@@ -381,6 +381,14 @@ int rad_weight_shard_span_parts(RadBuilder* b, rad_weight w, const int64_t* lo,
* projection contributes exactly nothing to the reduce. Additive export. */
int rad_weight_shard_zero(RadBuilder* b, rad_weight w);
/* THIS RANK KEEPS NO CACHE FOR THE GROUP. The group is declared as on every other rank -- the
* scheduler indexes groups by position and one block table serves every rank, so it must exist
* here with the same block size and layers -- and nothing is carved for it: it costs this rank no
* pool. It is for a rank the geometry gives no attention heads (rad_arch.h Geom::attn_zero),
* whose attention is a `fill` of zeros and never reads a key. No op on this rank may name the
* group as a RAD_KV operand after this; the engine refuses one at issue. Additive export. */
int rad_kv_group_zero(RadBuilder* b, rad_kvgroup g);
#define RAD_BUFS(...) ((const rad_buf[]){ __VA_ARGS__ }), \
(int)(sizeof((const rad_buf[]){ __VA_ARGS__ }) / sizeof(rad_buf))
+5
View File
@@ -1130,6 +1130,11 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c
lay.attn.qsa_exact_to = m.qsacfg.budget + m.qsacfg.ratio - 1;
}
RAD_ARCH_TRY(rad_bind_layer_kv(b, (int)l, m.kv_full));
/* AN ATTENTION-ZERO RANK KEEPS NO ATTENTION CACHE. Its block is a fill of zeros
* (rad_block_attn_gated_fp8.h) and never stores or reads a key; the indexer below
* reads only the group's block table, which every rank shares. Once per model: the
* group is one and the call is idempotent, but the first layer is the honest place. */
if (g.attn_zero && m.kv_full) RAD_ARCH_TRY(rad_kv_group_zero(b, m.kv_full));
lay.attn.ar_out_take = lay.hc_mix.ar_take();
RAD_ARCH_TRY(lay.attn.declare(b, nm, g, (int)l, m.kv_full, m.n_rot, aw, as,
false, false, kArNone, lay.hc_mix.op_ar_write != 0));
+1
View File
@@ -152,6 +152,7 @@ public:
int weight_shard_span_parts(rad_weight w, const int64_t* lo, const int64_t* hi,
const int64_t* total, int n);
int weight_shard_zero(rad_weight w);
int kv_group_zero(rad_kvgroup g);
/* Finalise: weight first/last use, KV group geometry, buffer liveness and packing. Returns
* RAD_OK, or the first structural error class. Resolution MISSES are not an error here -- an
+12
View File
@@ -926,6 +926,13 @@ int Builder::weight_shard_span_parts(rad_weight h, const int64_t* lo, const int6
return RAD_OK;
}
int Builder::kv_group_zero(rad_kvgroup h) {
if (finished_) { err(RAD_E_STATE, "kv_group_zero after declare finished"); return RAD_E_STATE; }
if (h == 0 || h >= prog_.kv_groups.size()) return RAD_E_INVAL;
prog_.kv_groups[h].zero = true;
return RAD_OK;
}
int Builder::weight_shard_zero(rad_weight h) {
if (finished_) { err(RAD_E_STATE, "weight_shard_zero after declare finished"); return RAD_E_STATE; }
if (h == 0 || h >= prog_.weights.size()) return RAD_E_INVAL;
@@ -1673,6 +1680,11 @@ int rad_weight_shard_zero(RadBuilder* b, rad_weight w) {
return B(b)->weight_shard_zero(w);
}
int rad_kv_group_zero(RadBuilder* b, rad_kvgroup g) {
if (!b) return RAD_E_INVAL;
return B(b)->kv_group_zero(g);
}
int rad_decl_name_map(RadBuilder* b, const RadNameMap* m) {
if (!b) return RAD_E_INVAL;
return B(b)->decl_name_map(m);
+17
View File
@@ -256,6 +256,14 @@ int KVManager::plan_groups(const std::vector<KVGroupInfo>& groups, const Config&
RAD_ERR("KV group '%s': unknown kind %d", p.name.c_str(), p.kind);
return RAD_E_INVAL;
}
/* A ZERO GROUP COSTS THIS RANK NOTHING: declared for its position and its block count,
* never carved, never read. The validation above still ran on the real sizes, so a zero
* group is a real group on every other rank. */
if (g.zero) {
p.zero = true;
p.bytes_per_block = 0;
p.bytes_per_state = 0;
}
plans_.push_back(std::move(p));
}
bundling_ = compare_bundling(bundles);
@@ -453,6 +461,15 @@ int KVManager::carve_into(Pool& kv_pool) {
int64_t bytes = p.paged() ? p.n_blocks * p.bytes_per_block
: p.n_states * p.bytes_per_state;
if (p.paged()) paged_taken += bytes;
if (p.zero) {
p.base = nullptr;
p.pool_offset = 0;
p.pool_bytes = 0;
RAD_INFO("KV group '%s': this rank keeps no cache for it (rad_kv_group_zero) -- %lld "
"blocks of the shared table, nothing carved", p.name.c_str(),
(long long)p.n_blocks);
continue;
}
int64_t off = 0;
void* ptr = kv_pool.reserve(p.name, bytes, RAD_ALIGN_UNIT, &off);
if (!ptr && !kv_pool.accounting_only()) {
+4
View File
@@ -74,6 +74,10 @@ struct KVGroupPlan {
int64_t window_blocks = 0; /* WINDOW: the per-sequence block cap */
int64_t n_blocks = 0; /* paged groups: blocks carved from the pool */
/* rad_kv_group_zero: this rank keeps no cache for the group. The plan exists for its position
* and its block count (one block table serves every rank), its costs are zero and nothing is
* carved; no op on this rank names it (issue refuses a group with no pool bound). */
bool zero = false;
int64_t n_states = 0; /* LINEAR/CONV: n_seq_states * state_copies */
/* HOW MANY PHYSICAL SLOTS ONE LOGICAL STATE COSTS. One, normally: a sequence owns a state.
* Under speculation a linear kernel needs somewhere to put what it cannot commit yet -- it
+1
View File
@@ -295,6 +295,7 @@ struct KVGroupInfo {
int64_t bytes_per_block = 0;
int64_t bytes_per_state = 0; /* linear/conv: per sequence */
std::vector<int> layers;
bool zero = false; /* rad_kv_group_zero: declared for position, nothing carved */
};
/* A RAD_KV_CONV GROUP'S WINDOW, IN ENTRIES: conv_width - 1 of history and one more for every
+6 -4
View File
@@ -760,10 +760,12 @@ int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
const uint32_t g = o.handle;
if ((size_t)g >= kv_.size() || !kv_[g].base)
return abort_step_msg(h, RAD_E_STATE,
fmt("operand %d names KV group %u, which has no pool bound. "
"The block manager allocates it at load and binds it "
"into the Ctx; a group with none is a startup bug, not "
"a runtime condition", i, g));
fmt("operand %d names KV group %u, which has no pool bound on "
"this rank. The block manager allocates it at load and "
"binds it into the Ctx; a group with none is a startup "
"bug -- or a group this rank declared with "
"rad_kv_group_zero and then issued against, which the "
"plugin must not do", i, g));
const KVPoolBinding& b = kv_[g];
/* A POSITION LOOKUP, not `o.offset - first_layer`. The subtraction is right only for a
* group whose layers are contiguous, and they need not be: with 15 attention layers
+41 -14
View File
@@ -66,7 +66,7 @@ struct RecMap {
std::vector<std::string> src;
int concat_dim = 0;
};
struct RecKV { std::string name; RadKVGroupDecl d{}; };
struct RecKV { std::string name; RadKVGroupDecl d{}; bool zero = false; };
struct RecIssue {
rad_op op = 0;
std::vector<RadOperand> opd;
@@ -173,7 +173,7 @@ extern "C" int rad_op_resolved(RadBuilder* b, rad_op h) {
extern "C" rad_kvgroup rad_decl_kv_group(RadBuilder* b, const char* name,
const RadKVGroupDecl* d) {
b->kv_groups.push_back({name, *d});
b->kv_groups.push_back({name, *d, false});
return (rad_kvgroup)b->kv_groups.size();
}
@@ -245,6 +245,11 @@ extern "C" int rad_weight_shard_zero(RadBuilder* b, rad_weight w) {
rw.zero = true;
return RAD_OK;
}
extern "C" int rad_kv_group_zero(RadBuilder* b, rad_kvgroup g) {
if (!b || g == 0 || g > b->kv_groups.size()) return RAD_E_INVAL;
b->kv_groups[g - 1].zero = true;
return RAD_OK;
}
extern "C" int rad_declare_drafter(RadBuilder* b, const RadDrafterDecl* d) {
(void)b; (void)d;
return RAD_OK;
@@ -2509,29 +2514,51 @@ TEST(flashnext_tp3_zeroes_the_third_ranks_attention_and_splits_gdn_heads_unevenl
REQUIRE_EQ(qwen4exp_fp8::declare(&b[r], &meta, &c[r]), RAD_OK);
}
/* The three programs declare the same ops in the same order and the same KV groups: the
* scheduler sees one program. */
/* EVERY RANK DECLARES THE SAME KV GROUPS IN THE SAME ORDER: the scheduler indexes groups by
* position and one block table serves every rank. The OPS are each rank's own -- issue walks
* a rank's program -- and the attention ranks declare the same ops as each other. */
for (int r = 1; r < world; ++r) {
REQUIRE_EQ(b[r].ops.size(), b[0].ops.size());
REQUIRE_EQ(b[r].kv_groups.size(), b[0].kv_groups.size());
for (size_t i = 0; i < b[0].ops.size(); ++i) CHECK_EQ(b[r].ops[i].op, b[0].ops[i].op);
for (size_t g = 0; g < b[0].kv_groups.size(); ++g) {
CHECK_EQ(b[r].kv_groups[g].name, b[0].kv_groups[g].name);
CHECK_EQ(b[r].kv_groups[g].d.kind, b[0].kv_groups[g].d.kind);
}
if (r < attn_world) {
REQUIRE_EQ(b[r].ops.size(), b[0].ops.size());
for (size_t i = 0; i < b[0].ops.size(); ++i) CHECK_EQ(b[r].ops[i].op, b[0].ops[i].op);
}
}
for (int r = 0; r < world; ++r) {
const bool zero = r >= attn_world;
/* (1) ATTENTION: every rank the two-rank geometry -- 12 heads, one KV head, gqa 12. */
const RecOp* at = find_op(b[r], "attn_paged");
REQUIRE(at != nullptr);
CHECK_EQ(pint(at, "n_head"), n_head / attn_world);
CHECK_EQ(pint(at, "gqa"), n_head / attn_world);
CHECK_EQ(b[r].kv_groups[0].d.n_head_kv, 1);
/* (2) THE PROJECTIONS: the span of this rank's heads in the container on every rank --
* rank 2 the last attention rank's, so its shapes match -- and zeros on rank 2. */
/* THE ATTENTION-ZERO RANK COMPUTES NO ATTENTION: no attention op, no projection weight,
* a `fill` of its output wire, and no cache carved for the attention group. */
if (zero) {
CHECK(find_op(b[r], "attn_paged") == nullptr);
CHECK(find_op(b[r], "kv_store") == nullptr);
CHECK(find_op(b[r], "fill") != nullptr);
CHECK(find_w(b[r], kq) == nullptr);
CHECK(find_w(b[r], qg) == nullptr);
CHECK(find_w(b[r], oo) == nullptr);
CHECK(b[r].kv_groups[0].zero);
/* The all-reduce it still joins, and the indexer it still runs. */
CHECK(find_op(b[r], "qsa_select") != nullptr);
} else {
CHECK(!b[r].kv_groups[0].zero);
CHECK(find_op(b[r], "fill") == nullptr);
}
/* (1) ATTENTION: every attention rank the two-rank geometry -- 12 heads, one KV head,
* gqa 12. */
const RecOp* at = find_op(b[r], "attn_paged");
if (!zero) {
REQUIRE(at != nullptr);
CHECK_EQ(pint(at, "n_head"), n_head / attn_world);
CHECK_EQ(pint(at, "gqa"), n_head / attn_world);
}
/* (2) THE PROJECTIONS: the span of this rank's heads in the container. */
for (const std::string& wn : {kq, kv}) {
if (zero) break;
const RecWeight* w = find_w(b[r], wn);
REQUIRE(w != nullptr);
CHECK_EQ(w->d.shape[0], head_dim);
@@ -2541,7 +2568,7 @@ TEST(flashnext_tp3_zeroes_the_third_ranks_attention_and_splits_gdn_heads_unevenl
CHECK_EQ(w->span_hi, w->span_lo + head_dim);
CHECK_EQ(w->zero, zero);
}
{
if (!zero) {
const RecWeight* w = find_w(b[r], qg);
REQUIRE(w != nullptr);
const int ar = zero ? attn_world - 1 : r;