drafter: a serial head that keeps no cache of its own

rad_drafter_shared_kv(b, pos_offset), an additive builder export: the
declared SERIAL head reads the trunk's K and V and writes none (Gemma 4's
assistant head has a query projection and nothing else per layer). A
plugin that does not call it drafts exactly as before; RAD_ABI_VERSION is
untouched (same pattern as rad_kv_group_zero).

For such a head the serial driver changes in two places:

- No history is owed. The history pass carries round 1 only: one row a
  drafting sequence, at the committed position, with the token the trunk
  chose after it and the trunk's hidden state there. A sequence that does
  not draft gets no row; a step with nothing to draft runs no pass.
- Every round runs at that same position, over the same context. seqused
  ends at the committed position: past it, the trunk's cache holds the K
  and V of drafts the verify rejected, or nothing.

`pos_offset` (0 or 1) is where the rows sit for the rotary, relative to the
committed position; it moves nothing else.

Card side: ADV_HIST and ADV_ROUND take the shape from AdvanceArgs::shared_kv
and rot_shift, which the scheduler fills from the Program. Host side:
serial_begin/serial_batch stage one row and a constant ceiling.

Tests: build_test checks the export's refusals; sched_test stages one verify
step both ways (the old path pinned: five history rows, rounds stepping),
checks that a cacheless head runs no pass when nothing drafts, and runs the
card's advance rows directly for both shapes.
This commit is contained in:
keptn42 committed 2026-10-10 16:29:34 +00:00
1 parent d59e0bb003
commit 997e1ae05f
11 files changed
+376 -10

No files matched your search

