block drafter: a short block chosen per step by load (--draft-short-from N)

A block drafter proposes its block whole, so the block's length is paid for every drafting
sequence whatever each verifies. At one sequence a long block pays: Qwen3.8-27B + DFlash2
(block 8, --draft-window adaptive) decodes one stream 135 -> 155-165 t/s against block 4. At
sixteen it does not: block 8 750-790 t/s, block 4 900, its drafter rows and weaker first
positions costing more than the extra drafts bring. --draft-window adaptive alone cannot help,
since it only shortens what each sequence verifies.

- rad_drafter_short_block(b, block): additive export (RAD_ABI_VERSION unchanged); the plugin
  declares that its query pass also runs at `block` rows a sequence. Builder validates it
  (block drafter, shorter than its own, still proposes).
- --draft-short-from N (default 0 = never): the engine stages the short block once N sequences
  decoded in the step just run (Scheduler::decoded_seqs).
- The engine reads the proposals at the drafter's declared row pitch, not at the step's count:
  dflash_select writes row s at s * pitch, and a short block fills only a row's front (with the
  pitch read as the count, every sequence but the first got the wrong tokens).
- Dflash2: a short block of 4 rows (depth 3) wherever its own is longer: its own convolution and
  selector ops at that block (every other op is per row); the query pass picks by T / n_seq and
  fails the step on a block it has no ops for. qwen35_fp8 and gemma3 declare it.
- Tests: build_test (the declaration's checks), arch_test (the qwen35 family's short query pass
  runs the short ops, a block without ops fails the step; the DFlash2 pin moves), config_test.

Measured (Qwen, speculation 7 adaptive, short block from 4 sequences, against production's
speculation 3 fixed): one stream +15-20 % (kratky) / +9 % (syslog), 4 streams +3 %, 16
streams equal (915 / 906 t/s).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 committed 2026-10-23 11:24:35 +00:00
1 parent 9eaf10da6e
commit 90e14c5015
16 files changed
+211 -6

No files matched your search

