diff --git a/abi/rad_builder.h b/abi/rad_builder.h index fc2cfddc..934f6ae4 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -686,6 +686,19 @@ int rad_drafter_shared_kv(RadBuilder* b, int pos_offset); * that decision. Additive export; RAD_ABI_VERSION does not move. */ int rad_chain_on(void); +/* A block drafter that also serves a shorter block: the plugin declared its query pass at `block` + * rows a sequence as well as at RadDrafterDecl::block, with the same anchor convention, so the + * core may stage a step's block at either. The short block proposes + * `block - (RadDrafterDecl::block - depth)` tokens. Called once, after rad_declare_drafter. + * Additive export; RAD_ABI_VERSION does not move. + * + * Why: the block is proposed whole, so its length is paid for every drafting sequence whatever + * each verifies. At one sequence a long block's extra drafts are nearly free and accepted often + * enough to pay; at many, the long block's drafter rows and its weaker first positions cost more + * than they bring (Qwen3.8-27B + DFlash2 at 16 streams: block 4 900 t/s, block 8 750-790). The + * core picks per step (--draft-short-from). */ +int rad_drafter_short_block(RadBuilder* b, int64_t block); + /* ================================================================== the encoder */ /* A media encoder the plugin runs on an encoder pass (RadBatch::enc). The core drives the pass and * reads its result, so the plugin declares it. A plugin that declares none serves text only, and diff --git a/arch/common/rad_block_dflash2.h b/arch/common/rad_block_dflash2.h index d26bf7ad..0dc7a52e 100644 --- a/arch/common/rad_block_dflash2.h +++ b/arch/common/rad_block_dflash2.h @@ -130,6 +130,11 @@ struct Dflash2 { /* Bytes of the next GEMM's weight read into the cache beside each all-reduce of the query * pass (prefetch_beside, rad_arch.h); 0 reads none. */ int64_t prefetch = 0; + + /* Also declare the short block's query pass (kShortBlock rows) for --draft-short-from; an + * architecture that does not call rad_drafter_short_block leaves it off and its graph as + * it was. */ + int short_block = 0; int64_t steps() const { return block - 1; } int64_t ng(int64_t n_embd) const { return group ? n_embd / group : 0; } /* The kernel projection emits [2, taps, NG] per token (prepare's set, then finish's); @@ -260,6 +265,10 @@ struct Dflash2 { rad_op op_gu[MAX_LAYERS] = {}, op_dn[MAX_LAYERS] = {}; rad_op op_gug[MAX_LAYERS] = {}; /* the gate_up|silu|quant fold; 0 when nothing serves it */ rad_op op_aconv[MAX_LAYERS] = {}, op_fconv[MAX_LAYERS] = {}; + /* The short block's (block_lo rows a sequence): the convolutions mask at the block's first row + * and the selector strides by it, so each has an op of its own; every other op is per row. */ + rad_op op_aconv_lo[MAX_LAYERS] = {}, op_fconv_lo[MAX_LAYERS] = {}, op_sel_lo = 0; + int64_t block_lo = 0; rad_op op_qrope = 0, op_krope = 0, op_store = 0, op_attn = 0; rad_op op_act = 0, op_ar = 0, op_add = 0, op_final = 0; /* The residual add folded into the norm after it (add_rmsnorm), issued in place of op_add and @@ -287,6 +296,9 @@ struct Dflash2 { static constexpr int64_t kRerankR = 32; int64_t top_w = 0; /* the coarse top-K's width: kRerankR or sel_topk */ rad_op op_hp = 0, op_sel = 0; + /* The short block: kShortBlock rows (depth 3, the window that pays at many sequences), when + * the drafter's own block is longer (rad_drafter_short_block). */ + static constexpr int64_t kShortBlock = 4; int declare(RadBuilder* b, Names& nm, const Geom& geom, const Config& c, int base_layer, rad_kvgroup group, rad_weight tok_embd, const char* ck_prefix, @@ -695,7 +707,26 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co * layers. Planned from those alone, its range would end before the coefficients' begins and * the plan could lay one over the other. Read by the last convolution as well, it is live * wherever the coefficients are. */ + block_lo = cfg.short_block && cfg.block > kShortBlock ? kShortBlock : 0; + for (int64_t l = 0; block_lo && l < g.n_layer; ++l) { + const int i = (int)l; + op_aconv_lo[i] = rw(b, RAD_OP(b, "dflash_conv", + RAD_PARAMS(RAD_RANGE("T", 1, qrows), RAD_INT("hidden_size", g.n_embd), + RAD_INT("taps", cfg.taps), RAD_INT("group_size", cfg.group), + RAD_INT("NG", NG), RAD_INT("block_size", block_lo), + RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w_acb[i])), + {w.h, w.coef}, {w.h2}); + op_fconv_lo[i] = rw(b, RAD_OP(b, "dflash_conv", + RAD_PARAMS(RAD_RANGE("T", 1, qrows), RAD_INT("hidden_size", g.n_embd), + RAD_INT("taps", cfg.taps), RAD_INT("group_size", cfg.group), + RAD_INT("NG", NG), RAD_INT("block_size", block_lo), + RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w_fcb[i])), + {w.h, w.coef}, {w.h2}); + } if (rad_op_reads(b, op_fconv[g.n_layer - 1], &w.attn, 1) < 0) return RAD_E_INVAL; + if (block_lo && rad_op_reads(b, op_fconv_lo[g.n_layer - 1], &w.attn, 1) < 0) return RAD_E_INVAL; op_qrope = rope(qrows, g.n_head, w.q); op_krope = rope(qrows, g.n_head_kv, w.k); op_store = rw(b, RAD_OP(b, "kv_store", @@ -909,6 +940,14 @@ inline int Dflash2::declare(RadBuilder* b, Names& nm, const Geom& geom, const Co RAD_INT("anchor_stride", cfg.block), RAD_STR("dtype", g.dtype)), RAD_WEIGHTS(w_pred, w_succ)), {w.cand, w.unary, w.hp}, {w.tokens}); + if (block_lo) + op_sel_lo = rw(b, RAD_OP(b, "dflash_select", + RAD_PARAMS(RAD_RANGE("M", 1, g.max_seqs), RAD_INT("steps", block_lo - 1), + RAD_INT("top_k", cfg.sel_topk), RAD_INT("rank", cfg.sel_rank), + RAD_INT("n_vocab", g.n_vocab_all), + RAD_INT("anchor_stride", block_lo), RAD_STR("dtype", g.dtype)), + RAD_WEIGHTS(w_pred, w_succ)), + {w.cand, w.unary, w.hp}, {w.tokens}); /* The reads beside the all-reduces, after every weight they read is declared. */ if (op_ar && cfg.prefetch > 0) @@ -983,6 +1022,17 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const { const int64_t NG = cfg.ng(g.n_embd); const int64_t half = cfg.taps * NG; /* prepare's coefficients, then finish's */ + /* The block this pass was staged at: every drafting sequence carries the same number of rows, + * the drafter's own block or its short one (rad_drafter_short_block). */ + const int64_t blk = batch->n_seq > 0 && T % batch->n_seq == 0 ? T / batch->n_seq : 0; + const bool lo = block_lo > 0 && blk == block_lo; + if (!lo && blk != cfg.block) { + rad_step_fail(c, "dflash2: a query pass staged at a block this drafter has no ops for"); + return; + } + const rad_op* aconv = lo ? op_aconv_lo : op_aconv; + const rad_op* fconv = lo ? op_fconv_lo : op_fconv; + RAD_ISSUE_N(c, op_embed, T, praw(batch->token_ids, RAD_I32, T), RAD_W(w_tok), brows(w.x, T)); if (op_embed_ar) RAD_ISSUE_N(c, op_embed_ar, (int64_t)T * g.n_embd, brows(w.x, T), RAD_NONE); @@ -1005,7 +1055,7 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const { /* ---- attention, wrapped in the two halves of one convolution */ norm(op_anorm[i], op_aan[i], w_anorm[i]); RAD_ISSUE_N(c, op_acoef[i], T, brows(w.h, T), RAD_W(w_acp[i]), brows(w.coef, T)); - RAD_ISSUE_N(c, op_aconv[i], T, brows(w.h, T), bcol(w.coef, 0, half, T), + RAD_ISSUE_N(c, aconv[i], T, brows(w.h, T), bcol(w.coef, 0, half, T), wrow_slice(w_acb[i], 0, 1, cfg.taps * g.n_embd), brows(w.h2, T)); RAD_ISSUE_N(c, op_qh2, T, brows(w.h2, T), brows(w.h2q, T), brows(w.h2s, T)); @@ -1044,14 +1094,14 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const { * replicated norm, so either order gives the same result; reducing first keeps x's * update agreed across ranks. */ if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE); - RAD_ISSUE_N(c, op_aconv[i], T, brows(w.h, T), bcol(w.coef, half, half, T), + RAD_ISSUE_N(c, aconv[i], T, brows(w.h, T), bcol(w.coef, half, half, T), wrow_slice(w_acb[i], 1, 1, cfg.taps * g.n_embd), brows(w.h2, T)); add_pending = true; /* ---- the feed-forward, wrapped in the other convolution */ norm(op_fnorm[i], op_afn[i], w_fnorm[i]); RAD_ISSUE_N(c, op_fcoef[i], T, brows(w.h, T), RAD_W(w_fcp[i]), brows(w.coef, T)); - RAD_ISSUE_N(c, op_fconv[i], T, brows(w.h, T), bcol(w.coef, 0, half, T), + RAD_ISSUE_N(c, fconv[i], T, brows(w.h, T), bcol(w.coef, 0, half, T), wrow_slice(w_fcb[i], 0, 1, cfg.taps * g.n_embd), brows(w.h2, T)); RAD_ISSUE_N(c, op_qh2, T, brows(w.h2, T), brows(w.h2q, T), brows(w.h2s, T)); if (op_gug[i] && T <= gated_rows) { @@ -1068,7 +1118,7 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const { RAD_W(w_dn[i].w), RAD_W(w_dn[i].s), brows(w.h, T), RAD_NONE, w_dn[i].bref()); for (int j = 0; j < 2; ++j) prefetch_beside(c, op_pff[i][j], pff_w[i][j], T); if (op_ar) RAD_ISSUE_N(c, op_ar, T * g.n_embd, brows(w.h, T), RAD_NONE); - RAD_ISSUE_N(c, op_fconv[i], T, brows(w.h, T), bcol(w.coef, half, half, T), + RAD_ISSUE_N(c, fconv[i], T, brows(w.h, T), bcol(w.coef, half, half, T), wrow_slice(w_fcb[i], 1, 1, cfg.taps * g.n_embd), brows(w.h2, T)); add_pending = true; } @@ -1107,7 +1157,7 @@ inline void Dflash2::step(RadCtx* c, const RadBatch* batch) const { } RAD_ISSUE_N(c, op_hp, S, brows(w.sel_h, S), RAD_W(w_selp), brows(w.hp, S)); - RAD_ISSUE_N(c, op_sel, batch->n_seq, bcol(w.cand, 0, cfg.sel_topk, S), + RAD_ISSUE_N(c, lo ? op_sel_lo : op_sel, batch->n_seq, bcol(w.cand, 0, cfg.sel_topk, S), bcol(w.unary, 0, cfg.sel_topk, S), brows(w.hp, S), praw(batch->token_ids, RAD_I32, T), RAD_W(w_pred), RAD_W(w_succ), brows(w.tokens, batch->n_seq)); diff --git a/arch/gemma3/gemma3.cpp b/arch/gemma3/gemma3.cpp index 6580ff94..f2f86170 100644 --- a/arch/gemma3/gemma3.cpp +++ b/arch/gemma3/gemma3.cpp @@ -793,6 +793,7 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui RAD_ARCH_TRY(drafter_geom(dg, m.g, meta, "gemma3 DFlash2")); Dflash2::Config dc{}; + dc.short_block = 1; /* rad_drafter_short_block below */ dc.n_taps = m.n_taps; dc.block = ctx->max_spec + 1; dc.taps = rad_meta_geti(meta, "draft.dflash_config.conv_kernel_size", 2); @@ -904,6 +905,7 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui return RAD_E_FORMAT; } RAD_ARCH_TRY(rad_declare_drafter(b, &drf)); + if (m.df.block_lo > 0) RAD_ARCH_TRY(rad_drafter_short_block(b, m.df.block_lo)); rad_note(b, "DFlash2 drafter: %lld layers at absolute %d.., %lld heads / %lld kv x %lld, ffn %lld, " "%s attention with a %lld-token window, %d trunk taps, embedding x %g, %s head", (long long)dg.n_layer, base_layer, (long long)dg.n_head, (long long)dg.n_head_kv, diff --git a/arch/qwen35_fp8/qwen35_fp8.cpp b/arch/qwen35_fp8/qwen35_fp8.cpp index ddfe97b5..5b54cd2f 100644 --- a/arch/qwen35_fp8/qwen35_fp8.cpp +++ b/arch/qwen35_fp8/qwen35_fp8.cpp @@ -264,6 +264,7 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui RAD_ARCH_TRY(drafter_geom(dg, m.g, meta, "DFlash2")); Dflash2::Config dc{}; + dc.short_block = 1; /* rad_drafter_short_block below */ dc.n_taps = m.n_taps; dc.block = ctx->max_spec + 1; dc.taps = rad_meta_geti(meta, "draft.dflash_config.conv_kernel_size", 2); @@ -414,6 +415,7 @@ static int declare_dflash2(RadBuilder* b, const RadModelMeta* meta, const RadBui return RAD_E_FORMAT; } RAD_ARCH_TRY(rad_declare_drafter(b, &drf)); + if (m.df.block_lo > 0) RAD_ARCH_TRY(rad_drafter_short_block(b, m.df.block_lo)); rad_note(b, "DFlash2 drafter: %lld layers at absolute %d..%d, %lld heads / %lld kv x %lld, " "ffn %lld, %s attention with a %lld-token window, %d trunk tap(s); one forward " diff --git a/core/build/rad_build.h b/core/build/rad_build.h index 55beeb7d..02879838 100644 --- a/core/build/rad_build.h +++ b/core/build/rad_build.h @@ -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); diff --git a/core/build/rad_builder.cpp b/core/build/rad_builder.cpp index 88a92e77..6879208a 100644 --- a/core/build/rad_builder.cpp +++ b/core/build/rad_builder.cpp @@ -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); diff --git a/core/config.cpp b/core/config.cpp index 4635b1e4..32c0088e 100644 --- a/core/config.cpp +++ b/core/config.cpp @@ -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); } }, diff --git a/core/engine.h b/core/engine.h index 725d6c5a..42eca7c1 100644 --- a/core/engine.h +++ b/core/engine.h @@ -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. */ diff --git a/core/engine_bringup.cpp b/core/engine_bringup.cpp index 59689caa..83853df0 100644 --- a/core/engine_bringup.cpp +++ b/core/engine_bringup.cpp @@ -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. */ diff --git a/core/rad_core.h b/core/rad_core.h index 06a10481..d026f63b 100644 --- a/core/rad_core.h +++ b/core/rad_core.h @@ -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) ---------------- * diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 939613e6..f2f41e53 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -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 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); } diff --git a/core/sched/scheduler.h b/core/sched/scheduler.h index 7b5c1568..891aa2cd 100644 --- a/core/sched/scheduler.h +++ b/core/sched/scheduler.h @@ -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(); diff --git a/tests/arch_fakes.h b/tests/arch_fakes.h index 970d1b04..c8449ac0 100644 --- a/tests/arch_fakes.h +++ b/tests/arch_fakes.h @@ -117,6 +117,7 @@ struct RadBuilder { std::string drafter_name; bool drafter_shared = false; int drafter_pos = -1; + int64_t drafter_short = 0; /* rad_drafter_short_block */ /* The buffers named to rad_buf_concurrent: the ones a second lane touches. */ std::vector concurrent; @@ -361,6 +362,15 @@ struct ChainOn { explicit ChainOn(int on) { g_chain_on = on; } ~ChainOn() { g_chain_on = 0; } }; + +extern "C" int rad_drafter_short_block(RadBuilder* b, int64_t block) { + const int64_t full = b ? (b->drafter.block > 0 ? b->drafter.block : b->drafter.depth + 1) : 0; + if (!b || b->drafter.kind != RAD_DRAFT_BLOCK || block >= full || + block - (full - b->drafter.depth) < 1) + return RAD_E_STATE; + b->drafter_short = block; + return RAD_OK; +} extern "C" int rad_declare_encoder(RadBuilder* b, const RadEncoderDecl* d) { if (!b || !d || !d->name || !d->modalities) return RAD_E_INVAL; if (d->modalities == RAD_MM_AUDIO) { diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index c44abaa3..d1e20da9 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -6149,6 +6149,26 @@ void lint_qwen35_fp8_family(Fn declare, StepFn step, const RadModelMeta* meta, i const auto v = run_passes(b, step, all, rank, world); CHECK_PASSES(b, v); for (size_t i = 1; i < v.size(); ++i) CHECK(each_gemm_nt_once(b, *v[i])); + if (!block_drafter || block <= 4) continue; + /* The short block (rad_drafter_short_block): a query pass staged at 4 rows a sequence + * runs the short ops, its selector proposing 3; a block with no ops fails the step. */ + CHECK_EQ(b.drafter_short, 4); + const auto ctx2 = context_pass(b, *ver); + const auto qs = query_pass(b, 4, b.drafter_short, b.drafter_short - 1); + const auto vs = run_passes(b, step, { ver.get(), ctx2.get(), qs.get() }, rank, world); + CHECK_PASSES(b, vs); + CHECK(vs[2]->step_fail.empty()); + int64_t sel_steps = -1, sel_stride = -1; + for (const RecIssue& i : vs[2]->issues) + if (const RecOp* o = b.op_of(i.op); o && o->op == "dflash_select") { + sel_steps = pint(o, "steps"); + sel_stride = pint(o, "anchor_stride"); + } + CHECK_EQ(sel_steps, 3); + CHECK_EQ(sel_stride, 4); + const auto qbad = query_pass(b, 4, 6, 5); + const auto vb = run_passes(b, step, { qbad.get() }, rank, world); + CHECK(!vb[0]->step_fail.empty()); } } diff --git a/tests/build_test.cpp b/tests/build_test.cpp index 0b407299..6b76b8cb 100644 --- a/tests/build_test.cpp +++ b/tests/build_test.cpp @@ -1072,6 +1072,39 @@ TEST(the_drafter_name_survives_the_program_leaving_the_builder) { CHECK_EQ(p.drafter.depth, 3); } +/* rad_drafter_short_block applies only to a declared block drafter, and only to a block shorter + * than its own that still proposes a token; anything else is refused at declare, since the core + * would stage a block the plugin has no ops for. */ +TEST(a_block_drafters_short_block_is_shorter_and_still_proposes) { + Registry reg; + { + Builder b(reg, make_meta(), make_ctx()); + CHECK_EQ(rad_drafter_short_block(&b, 4), RAD_E_STATE); /* nothing declared */ + } + { + 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_short_block(&b, 3), RAD_E_STATE); /* a serial head */ + } + { + 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; /* block 5: the anchor + 4 */ + CHECK_OK(rad_declare_drafter(&b, &d)); + CHECK_EQ(rad_drafter_short_block(&b, 5), RAD_E_INVAL); /* not shorter */ + CHECK_EQ(rad_drafter_short_block(&b, 1), RAD_E_INVAL); /* proposes nothing */ + CHECK_EQ(b.program().drafter_short_block, 0); + CHECK_OK(rad_drafter_short_block(&b, 3)); + CHECK_EQ(b.program().drafter_short_block, 3); + } +} + /* rad_drafter_shared_kv applies only to a declared serial drafter. With none declared, a block * drafter, or a position no head occupies, it is refused at declare; a head staged the wrong way * silently costs acceptance. */ diff --git a/tests/config_test.cpp b/tests/config_test.cpp index 7a40350a..4a812300 100644 --- a/tests/config_test.cpp +++ b/tests/config_test.cpp @@ -435,7 +435,7 @@ const std::map>& legal_values() { { "--placement", { "all_vram" } }, { "--checkpoint-interval", { "256" } }, { "--prefill-span", { "2" } }, { "--checkpoint-slots", { "4" } }, { "--checkpoint-policy", { "tip-only" } }, - { "--num-speculative-tokens", { "2" } }, { "--draft-window", { "fixed" } }, + { "--num-speculative-tokens", { "2" } }, { "--draft-window", { "fixed" } }, { "--draft-short-from", { "4" } }, { "--host", { "127.0.0.1" } }, { "--port", { "8001" } }, { "--api-key", { "k" } }, { "--served-model-name", { "m" } }, { "--mm-max-patches", { "1024" } }, { "--control-vector", { "off" } },