+1
View File
@@ -134,6 +134,7 @@ public:
int declare_logits(rad_buf b);
int declare_pooled(rad_buf b);
int declare_drafter(const RadDrafterDecl* d);
int drafter_shared_kv(int pos_offset);
int declare_encoder(const RadEncoderDecl* d);
void note_v (const char* f, va_list ap);
+31
View File
@@ -1272,6 +1272,32 @@ int Builder::declare_drafter(const RadDrafterDecl* d) {
return RAD_OK;
}
/* A SERIAL HEAD THAT READS THE TRUNK'S CACHE AND KEEPS NONE (rad_builder.h). Checked here for the
* reason declare_drafter checks everything: the driver only reaches the head once a request
* speculates, and a head staged the wrong way round costs acceptance and says nothing. */
int Builder::drafter_shared_kv(int pos_offset) {
if (finished_) {
err(RAD_E_STATE, "drafter_shared_kv after declare finished");
return RAD_E_STATE;
}
if (prog_.drafter.kind != RAD_DRAFT_SERIAL) {
err(RAD_E_STATE, "drafter_shared_kv: %s. Only a SERIAL head runs round after round at one "
"position; declare it with rad_declare_drafter first.",
prog_.drafter.kind == RAD_DRAFT_NONE ? "no drafter is declared"
: "the declared drafter is a BLOCK drafter");
return RAD_E_STATE;
}
if (pos_offset != 0 && pos_offset != 1) {
err(RAD_E_INVAL, "drafter_shared_kv '%s': pos_offset %d. The rows sit at the committed "
"position (0) or one past it (1); nothing else names a position the head "
"was trained at.", prog_.drafter_name.c_str(), pos_offset);
return RAD_E_INVAL;
}
prog_.drafter_shared_kv = true;
prog_.drafter_pos_offset = pos_offset;
return RAD_OK;
}
void Builder::note_v(const char* f, va_list ap) {
char msg[1024];
vsnprintf(msg, sizeof msg, f ? f : "", ap);
@@ -1762,6 +1788,11 @@ int rad_declare_drafter(RadBuilder* b, const RadDrafterDecl* d) {
return B(b)->declare_drafter(d);
}
int rad_drafter_shared_kv(RadBuilder* b, int pos_offset) {
if (!b) return RAD_E_INVAL;
return B(b)->drafter_shared_kv(pos_offset);
}
int rad_declare_encoder(RadBuilder* b, const RadEncoderDecl* d) {
if (!b) return RAD_E_INVAL;
return B(b)->declare_encoder(d);
+4
View File
@@ -289,6 +289,10 @@ std::string dump_graph_str(const Program& p, const std::vector<std::string>* err
"%d, drafter window %lld\n",
(long long)p.drafter.block, p.drafter.depth,
(int)p.drafter.mask_token, (long long)p.drafter.window);
if (p.drafter_shared_kv)
s += fmt(" cache none of its own: it reads the trunk's, owes no history, and "
"every round runs at the committed position%s\n",
p.drafter_pos_offset ? " (rotated one past it)" : "");
s += "\n";
}
+6
View File
@@ -357,6 +357,12 @@ public:
* RadDrafterDecl::name points into the plugin's own storage. */
RadDrafterDecl drafter{};
std::string drafter_name;
/* A SERIAL HEAD WITH NO CACHE OF ITS OWN (rad_drafter_shared_kv): it reads the trunk's K and V
* and writes none, so the driver owes it no history and runs every round at the committed
* position. `drafter_pos_offset` is where those rows sit for the rotary, 0 or 1 past it. Both
* are false and 0 for every drafter that did not ask, which is every one that predates it. */
bool drafter_shared_kv = false;
int32_t drafter_pos_offset = 0;
/* THE MEDIA ENCODER THIS PLUGIN DECLARED (rad_declare_encoder). `modalities == 0` when it
* declared none, which is every text-only program and every deployment that asked for no
+7
View File
@@ -94,6 +94,13 @@ struct AdvanceArgs {
* the host's position keeps the host's three components, which is what a media token needs. */
int64_t rope_off = -1;
int32_t rope_pitch = 0;
/* A SERIAL HEAD WITH NO CACHE OF ITS OWN (rad_drafter_shared_kv), read by ADV_HIST and
* ADV_ROUND alone: its history pass is one row a sequence at the committed position, and every
* round sits there too, its context ending at that position. `rot_shift` is where those rows
* sit for the rotary, 0 or 1 past the committed position. Zero for every other head, whose
* passes advance exactly as they always did. */
int32_t shared_kv = 0;
int32_t rot_shift = 0;
};
/* Issue one advance on `s`. The rows are independent, so the card runs them in parallel. */
+21 -3
View File
@@ -109,6 +109,22 @@ RAD_ADV_FN void adv_row(const AdvanceArgs& a, int i) {
return;
}
case ADV_HIST: {
if (a.shared_kv) {
/* A HEAD THAT READS THE TRUNK'S CACHE AND KEEPS NONE (rad_drafter_shared_kv). There is
* no history to fill, so the pass is round 1 alone and its one row is the last one of
* the run below: the committed position, the trunk's row there, the token the trunk
* chose after it. Its context is the committed positions and NOT the verify step's
* whole run -- the cache past the committed position holds the K and V of the drafts
* this verify rejected. The host staged the run's first trunk row; the card adds the
* drafts it accepted. */
int32_t* out = adv_i32(a, a.out_off);
tok[k] = a.src[rout + S.acc];
adv_pos(a, pos, k, S.head + a.rot_shift);
out[k] += S.acc;
ctx[i] = S.head;
adv_groups(a, i, k, S.head, 1);
return;
}
/* `aux` positions the host already knew the tokens of, then the verify step's own
* positions up to the last one it committed, each with the token committed after it --
* the sampler's pick there, since the drafts before it agreed with it. The pass has a
@@ -133,12 +149,14 @@ RAD_ADV_FN void adv_row(const AdvanceArgs& a, int i) {
}
case ADV_ROUND: {
/* Round 2 onward: round 1 is the history pass's last row (ADV_HIST above), so every
* round embeds the proposal of the round before it. */
const int32_t base = S.head + a.round - 1;
* round embeds the proposal of the round before it. A head with no cache of its own does
* not step forward: it stays at the committed position over the committed context, and
* what it proposed reaches the next round through its own hidden state. */
const int32_t base = a.shared_kv ? S.head : S.head + a.round - 1;
const int32_t t = a.src[(int64_t)i * a.src_pitch];
S.tok[a.round - 1] = t;
tok[k] = t;
adv_pos(a, pos, k, base);
adv_pos(a, pos, k, a.shared_kv ? base + a.rot_shift : base);
ctx[i] = base;
adv_groups(a, i, k, base, 1);
return;
+27 -5
View File
@@ -110,6 +110,14 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac
"drafter's window is recomputed", (long long)draft_refill_);
}
serial_shared_ = cfg.n_spec > 0 && prog.drafter.kind == RAD_DRAFT_SERIAL &&
prog.drafter_shared_kv;
serial_rot_shift_ = serial_shared_ ? prog.drafter_pos_offset : 0;
if (serial_shared_)
RAD_INFO("serial drafter: reads the trunk's cache and keeps none -- no history pass, one "
"row a drafting sequence, every round at the committed position%s",
serial_rot_shift_ ? " (rotated one past it)" : "");
DraftController::Params dp;
dp.max_depth = cfg.n_spec;
/* THE ADAPTIVE WINDOW IS THE SERIAL DRAFTER'S POLICY (draft.h): it prices a draft as one more
@@ -2193,9 +2201,19 @@ int Scheduler::serial_begin(int* rounds, SerialRow* out, int max) {
const int64_t owed_end = e.produces_token ? vstart : (int64_t)e.ctx_len + e.n_tokens;
int64_t hs = s.draft_filled > e.ctx_len ? s.draft_filled : (int64_t)e.ctx_len;
if (hs > owed_end) hs = owed_end;
const int64_t lead = owed_end - hs;
int64_t lead = owed_end - hs;
const int64_t verified = e.produces_token ? e.n_spec + 1 : 0;
const int64_t hist_n = lead + verified;
int64_t hist_n = lead + verified;
/* A HEAD WITH NO CACHE OF ITS OWN IS OWED NO HISTORY (rad_drafter_shared_kv). Its one row
* is round 1's -- the committed position, whichever of the verified ones the card says it
* is -- and a sequence that does not draft has nothing to run at all. */
if (serial_shared_) {
if (!can_draft) continue;
hs = vstart;
lead = 0;
hist_n = 1;
}
if (!can_draft && hist_n <= 0) continue; /* nothing owed and nothing to draft */
@@ -2205,7 +2223,9 @@ int Scheduler::serial_begin(int* rounds, SerialRow* out, int max) {
m.card = e.card;
m.hist_row = (int32_t)(base + (hs - e.ctx_len));
m.hist_n = (int32_t)hist_n;
m.hist_pos = hs;
/* The shared head's one row sits wherever the card's acceptance puts it; the host stages
* the furthest that can be, as it does for every round. */
m.hist_pos = serial_shared_ ? head_max : hs;
m.dev = e.produces_token;
m.dev_tok = (int32_t)(base + (vstart - e.ctx_len));
m.dev_out = e.dev_out;
@@ -2264,8 +2284,10 @@ const RadBatch* Scheduler::serial_batch(int round, int step) {
e.card = m.card;
e.n_tokens = round == 0 ? m.hist_n : 1;
/* A round's position is the card's; the host stages the furthest it can be, which is what
* every host-side ceiling reads. The history's are known here exactly. */
e.ctx_len = (int32_t)(round == 0 ? m.hist_pos : m.base_pos + (round - 1));
* every host-side ceiling reads. The history's are known here exactly. A head with no
* cache of its own does not step forward, so neither does its ceiling. */
e.ctx_len = (int32_t)(round == 0 ? m.hist_pos
: m.base_pos + (serial_shared_ ? 0 : round - 1));
e.n_spec = 0;
/* One-based and constant: a head pass commits exactly the tokens it was handed. The head
* issues no linear-state op, so nothing reads it; it is set rather than left at a value
+14 -2
View File
@@ -191,8 +191,14 @@ public:
/* The batch the last build produced for one rank; see BatchBuilder::batch_for. */
const RadBatch* batch_for(int rank) const { return builder_.batch_for(rank); }
/* The advance's view of that build on one rank; see BatchBuilder::advance_args. */
AdvanceArgs advance_args(int rank) const { return builder_.advance_args(rank); }
/* The advance's view of that build on one rank; see BatchBuilder::advance_args. It carries
* the serial head's shape as well, which only the draft passes' modes read. */
AdvanceArgs advance_args(int rank) const {
AdvanceArgs a = builder_.advance_args(rank);
a.shared_kv = serial_shared_ ? 1 : 0;
a.rot_shift = serial_rot_shift_;
return a;
}
void fini();
/* ---------------------------------------------------------- off the step path */
@@ -522,6 +528,12 @@ private:
* that point would give back more than two windows is kept whole. The drafter's window, or 0
* where no hit can leave such a hole. */
int64_t draft_refill_ = 0;
/* A SERIAL HEAD WITH NO CACHE OF ITS OWN (rad_drafter_shared_kv), off the Program at init. It
* owes no history, so serial_begin gives it one row a drafting sequence and none to a sequence
* that does not draft, and its rounds stay at the committed position (advance.h). False and 0
* for every other drafter, whose passes are staged exactly as before. */
bool serial_shared_ = false;
int32_t serial_rot_shift_ = 0;
BatchBuilder builder_;
DraftController draft_;