From cb5a69ac3f993560ed8dcb32fa524b3b9f987c62 Mon Sep 17 00:00:00 2001 From: Ashlyn Date: Sat, 17 Oct 2026 10:17:39 -0500 Subject: [PATCH] engine: a block drafter on the card's acceptance -- its passes queued behind the step, the next step issued ahead A block drafter (DFlash) ran its two passes after the commit, staged from the committed sequence: the verify, a host wait and the commit, the context and query passes, a second host wait for the proposals, then the next step's build and issue, with the cards idle through each host turn and the ranks starting every pass at different times. Pipelining was off for block drafters. It now takes the serial drafters' path (sched/advance.h). After the trunk's ranks join, the acceptance runs on every rank (ADV_ACCEPT). block_begin runs before the commit (on_card): the context pass mirrors the trunk step, whose positions the host knows (a row issued ahead continued from the committed end, as commit() reads it); the query block's rows are device rows staged at the furthest anchor acceptance could leave, with the KV reservation that far, and ADV_BLOCK puts the anchor (the token the verify chose) and the masks at the card's committed end. After the query pass, ADV_BTAKE takes the proposals into each StepSlot behind the anchor, which is the token row the next verify reads (ADV_TRUNK), so issue_ahead verifies them with no host round trip. The slots come back with the step's one readback; finish_block_drafter checks the card's acceptance against the host's, as the serial driver does, publishes the proposals and moves the drafter's filled range. RADIANCE_DRAFT_BLOCK_HOST=1 keeps the host's driver, for the A/B, and has a row in GUIDE.md's environment table. On libref with the tiny 31B-shaped checkpoint (and one doctored so its drafter is right part of the time) the two drivers give the same replies, the same draft acceptance and the same sampled text, and greedy matches plain decoding. sched_test runs 24 pipelined steps against the host's driver: the same trunk steps and the same context and query passes, positions, lengths and slots (the context pass's token ids aside, which that pass does not read); advance_test covers the two modes and the chain. Co-Authored-By: Claude Opus 5.5 --- core/engine.cpp | 33 +++++--- core/engine.h | 17 +++- core/engine_draft.cpp | 134 +++++++++++++++++++++++------ core/sched/advance.h | 12 ++- core/sched/advance_impl.h | 17 ++++ core/sched/scheduler.cpp | 39 +++++++-- core/sched/scheduler.h | 13 ++- docs/GUIDE.md | 1 + spec.md | 4 +- tests/advance_test.cpp | 120 ++++++++++++++++++++++++++ tests/sched_test.cpp | 173 ++++++++++++++++++++++++++++++++++++++ 11 files changed, 508 insertions(+), 55 deletions(-) diff --git a/core/engine.cpp b/core/engine.cpp index fff9bb5f..f5d0d25e 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -353,6 +353,8 @@ int Engine::issue_ahead(const RadBatch** out) { if (!pipelining_) return RAD_OK; for (int i = 0; i < serial_rows_ && serial_done_ > 0; ++i) sched_.expect_draft(serial_row_[(size_t)i].seq, serial_done_); + for (int i = 0; i < block_rows_; ++i) + sched_.expect_draft(block_row_[(size_t)i].seq, drafter_.depth); const RadBatch* nb = sched_.step_next(); if (!nb) return RAD_OK; /* Its rows' tokens, positions and slots, advanced on the card behind this step's acceptance @@ -403,9 +405,13 @@ int Engine::step_loop() { int idle = 0; std::vector cancel; /* Not in KL mode: its scored rows are copied out of the logits buffer after collection, and a - * pass issued ahead would overwrite them. */ - pipelining_ = !cfg_.kld() && !embedding_ && drafter_.kind != RAD_DRAFT_BLOCK && - (serial_drafting() || cfg_.n_spec <= 0); + * pass issued ahead would overwrite them. A block drafter pipelines only on the card + * (engine_draft.cpp); the host's driver stages from the committed sequence. */ + block_card_ = drafter_.kind == RAD_DRAFT_BLOCK && drafter_.depth > 0 && !cfg_.kld() && + !embedding_ && [] { const char* v = std::getenv("RADIANCE_DRAFT_BLOCK_HOST"); + return !(v && *v && *v != '0'); }(); + pipelining_ = !cfg_.kld() && !embedding_ && (drafter_.kind != RAD_DRAFT_BLOCK || block_card_) && + (serial_drafting() || block_card_ || cfg_.n_spec <= 0); const bool t_on = (int)log_level() >= (int)Log::Debug; /* The next step's forward pass when already issued behind the previous step, before its * readback (Scheduler::step_next). */ @@ -696,10 +702,10 @@ int Engine::step_loop() { } if (t_on) phase_mark(PH_SAVE); - /* Queue the serial drafter's passes behind the step before waiting: the card computes - * acceptance itself and every pass reads it there (sched/advance.h), so one wait covers - * the whole step. */ - st = issue_serial_drafter(b->step, so); + /* Queue the drafter's passes behind the step before waiting: the card computes acceptance + * itself and every pass reads it there (sched/advance.h), so one wait covers the whole + * step. */ + st = block_card_ ? issue_block_drafter(b->step, so) : issue_serial_drafter(b->step, so); if (st < 0) return fail_loop(st); st = mark_step_end(b, so); if (st < 0) return fail_loop(st); @@ -763,12 +769,13 @@ int Engine::step_loop() { if (t_on) phase_mark(PH_COMMIT); if (st < 0) { RAD_ERR("commit: %s", rad_strerror(st)); return fail_loop(st); } - /* The drafter for the next step. A block drafter runs here, since its passes are staged - * from the committed sequence. A serial one already ran on the card; what remains is to - * check its acceptance against the host's and publish its proposals. Both drivers return - * immediately for a kind that is not theirs. */ - st = drafter_.kind == RAD_DRAFT_BLOCK ? run_block_drafter(b->step) - : finish_serial_drafter(so); + /* The drafter for the next step. The host's block driver runs here, since it stages from + * the committed sequence. A serial one, or a block one on the card, already ran; what + * remains is to check its acceptance against the host's and publish its proposals. Each + * driver returns immediately for a kind that is not its own. */ + st = drafter_.kind != RAD_DRAFT_BLOCK ? finish_serial_drafter(so) + : block_card_ ? finish_block_drafter(so) + : run_block_drafter(b->step); if (st < 0) return fail_loop(st); /* The step ends here, after its draft rounds, so step timing includes them * (Scheduler::step_finished). */ diff --git a/core/engine.h b/core/engine.h index e8b4e180..ca9c8e36 100644 --- a/core/engine.h +++ b/core/engine.h @@ -382,8 +382,8 @@ private: int issue_ahead(const RadBatch** out); /* The arena level and the followers' pools, before any release that runs a forward pass. */ int prepare_release(const RadBatch* b); - /* Whether a step may be issued ahead: requires that the drafter, if any, runs on the card (a - * block drafter and the prompt-lookup drafter both read the committed sequence). */ + /* Whether a step may be issued ahead: requires that the drafter, if any, runs on the card (the + * prompt-lookup drafter and the host's block driver both read the committed sequence). */ bool pipelining_ = false; int finish_serial_drafter(const StepOut& so); /* Handed from issue_serial_drafter to finish_serial_drafter: the drafting rows, the rounds @@ -407,8 +407,19 @@ private: void phase_mark(int p); /* Block driver (spec §10): a context pass mirroring the step just run, then one pass that - * fills every drafted position at once. DFlash2 has this shape. */ + * fills every drafted position at once. DFlash2 has this shape. run_block_drafter stages both + * after the commit; on the card (`block_card_`, the default), issue_block_drafter queues them + * behind the step before its readback and finish_block_drafter publishes the proposals after + * the commit, so the step can be issued ahead. RADIANCE_DRAFT_BLOCK_HOST=1 keeps the host's. */ int run_block_drafter(int step); + int block_pass(const RadBatch* db, const char* what, int step); + bool block_card_ = false; + int issue_block_drafter(int step, StepOut& so); + int finish_block_drafter(const StepOut& so); + /* Handed from issue_block_drafter to finish_block_drafter and issue_ahead: the drafting rows, + * in the query pass's order, and how many there are (0 when no query pass ran). */ + std::vector block_row_; + int block_rows_ = 0; /* Encoder driver (spec §11, core/engine_mm.cpp): encodes every media item the scheduler waits * on, in passes of whole segments, and reads it back into the item. Runs between steps, never * inside one. */ diff --git a/core/engine_draft.cpp b/core/engine_draft.cpp index 61a12d26..cc43a80f 100644 --- a/core/engine_draft.cpp +++ b/core/engine_draft.cpp @@ -275,7 +275,37 @@ int Engine::finish_serial_drafter(const StepOut& so) { * one. * * Proposals go out through set_draft() like every other. + * + * Two drivers run these passes. run_block_drafter, after the step commits, stages both from the + * committed sequence and reads the proposals back: two host waits a step with the card idle + * between them, and no step issued ahead. The card's driver (issue_block_drafter, the default) + * queues both behind the step before its readback, from the card's acceptance, as the serial + * driver does; RADIANCE_DRAFT_BLOCK_HOST=1 selects the host's. */ + +/* One drafter pass through the barrier. Neither pass samples (no logits in the context pass; the + * query pass has its own selector), so the sampler chain is zero rows. */ +int Engine::block_pass(const RadBatch* db, const char* what, int step) { + for (auto& rk : ranks_) { + int s = rk->sampler.build_greedy(0); + if (s < 0) { + RAD_ERR("drafter '%s': build_greedy: %s", drafter_.name, rad_strerror(s)); + return s; + } + } + issue_what_.store(kIssueAll, std::memory_order_release); + step_batch_.store(db, std::memory_order_release); + barrier_->arrive_and_wait(); + barrier_->arrive_and_wait(); + const int s = step_status_.load(std::memory_order_acquire); + if (s < 0) + RAD_ERR("drafter '%s': %s pass of step %d failed: %s -- the engine cannot continue", + drafter_.name, what, step, rad_strerror(s)); + /* Probe after each pass; by the step loop's dump these buffers hold stale values. */ + if (s >= 0) dump_buffers(ranks_[0]->ctx, step, what); + return s; +} + int Engine::run_block_drafter(int step) { if (drafter_.kind != RAD_DRAFT_BLOCK || drafter_.depth <= 0) return RAD_OK; @@ -294,39 +324,16 @@ int Engine::run_block_drafter(int step) { return RAD_E_STATE; } - /* Neither pass samples (no logits in the context pass; the query pass has its own selector), - * so the sampler chain is zero rows. */ - auto run_pass = [&](const RadBatch* db, const char* what) -> int { - for (auto& rk : ranks_) { - int s = rk->sampler.build_greedy(0); - if (s < 0) { - RAD_ERR("drafter '%s': build_greedy: %s", drafter_.name, rad_strerror(s)); - return s; - } - } - issue_what_.store(kIssueAll, std::memory_order_release); - step_batch_.store(db, std::memory_order_release); - barrier_->arrive_and_wait(); - barrier_->arrive_and_wait(); - const int s = step_status_.load(std::memory_order_acquire); - if (s < 0) - RAD_ERR("drafter '%s': %s pass of step %d failed: %s -- the engine cannot continue", - drafter_.name, what, step, rad_strerror(s)); - /* Probe after each pass; by the step loop's dump these buffers hold stale values. */ - if (s >= 0) dump_buffers(r0.ctx, step, what); - return s; - }; - /* The context pass runs even when nothing drafts: a mid-prompt sequence has hidden states the * drafter will need, and skipping would leave a permanent hole in its cache. */ - if (const RadBatch* cb = sched_.block_context(step)) RAD_TRY(run_pass(cb, "context")); + if (const RadBatch* cb = sched_.block_context(step)) RAD_TRY(block_pass(cb, "context", step)); /* Cleared so a query batch that could not be built does not republish the previous step's * proposals. */ draft_out_.clear(); if (n > 0) { if (const RadBatch* qb = sched_.block_query(step)) { - RAD_TRY(run_pass(qb, "query")); + RAD_TRY(block_pass(qb, "query", step)); const int64_t n_raw = (int64_t)n * steps; int32_t* const raw = readback_.reserve(n_raw); if (!raw) return RAD_E_NOMEM; @@ -346,4 +353,81 @@ int Engine::run_block_drafter(int step) { return RAD_OK; } +/* The card's driver: both passes behind the step, without waiting. After the trunk's ranks join, + * the acceptance runs on every rank (ADV_ACCEPT), leaving each sequence's anchor and committed + * end in its StepSlot. The context pass mirrors the trunk step, whose positions the host knows. + * The query pass's rows are device rows that the card positions at the committed end (ADV_BLOCK), + * and its proposals go into the slots behind the anchor (ADV_BTAKE): exactly the token row the + * next verify reads (ADV_TRUNK), so the step issued ahead verifies them with no host round trip. + * collect_step's wait covers both passes and the slots' readback. */ +int Engine::issue_block_drafter(int step, StepOut& so) { + block_rows_ = 0; + if (!block_card_) return RAD_OK; + + RAD_TRY(advance_all(ADV_ACCEPT, 0, false)); + /* The sampled ids before either pass, as the serial driver reads them. */ + RAD_TRY(collect_issue(so)); + + const int steps = drafter_.depth; + const int block = drafter_.block > 0 ? (int)drafter_.block : drafter_.depth + 1; + block_row_.resize((size_t)cfg_.max_seqs); + const int n = sched_.block_begin(block, steps, (int)drafter_.window, drafter_.mask_token, + block_row_.data(), (int)block_row_.size(), /*on_card=*/true); + + /* Even when nothing drafts; see run_block_drafter. */ + if (const RadBatch* cb = sched_.block_context(step)) RAD_TRY(block_pass(cb, "context", step)); + if (n <= 0) return RAD_OK; + const RadBatch* qb = sched_.block_query(step); + if (!qb) return RAD_OK; + RAD_TRY(advance_all(ADV_BLOCK, 0, false)); + RAD_TRY(block_pass(qb, "query", step)); + RAD_TRY(advance_all(ADV_BTAKE, steps, true, n)); + Rank& r0 = *ranks_[0]; + RAD_TRY(rad_memcpy_async(slots_host_, r0.step_slots, + (int64_t)cfg_.max_seqs * (int64_t)sizeof(StepSlot), r0.ctx.stream())); + block_rows_ = n; + return RAD_OK; +} + +/* After commit: check the host's acceptance against the card's, which positioned the query pass + * (a disagreement is a defect and the step is refused, as in finish_serial_drafter), move the + * drafter's filled range, and publish each row's proposal from the slots. */ +int Engine::finish_block_drafter(const StepOut& so) { + if (!block_card_) return RAD_OK; + if (block_rows_ > 0) { + const StepPlan& plan = sched_.plan(); + for (size_t i = 0; i < plan.e.size(); ++i) { + const StepEntry& e = plan.e[i]; + if (so.first_row[i] < 0) continue; + const StepSlot& S = slots_host_[e.card]; /* in range: the builder refuses others */ + if (S.acc != so.accepted[i] || S.tok[0] != so.token[i]) { + RAD_ERR("drafter '%s': the card accepted %d draft(s) ending in token %d for " + "sequence %llu and the host %d ending in %d; the query pass ran from the " + "card's", drafter_.name, S.acc, S.tok[0], (unsigned long long)e.seq, + so.accepted[i], so.token[i]); + sched_.block_end(); + return RAD_E_STATE; + } + } + } + sched_.block_end(); + if (block_rows_ <= 0) return RAD_OK; + + Rank& r0 = *ranks_[0]; + const int steps = drafter_.depth; + draft_out_.assign((size_t)block_rows_ * (size_t)steps, 0); + for (int i = 0; i < block_rows_; ++i) { + const StepSlot& S = slots_host_[block_row_[(size_t)i].card]; + for (int j = 0; j < steps; ++j) + draft_out_[(size_t)i * (size_t)steps + (size_t)j] = S.tok[1 + j]; + } + for (int i = 0; i < block_rows_; ++i) { + /* As in finish_serial_drafter: the step issued ahead only carries a request whose answer + * is fixed (Scheduler::step_next_locked), so this agrees with the count it verifies. */ + if (!r0.sampler.allows_speculation(block_row_[(size_t)i].seq)) continue; + sched_.set_draft(block_row_[(size_t)i].seq, &draft_out_[(size_t)i * (size_t)steps], steps); + } + return RAD_OK; +} + } /* namespace rad */ diff --git a/core/sched/advance.h b/core/sched/advance.h index ec6dc705..6776a123 100644 --- a/core/sched/advance.h +++ b/core/sched/advance.h @@ -44,6 +44,9 @@ enum AdvMode : int32_t { ADV_HIST = 3, /* before the draft head's history pass: the verified positions */ ADV_ROUND = 4, /* before draft round `round` >= 2: the next position, the last proposal */ ADV_TAKE = 5, /* after the last round: its proposal into the state */ + /* A block drafter (RAD_DRAFT_BLOCK) on the card: */ + ADV_BLOCK = 6, /* before the query pass: the anchor at the committed end, then the masks */ + ADV_BTAKE = 7, /* after it: its `round` proposals into the state, behind the bonus token */ }; /* One KV group, as the builder stages it for this build. Offsets are bytes into the rank's slab, @@ -63,7 +66,8 @@ struct AdvGroup { * kernel's argument. */ struct AdvanceArgs { int32_t mode = 0; - int32_t round = 0; /* ADV_ROUND: the draft round, from 2; ADV_TAKE: the last, from 1 */ + int32_t round = 0; /* ADV_ROUND: the draft round, from 2; ADV_TAKE: the last, from 1; + * ADV_BTAKE: how many proposals a row */ int32_t n_seq = 0; int32_t n_groups = 0; char* slab = nullptr; /* this rank's device slab */ @@ -72,13 +76,15 @@ struct AdvanceArgs { /* [4][row_pitch] int32: device-row flag; the verify step's token row of the row's first * verified position; its first sampled row, or -1 for a row that sampled nothing; and one * number the mode reads -- ADV_ACCEPT the row's draft count, ADV_HIST how many of the row's - * positions come before the verified ones, whose tokens the host staged. */ + * positions come before the verified ones, whose tokens the host staged, ADV_BLOCK the token + * id its mask rows embed. */ int64_t row_off = 0; int32_t row_pitch = 0; int64_t grp_off = 0; /* [n_groups] AdvGroup */ StepSlot* st = nullptr; /* [max_seqs], by scheduler slot */ /* ADV_ACCEPT and ADV_HIST: the verify step's sampled tokens, one a row. ADV_ROUND and - * ADV_TAKE: the last round's proposals, row i at i * src_pitch. */ + * ADV_TAKE: the last round's proposals, row i at i * src_pitch. ADV_BTAKE: the block's + * proposals, row i's from i * src_pitch. */ const int32_t* src = nullptr; int64_t src_pitch = 1; const int32_t* eog = nullptr; /* end-of-generation ids, sorted */ diff --git a/core/sched/advance_impl.h b/core/sched/advance_impl.h index df289759..bf331cce 100644 --- a/core/sched/advance_impl.h +++ b/core/sched/advance_impl.h @@ -159,6 +159,23 @@ RAD_ADV_FN void adv_row(const AdvanceArgs& a, int i) { case ADV_TAKE: S.tok[a.round] = a.src[(int64_t)i * a.src_pitch]; return; + case ADV_BLOCK: { + /* A block drafter's query row: the anchor (the token the verify just chose) at the + * committed end, where the next verify starts, then `aux`, the mask id, at every position + * after it. The host staged the furthest the anchor can be. */ + const int32_t base = S.ctx; + tok[k] = S.tok[0]; + for (int32_t j = 1; j < n; ++j) tok[k + j] = aux; + for (int32_t j = 0; j < n; ++j) adv_pos(a, pos, k + j, base + j); + ctx[i] = base; + adv_groups(a, i, k, base, n); + return; + } + case ADV_BTAKE: + /* Row i is the query pass's i-th block, which is the i-th drafting sequence. */ + for (int32_t j = 0; j < a.round; ++j) + S.tok[1 + j] = a.src[(int64_t)i * a.src_pitch + j]; + return; default: return; } diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index b2fac3e8..f2bac4e9 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -2300,8 +2300,14 @@ void Scheduler::serial_end(int rounds_done) { * it exactly, rejected suffix included: the drafter reads hidden states positionally from its * own buffer, so batch row i must be buffer row i. The rejected positions lie in [n_computed, * n_computed + block), which the query pass overwrites before anything reads them, and the - * ensure() below covers them since n_computed + block reaches the trunk step's own end. */ -int Scheduler::block_begin(int block, int depth, int window, int32_t mask, BlockRow* out, int max) { + * ensure() below covers them since n_computed + block reaches the trunk step's own end. + * + * On the card the step has not committed yet. The step began at n_computed (a row issued ahead + * was staged at its furthest reach; it really continued from the committed end, as commit() + * reads it), and the anchor lands somewhere in [that + n_tokens - n_spec, that + n_tokens] + * depending on acceptance: the gates take the end that binds them, the query rows the furthest. */ +int Scheduler::block_begin(int block, int depth, int window, int32_t mask, BlockRow* out, int max, + bool on_card) { std::lock_guard lk(mu_); block_.clear(); block_len_ = 0; @@ -2348,20 +2354,28 @@ int Scheduler::block_begin(int block, int depth, int window, int32_t mask, Block } SchedReq& s = *sp; Request& r = *s.req; - d.base_pos = r.n_computed; + if (on_card && e.dev) d.ctx_len = (int32_t)r.n_computed; + /* The anchor's position, the sequence's committed end: lowest and highest it can be. */ + const int64_t next_lo = on_card ? (int64_t)d.ctx_len + e.n_tokens - e.n_spec : r.n_computed; + const int64_t next_hi = on_card ? (int64_t)d.ctx_len + e.n_tokens : r.n_computed; + d.base_pos = next_hi; + d.dev = on_card; /* May it propose? It needs an anchor token (the one the sampler just chose, so the entry * produced one) and a draft cache covering what the block's attention reads. The high end * is this step's context pass, about to cover everything up to the trunk's end. The low - * end is draft_lo, non-zero only after a prefix-cache hit that left a hole. */ - const bool have_tok = e.produces_token && !r.output.empty() && r.n_computed >= 1; + * end is draft_lo, non-zero only after a prefix-cache hit that left a hole. On the card + * the anchor is the card's, and a request that may not speculate is left out here rather + * than at set_draft, since the step issued ahead verifies what this pass proposes. */ + const bool have_tok = on_card ? e.produces_token && r.may_speculate + : e.produces_token && !r.output.empty() && r.n_computed >= 1; /* Where this sequence's draft cache starts once the context pass has run: its own floor, * or for a sequence that never had one, this step's first row (0 for an ordinary prefill, * the hit length after a prefix-cache hit). The binding query row is the lowest, at * n_computed, and it reads back to n_computed - window + 1. */ - const int64_t lo = draft_floor(s, (int64_t)e.ctx_len); + const int64_t lo = draft_floor(s, (int64_t)d.ctx_len); const bool reach = lo <= 0 ? true - : (window > 0 && r.n_computed - window + 1 >= lo); + : (window > 0 && next_lo - window + 1 >= lo); /* RADIANCE_DEBUG_DRAFT counts which gate declined. A drafter that silently stops * proposing looks like collapsed acceptance downstream (accept% is conditional on @@ -2369,9 +2383,9 @@ int Scheduler::block_begin(int block, int depth, int window, int32_t mask, Block const bool ok_room = n_draft_rows < max; /* The query block writes positions n_computed .. n_computed + block - 1, and none of them * may be at or past the end of the context. */ - const bool ok_ctx = ctx_limit_ <= 0 || r.n_computed + block <= ctx_limit_; + const bool ok_ctx = ctx_limit_ <= 0 || next_hi + block <= ctx_limit_; const bool ok_kv = ok_room && ok_ctx && - kv_->ensure_from(r.id, r.n_computed + block, r.n_computed) >= 0; + kv_->ensure_from(r.id, next_hi + block, r.n_computed) >= 0; static const bool dbg = [] { const char* v = std::getenv("RADIANCE_DEBUG_DRAFT"); return v && *v && *v != '0'; }(); if (dbg) { @@ -2483,6 +2497,13 @@ const RadBatch* Scheduler::block_query(int step) { * block = depth it starts on it. */ e.hidden_row = row + block - depth; e.hidden_n = depth; + /* On the card the anchor, its position and every mask come from the card (ADV_BLOCK), + * which reads the mask id here; the host staged them at the furthest position. */ + if (d.dev) { + e.dev = true; + e.dev_out = -1; + e.dev_aux = block_mask_.empty() ? 0 : block_mask_[0]; + } block_plan_.e.push_back(e); block_plan_.n_tok += block; row += block; diff --git a/core/sched/scheduler.h b/core/sched/scheduler.h index 06aa8711..7919e54e 100644 --- a/core/sched/scheduler.h +++ b/core/sched/scheduler.h @@ -333,6 +333,13 @@ public: * * The drafter writes its proposal to its declared proposal buffer, not through the sampler, * and the engine publishes it through set_draft(). + * + * On the card (`on_card`), block_begin runs before commit() instead, right after the step is + * issued, so both passes queue behind it with no readback in between. The context pass is + * unchanged: it mirrors the trunk step, whose positions the host knows. The query pass's + * anchor depends on acceptance, so its rows are device rows (advance.h, ADV_BLOCK) staged at + * the furthest position acceptance could leave, and the reservation covers that far. + * block_end still runs after commit(). */ struct BlockRow { uint64_t seq = 0; @@ -343,7 +350,8 @@ public: * last `depth` rows), `window` its attention window (0 for none) and `mask` the token id its * masked query slots embed. Returns how many sequences may draft and fills `out` with them, * in the order block_query() emits them. */ - int block_begin(int block, int depth, int window, int32_t mask, BlockRow* out, int max); + int block_begin(int block, int depth, int window, int32_t mask, BlockRow* out, int max, + bool on_card = false); const RadBatch* block_context(int step); const RadBatch* block_query(int step); void block_end(); @@ -570,6 +578,9 @@ private: int32_t n_tokens = 0; int64_t base_pos = 0; /* the anchor's position: the sequence's next one */ bool can_draft = false; + /* On the card: the anchor's position is the card's, and `base_pos` the furthest it can + * be (Scheduler::block_begin). */ + bool dev = false; /* A placeholder: the trunk step finished this sequence, so it has no KV and drafts * nothing, but keeps its row so later rows stay aligned with the trunk's. The pass stores * nothing for it (a negative slot is padding). */ diff --git a/docs/GUIDE.md b/docs/GUIDE.md index f09baa09..e2ee6d0c 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -3088,6 +3088,7 @@ reductions running on fewer blocks: Qwen3.8-27B's 41k-token prefill 9.5 s uncapp | `RADIANCE_LOG_STEPS` | log one line per step: its phase, sequences, and each one's token count and context (§4.5) | | `RADIANCE_GEMMA4_OVERLAP` | the Gemma 4 31B's prefill overlap (`arch/gemma4_bf16`, two or more ranks, fused junctions, the exact all-reduce wire): a prefill step of 1024 rows or more runs as two slices, each slice's all-reduces on a second stream beside the other's compute. On by default; `0` turns it off and runs every step whole (the startup notes say which) | | `RADIANCE_AR8_SHARD_WEIGHTS`, `RADIANCE_AR8_PIECES` | the eight-rank two-shot's shard weights (`auto`, the default, reads the PCIe links; `off`; or eight integers) and piece count, with `RADIANCE_AR8_SHARD_WEIGHTS_MIN_ELEMS` and `RADIANCE_AR8_PIECES_MIN_ELEMS` for the message size each starts at (§4.7) | +| `RADIANCE_DRAFT_BLOCK_HOST` | `1` runs a block drafter's (DFlash) passes from the host after each commit, two waits a step and no step issued ahead, instead of on the card's acceptance. For comparing the two drivers; the replies and the drafts' acceptance are the same | Engine development has its own instruments: `RADIANCE_DEBUG_*` (residual probes, routing and issue dumps), `RADIANCE_OP_ORACLE*` (named ops checked against `libref` on the operands the model issued; diff --git a/spec.md b/spec.md index 1ace7bff..8faee7f3 100644 --- a/spec.md +++ b/spec.md @@ -1183,7 +1183,9 @@ core. **Kind is a shape, not a model.** `RAD_DRAFT_SERIAL` runs `depth` dependent passes, each embedding what the last proposed. `RAD_DRAFT_BLOCK` runs a context pass, then one pass that fills every drafted position. MTP and EAGLE-style heads are serial; block-diffusion drafters (DFlash2, DSpark) -are block. +are block. Both run on the card's acceptance (`core/sched/advance.h`): the verify's acceptance, the +positions of every pass after it and the proposals stay on the card, so a step's passes and the +next step are issued before the step is read back, and the host's commit runs beside the card. Separate draft models are not supported; a drafter is part of the model's source. `rad-convert --draft-model` merges a drafter that ships as its own repository into the container diff --git a/tests/advance_test.cpp b/tests/advance_test.cpp index 2e17a85b..60b77bcd 100644 --- a/tests/advance_test.cpp +++ b/tests/advance_test.cpp @@ -419,4 +419,124 @@ TEST(the_card_stages_a_cacheless_head_at_the_committed_position) { } } +/* ================================================================== the block drafter */ + +/* A block drafter's query row on the card: the anchor (the verify's chosen token) at the + * committed end, the row's mask id (`aux`) at every position after it, slots through the row's + * own table and the group's length to the block's end. The host staged the furthest the anchor + * could be; a host row is left as staged. A wrong base drafts from the wrong position and + * acceptance collapses with no error; a wrong slot writes the drafter's K/V into another block. */ +TEST(a_block_query_row_takes_its_anchor_and_position_from_the_step_slot) { + Slab s(ADV_BLOCK, /*n_groups=*/1, /*block=*/4); + /* Row 0: a block of four for card 3, staged at 60; the card has it at 50. */ + s.row(0, true, 0, 4, 60, 3, 0, -1, /*mask=*/4); + for (int b = 0; b < Slab::kPitch; ++b) s.bt(0, 0)[b] = 100 + b; + s.st[3].ctx = 50; s.st[3].tok[0] = 777; + /* Row 1: host-staged, untouched. */ + s.row(1, false, 4, 2, 30, 4, 0, -1, 9); + s.w[Slab::TOK + 4] = 900; s.w[Slab::TOK + 5] = 901; + s.w[Slab::POS + 4] = 30; s.w[Slab::POS + 5] = 31; + /* Row 2: card 1 at 7 with mask 9; its block 1 (positions 4..7) was never assigned. */ + s.row(2, true, 6, 4, 20, 1, 0, -1, 9); + s.bt(0, 2)[1] = -1; + s.st[1].ctx = 7; s.st[1].tok[0] = 555; + s.run(); + + const int32_t tok[10] = { 777, 4, 4, 4, 900, 901, 555, 9, 9, 9 }; + const int32_t pos[10] = { 50, 51, 52, 53, 30, 31, 7, 8, 9, 10 }; + const int32_t slot[10] = { 112 * 4 + 2, 112 * 4 + 3, 113 * 4 + 0, 113 * 4 + 1, 0, 0, + -1, 8, 9, 10 }; + for (int t = 0; t < 10; ++t) { + CHECK_EQ(s.w[Slab::TOK + t], tok[t]); + CHECK_EQ(s.w[Slab::POS + t], pos[t]); + CHECK_EQ(s.slot(0, t), slot[t]); + } + CHECK_EQ(s.w[Slab::CTX + 0], 50); + CHECK_EQ(s.w[Slab::CTX + 1], 30); + CHECK_EQ(s.w[Slab::CTX + 2], 7); + CHECK_EQ(s.used(0, 0), 54); + CHECK_EQ(s.used(0, 2), 11); + CHECK_EQ(s.st[3].tok[0], 777); /* the anchor stays the next verify's first token */ +} + +/* The block's proposals go into the slot behind the anchor, row i's from i * src_pitch: exactly + * the tokens the next verify reads after its first. The anchor and host rows are untouched. */ +TEST(a_block_take_puts_each_rows_proposals_behind_its_anchor) { + Slab s(ADV_BTAKE); + s.a.round = 3; + s.a.src_pitch = 3; + for (int i = 0; i < 3; ++i) { + s.row(i, true, 4 * i, 4, 0, /*card=*/2 - i, 0, -1, 4); + s.st[2 - i].tok[0] = 50 + i; + for (int c = 0; c < 3; ++c) s.src[i * 3 + c] = 1000 * (i + 1) + c; + } + s.row(3, false, 12, 4, 0, 5, 0, -1, 4); + s.src[9] = 4242; + s.run(); + for (int i = 0; i < 3; ++i) { + const StepSlot& S = s.st[2 - i]; + CHECK_EQ(S.tok[0], 50 + i); + for (int c = 0; c < 3; ++c) CHECK_EQ(S.tok[1 + c], 1000 * (i + 1) + c); + } + CHECK(s.st[5].tok[1] != 4242); +} + +/* The chain a pipelined block-drafter step runs, row for row: the verify's acceptance, the query + * block at the committed end, the take, then the step issued ahead. A verify at 101..105 (four + * drafts) accepts two, so the anchor is the target's third pick at 104, and the next verify + * starts there with the anchor and the block's proposals. */ +TEST(a_block_drafter_step_on_the_card_continues_where_the_verify_left_off) { + StepSlot st; + /* The verify: 7000 then drafts 7101..7104; the target picks 7101, 7102, 9999, ... */ + { + Slab v(ADV_ACCEPT); + v.row(0, false, 0, 5, 101, 1, 0, 0, 4); + const int32_t t[5] = { 7000, 7101, 7102, 7103, 7104 }; + const int32_t y[5] = { 7101, 7102, 9999, 1, 2 }; + for (int j = 0; j < 5; ++j) { v.w[Slab::TOK + j] = t[j]; v.src[j] = y[j]; } + v.run(); + st = v.st[1]; + } + CHECK_EQ(st.acc, 2); + CHECK_EQ(st.ctx, 104); + CHECK_EQ(st.tok[0], 9999); + /* The query block of four, staged at the furthest anchor, 106. */ + { + Slab q(ADV_BLOCK); + q.st[1] = st; + q.row(0, true, 0, 4, 106, 1, 0, -1, 4); + q.run(); + const int32_t tok[4] = { 9999, 4, 4, 4 }; + for (int j = 0; j < 4; ++j) { + CHECK_EQ(q.w[Slab::TOK + j], tok[j]); + CHECK_EQ(q.w[Slab::POS + j], 104 + j); + } + st = q.st[1]; + } + /* Its three proposals, then the next verify of 1 + 3. */ + { + Slab t(ADV_BTAKE); + t.st[1] = st; + t.a.round = 3; + t.a.src_pitch = 3; + t.row(0, true, 0, 4, 106, 1, 0, -1, 4); + t.src[0] = 8001; t.src[1] = 8002; t.src[2] = 8003; + t.run(); + st = t.st[1]; + } + { + Slab n(ADV_TRUNK); + n.st[1] = st; + n.row(0, true, 0, 4, 106, 1, 0, 0, 3); + n.run(); + const int32_t tok[4] = { 9999, 8001, 8002, 8003 }; + for (int j = 0; j < 4; ++j) { + CHECK_EQ(n.w[Slab::TOK + j], tok[j]); + CHECK_EQ(n.w[Slab::POS + j], 104 + j); + } + CHECK_EQ(n.w[Slab::CTX], 104); + CHECK_EQ(n.w[Slab::ACC], 3); + } +} + RAD_TEST_MAIN() diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index 4fd32561..294e8b7b 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -3937,6 +3937,179 @@ TEST(a_step_issued_ahead_equals_the_step_built_after_the_commit) { } } +/* ================================================================== a block drafter on the card */ +namespace { +/* Every pass of a block drafter's run: each step's trunk batch, then its context and query + * passes, as the card holds them once its advances have run. */ +struct BlockRun { + std::vector trunk, context, query; + std::vector> out; + int ahead = 0; + int card_host_disagree = 0; +}; +constexpr int kBlk = 8, kBlkDepth = 7; +constexpr int32_t kBlkMask = 99; + +/* The host's driver: both passes built after the commit, from the committed sequence. */ +BlockRun block_reference(int steps) { + Fixture f; + std::vector ids; + ahead_setup(f, kBlkDepth, ids); + BlockRun run; + const RadBatch* b = f.sch.step(); + if (!b) return run; + run.trunk.push_back(read_rows(b, f.sch.plan(), f.kv)); + Scheduler::BlockRow rows[4]; + for (int t = 0; t < steps; ++t) { + CHECK_OK(ahead_commit(f, t)); + CHECK_EQ(f.sch.block_begin(kBlk, kBlkDepth, 0, kBlkMask, rows, 4), 3); + const RadBatch* cb = f.sch.block_context(t + 1); + if (!cb) { CHECK(cb != nullptr); return run; } + run.context.push_back(read_rows(cb, f.sch.plan(), f.kv)); + const RadBatch* qb = f.sch.block_query(t + 1); + if (!qb) { CHECK(qb != nullptr); return run; } + /* The query plan is the scheduler's own; its rows are the drafting sequences in order. */ + StepPlan qp; + for (int i = 0; i < 3; ++i) { StepEntry e; e.seq = rows[i].seq; qp.e.push_back(e); } + run.query.push_back(read_rows(qb, qp, f.kv)); + f.sch.block_end(); + ahead_publish(f, ids, kBlkDepth, t + 1); + b = f.sch.step(); + if (!b) break; + run.trunk.push_back(read_rows(b, f.sch.plan(), f.kv)); + } + for (uint64_t id : ids) run.out.push_back(f.sch.find(id)->output); + return run; +} + +/* The card's driver (Engine::issue_block_drafter): the step's acceptance into the StepSlots, both + * passes built before the commit with the query rows placed by ADV_BLOCK, the proposals into the + * slots (ADV_BTAKE), expect_draft and step_next with ADV_TRUNK, then the commit, block_end, the + * drafts published and promote_next. */ +BlockRun block_pipelined(int steps) { + Fixture f; + std::vector ids; + ahead_setup(f, kBlkDepth, ids); + BlockRun run; + std::vector slots((size_t)f.cfg.max_seqs); + const RadBatch* b = f.sch.step(); + if (!b) return run; + run.trunk.push_back(read_rows(b, f.sch.plan(), f.kv)); + Scheduler::BlockRow rows[4]; + for (int t = 0; t < steps; ++t) { + const StepPlan& p = f.sch.plan(); + std::vector y((size_t)b->n_out + 1, -1); + for (const StepEntry& e : p.e) { + if (e.dev_out < 0) continue; + const int32_t a = ahead_acc(e.seq, t, e.n_spec); + for (int j = 0; j <= e.n_spec; ++j) + y[(size_t)(e.dev_out + j)] = j < a ? ahead_draft(e.seq, t, j) : ahead_tok(e.seq, t); + } + AdvanceArgs acc = f.sch.advance_args(0); + acc.mode = ADV_ACCEPT; + acc.st = slots.data(); + acc.src = y.data(); + for (int i = 0; i < acc.n_seq; ++i) adv_row(acc, i); + for (const StepEntry& e : p.e) { + const StepSlot& S = slots[(size_t)e.card]; + if (S.acc != ahead_acc(e.seq, t, e.n_spec) || S.tok[0] != ahead_tok(e.seq, t)) + ++run.card_host_disagree; + } + + CHECK_EQ(f.sch.block_begin(kBlk, kBlkDepth, 0, kBlkMask, rows, 4, /*on_card=*/true), 3); + const RadBatch* cb = f.sch.block_context(t + 1); + if (!cb) { CHECK(cb != nullptr); return run; } + run.context.push_back(read_rows(cb, f.sch.plan(), f.kv)); + const RadBatch* qb = f.sch.block_query(t + 1); + if (!qb) { CHECK(qb != nullptr); return run; } + AdvanceArgs q = f.sch.advance_args(0); + q.mode = ADV_BLOCK; + q.st = slots.data(); + for (int i = 0; i < q.n_seq; ++i) adv_row(q, i); + StepPlan qp; + for (int i = 0; i < 3; ++i) { StepEntry e; e.seq = rows[i].seq; qp.e.push_back(e); } + run.query.push_back(read_rows(qb, qp, f.kv)); + /* The block's proposals for step t + 1, taken behind the anchor. */ + std::vector prop((size_t)3 * kBlkDepth); + for (int i = 0; i < 3; ++i) + for (int j = 0; j < kBlkDepth; ++j) + prop[(size_t)(i * kBlkDepth + j)] = ahead_draft(rows[i].seq, t + 1, j); + AdvanceArgs tk = f.sch.advance_args(0); + tk.mode = ADV_BTAKE; + tk.round = kBlkDepth; + tk.src = prop.data(); + tk.src_pitch = kBlkDepth; + tk.st = slots.data(); + for (int i = 0; i < tk.n_seq; ++i) adv_row(tk, i); + for (int i = 0; i < 3; ++i) f.sch.expect_draft(rows[i].seq, kBlkDepth); + + const RadBatch* nb = f.sch.step_next(); + if (nb) { + AdvanceArgs tr = f.sch.advance_args(0); + tr.mode = ADV_TRUNK; + tr.st = slots.data(); + for (int i = 0; i < tr.n_seq; ++i) adv_row(tr, i); + } + CHECK_OK(ahead_commit(f, t)); + f.sch.block_end(); + ahead_publish(f, ids, kBlkDepth, t + 1); + if (nb) { + f.sch.promote_next(); + b = nb; + ++run.ahead; + } else { + b = f.sch.step(); + if (!b) break; + } + run.trunk.push_back(read_rows(b, f.sch.plan(), f.kv)); + } + for (uint64_t id : ids) run.out.push_back(f.sch.find(id)->output); + return run; +} +} /* namespace */ + +/* A block drafter on the card runs the passes the host's driver runs after the commit: the same + * trunk steps, and context and query passes with the same positions, lengths and slots once the + * card's advances have filled in what the host only bounded, and the same query tokens. Its query + * block is staged before the step's acceptance is known, at the furthest anchor; a wrong position + * drafts from the wrong place (acceptance collapses, no error) and a wrong slot writes the + * drafter's K/V into another block. Every step after the first runs issued ahead. + * + * The context pass's token ids are not compared: that pass reads hidden states and no ids + * (Scheduler::block_context), and staged before the commit, its accepted positions are not in the + * sequence yet. */ +TEST(a_block_drafter_on_the_card_runs_the_passes_the_hosts_driver_runs) { + const int T = 24; + const BlockRun a = block_reference(T); + const BlockRun b = block_pipelined(T); + CHECK_EQ(b.ahead, T); + CHECK_EQ(b.card_host_disagree, 0); + REQUIRE_EQ(a.trunk.size(), (size_t)T + 1); + REQUIRE_EQ(a.trunk.size(), b.trunk.size()); + REQUIRE_EQ(a.context.size(), b.context.size()); + REQUIRE_EQ(a.query.size(), b.query.size()); + for (size_t t = 0; t < a.trunk.size(); ++t) { + CHECK(a.trunk[t] == b.trunk[t]); + CHECK_EQ(b.trunk[t].bad_slots, 0); + } + for (size_t t = 0; t < a.context.size(); ++t) { + BatchRows ca = a.context[t], cb = b.context[t]; + ca.tok.clear(); + cb.tok.clear(); + const bool same_c = ca == cb, same_q = a.query[t] == b.query[t]; + CHECK(same_c); + CHECK(same_q); + if (!same_c || !same_q) + std::fprintf(stderr, " step %zu: context %s, query %s\n", t, + same_c ? "same" : "differs", same_q ? "same" : "differs"); + CHECK_EQ(a.context[t].bad_slots, 0); + CHECK_EQ(b.context[t].bad_slots, 0); + CHECK_EQ(a.query[t].bad_slots, 0); + CHECK_EQ(b.query[t].bad_slots, 0); + } + CHECK(a.out == b.out); +} + /* ================================================================== the token budget per token */ /* A verify step emits up to 1 + n_spec tokens at once. The budget (max_tokens, and the context) * is applied token by token before each reaches the sink: trimming `output` afterwards would leave