+1
View File
@@ -128,6 +128,7 @@ public:
int lora_root(const char* native, const char* spellings);
int lora_alias(const char* native, const char* from, const char* to, int64_t from_lo,
int64_t from_hi, int64_t to_lo);
int drafter_short_block(int64_t block);
int declare_encoder(const RadEncoderDecl* d);
int declare_generator(const RadGeneratorDecl* d);
int declare_audio_generator(const RadAudioGenDecl* d);
+32
View File
@@ -1915,6 +1915,33 @@ int Builder::lora_alias(const char* native, const char* from, const char* to, in
return RAD_OK;
}
/* A block drafter's shorter block (rad_builder.h). Validated at declare: the core stages the block
* from it, and a block the plugin did not declare its ops at would fail the step. */
int Builder::drafter_short_block(int64_t block) {
if (finished_) {
err(RAD_E_STATE, "drafter_short_block after declare finished");
return RAD_E_STATE;
}
if (prog_.drafter.kind != RAD_DRAFT_BLOCK) {
err(RAD_E_STATE, "drafter_short_block: %s. Only a BLOCK drafter proposes a block; declare "
"it with rad_declare_drafter first.",
prog_.drafter.kind == RAD_DRAFT_NONE ? "no drafter is declared"
: "the declared drafter is a SERIAL head");
return RAD_E_STATE;
}
const int64_t full = prog_.drafter.block > 0 ? prog_.drafter.block : prog_.drafter.depth + 1;
const int64_t lead = full - prog_.drafter.depth; /* rows ahead of the proposing ones */
if (block >= full || block - lead < 1) {
err(RAD_E_INVAL, "drafter_short_block '%s': %lld rows. The short block must be shorter "
"than the drafter's own (%lld) and still propose a token (more than %lld "
"rows).", prog_.drafter_name.c_str(), (long long)block, (long long)full,
(long long)lead);
return RAD_E_INVAL;
}
prog_.drafter_short_block = block;
return RAD_OK;
}
void Builder::note_v(const char* f, va_list ap) {
char msg[1024];
vsnprintf(msg, sizeof msg, f ? f : "", ap);
@@ -2401,6 +2428,11 @@ int rad_lora_alias(RadBuilder* b, const char* native, const char* from, const ch
int rad_chain_on(void) { return rad_dev_chain_enabled(); }
int rad_drafter_short_block(RadBuilder* b, int64_t block) {
if (!b) return RAD_E_INVAL;
return B(b)->drafter_short_block(block);
}
int rad_declare_encoder(RadBuilder* b, const RadEncoderDecl* d) {
if (!b) return RAD_E_INVAL;
return B(b)->declare_encoder(d);
+6
View File
@@ -158,6 +158,9 @@ static const Flag g_flags[] = {
" own operating point, which the plugin states. A container\n"
" with no draft head is unaffected -- auto resolves to 0." },
{ "--draft-window", "MODE", g_draft_window_help.c_str() },
{ "--draft-short-from", "N", "a block drafter that also serves a shorter block runs it\n"
"from N decoding sequences on (0 never, the default): the\n"
"long block pays at one sequence, the short one at many" },
{ "--draft-model", "PATH", "NOT IMPLEMENTED -- merge the drafter at convert time" },
{ "--host", "ADDR", "listen address" },
@@ -652,6 +655,7 @@ int config_parse(int argc, char** argv, Config* c) {
return RAD_E_INVAL;
}
}
else if (S("--draft-short-from")) { if (!num(0, INT32_MAX, &c->draft_short_from)) return RAD_E_INVAL; }
else if (S("--draft-model")) {
/* Refused, not ignored: separate draft models are not supported (spec §10), and the
* drafter comes from the container (rad-convert --draft-model merges one in; an MTP
@@ -1087,6 +1091,8 @@ const ShowRow kShow[] = {
{ "--checkpoint-policy", [](const Config& c) { return c.checkpoint_policy; } },
{ "--num-speculative-tokens", [](const Config& c) { return c.n_spec < 0 ? std::string("auto") : num(c.n_spec); } },
{ "--draft-window", [](const Config& c) { return std::string(draft_window_name(c.draft_window)); } },
{ "--draft-short-from", [](const Config& c) {
return c.draft_short_from > 0 ? num(c.draft_short_from) : std::string("0 (never)"); } },
{ "--draft-model", [](const Config&) { return std::string("not implemented (the container's own drafter)"); } },
{ "--host", [](const Config& c) { return c.host; } },
{ "--port", [](const Config& c) { return num(c.port); } },
+3
View File
@@ -338,6 +338,9 @@ private:
* string because RadDrafterDecl::name points into the Program. */
RadDrafterDecl drafter_{};
std::string drafter_name_;
/* The block drafter's shorter block (rad_drafter_short_block), 0 when it has none or
* --draft-short-from is 0. */
int64_t drafter_short_ = 0;
/* Serial driver (spec §10): `depth` dependent passes after the step commits, each embedding
* the previous proposal. MTP, EAGLE and Medusa-serial have this shape. */
+14
View File
@@ -1303,6 +1303,20 @@ int Engine::declare() {
drafter_name_ = p0.drafter_name;
drafter_.name = drafter_name_.c_str();
if (cfg_.n_spec <= 0) drafter_ = RadDrafterDecl{};
drafter_short_ = drafter_.kind == RAD_DRAFT_BLOCK && cfg_.draft_short_from > 0
? p0.drafter_short_block : 0;
if (cfg_.draft_short_from > 0 && drafter_.kind != RAD_DRAFT_NONE && !drafter_short_)
RAD_WARN("--draft-short-from %lld: '%s' declares no shorter block, so its block stays %lld",
(long long)cfg_.draft_short_from, drafter_name_.c_str(),
(long long)drafter_.block);
if (drafter_short_)
RAD_INFO("speculation: from %lld decoding sequences on, '%s' runs its short block of %lld "
"row(s), %lld token(s), on the host's block driver", (long long)cfg_.draft_short_from,
drafter_name_.c_str(),
(long long)drafter_short_,
(long long)(drafter_short_ - ((drafter_.block > 0 ? drafter_.block
: drafter_.depth + 1) -
drafter_.depth)));
if (drafter_.kind != RAD_DRAFT_NONE) {
/* A zero proposal handle means the sampler's token buffer: the head's logits go through
* the ordinary sampler chain. */
+5
View File
@@ -392,6 +392,8 @@ public:
* for drafters that did not ask. */
bool drafter_shared_kv = false;
int32_t drafter_pos_offset = 0;
/* A block drafter's shorter block (rad_drafter_short_block), 0 when it serves only its own. */
int64_t drafter_short_block = 0;
/* The declared media encoder (rad_declare_encoder). `modalities == 0` when none: every
* text-only program, and every deployment with RadBuildCtx::max_enc_patches 0.
@@ -898,6 +900,9 @@ struct Config {
* VRAM and still decode one token a step. */
int n_spec = -1; /* --num-speculative-tokens; -1 = auto, 0 = off */
DraftWindow draft_window = kDraftWindowDefault; /* --draft-window */
/* A block drafter's short block (rad_drafter_short_block) from this many decoding sequences in
* the step just run; 0 never. Ignored without a drafter that declares one. */
int64_t draft_short_from = 0; /* --draft-short-from */
/* ---------------- KL DIVERGENCE AGAINST A REFERENCE (core/kld.h) ----------------
*
+11
View File
@@ -2491,6 +2491,17 @@ int Scheduler::block_begin(int block, int depth, int window, int32_t mask, Block
return n_draft_rows;
}
int Scheduler::decoded_seqs() const {
std::lock_guard<std::mutex> lk(mu_);
int n = 0;
for (const StepEntry& e : plan_.e) {
if (!e.produces_token || e.slot < 0 || (size_t)e.slot >= reqs_.size()) continue;
const SchedReq& s = *reqs_[(size_t)e.slot];
if (s.req && s.req->state == ReqState::Running && s.running) ++n;
}
return n;
}
const Request* Scheduler::block_req(const BlockSeq& d) const { return live_req(d.slot, d.seq); }
const Request* Scheduler::serial_req(const SerialSeq& m) const { return live_req(m.slot, m.seq); }
+3
View File
@@ -361,6 +361,9 @@ public:
* in the order block_query() emits them. */
int block_begin(int block, int depth, int window, int32_t mask, BlockRow* out, int max,
bool on_card = false);
/* How many running sequences the step just run decoded (produced a token for), the load a
* block drafter's block length is chosen by. Read before block_begin(). */
int decoded_seqs() const;
const RadBatch* block_context(int step);
const RadBatch* block_query(int step);
void block_end();