Archived
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:
1 parent
d59e0bb003
commit
997e1ae05f
11 files changed
+376
-10
No files matched your search
@@ -546,6 +546,31 @@ typedef struct RadDrafterDecl {
|
||||
|
||||
int rad_declare_drafter(RadBuilder* b, const RadDrafterDecl* d);
|
||||
|
||||
/* A SERIAL HEAD WITH NO CACHE OF ITS OWN. Every attention layer of it reads one of the TRUNK's
|
||||
* layers' K and V and writes none -- Gemma 4's assistant head, whose layers have a query
|
||||
* projection and nothing else, reads the last sliding and the last global trunk layer. Called
|
||||
* once, after rad_declare_drafter, by a plugin whose serial head is that shape. Additive export: a
|
||||
* plugin that does not call it drafts exactly as before, and RAD_ABI_VERSION does not move.
|
||||
*
|
||||
* What it changes in the serial driver, and each item follows from the head writing nothing:
|
||||
*
|
||||
* NO HISTORY IS OWED. A history pass exists to fill a head's own K and V, and there are none to
|
||||
* fill. The pass carries round 1 alone: ONE row a drafting sequence, at the committed position,
|
||||
* with the token the trunk chose after it and the trunk's hidden state there (out_ids). A
|
||||
* sequence that does not draft gets no row, and a step with nothing to draft no pass.
|
||||
*
|
||||
* EVERY ROUND RUNS AT THAT SAME POSITION, OVER THE SAME CONTEXT. A head with a cache of its own
|
||||
* steps one position a round and attends to what it wrote there. This one has nowhere to write,
|
||||
* and the trunk's cache past the committed position holds the K and V of drafts the last verify
|
||||
* REJECTED, or of nothing. So every round's `seqused` ends at the committed position, and what
|
||||
* the head proposed travels forward in its hidden state and its next input token instead.
|
||||
*
|
||||
* `pos_offset` is where those rows sit for the rotary, as an offset from the committed position:
|
||||
* 0 is the position of the trunk hidden state the head continues from, 1 the position of the token
|
||||
* it embeds. It moves nothing else -- the context ends at the committed position either way -- and
|
||||
* which one a checkpoint was trained with is the plugin's to know. */
|
||||
int rad_drafter_shared_kv(RadBuilder* b, int pos_offset);
|
||||
|
||||
/* ================================================================== the encoder */
|
||||
/* A MEDIA ENCODER THE PLUGIN RUNS ON AN ENCODER PASS (RadBatch::enc), declared for the reason the
|
||||
* logits buffer and the drafter are: the core drives the pass and reads its result, so it has to
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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. */
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
@@ -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_;
|
||||
|
||||
|
||||
@@ -1005,6 +1005,49 @@ TEST(the_drafter_name_survives_the_program_leaving_the_builder) {
|
||||
CHECK_EQ(p.drafter.depth, 3);
|
||||
}
|
||||
|
||||
/* A HEAD THAT KEEPS NO CACHE OF ITS OWN (rad_drafter_shared_kv) is a property of a declared SERIAL
|
||||
* drafter. With none declared, with a block drafter, or at a position no head sits at, the call is
|
||||
* refused at declare -- a head staged the wrong way round costs acceptance and says nothing. */
|
||||
TEST(a_head_that_keeps_no_cache_of_its_own_is_a_serial_one) {
|
||||
Registry reg;
|
||||
{
|
||||
Builder b(reg, make_meta(), make_ctx());
|
||||
CHECK_EQ(rad_drafter_shared_kv(&b, 0), RAD_E_STATE); /* nothing declared */
|
||||
}
|
||||
{
|
||||
Builder b(reg, make_meta(), make_ctx());
|
||||
RadBufDecl bd{};
|
||||
bd.dtype = RAD_I32; bd.rank = 2; bd.shape[0] = 256; bd.shape[1] = 4;
|
||||
const rad_buf prop = rad_decl_buffer(&b, "prop", &bd);
|
||||
RadDrafterDecl d{};
|
||||
d.kind = RAD_DRAFT_BLOCK; d.name = "df"; d.depth = 4; d.mask_token = 5;
|
||||
d.proposal = prop; d.proposal_pitch = 4;
|
||||
CHECK_OK(rad_declare_drafter(&b, &d));
|
||||
CHECK_EQ(rad_drafter_shared_kv(&b, 0), RAD_E_STATE); /* a block drafter */
|
||||
CHECK(!b.program().drafter_shared_kv);
|
||||
}
|
||||
{
|
||||
Builder b(reg, make_meta(), make_ctx());
|
||||
RadDrafterDecl d{};
|
||||
d.kind = RAD_DRAFT_SERIAL; d.name = "mtp"; d.depth = 4;
|
||||
CHECK_OK(rad_declare_drafter(&b, &d));
|
||||
CHECK_EQ(rad_drafter_shared_kv(&b, 2), RAD_E_INVAL); /* no position a head sits at */
|
||||
CHECK(!b.program().drafter_shared_kv);
|
||||
CHECK_OK(rad_drafter_shared_kv(&b, 1));
|
||||
CHECK(b.program().drafter_shared_kv);
|
||||
CHECK_EQ(b.program().drafter_pos_offset, 1);
|
||||
}
|
||||
{
|
||||
/* Every drafter that does not ask keeps the old shape. */
|
||||
Builder b(reg, make_meta(), make_ctx());
|
||||
RadDrafterDecl d{};
|
||||
d.kind = RAD_DRAFT_SERIAL; d.name = "mtp"; d.depth = 4;
|
||||
CHECK_OK(rad_declare_drafter(&b, &d));
|
||||
CHECK(!b.program().drafter_shared_kv);
|
||||
CHECK_EQ(b.program().drafter_pos_offset, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* ================================================================== the enum name tables
|
||||
*
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "rad_test.h"
|
||||
|
||||
#include "sched/scheduler.h"
|
||||
#include "sched/advance_impl.h"
|
||||
#include "sched/derive.h"
|
||||
#include "server/sink.h"
|
||||
|
||||
@@ -3014,6 +3015,202 @@ TEST(the_history_pass_carries_draft_round_one) {
|
||||
f.sch.serial_end(2);
|
||||
}
|
||||
|
||||
/* THE SERIAL DRIVER'S TWO SHAPES, side by side over one verify step. A head with a cache of its own
|
||||
* (every drafter but one) owes the history of every verified position and steps one position a
|
||||
* round; a head that reads the trunk's cache and keeps none (rad_drafter_shared_kv) owes nothing,
|
||||
* runs one row a drafting sequence, and does not step. The first half of this is the old path and
|
||||
* must not move: it is what Qwen's MTP head is staged with. */
|
||||
namespace {
|
||||
struct SerialShape {
|
||||
int hist_tok = 0, hist_out = 0, hist_dout = 0;
|
||||
int32_t hist_ctx = 0, hist_row = 0;
|
||||
int32_t round_ctx[4] = {0, 0, 0, 0};
|
||||
};
|
||||
|
||||
void serial_shape(bool shared, SerialShape& r) {
|
||||
Fixture f;
|
||||
f.build(/*blocks=*/4096, /*states=*/64);
|
||||
f.cfg.n_spec = 4;
|
||||
f.prog.drafter.kind = RAD_DRAFT_SERIAL;
|
||||
f.prog.drafter.depth = 4;
|
||||
f.prog.drafter_shared_kv = shared;
|
||||
CHECK_OK(f.start());
|
||||
|
||||
const uint64_t a = f.submit(160, 64);
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
f.commit_plain(11);
|
||||
int32_t draft[4] = { 21, 22, 23, 24 };
|
||||
CHECK_OK(f.sch.set_draft(a, draft, 4));
|
||||
const RadBatch* b = f.sch.step();
|
||||
REQUIRE(b != nullptr);
|
||||
CHECK_EQ(b->n_spec, 4);
|
||||
|
||||
int rounds = 4;
|
||||
Scheduler::SerialRow rows[4];
|
||||
CHECK_EQ(f.sch.serial_begin(&rounds, rows, 4), 1);
|
||||
const RadBatch* hb = f.sch.serial_batch(0, 1);
|
||||
REQUIRE(hb != nullptr);
|
||||
CHECK(hb->draft_pass < 0);
|
||||
r.hist_tok = (int)hb->n_tok;
|
||||
r.hist_out = (int)hb->n_out;
|
||||
r.hist_dout = (int)hb->n_draft_out;
|
||||
r.hist_ctx = hb->ctx_lens[0];
|
||||
r.hist_row = hb->out_ids[0];
|
||||
for (int rd = 2; rd <= 3; ++rd) {
|
||||
const RadBatch* rb = f.sch.serial_batch(rd, 1);
|
||||
REQUIRE(rb != nullptr);
|
||||
CHECK_EQ(rb->n_tok, 1);
|
||||
r.round_ctx[rd] = rb->ctx_lens[0];
|
||||
}
|
||||
f.sch.serial_end(3);
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
TEST(a_head_with_a_cache_of_its_own_is_staged_as_it_always_was) {
|
||||
SerialShape o;
|
||||
serial_shape(false, o);
|
||||
CHECK_EQ(o.hist_tok, 5); /* every verified position */
|
||||
CHECK_EQ(o.hist_out, 5); /* one trunk row each */
|
||||
CHECK_EQ(o.hist_dout, 1);
|
||||
CHECK_EQ(o.hist_ctx, 160); /* the history starts at the first verified one */
|
||||
CHECK_EQ(o.hist_row, 0); /* the verify step's first row */
|
||||
CHECK_EQ(o.round_ctx[2], 165); /* the furthest round 2 can sit: one past the last */
|
||||
CHECK_EQ(o.round_ctx[3], 166); /* ...and the head steps one position a round */
|
||||
}
|
||||
|
||||
TEST(a_head_with_no_cache_of_its_own_runs_one_row_and_stays_put) {
|
||||
SerialShape s;
|
||||
serial_shape(true, s);
|
||||
CHECK_EQ(s.hist_tok, 1); /* round 1 alone */
|
||||
CHECK_EQ(s.hist_out, 1);
|
||||
CHECK_EQ(s.hist_dout, 1);
|
||||
CHECK_EQ(s.hist_ctx, 164); /* the furthest the committed position can be */
|
||||
CHECK_EQ(s.hist_row, 0); /* the run's first trunk row; the card adds acc */
|
||||
CHECK_EQ(s.round_ctx[2], 164); /* and every round stays there */
|
||||
CHECK_EQ(s.round_ctx[3], 164);
|
||||
}
|
||||
|
||||
/* A HEAD WITH NO CACHE OF ITS OWN IS OWED NO HISTORY: a prefill chunk that drafts nothing runs no
|
||||
* pass for it at all, where a head with its own cache is given the whole chunk. */
|
||||
TEST(a_head_with_no_cache_of_its_own_runs_no_pass_when_nothing_drafts) {
|
||||
for (int shared = 0; shared < 2; ++shared) {
|
||||
Fixture f;
|
||||
f.build(/*blocks=*/10, /*states=*/64); /* exactly the prompt: nothing to look ahead into */
|
||||
f.cfg.n_spec = 4;
|
||||
f.prog.drafter.kind = RAD_DRAFT_SERIAL;
|
||||
f.prog.drafter.depth = 4;
|
||||
f.prog.drafter_shared_kv = shared != 0;
|
||||
CHECK_OK(f.start());
|
||||
f.submit(160, 64);
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
f.commit_plain();
|
||||
int rounds = 4;
|
||||
Scheduler::SerialRow rows[4];
|
||||
CHECK_EQ(f.sch.serial_begin(&rounds, rows, 4), 0);
|
||||
const RadBatch* hb = f.sch.serial_batch(0, 1);
|
||||
if (shared) {
|
||||
CHECK(hb == nullptr);
|
||||
} else {
|
||||
REQUIRE(hb != nullptr);
|
||||
CHECK_EQ(hb->n_tok, 160);
|
||||
}
|
||||
f.sch.serial_end(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* WHAT THE CARD WRITES INTO A DRAFT PASS, row by row (advance_impl.h), for both shapes. One verify
|
||||
* step at positions 101..105 accepted two drafts, so the committed position is 103 and the trunk
|
||||
* chose token 7003 there; the block table maps position p to slot p. A head with a cache of its own
|
||||
* gets the verified run with its tail repeated and a context that runs to its end; a head with no
|
||||
* cache of its own gets the committed position and a context that ends there, in every round. */
|
||||
namespace {
|
||||
struct AdvSlab {
|
||||
alignas(16) int32_t w[512] = {};
|
||||
rad::StepSlot st{};
|
||||
int32_t src[8] = { 7001, 7002, 7003, 7004, 7005, 0, 0, 0 };
|
||||
rad::AdvanceArgs a;
|
||||
|
||||
/* word offsets */
|
||||
enum { ROWS = 0, TOK = 8, POS = 16, CTX = 24, ACC = 28, OUT = 32, SID = 40, CU = 44, Q = 48,
|
||||
GRP = 64, SLOT = 96, USED = 104, BT = 112, FIRST = 128 };
|
||||
|
||||
AdvSlab(int mode, int n_rows, int round, bool shared, int shift) {
|
||||
a.mode = mode;
|
||||
a.round = round;
|
||||
a.n_seq = 1;
|
||||
a.n_groups = 1;
|
||||
a.slab = reinterpret_cast<char*>(w);
|
||||
a.row_off = ROWS * 4; a.row_pitch = 1;
|
||||
a.tok_off = TOK * 4; a.pos_off = POS * 4; a.ctx_off = CTX * 4; a.acc_off = ACC * 4;
|
||||
a.out_off = OUT * 4; a.sid_off = SID * 4; a.cu_off = CU * 4; a.q_off = Q * 4;
|
||||
a.grp_off = GRP * 4;
|
||||
a.st = &st;
|
||||
a.src = src;
|
||||
a.shared_kv = shared ? 1 : 0;
|
||||
a.rot_shift = shift;
|
||||
w[ROWS + 0] = 1; /* a device row */
|
||||
w[ROWS + 1] = 0; /* its first verified token row */
|
||||
w[ROWS + 2] = 0; /* its first sampled row */
|
||||
w[ROWS + 3] = 0; /* no history before the verified positions */
|
||||
w[SID] = 0; w[CU] = 0; w[Q] = n_rows;
|
||||
w[OUT] = 40; /* the host staged the run's first trunk row */
|
||||
rad::AdvGroup g{};
|
||||
g.block_size = 16; g.paged = 1; g.pitch = 16;
|
||||
g.slot_off = SLOT * 4; g.used_off = USED * 4; g.bt_off = BT * 4; g.first_off = FIRST * 4;
|
||||
std::memcpy(&w[GRP], &g, sizeof g);
|
||||
for (int i = 0; i < 16; ++i) w[BT + i] = i; /* block i holds positions 16i.. */
|
||||
w[FIRST] = 0;
|
||||
st.head = 103; st.acc = 2; st.ctx = 104; st.n_acc = 3; st.tok[0] = 7003;
|
||||
}
|
||||
};
|
||||
} /* namespace */
|
||||
|
||||
TEST(the_card_stages_a_cacheless_head_at_the_committed_position) {
|
||||
/* The history pass. */
|
||||
{
|
||||
AdvSlab o(rad::ADV_HIST, 5, 0, false, 0);
|
||||
rad::adv_row(o.a, 0);
|
||||
const int32_t pos[5] = { 101, 102, 103, 103, 103 }, tok[5] = { 7001, 7002, 7003, 7003, 7003 };
|
||||
for (int j = 0; j < 5; ++j) {
|
||||
CHECK_EQ(o.w[AdvSlab::POS + j], pos[j]);
|
||||
CHECK_EQ(o.w[AdvSlab::TOK + j], tok[j]);
|
||||
CHECK_EQ(o.w[AdvSlab::OUT + j], 40 + (j < 2 ? j : 2));
|
||||
CHECK_EQ(o.w[AdvSlab::SLOT + j], pos[j]);
|
||||
}
|
||||
CHECK_EQ(o.w[AdvSlab::USED], 106); /* the verify step's whole run */
|
||||
CHECK_EQ(o.w[AdvSlab::CTX], 101);
|
||||
}
|
||||
for (int shift = 0; shift < 2; ++shift) {
|
||||
AdvSlab s(rad::ADV_HIST, 1, 0, true, shift);
|
||||
rad::adv_row(s.a, 0);
|
||||
CHECK_EQ(s.w[AdvSlab::POS], 103 + shift);
|
||||
CHECK_EQ(s.w[AdvSlab::TOK], 7003);
|
||||
CHECK_EQ(s.w[AdvSlab::OUT], 42);
|
||||
CHECK_EQ(s.w[AdvSlab::USED], 104); /* the committed positions and no more */
|
||||
CHECK_EQ(s.w[AdvSlab::CTX], 103);
|
||||
}
|
||||
/* Round 3: the old head two positions on, the cacheless one where it was. */
|
||||
{
|
||||
AdvSlab o(rad::ADV_ROUND, 1, 3, false, 0);
|
||||
o.src[0] = 9001;
|
||||
rad::adv_row(o.a, 0);
|
||||
CHECK_EQ(o.w[AdvSlab::POS], 105);
|
||||
CHECK_EQ(o.w[AdvSlab::USED], 106);
|
||||
CHECK_EQ(o.w[AdvSlab::TOK], 9001);
|
||||
CHECK_EQ(o.st.tok[2], 9001);
|
||||
}
|
||||
for (int shift = 0; shift < 2; ++shift) {
|
||||
AdvSlab s(rad::ADV_ROUND, 1, 3, true, shift);
|
||||
s.src[0] = 9001;
|
||||
rad::adv_row(s.a, 0);
|
||||
CHECK_EQ(s.w[AdvSlab::POS], 103 + shift);
|
||||
CHECK_EQ(s.w[AdvSlab::USED], 104);
|
||||
CHECK_EQ(s.w[AdvSlab::CTX], 103);
|
||||
CHECK_EQ(s.w[AdvSlab::TOK], 9001);
|
||||
CHECK_EQ(s.st.tok[2], 9001);
|
||||
}
|
||||
}
|
||||
|
||||
/* A STEP IS ISSUED AHEAD OF A COMMIT ONLY FOR A REQUEST WHOSE SPECULATION THAT COMMIT CANNOT
|
||||
* CHANGE. The ahead step is sized before the commit in front of it, so a request the sampler has
|
||||
* not started yet -- whose grammar, if any, nobody has asked -- is not issued ahead; one whose
|
||||
|
||||
Reference in new issue
Block a user