spec: --draft-depth adaptive stops the MTP chain inside the step

A coupled serial chain stops as soon as the product of the head's own
probabilities along it falls under --draft-depth-stop (default 0.85; top or
path statistic, at most --draft-depth-max rounds, at least --draft-depth-min),
and the next verify carries exactly the drafts made. The decision trails the
chain by --draft-depth-lag rounds (default 1), so the host's readback of one
state row and its issue of the next round hide behind the running round.

The rule reads the draft's laws and the tokens drawn before the one a decision
admits, never the target and never the admitted token, so the row-wise and the
traversal verify stay exact; mtp_rrs_test checks it by chi-square with two rules
that must fail (one reads the admitted draft's own mass, one the target).

It excludes --draft-window adaptive, which picks the next step's window from
earlier acceptance and step cost and so is a second controller on the same chain.

The recorded-pass cache holds 48 passes instead of 24: every row count is its
own pass shape and the adaptive chain makes more of them than 24 hold.

On Flash-Next IQR-V4 (TP2) on our branch, against chain 3 with coupling and
traversal: +3.0% tok/s [+1.0, +5.0] over 117 paired requests; the 48-entry cache
is +1.7% [+1.1, +2.3] of that for this mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Code000andClaude Opus 5.5 committed 2026-11-17 22:11:30 +01:00
1 parent d0fd4aa1bb
commit e78d484e60
13 files changed
+1054 -4

No files matched your search

+76
View File
@@ -223,6 +223,21 @@ static const Flag g_flags[] = {
" root-to-leaf path bottom-up (Traversal Verification, arXiv\n"
" 2505.12398; needs --draft-coupling on). The same output law in\n"
" every mode, more drafts accepted per step in block and traversal" },
{ "--draft-depth", "MODE", "fixed (default) | adaptive -- a coupled serial chain stops within\n"
" the step once the product of the head's probabilities along it falls\n"
" below --draft-depth-stop, and the next verify carries only the drafts\n"
" made (1 .. --draft-depth-max). Same output law (the rule reads the\n"
" draft's own laws only); needs --draft-coupling on and --draft-window\n"
" fixed (both choose the depth, so they are not served together)" },
{ "--draft-depth-max", "N", "--draft-depth adaptive: the deepest chain (default 0 =\n"
"num-speculative-tokens)" },
{ "--draft-depth-min", "N", "--draft-depth adaptive: rounds that always run (default 1)" },
{ "--draft-depth-stop", "P", "--draft-depth adaptive: the chain goes on while its confidence\n"
"is at least P (default 0.85)" },
{ "--draft-depth-lag", "K", "--draft-depth adaptive: rounds the decision trails the chain,\n"
"0 or 1 (default 1: the host decides while the next round runs)" },
{ "--draft-depth-stat", "S", "--draft-depth adaptive: top (default, each round's largest\n"
"probability) | path (the probability of the token it drew)" },
{ "--draft-model", "PATH", "NOT IMPLEMENTED -- merge the drafter at convert time" },
{ "--host", "ADDR", "listen address" },
@@ -924,6 +939,37 @@ int config_parse(int argc, char** argv, Config* c) {
return RAD_E_INVAL;
}
}
else if (S("--draft-depth")) {
if (!(v = need(i))) return RAD_E_INVAL;
if (!std::strcmp(v, "fixed")) c->draft_depth_adaptive = false;
else if (!std::strcmp(v, "adaptive")) c->draft_depth_adaptive = true;
else {
RAD_ERR("--draft-depth takes fixed or adaptive, not '%s'", v);
return RAD_E_INVAL;
}
}
else if (S("--draft-depth-max")) { if (!num(0, 16, &c->draft_depth_max)) return RAD_E_INVAL; }
else if (S("--draft-depth-min")) { if (!num(1, 16, &c->draft_depth_min)) return RAD_E_INVAL; }
else if (S("--draft-depth-lag")) { if (!num(0, 1, &c->draft_depth_lag)) return RAD_E_INVAL; }
else if (S("--draft-depth-stop")) {
if (!(v = need(i))) return RAD_E_INVAL;
double d = 0;
if (!parse_num(a.c_str(), v, &d)) return RAD_E_INVAL;
if (!(d >= 0.0 && d <= 1.0)) {
RAD_ERR("--draft-depth-stop must be a probability in [0, 1], not '%s'", v);
return RAD_E_INVAL;
}
c->draft_depth_stop = d;
}
else if (S("--draft-depth-stat")) {
if (!(v = need(i))) return RAD_E_INVAL;
if (!std::strcmp(v, "top")) c->draft_depth_stat = 0;
else if (!std::strcmp(v, "path")) c->draft_depth_stat = 1;
else {
RAD_ERR("--draft-depth-stat takes top or path, not '%s'", v);
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
@@ -1431,6 +1477,29 @@ int config_parse(int argc, char** argv, Config* c) {
RAD_ERR("--video-min-pixels is above --video-max-pixels"); return RAD_E_INVAL; }
if (c->video_min_frames && c->video_max_frames && *c->video_min_frames > *c->video_max_frames) {
RAD_ERR("--video-min-frames is above --video-max-frames"); return RAD_E_INVAL; }
/* --draft-depth adaptive reads the coupled chain's laws and chooses the chain's depth itself. */
if (c->draft_depth_adaptive) {
if (c->given.count("--draft-coupling") && !c->draft_coupling) {
RAD_ERR("--draft-depth adaptive needs the coupled drafting (--draft-coupling on): the "
"stop rule reads the law each draft was drawn from");
return RAD_E_INVAL;
}
if (c->draft_window == DraftWindow::Adaptive) {
RAD_ERR("--draft-depth adaptive and --draft-window adaptive both choose the chain's "
"depth; serve one of them");
return RAD_E_INVAL;
}
if (c->draft_verify_block) {
RAD_ERR("--draft-depth adaptive is verified token by token or by traversal "
"(--draft-verify token | traversal), not as a fixed block");
return RAD_E_INVAL;
}
if (c->draft_depth_min > 1 && c->draft_depth_max > 0 && c->draft_depth_min > c->draft_depth_max) {
RAD_ERR("--draft-depth-min is above --draft-depth-max");
return RAD_E_INVAL;
}
c->draft_coupling = true;
}
/* --draft-verify block verifies a linear chain with the coupled drafting's states, so it
* switches the coupling on; traversal needs it and says so rather than switching it on. */
if (c->draft_verify_block) {
@@ -1573,6 +1642,13 @@ const ShowRow kShow[] = {
{ "--draft-coupling", [](const Config& c) { return onoff(c.draft_coupling); } },
{ "--draft-verify", [](const Config& c) {
return std::string(c.draft_verify_block ? "block" : c.draft_verify_traversal ? "traversal" : "token"); } },
{ "--draft-depth", [](const Config& c) { return std::string(c.draft_depth_adaptive ? "adaptive" : "fixed"); } },
{ "--draft-depth-max", [](const Config& c) {
return c.draft_depth_max > 0 ? num(c.draft_depth_max) : std::string("auto"); } },
{ "--draft-depth-min", [](const Config& c) { return num(c.draft_depth_min); } },
{ "--draft-depth-stop", [](const Config& c) { return fnum(c.draft_depth_stop); } },
{ "--draft-depth-lag", [](const Config& c) { return num(c.draft_depth_lag); } },
{ "--draft-depth-stat", [](const Config& c) { return std::string(c.draft_depth_stat == 1 ? "path" : "top"); } },
{ "--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); } },
+4 -1
View File
@@ -757,6 +757,7 @@ Engine::Readback::~Readback() {
}
Engine::~Engine() {
depth_log_stats("final");
/* The control vector's per-rank direction copies. Uploaded at declare, outside any arena, so
* nothing else frees them; each rank's copy is freed with that rank's device bound, since the
* allocator is per device. */
@@ -1225,7 +1226,9 @@ int Engine::issue_ahead(const RadBatch** out) {
if (flex_pending_.load(std::memory_order_acquire) || quiet_pending_.load(std::memory_order_acquire))
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_);
/* --draft-depth adaptive: the row's own chain (Engine::depth_row_nodes); else the rounds
* every row ran. */
sched_.expect_draft(serial_row_[(size_t)i].seq, depth_row_nodes(i));
for (int i = 0; i < block_rows_; ++i)
sched_.expect_draft(block_row_[(size_t)i].seq, block_steps_);
const RadBatch* nb = sched_.step_next();
+37
View File
@@ -29,6 +29,7 @@
#include "runtime/ctx.h" /* KVStageRun */
#include "sched/mem_iface.h"
#include "sched/scheduler.h"
#include "sched/draft_depth.h" /* --draft-depth adaptive: the stop rule */
#include "sample/vocab_view.h"
#include "runtime/rank_barrier.h"
#include "text/tokenizer.h"
@@ -677,6 +678,42 @@ private:
bool couple_evidence_ = false; /* the completed-verify line was written (once a process) */
bool couple_host_note_ = false; /* the host-domain counterpart was written (once) */
bool couple_malformed_note_ = false; /* a status -1 round was reported (once) */
int64_t couple_planes_ = 0; /* the rounds the state plane holds */
/* --draft-depth adaptive (sched/draft_depth.h, engine_draft.cpp): the chain of a step stops by
* the head's own laws, so its rounds are issued one decision at a time. After the pass of
* round r the engine copies that round's state rows to depth_rb_ and records depth_ev_[r - 1];
* the driver waits for the event of the round a decision needs, reads the statistic and issues
* the next round or ends the chain. depth_of_[i] is drafting row i's final depth, which
* expect_draft, set_draft and the coupling's bind all take in place of the rounds that ran. */
DraftDepthCfg depth_cfg_;
RadEvent depth_ev_[RAD_MTP_COUPLE_MAX_ROUNDS] = {};
bool depth_issued_[RAD_MTP_COUPLE_MAX_ROUNDS] = {};
RadMtpCoupleState* depth_rb_ = nullptr; /* pinned [rounds, couple_rows_] */
std::vector<DraftDepthRow> depth_row_;
std::vector<int32_t> depth_of_;
int depth_seen_ = 0; /* rounds observed this step */
int64_t depth_spin_us_ = 3000; /* RADIANCE_DEPTH_SPIN_US: poll this long, then block */
struct DepthStats {
uint64_t steps = 0, rows = 0, rounds = 0, drafts = 0, lawless = 0, waits = 0, blocked = 0;
uint64_t hist[RAD_MTP_COUPLE_MAX_ROUNDS + 1] = {};
double wait_us = 0;
uint64_t late = 0;
/* By the drafts a step made (d): the card's cycle from that step's first statistic to the
* next step's (its later rounds, the verify of d + 1 rows, the sampler, the next history
* pass), and what the verify of d drafts accepted. */
double cyc_us[RAD_MTP_COUPLE_MAX_ROUNDS + 1] = {};
uint64_t cyc_n[RAD_MTP_COUPLE_MAX_ROUNDS + 1] = {};
uint64_t acc[RAD_MTP_COUPLE_MAX_ROUNDS + 1] = {}, acc_n[RAD_MTP_COUPLE_MAX_ROUNDS + 1] = {};
} depth_stats_;
std::chrono::steady_clock::time_point depth_e1_{}; /* the last step's first statistic */
uint64_t depth_logged_ = 0; /* depth_stats_.rows at the last line */
int depth_last_ = 0; /* and the deepest chain that step made */
/* Drafting row i's depth after issue_serial_drafter: its own under the adaptive rule, else the
* rounds every row ran. */
int depth_row_nodes(int i) const;
int depth_readback_issue(int round, int n);
int depth_observe_upto(int round, int n, const StepOut& so);
void depth_log_stats(const char* when);
/* `rows` are serial_row_ or block_row_ (Scheduler::SerialRow / BlockRow). */
template <class Row> int stage_couple_draws(const Row* rows, int n);
int bind_couple(uint64_t seq, int i, int done, bool drafted, int32_t card);
+249 -2
View File
@@ -100,6 +100,7 @@ int Engine::step_state_init() {
return RAD_E_STATE;
}
couple_rows_ = sd.shape[1];
couple_planes_ = sd.shape[0];
/* One entry per card slot, indexed by StepEntry::card as slots_host_ is. */
couple_by_card_.assign((size_t)n_slot, RadMtpCoupleDraw{});
couple_draw_stage_ = static_cast<RadMtpCoupleDraw*>(
@@ -117,6 +118,49 @@ int Engine::step_state_init() {
drafter_.depth);
}
/* --draft-depth adaptive: the rule, the per-round readback of the chain's states and one host
* event a round. Refused wherever the chain it stops does not exist, rather than served fixed. */
depth_cfg_ = DraftDepthCfg{};
if (cfg_.draft_depth_adaptive) {
if (!mtp_couple_ || drafter_.kind != RAD_DRAFT_SERIAL) {
RAD_ERR("--draft-depth adaptive needs a coupled serial draft chain (--draft-coupling on, a "
"serial head); this deployment's drafter is '%s'",
drafter_.name ? drafter_.name : "(none)");
return RAD_E_INVAL;
}
/* --draft-window adaptive picks a window from the acceptance and the cost of the steps
* before (sched/draft.h); this rule picks the depth inside the step from the head's laws.
* Two controllers on one chain would fight over it. */
if (cfg_.draft_window == DraftWindow::Adaptive) {
RAD_ERR("--draft-depth adaptive and --draft-window adaptive both choose the chain's "
"depth; serve one of them");
return RAD_E_INVAL;
}
depth_cfg_.on = true;
depth_cfg_.max = (int)std::min<int64_t>(drafter_.depth, couple_planes_);
if (cfg_.draft_depth_max > 0) depth_cfg_.max = std::min(depth_cfg_.max, cfg_.draft_depth_max);
depth_cfg_.max = std::min(depth_cfg_.max, (int)RAD_MTP_COUPLE_MAX_ROUNDS);
depth_cfg_.min = std::min(std::max(cfg_.draft_depth_min, 1), depth_cfg_.max);
depth_cfg_.lag = cfg_.draft_depth_lag;
depth_cfg_.stat = cfg_.draft_depth_stat;
depth_cfg_.stop = cfg_.draft_depth_stop;
depth_cfg_.lawless = std::min(3, depth_cfg_.max);
depth_rb_ = static_cast<RadMtpCoupleState*>(rad_dev_alloc(
(int64_t)RAD_MTP_COUPLE_MAX_ROUNDS * couple_rows_ * (int64_t)sizeof(RadMtpCoupleState),
RAD_MEM_HOST_PINNED));
if (!depth_rb_) return RAD_E_NOMEM;
for (RadEvent& e : depth_ev_)
RAD_TRY(rad_event_create_as(&e, RAD_EVENT_LOCAL | RAD_EVENT_HOST_WAIT));
depth_row_.assign((size_t)n_slot, DraftDepthRow{});
depth_of_.assign((size_t)n_slot, 0);
if (const char* e = std::getenv("RADIANCE_DEPTH_SPIN_US")) depth_spin_us_ = std::atoll(e);
RAD_INFO("draft depth adaptive: 1..%d draft(s) a step, the chain goes on while the product of "
"the head's %s probabilities is >= %.3f (decided %d round(s) behind, %d round(s) "
"always run); a row with no law drafts %d",
depth_cfg_.max, depth_cfg_.stat == DRAFT_DEPTH_PATH ? "drawn-token" : "top",
depth_cfg_.stop, depth_cfg_.lag, depth_cfg_.min, depth_cfg_.lawless);
}
RAD_TRY(rad_event_create_as(&step_end_, RAD_EVENT_LOCAL | RAD_EVENT_HOST_WAIT));
RAD_TRY(rad_event_create_as(&tok_end_, RAD_EVENT_LOCAL | RAD_EVENT_HOST_WAIT));
return RAD_OK;
@@ -136,6 +180,9 @@ void Engine::step_state_fini() {
if (couple_readback_) rad_dev_free(couple_readback_, RAD_MEM_HOST_PINNED);
couple_draw_stage_ = nullptr;
couple_readback_ = nullptr;
if (depth_rb_) rad_dev_free(depth_rb_, RAD_MEM_HOST_PINNED);
depth_rb_ = nullptr;
for (RadEvent& e : depth_ev_) { if (e) rad_event_destroy(e); e = nullptr; }
if (step_end_) rad_event_destroy(step_end_);
step_end_ = nullptr;
if (tok_end_) rad_event_destroy(tok_end_);
@@ -244,6 +291,144 @@ int Engine::tree_compact(int rows) {
return RAD_OK;
}
/* ------------------------------------------------------------------ --draft-depth adaptive
*
* The chain's depth is decided while it is drafted (sched/draft_depth.h), so the rounds cannot all
* be queued behind the step as a fixed chain's are: round j is issued once the statistic of round
* j - 1 - lag is known. That costs the host one tiny readback a round (the round's state rows of
* the drafting sequences, rank 0's copy; every rank holds the same) and a wait for it. With lag 1
* the card is running round j - 1 meanwhile, so nothing idles as long as the host answers within a
* round (about 0.5 ms; reading the row and issuing a recorded pass takes about 0.1 ms). The next
* verify is issued the same way, right after the chain's last decision, with exactly the rows the
* chain has (Engine::issue_ahead): each row count is its own recorded pass.
*
* The commit of the step in flight moves behind those decisions (the step loop collects after
* issue_ahead), so its tokens are streamed up to the chain's rounds later than with a fixed chain;
* the card is not held up by that. */
int Engine::depth_row_nodes(int i) const {
if (!depth_cfg_.on) return serial_done_;
if (i < 0 || (size_t)i >= depth_of_.size()) return 0;
return depth_of_[(size_t)i];
}
/* Behind the pass of round `round`: its state rows of the `n` drafting sequences to the host, and
* the event the driver waits on. */
int Engine::depth_readback_issue(int round, int n) {
if (round < 1 || round > (int)RAD_MTP_COUPLE_MAX_ROUNDS || round > (int)couple_planes_ || n <= 0)
return RAD_OK;
Rank& r0 = *ranks_[0];
const char* state = static_cast<const char*>(r0.ctx.buf_ptr(couple_state_buf_));
if (!state) return RAD_E_STATE;
const int64_t plane = couple_rows_ * (int64_t)sizeof(RadMtpCoupleState);
RAD_TRY(rad_memcpy_async(depth_rb_ + (int64_t)(round - 1) * couple_rows_,
state + (int64_t)(round - 1) * plane,
(int64_t)n * (int64_t)sizeof(RadMtpCoupleState), r0.ctx.stream()));
RAD_TRY(rad_event_record(depth_ev_[round - 1], r0.ctx.stream()));
depth_issued_[round - 1] = true;
return RAD_OK;
}
/* Waits for the statistics of every round up to `round` and feeds them to the rows. A round whose
* readback was never issued (its pass did not run) leaves the rows without a law. */
int Engine::depth_observe_upto(int round, int n, const StepOut& so) {
while (depth_seen_ < round) {
const int r = depth_seen_ + 1;
const bool have = r <= (int)RAD_MTP_COUPLE_MAX_ROUNDS && depth_issued_[r - 1];
if (have) {
RadEvent ev = depth_ev_[r - 1];
const auto t0 = std::chrono::steady_clock::now();
int q = rad_event_query(ev);
if (q < 0) return q;
if (q == 0) {
/* The ids' copy sits ahead of every draft pass: block for it (the trunk's whole
* forward pass may be in front), then poll, which answers within a microsecond of
* the card where an interrupt takes tens. */
if (so.early && depth_seen_ == 0) RAD_TRY(rad_event_sync(tok_end_));
const auto t1 = std::chrono::steady_clock::now();
bool blocked = false;
for (uint32_t spin = 0;; ++spin) {
q = rad_event_query(ev);
if (q != 0) break;
if ((spin & 255u) == 255u &&
std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t1)
.count() > (double)depth_spin_us_) {
blocked = true;
break;
}
#if defined(__x86_64__) || defined(__i386__)
__builtin_ia32_pause();
#endif
}
if (q < 0) return q;
if (blocked) ++depth_stats_.blocked;
++depth_stats_.waits;
} else {
/* Already there: the host was behind the card by the time it asked. */
++depth_stats_.late;
}
/* The wait proper: what a host wait does to the fences of the dispatches that follow
* (the copy wrote host memory), and the block when the poll gave up. */
RAD_TRY(rad_event_sync(ev));
const auto t2 = std::chrono::steady_clock::now();
depth_stats_.wait_us += std::chrono::duration<double, std::micro>(t2 - t0).count();
/* The card's cycle by the last step's depth: the host waits for every step's first
* statistic, so two of them in a row are one cycle apart. A gap of 30 ms or more is
* no decode cycle (an idle engine, or the next request's prefill, in between). */
if (r == 1) {
const double us = std::chrono::duration<double, std::micro>(t2 - depth_e1_).count();
if (q == 1 && depth_last_ >= 1 && depth_last_ <= (int)RAD_MTP_COUPLE_MAX_ROUNDS &&
us > 0 && us < 30000.0) {
depth_stats_.cyc_us[depth_last_] += us;
++depth_stats_.cyc_n[depth_last_];
}
depth_e1_ = t2;
}
}
for (int i = 0; i < n; ++i) {
DraftDepthRow& row = depth_row_[(size_t)i];
double st = -1.0;
if (have && !row.lawless) {
const RadMtpCoupleState& cs = depth_rb_[(int64_t)(r - 1) * couple_rows_ + i];
/* this step's draw for this row, or a stale plane: no law */
if (!std::memcmp(&cs.draw, &couple_draw_stage_[(size_t)i], sizeof cs.draw))
st = draft_depth_stat(cs, r, depth_cfg_.stat);
}
draft_depth_observe(&row, r, st);
}
depth_seen_ = r;
}
return RAD_OK;
}
/* The adaptive chain's tuning line: the shares of each depth and what the host waits cost, then
* by the drafts a step made, the card's cycle and the drafts its verify accepted. Logged every
* 4096 drafting rows and at shutdown. */
void Engine::depth_log_stats(const char* when) {
const DepthStats& ds = depth_stats_;
if (!depth_cfg_.on || ds.rows == 0 || (depth_logged_ == ds.rows && !std::strcmp(when, "final"))) return;
depth_logged_ = ds.rows;
std::string h;
for (int d2 = 1; d2 <= depth_cfg_.max; ++d2)
h += fmt("%s%d:%.3f", d2 > 1 ? " " : "", d2, (double)ds.hist[d2] / (double)ds.rows);
RAD_INFO("draft depth adaptive (%s): %llu drafting step(s), drafts/row %.3f, rounds/step %.3f, "
"depth shares [%s], lawless rows %llu; host waits %llu (%.1f us each, %llu ended "
"blocked), statistics already there %llu",
when, (unsigned long long)ds.steps, (double)ds.drafts / (double)ds.rows,
(double)ds.rounds / (double)ds.steps, h.c_str(), (unsigned long long)ds.lawless,
(unsigned long long)ds.waits,
ds.waits + ds.late ? ds.wait_us / (double)(ds.waits + ds.late) : 0.0,
(unsigned long long)ds.blocked, (unsigned long long)ds.late);
std::string by;
for (int d2 = 1; d2 <= depth_cfg_.max; ++d2)
by += fmt("%s%d: %.0f us (%llu), %.3f accepted (%llu)", d2 > 1 ? "; " : "", d2,
ds.cyc_n[d2] ? ds.cyc_us[d2] / (double)ds.cyc_n[d2] : 0.0,
(unsigned long long)ds.cyc_n[d2],
ds.acc_n[d2] ? (double)ds.acc[d2] / (double)ds.acc_n[d2] : 0.0,
(unsigned long long)ds.acc_n[d2]);
RAD_INFO("draft depth adaptive (%s): by drafts made, the card's cycle and the drafts the "
"verify accepted -- %s", when, by.c_str());
}
/* Issue the step's draft passes behind it, without waiting. After the trunk's ranks join: the
* acceptance on every rank, the sampled-id readback (before a round can overwrite them), the
* history pass and every round, then the final proposal into the slots and their readback.
@@ -263,6 +448,8 @@ int Engine::issue_serial_drafter(int step, StepOut& so) {
RAD_TRY(collect_issue(so, /*fence=*/true));
int rounds = drafter_.depth;
/* --draft-depth-max below the head's declared depth: no round past it is ever asked for. */
if (depth_cfg_.on && rounds > depth_cfg_.max) rounds = depth_cfg_.max;
serial_row_.resize((size_t)cfg_.max_seqs);
/* `n` counts rows that can draft. It can be zero while the history pass still has work (a
* mid-prompt sequence has hidden states the head needs but no token to propose from). */
@@ -280,9 +467,30 @@ int Engine::issue_serial_drafter(int step, StepOut& so) {
static double a_batch = 0, a_samp = 0, a_rel = 0, a_iss = 0;
static long n_pass = 0;
/* --draft-depth adaptive: every row starts a chain; a greedy row (no draw) has no law. */
const bool adepth = depth_cfg_.on && n > 0;
if (adepth) {
depth_seen_ = 0;
for (bool& b : depth_issued_) b = false;
for (int i = 0; i < n; ++i) {
draft_depth_begin(depth_cfg_, &depth_row_[(size_t)i], couple_draw_stage_[(size_t)i].mode != 1);
depth_of_[(size_t)i] = 0;
}
}
/* Pass 0, then rounds 2..rounds: round 1 has no pass of its own. */
for (int rd = 0; rd <= rounds; rd += rd == 0 ? 2 : 1) {
if (rd > 0 && n <= 0) break;
/* The adaptive chain: round rd runs only for a row whose confidence up to round
* rd - 1 - lag holds (sched/draft_depth.h); the pass runs while any row goes on. */
if (adepth && rd >= 2) {
const int need = draft_depth_need(depth_cfg_, rd);
if (need >= 1) RAD_TRY(depth_observe_upto(need, n, so));
bool any = false;
for (int i = 0; i < n; ++i)
any = draft_depth_go(depth_cfg_, &depth_row_[(size_t)i], rd) || any;
if (!any) break;
}
const auto tA = std::chrono::steady_clock::now();
const RadBatch* db = sched_.serial_batch(rd, step);
if (!db) { if (rd == 0) continue; break; }
@@ -335,8 +543,31 @@ int Engine::issue_serial_drafter(int step, StepOut& so) {
}
if (rd > 0) serial_done_ = rd;
else if (drafted > 0) serial_done_ = 1;
/* This round's states to the host, when a later round's decision reads them. */
if (adepth && serial_done_ == (rd > 0 ? rd : 1) &&
serial_done_ <= draft_depth_need(depth_cfg_, rounds))
RAD_TRY(depth_readback_issue(serial_done_, n));
}
if (serial_done_ <= 0 || n <= 0) return RAD_OK;
if (adepth) {
DepthStats& ds = depth_stats_;
++ds.steps;
ds.rounds += (uint64_t)serial_done_;
for (int i = 0; i < n; ++i) {
const DraftDepthRow& row = depth_row_[(size_t)i];
const int d = std::max(1, std::min(row.depth, serial_done_));
depth_of_[(size_t)i] = d;
++ds.rows;
ds.drafts += (uint64_t)d;
ds.lawless += row.lawless;
++ds.hist[std::min(d, (int)RAD_MTP_COUPLE_MAX_ROUNDS)];
}
depth_last_ = 0;
for (int i = 0; i < n; ++i) depth_last_ = std::max(depth_last_, (int)depth_of_[(size_t)i]);
if (ds.rows >= depth_logged_ + 4096) depth_log_stats("running");
} else {
depth_last_ = 0;
}
/* The last round's proposal into the slots, then rank 0's slots back to the host. Only the
* drafting rows: at depth 1 the last pass is the history pass, whose later rows only carried
@@ -377,6 +608,19 @@ int Engine::finish_serial_drafter(const StepOut& so) {
}
}
}
/* --draft-depth adaptive: what the verify of d drafts accepted, by d (the tuning line). */
if (depth_cfg_.on) {
for (size_t i = 0; i < plan.e.size(); ++i) {
const StepEntry& e = plan.e[i];
if (so.first_row[i] < 0 || e.n_spec <= 0 || e.n_spec > (int)RAD_MTP_COUPLE_MAX_ROUNDS ||
e.card < 0 || (size_t)e.card >= couple_by_card_.size())
continue;
const RadMtpCoupleDraw& d = couple_by_card_[(size_t)e.card];
if (d.seq != e.seq || d.mode != 1) continue;
depth_stats_.acc[e.n_spec] += (uint64_t)std::max(0, so.accepted[i]);
++depth_stats_.acc_n[e.n_spec];
}
}
sched_.serial_end(serial_done_);
if (serial_done_ <= 0 || serial_rows_ <= 0) return RAD_OK;
@@ -393,9 +637,12 @@ int Engine::finish_serial_drafter(const StepOut& so) {
* cannot snapshot its state); the sampler would refuse the next step otherwise. */
if (!r0.sampler.allows_speculation(serial_row_[(size_t)i].seq)) continue;
const uint64_t seq = serial_row_[(size_t)i].seq;
const int rc = sched_.set_draft(seq, &draft_out_[(size_t)i * (size_t)rounds], done);
/* Under --draft-depth adaptive the row's own chain: the drafts the step issued ahead was
* planned with (issue_ahead's expect_draft), a prefix of the rounds that ran. */
const int row_nodes = depth_cfg_.on ? depth_row_nodes(i) : done;
const int rc = sched_.set_draft(seq, &draft_out_[(size_t)i * (size_t)rounds], row_nodes);
if (mtp_couple_)
RAD_TRY(bind_couple(seq, i, done, rc == RAD_OK, serial_row_[(size_t)i].card));
RAD_TRY(bind_couple(seq, i, row_nodes, rc == RAD_OK, serial_row_[(size_t)i].card));
}
return RAD_OK;
}
+15
View File
@@ -1047,6 +1047,21 @@ struct Config {
* tokens than token-wise, and it needs --draft-coupling on. */
bool draft_verify_block = false;
bool draft_verify_traversal = false;
/* --draft-depth fixed|adaptive (core/sched/draft_depth.h): adaptive stops a coupled serial
* chain within the step as soon as the head's own laws make the next draft unlikely, so a step
* drafts 1 .. draft_depth_max tokens (0: num-speculative-tokens) and its verify carries that
* many rows. The chain goes on while the product of the rounds' statistic (stat 0: the largest
* mass of each round's law, 1: the mass of the token it drew) stays >= draft_depth_stop; the
* decision trails the chain by draft_depth_lag rounds (0 or 1; 1 hides the host's readback
* behind a running round), draft_depth_min rounds always run. Needs --draft-coupling on, a
* serial head's chain and --draft-window fixed. Lossless: the rule reads draft-side quantities
* only. Off by default, which is the fixed chain bit for bit. */
bool draft_depth_adaptive = false;
int draft_depth_max = 0;
int draft_depth_min = 1;
int draft_depth_lag = 1;
int draft_depth_stat = 0;
double draft_depth_stop = 0.85;
/* ---------------- KL DIVERGENCE AGAINST A REFERENCE (core/kld.h) ----------------
*
+1 -1
View File
@@ -568,7 +568,7 @@ private:
int rec1);
int haz_mark(PassTape& t);
static constexpr size_t kTapes = 24; /* recorded passes kept, least recently used out */
static constexpr size_t kTapes = 48; /* recorded passes kept, least recently used out */
static constexpr int64_t kAuditPlays = 1024;
std::vector<PassTape> tapes_;
+131
View File
@@ -0,0 +1,131 @@
/* draft_depth.h -- same-step adaptive depth of a coupled serial draft chain (--draft-depth adaptive).
*
* A fixed chain drafts N tokens every step and its verify pays for N + 1 rows whatever the head
* thought of them. Here the chain of a step stops as soon as the head's own laws say the next draft
* is unlikely to be accepted: round j (j >= 2) runs only while the chain's confidence stays at or
* above the threshold,
*
* round j runs iff j <= max and ( j <= min or conf(j - 1 - lag) >= stop ),
* conf(m) = prod_{i = 1..m} s_i, conf(m <= 0) = 1,
*
* where s_i is read from round i's coupled state (abi/rad_spechub.h): the largest mass of the law
* Q_i the round drew from (`top`), or the mass of the token it drew (`path`, the probability of
* the drafted path). The first round that does not run ends the chain; its depth D is the number
* of rounds that ran, and the next verify carries D drafts (D + 1 rows).
*
* `lag` is how many rounds the decision trails the chain: with lag 0 round j is decided by every
* round before it, so the card waits for the host between two rounds; with lag 1 round j is decided
* when round j - 2 has finished, while round j - 1 runs, so the host's decision (a readback of one
* state row, then the next round's or the next verify's issue) hides behind a running round.
*
* Why this keeps the verify exact. The decision "x_j is drafted" reads Q_1 .. Q_{j-1-lag} and, for
* `path`, x_1 .. x_{j-1-lag}: draft-side quantities of the chain's ANCESTORS of x_j only, never a
* target output and never x_j itself. For a row-wise verify any such stopping time is exact (each
* verified position is exact on its own). For the traversal walk (abi/rad_mtp_traversal.h) the step
* of its induction at a node v needs v's number of children to be independent of the tokens drawn
* AT v; the shape of a child's subtree may depend on the child's own token, because the induction
* hypothesis is applied per child value. Here node v (the prefix x_1 .. x_{j-1}) has a child iff
* the rule above holds, a function of v's own path. A rule that read x_j's own mass to admit x_j
* would not be exact (tests/mtp_rrs_test.cpp shows the statistic that catches it, and one that
* peeks at the target).
*
* Plain code, no engine types beyond the wire row: the engine's driver, the scheduler's tests and
* the exactness test share it.
*/
#pragma once
#include <cstdint>
#include "rad_spechub.h"
namespace rad {
enum DraftDepthStat : int32_t {
DRAFT_DEPTH_TOP = 0, /* s_i = max_x Q_i(x) */
DRAFT_DEPTH_PATH = 1, /* s_i = Q_i(x_i), the drawn token's mass */
};
struct DraftDepthCfg {
bool on = false;
int max = 0; /* the deepest chain: rounds 1 .. max (the head's declared depth caps it) */
int min = 1; /* rounds that always run */
int lag = 1; /* rounds the decision trails the chain, 0 or more */
int stat = DRAFT_DEPTH_TOP;
double stop = 0.85; /* the chain goes on while conf >= stop */
int lawless = 3; /* the depth of a row with no law (a greedy request, a malformed round) */
};
/* One drafting row's chain while its step is drafted. */
struct DraftDepthRow {
double conf = 1.0; /* conf(seen) */
int seen = 0; /* rounds whose state went into conf */
int depth = 1; /* rounds decided to run so far (round 1 always does) */
bool done = false; /* a round was refused: depth is final */
bool lawless = false; /* no law: the fixed fallback depth */
};
/* Round `round`'s statistic from its state, or a negative number when the state carries no law
* (status != 1: a greedy row or a shortlist the proposer could not turn into Q). */
inline double draft_depth_stat(const RadMtpCoupleState& s, int round, int stat) {
if (s.status != 1 || s.round != round || s.count < 1 || s.count > 8) return -1.0;
double top = 0.0, own = -1.0;
for (int i = 0; i < s.count; ++i) {
const double q = s.q[i];
if (!(q > 0.0) || q > 1.0) return -1.0;
if (q > top) top = q;
if (s.ids[i] == s.x) own = q;
}
return stat == DRAFT_DEPTH_PATH ? own : top;
}
/* The round whose statistic decides round `j` (>= 2); <= 0 when nothing does (conf = 1). */
inline int draft_depth_need(const DraftDepthCfg& c, int j) { return j - 1 - (c.lag > 0 ? c.lag : 0); }
/* The start of a step's chain for one row. `lawless`: the row draws nothing (a greedy request). */
inline void draft_depth_begin(const DraftDepthCfg& c, DraftDepthRow* r, bool lawless) {
*r = DraftDepthRow{};
r->lawless = lawless;
(void)c;
}
/* Feed round `round`'s statistic (rounds in order, each once). A negative one makes the row lawless. */
inline void draft_depth_observe(DraftDepthRow* r, int round, double s) {
if (round != r->seen + 1) return;
r->seen = round;
if (s < 0.0) { r->lawless = true; return; }
r->conf *= s;
}
/* Whether round `j` (>= 2) runs for this row, given every statistic up to round
* draft_depth_need(c, j) was observed. Rounds are asked in order; once one is refused the chain has
* ended. Records the answer in r->depth / r->done. */
inline bool draft_depth_go(const DraftDepthCfg& c, DraftDepthRow* r, int j) {
if (r->done || j != r->depth + 1) return false;
bool go;
if (j > c.max) go = false;
else if (r->lawless) go = j <= (c.lawless > 0 ? c.lawless : 1);
else if (j <= c.min) go = true;
else {
/* conf(need): the row has seen exactly `need` rounds when asked in order with lag >= 0, or
* more when the caller observed ahead; recompute is not possible, so the caller observes
* no further than draft_depth_need before asking (the driver does). */
go = r->conf >= c.stop;
}
if (go) r->depth = j; else r->done = true;
return go;
}
/* The whole rule at once, for a chain whose statistics are all known (tests, offline): s[i] is
* round i + 1's, n of them (n >= max - 1 - lag is enough). Returns the depth D in [1, max]. */
inline int draft_depth_of(const DraftDepthCfg& c, const double* s, int n) {
DraftDepthRow r;
draft_depth_begin(c, &r, false);
for (int j = 2; j <= c.max; ++j) {
const int need = draft_depth_need(c, j);
while (r.seen < need && r.seen < n) draft_depth_observe(&r, r.seen + 1, s[r.seen]);
if (!draft_depth_go(c, &r, j)) break;
}
return r.depth < 1 ? 1 : r.depth;
}
} /* namespace rad */
+47
View File
@@ -1625,6 +1625,47 @@ the head's window. At temperature 0 the text
is that of a fixed depth wherever fixed depths agree; above 0 it is distribution-exact.
`radiance:draft_depth_adaptive` and `radiance:draft_verify_depth_total` (§6.3) show the windows.
**Adaptive chain depth: `draft-depth: adaptive`.** `draft-window: adaptive` picks the window of the
next step from the acceptance and the cost of the steps before it. A coupled serial head can also
stop the chain of a step within that step, from the head's own laws: a fixed chain drafts N tokens
every step and its verify pays for N + 1 rows whatever the head thought of them. With `draft-depth:
adaptive` (`draft-coupling: on` and a serial head; `draft-window` stays `fixed`, since both decide
the depth and the engine refuses them together) `num-speculative-tokens` (or `draft-depth-max`) is
the deepest chain, and round j runs only while the product of the earlier rounds' probabilities is at
least `draft-depth-stop` -- each round's largest probability (`draft-depth-stat: top`, the default)
or the probability of the token it drew (`path`). The next verify then carries exactly the drafts
made (1 to N, each row count its own recorded pass), so a step whose head is unsure costs one or two
rows and a confident one drafts deep.
```yaml
num-speculative-tokens: 5 # the deepest chain
draft-coupling: on
draft-verify: traversal
draft-depth: adaptive
draft-depth-stop: 0.85
```
The next verify is issued before the drafts exist (it runs behind them on the card), so its row count
has to be known when it is issued. The host therefore reads one state row back after a round and
decides the round after the next one (`draft-depth-lag: 1`, the default): the card is running the
round in between, so the readback, the decision and the issue of the next round or of the verify
(about 0.1 ms to the first packet) hide behind it and nothing waits. `draft-depth-lag: 0` decides
every round from all the rounds before it; the card then waits for the host between two rounds. The
step's tokens are streamed after the chain's last decision rather than right after the sampler (up
to a millisecond or so later; the rate is the same). The rule reads the draft's side only -- the laws
the rounds drew from and, for `path`, the tokens drawn before the one a decision admits -- never the
target and never the admitted token itself, so the row-wise and the traversal verify stay exact
(`core/sched/draft_depth.h` has the argument, `tests/mtp_rrs_test.cpp` the chi-square and the two
rules that fail it). A greedy request has no law to read and drafts min(3, N). Shutdown logs the
depths taken, and per depth the card's cycle and the drafts its verify accepted (`draft depth
adaptive (final)`). On Qwen3.8-Flash-Next (2x R9700, traversal verify) a cap of 5 at stop 0.85 took
2.79 tokens a step where the fixed chain of 3 took 2.70, at the same mean of 3.0 drafts and a step
0.7% longer: +3.0% tokens a second (95% interval +1.0 to +5.0%) over temperatures 0.7 and 1.0; a
cap of 7 gained no more, a deep verify row costing more than a shallow one. The stop is not
sensitive between 0.80 and 0.90. The engine keeps 48 recorded passes instead of 24 (an adaptive
chain makes more pass shapes than 24 hold: 3352 recordings per benchmark became 1472, +1.7% tokens a
second for this mode); the other modes touch no more than they did.
**Chained launches.** Where the model's plugin measured its step faster with them, the AQL backend
lets a kernel that depends on the one before it on its queue wait in its own prologue instead of
behind the packet processor's barrier bit, which saves most of the launch gap between dependent
@@ -2421,6 +2462,12 @@ switch is set with `true`. The fleet's own keys (a model's `aliases`, `priority`
| `spec-ngram: K` | 0 | prompt lookup alone: up to K drafts a step from the sequence's own n-grams, each proposal verified when it pays at the measured step cost; a container that declares a draft head is refused; sets the window to K (§4.6) |
| `spec-ngram-min: N`, `spec-ngram-max: N` | 2, 4 | the pattern lengths prompt lookup tries, longest first (at most 15), wherever it drafts |
| `draft-window: MODE` | fixed | `fixed` drafts N every step; `adaptive` follows each sequence's acceptance and the measured step cost, for a serial draft head or prompt lookup alone (§4.6) |
| `draft-depth: MODE` | fixed | `adaptive` stops a coupled serial chain within the step once the product of the head's probabilities along it falls below `draft-depth-stop`; the next verify carries only the drafts made (§4.6). Needs `draft-coupling: on` and `draft-window: fixed` |
| `draft-depth-max: N` | auto | `draft-depth: adaptive`: the deepest chain (auto: `num-speculative-tokens`) |
| `draft-depth-min: N` | 1 | `draft-depth: adaptive`: rounds that always run |
| `draft-depth-stop: P` | 0.85 | `draft-depth: adaptive`: the chain goes on while its confidence is at least P |
| `draft-depth-lag: K` | 1 | `draft-depth: adaptive`: rounds the decision trails the chain (0 or 1); 1 hides the host's decision behind a running round |
| `draft-depth-stat: S` | top | `draft-depth: adaptive`: `top` multiplies each round's largest probability, `path` the probability of the token it drew |
| `draft-short-from: N` | auto | a block drafter that declares a shorter block runs it from N decoding sequences on, its own block again two below or at one sequence; 0 never; auto never for a chain and 2 for a tree drafter, which then verifies the short block's nodes (§4.6) |
| `draft-coupling: MODE` | off | `on`: a linear MTP drafter with a 2-bit candidate head (Qwen3.8-Flash-Next) samples each proposal from its reranked top-8 and the sampler maximally couples the target's draw to it. The output distribution is unchanged and a fixed seed yields other tokens than `off`. The head's law is taken at temperature 1, so it pays at high request temperatures and costs at low ones: on Flash-Next TP2 at 2x R9700, tokens per decode step +18% at temperature 1.0 and c1, +11% at 0.7 and c4, -4% at 0.3. Greedy rows are unchanged. A DFlash2 drafter (Qwen3.5/3.8) couples its candidate walk instead: each position is sampled from the softmax of the walk's own scores over its best eight candidates, at temperature 0.6 (fixed in `qwen35_fp8`, where acceptance peaked); on Qwen3.8-27B MXFP4 TP2 at 2x R9700 and request temperature 1, tokens per verify step +8% on held-out prompts at c4 (3.53 to 3.82) and decode +2..8% per stream at c1/c4. Any other drafter is refused at startup |
| `draft-verify: MODE` | token | how a coupled draft chain is verified: `token` decides each row on its own (maximal coupling); `block` decides the whole chain at once (Block Verification, arXiv 2403.10444) and switches `draft-coupling` on; `traversal` walks the chain bottom-up (Traversal Verification, arXiv 2505.12398) and needs `draft-coupling: on`. The output distribution is the same in all three, every draft a step accepts is one the target's own law would have produced, and `block` and `traversal` accept at least as many drafts a step as `token`. Sampled rows only: a greedy row is verified as before. The verify runs as one workgroup a request, with no scratch (§6) |
+43
View File
@@ -408,6 +408,49 @@ TEST(a_step_issued_ahead_takes_every_rows_tokens_positions_and_slots_from_its_st
for (int t = 0; t < 9; ++t) CHECK_EQ(s.slot(1, t), 0);
}
/* --draft-depth adaptive (core/sched/draft_depth.h): the head ran for the deepest row of its step,
* so a slot holds every round's proposal, but the step issued ahead verifies only the chain each row
* really made -- its first d proposals, in 1 + d rows. The trunk advance takes exactly those (the
* rows' count is the batch's, never the slot's) and the acceptance behind that verify can accept no
* more than d, whatever the slot still holds past them. Two rows of one step differ in depth. */
TEST(a_verify_of_a_chain_cut_short_takes_and_accepts_only_its_own_first_drafts) {
Slab s(ADV_TRUNK);
/* card 1 drafted five rounds and verifies two of them; card 3 verifies four */
s.row(0, true, 0, 3, /*ctx bound=*/40, /*card=*/1, 0, 0, 2);
s.row(1, true, 3, 5, 90, 3, 3, 3, 4);
for (int j = 0; j <= 5; ++j) { s.st[1].tok[j] = 700 + j; s.st[3].tok[j] = 800 + j; }
s.st[1].ctx = 20; s.st[1].n_acc = 3;
s.st[3].ctx = 60; s.st[3].n_acc = 1;
for (int t = 0; t < 8; ++t) s.w[Slab::TOK + t] = -5;
s.w[Slab::TOK + 8] = -5;
s.run();
const int32_t tok[8] = { 700, 701, 702, 800, 801, 802, 803, 804 };
const int32_t pos[8] = { 20, 21, 22, 60, 61, 62, 63, 64 };
for (int t = 0; t < 8; ++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), pos[t]);
}
CHECK_EQ(s.w[Slab::TOK + 8], -5); /* nothing past the rows the step has */
CHECK_EQ(s.used(0, 0), 23);
CHECK_EQ(s.used(0, 1), 65);
CHECK_EQ(s.w[Slab::ACC + 0], 3);
CHECK_EQ(s.w[Slab::ACC + 1], 1);
/* The acceptance behind that verify: the target agrees with every proposal the slot holds, and
* the row still accepts its own d only (the row descriptor's draft count), then emits the
* target's token at the row after them. */
Slab v(ADV_ACCEPT);
v.row(0, true, 0, 3, 20, 1, 0, 0, 2);
v.row(1, true, 3, 5, 60, 3, 3, 3, 4);
for (int t = 0; t < 8; ++t) v.w[Slab::TOK + t] = tok[t];
const int32_t y[8] = { 701, 702, 703, 801, 802, 803, 804, 805 };
for (int t = 0; t < 8; ++t) v.src[t] = y[t];
v.run();
CHECK_EQ(v.st[1].acc, 2); CHECK_EQ(v.st[1].tok[0], 703); CHECK_EQ(v.st[1].ctx, 23);
CHECK_EQ(v.st[3].acc, 4); CHECK_EQ(v.st[3].tok[0], 805); CHECK_EQ(v.st[3].ctx, 65);
}
/* A row the card moves keeps the host's offset between its position and its rotary position on
* all three planes (a text row after an image rotates at its index plus a shift); a row whose
* position does not move keeps the host's three components, which differ for a media token. A
+60
View File
@@ -11120,6 +11120,66 @@ TEST(flashnext_draft_passes_refill_the_rope_table_where_the_carry_is_live) {
/* The vocab-parallel draft head gathers each rank's top-R as (id, value) pairs: a row is 2R
* floats, whole 16-byte words, and the gather moves every draft row's. A row of one pair (8 bytes)
* is what the two-rank gather cannot take. */
/* --draft-depth adaptive (core/sched/draft_depth.h): a deployment declared for the deepest chain D
* verifies, step by step, the 1 .. D drafts the chain of that step really made. The plugin learns the
* depth from the batch's rows alone; a verify of d drafts under the deep declaration issues exactly
* the ops, in the order, of a chain deployment declared at depth d, and so does the history pass
* behind it (whose rows mirror the verify's). Sequences of one step may differ in depth. */
TEST(flashnext_a_shorter_verify_under_a_deeper_coupled_chain_issues_that_chains_ops) {
MetaKV mk = q4_small();
const RadModelMeta& meta = *mk.meta();
const int D = 5;
auto seq = [](const RadBuilder& b, const RadCtx& c) {
std::vector<std::string> v;
for (const auto& i : c.issues) if (const RecOp* o = b.op_of(i.op)) v.push_back(o->op);
return v;
};
/* One plugin instance keeps its handles per slot: each deployment is declared right before its
* steps run. */
auto run = [&](int declared, const std::vector<int>& rows, std::vector<std::string>* ver,
std::vector<std::string>* his) {
RadBuilder b;
b.encs = { { "draft_head", u2_head() } };
RadBuildCtx c = q4_ctx(0, 1, 4);
c.max_spec = declared;
c.draft_coupling = 1;
REQUIRE_EQ(qwen4exp_fp8::declare(&b, &meta, &c), RAD_OK);
CHECK_EQ(b.drafter.depth, declared);
const auto k = kit(b, rows, {});
const auto h = history_pass(b, *k);
const auto v = run_passes(b, qwen4exp_fp8::step, { k.get(), h.get() });
CHECK_PASSES(b, v);
*ver = seq(b, *v[0]);
*his = seq(b, *v[1]);
};
for (int d = 1; d <= D; ++d) {
const std::vector<int> rows((size_t)4, d + 1);
std::vector<std::string> fv, fh, av, ah;
run(d, rows, &fv, &fh);
/* A head of one round ends in its history pass; with more rounds that pass also hands its
* output to round 2 (one cast), whatever the verify behind it carried. The history pass of a
* one-draft verify is therefore compared under a two-round declaration. */
if (d == 1) {
std::vector<std::string> v2;
run(2, rows, &v2, &fh);
CHECK(v2 == fv);
}
run(D, rows, &av, &ah);
CHECK(!fv.empty() && !fh.empty());
CHECK(av == fv);
CHECK(ah == fh);
if (av != fv || ah != fh) std::fprintf(stderr, " depth %d under %d: issue pattern differs\n", d, D);
}
/* depths that differ between the sequences of one step: the ops of the full-depth step. The
* rows add up past kPrefetchRows, as the full-depth step's do: below it the model issues a
* step by its row count (the decode-height issue order), and that is not this property. */
std::vector<std::string> mv, mh, dv, dh;
run(D, { 2, D + 1, D + 1, D + 1 }, &mv, &mh);
run(D, { D + 1, D + 1, D + 1, D + 1 }, &dv, &dh);
CHECK(mv == dv);
CHECK(mh == dh);
}
TEST(flashnext_draft_head_gathers_whole_candidate_rows_at_two_ranks) {
MetaKV mk = q4_small();
const RadModelMeta& meta = *mk.meta();
+46
View File
@@ -588,6 +588,9 @@ const std::map<std::string, std::vector<std::string>>& legal_values() {
{ "--spec-ngram-min", { "1" } }, { "--spec-ngram-max", { "6" } },
{ "--draft-window", { "fixed" } }, { "--draft-short-from", { "4" } },
{ "--draft-coupling", { "on" } }, { "--draft-verify", { "block" } },
{ "--draft-depth", { "adaptive" } }, { "--draft-depth-max", { "5" } },
{ "--draft-depth-min", { "2" } }, { "--draft-depth-stop", { "0.7" } },
{ "--draft-depth-lag", { "0" } }, { "--draft-depth-stat", { "path" } },
{ "--host", { "127.0.0.1" } }, { "--port", { "8001" } }, { "--api-key", { "k" } },
{ "--api-key-env", { "RAD_KEY_ENV_INVENTORY" } },
{ "--allow-host", { "box" } },
@@ -1037,6 +1040,49 @@ TEST(the_draft_window_is_fixed_or_adaptive) {
CHECK_EQ(std::string(draft_window_name(DraftWindow::Fixed)), std::string("fixed"));
}
/* --draft-depth is fixed unless asked for. adaptive switches the coupled drafting on (its rule reads
* the draft's laws), excludes the fixed block verify and --draft-window adaptive, and takes its knobs
* in range. */
TEST(draft_depth_adaptive_parses_and_needs_a_coupled_chain) {
Config c;
CHECK_EQ(parse({}, &c), RAD_OK);
CHECK(!c.draft_depth_adaptive);
CHECK_EQ(c.draft_depth_max, 0);
CHECK_EQ(c.draft_depth_min, 1);
CHECK_EQ(c.draft_depth_lag, 1);
CHECK_EQ(c.draft_depth_stat, 0);
CHECK(c.draft_depth_stop == 0.85);
CHECK(!c.draft_coupling);
Config a;
CHECK_EQ(parse({ "--draft-depth", "adaptive", "--draft-depth-max", "6", "--draft-depth-stop", "0.6",
"--draft-depth-lag", "0", "--draft-depth-stat", "path", "--draft-depth-min", "2" }, &a),
RAD_OK);
CHECK(a.draft_depth_adaptive);
CHECK(a.draft_coupling);
CHECK_EQ(a.draft_depth_max, 6);
CHECK_EQ(a.draft_depth_min, 2);
CHECK_EQ(a.draft_depth_lag, 0);
CHECK_EQ(a.draft_depth_stat, 1);
CHECK(a.draft_depth_stop == 0.6);
CHECK_EQ(parse({ "--draft-depth", "fixed" }, &a), RAD_OK);
CHECK(!a.draft_depth_adaptive);
CHECK_EQ(parse_fresh({ "--draft-depth", "adaptive", "--draft-verify", "traversal" }), RAD_OK);
for (const char* bad : { "on", "auto", "" }) CHECK_EQ(parse_fresh({ "--draft-depth", bad }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--draft-depth" }), RAD_E_INVAL);
for (const char* bad : { "-0.1", "1.5", "x" }) CHECK_EQ(parse_fresh({ "--draft-depth-stop", bad }), RAD_E_INVAL);
for (const char* bad : { "2", "-1" }) CHECK_EQ(parse_fresh({ "--draft-depth-lag", bad }), RAD_E_INVAL);
for (const char* bad : { "17", "-1" }) CHECK_EQ(parse_fresh({ "--draft-depth-max", bad }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--draft-depth-min", "0" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--draft-depth-stat", "mean" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--draft-depth", "adaptive", "--draft-coupling", "off" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--draft-depth", "adaptive", "--draft-window", "adaptive" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--draft-depth", "adaptive", "--draft-window", "fixed" }), RAD_OK);
CHECK_EQ(parse_fresh({ "--draft-depth", "adaptive", "--draft-verify", "block" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--draft-depth", "adaptive", "--draft-depth-min", "4", "--draft-depth-max", "3" }),
RAD_E_INVAL);
}
/* --draft-verify is token (alias rowwise) unless asked for; block and traversal parse. block
* implies the coupled drafting and refuses coupling off; traversal needs the coupling and says so. */
TEST(the_draft_verify_is_token_block_or_traversal) {
+199
View File
@@ -17,6 +17,8 @@
* sees each of them
* degenerate Q == P accepts the first child; disjoint supports reject all and emit
* from P
* adaptive depth a chain cut by the rule of sched/draft_depth.h keeps the target law
* under both verifies; two rules that read what they may not do not
* fixed point the walk's integer exponential tracks exp to 2^-22, its law is the
* softmax to FP32 accuracy, and by enumeration the walk emits exactly
* the law it reads
@@ -31,6 +33,7 @@
#include "rad_mtp_rrs.h"
#include "rad_mtp_traversal.h"
#include "../plugin/spechub/spechub.h"
#include "sched/draft_depth.h"
#include <algorithm>
#include <functional>
@@ -1143,4 +1146,200 @@ TEST(traversal_verification_of_rheo_siblings_is_exact) {
CHECK(worst_bad > 20.0);
}
/* ==================================================================== --draft-depth adaptive
*
* The chain of a step stops by the head's own laws (core/sched/draft_depth.h): round j is drafted
* only while the product of the earlier rounds' statistic holds. The rule under test is the
* engine's own function over the states the real propose row wrote; the verify is the real
* row-wise coupling and the real traversal walk. A generation runs over several steps, so the
* depth of every step depends on the tokens the steps before it committed and on its own drafts.
* Exact for every rule the engine offers (top / path statistic, decided 0 or 1 round behind);
* the two mistakes a rule can make are caught by the same statistic:
* control 1 admits a draft by ITS OWN mass under the head's law (a draft-side quantity, but of
* the token the decision admits: the chain keeps the drafts the head liked);
* control 2 peeks at the target: admits a draft only where the target's mass on it is at least
* the head's (the chain keeps the drafts the verify is going to like). */
using namespace rad;
namespace {
struct DepthRun { double z3, z1, mean_acc, mean_depth; double share[8]; };
/* A drafted chain of at most cfg.max rounds, cut by the rule (ctl 0) or by a control. */
int draft_depth_chain(Tree* t, uint64_t seed, uint64_t base, const DraftDepthCfg& cfg, double noise,
uint64_t h0, int ctl, double ctl_thr) {
draft_chain(t, seed, base, cfg.max, 0, noise, h0);
int D = cfg.max;
if (ctl == 0) {
double st[RAD_MTP_COUPLE_MAX_ROUNDS];
for (int r = 1; r <= cfg.max; ++r) st[r - 1] = draft_depth_stat(t->st[r - 1], r, cfg.stat);
D = draft_depth_of(cfg, st, cfg.max);
} else {
for (int r = 1; r <= cfg.max; ++r) {
const RadMtpCoupleState& c = t->st[r - 1];
double own = 0;
for (int i = 0; i < c.count; ++i) if (c.ids[i] == c.x) own = c.q[i];
bool keep;
if (ctl == 1) keep = own >= ctl_thr;
else {
const Law law = law_at(t->hash[r - 1], noise);
keep = law.p[c.x] >= own;
}
if (!keep) { D = r - 1; break; }
}
}
/* the rounds past the stop never ran: no node, no state */
for (int j = D; j < 64; ++j) t->st[j] = RadMtpCoupleState{};
t->n = D;
return D;
}
/* `trials` generations of three tokens, each step an adaptive chain verified by the traversal walk
* (trav) or row by row; the joint law of the three against the target's. */
DepthRun run_depth(const DraftDepthCfg& cfg, bool trav, int trials, double noise, int ctl = 0,
double ctl_thr = 0) {
std::vector<double> cnt3((size_t)V * V * V, 0), cnt1((size_t)V, 0);
DepthRun R{};
double steps = 0;
for (int s = 0; s < trials; ++s) {
const uint64_t seed = 0x51ed27ull + (uint64_t)s * 104729ull;
int out[16], len = 0;
uint64_t h = 1;
while (len < 3) {
Tree t;
const int D = draft_depth_chain(&t, seed, (uint64_t)len, cfg, noise, h, ctl, ctl_thr);
R.mean_depth += D; ++R.share[D < 8 ? D : 7]; ++steps;
int step_out[64], accepted = 0, n;
if (D == 0) {
/* no draft at all (a control cut the first one): the ordinary pick */
step_out[0] = sample_law(law_at(h, noise), seed, (uint64_t)len, 0);
n = 1;
} else if (trav) {
n = walk_trav(t, seed, (uint64_t)len, noise, 0, step_out, &accepted, RadMtpTravOpt{});
} else {
n = walk(t, seed, (uint64_t)len, noise, 0, step_out, &accepted);
}
R.mean_acc += accepted;
for (int i = 0; i < n && len < 16; ++i) { out[len++] = step_out[i]; h = child_hash(h, step_out[i]); }
}
++cnt1[(size_t)out[0]];
++cnt3[(size_t)((out[0] * V + out[1]) * V + out[2])];
}
std::vector<double> e1((size_t)V, 0), e3((size_t)V * V * V, 0);
const Law l0 = law_at(1, noise);
for (int a = 0; a < V; ++a) {
e1[(size_t)a] = l0.p[a];
const uint64_t ha = child_hash(1, a);
const Law l1 = law_at(ha, noise);
for (int b = 0; b < V; ++b) {
const Law l2 = law_at(child_hash(ha, b), noise);
for (int c = 0; c < V; ++c) e3[(size_t)((a * V + b) * V + c)] = l0.p[a] * l1.p[b] * l2.p[c];
}
}
R.z3 = zscore(cnt3, e3, trials);
R.z1 = zscore(cnt1, e1, trials);
R.mean_acc /= steps; R.mean_depth /= steps;
for (double& x : R.share) x /= steps;
return R;
}
DraftDepthCfg depth_cfg(int max, int lag, int stat, double stop, int min = 1) {
DraftDepthCfg c;
c.on = true; c.max = max; c.min = min; c.lag = lag; c.stat = stat; c.stop = stop;
return c;
}
} /* namespace */
/* The rule itself: rounds are decided in order, `lag` rounds behind, and the first refusal ends the
* chain; min rounds always run, max bounds it, a row with no law takes the fixed fallback. */
TEST(draft_depth_rule_decides_each_round_from_the_rounds_it_may_read) {
const double s[] = { 0.9, 0.8, 0.5, 0.9, 0.9 }; /* conf: .9 .72 .36 .324 .2916 */
CHECK_EQ(draft_depth_of(depth_cfg(5, 0, 0, 0.7), s, 5), 3); /* round 4 needs conf(3) = .36 */
CHECK_EQ(draft_depth_of(depth_cfg(5, 1, 0, 0.7), s, 5), 4); /* a round later: conf(2) admits round 4 */
CHECK_EQ(draft_depth_of(depth_cfg(5, 0, 0, 0.95), s, 5), 1);
CHECK_EQ(draft_depth_of(depth_cfg(5, 1, 0, 0.95), s, 5), 2); /* round 2 reads nothing: it always runs */
CHECK_EQ(draft_depth_of(depth_cfg(5, 0, 0, 0.0), s, 5), 5);
CHECK_EQ(draft_depth_of(depth_cfg(3, 0, 0, 0.0), s, 5), 3);
CHECK_EQ(draft_depth_of(depth_cfg(5, 0, 0, 0.95, 3), s, 5), 3); /* min */
CHECK_EQ(draft_depth_of(depth_cfg(1, 0, 0, 0.0), s, 5), 1);
/* the engine's incremental form: a decision never reads past draft_depth_need */
const DraftDepthCfg c = depth_cfg(5, 1, 0, 0.7);
CHECK_EQ(draft_depth_need(c, 2), 0);
CHECK_EQ(draft_depth_need(c, 5), 3);
DraftDepthRow r;
draft_depth_begin(c, &r, false);
CHECK(draft_depth_go(c, &r, 2));
draft_depth_observe(&r, 1, 0.9);
CHECK(draft_depth_go(c, &r, 3));
draft_depth_observe(&r, 2, 0.8);
CHECK(draft_depth_go(c, &r, 4));
draft_depth_observe(&r, 3, 0.5);
CHECK(!draft_depth_go(c, &r, 5));
CHECK(r.done); CHECK_EQ(r.depth, 4);
CHECK(!draft_depth_go(c, &r, 5)); /* the chain has ended */
/* no law: a greedy row, or a state the proposer could not make a law of */
DraftDepthRow g;
draft_depth_begin(c, &g, true);
CHECK(draft_depth_go(c, &g, 2)); CHECK(draft_depth_go(c, &g, 3)); CHECK(!draft_depth_go(c, &g, 4));
DraftDepthRow m;
draft_depth_begin(c, &m, false);
CHECK(draft_depth_go(c, &m, 2));
draft_depth_observe(&m, 1, -1.0);
CHECK(m.lawless);
CHECK(draft_depth_go(c, &m, 3)); CHECK(!draft_depth_go(c, &m, 4));
/* the statistic of a state: the largest mass, or the drawn token's; none without a law */
RadMtpCoupleState st{};
st.status = 1; st.round = 2; st.count = 3; st.x = 7;
st.ids[0] = 5; st.ids[1] = 7; st.ids[2] = 9;
st.q[0] = 0.6; st.q[1] = 0.3; st.q[2] = 0.1;
CHECK(draft_depth_stat(st, 2, DRAFT_DEPTH_TOP) == 0.6);
CHECK(draft_depth_stat(st, 2, DRAFT_DEPTH_PATH) == 0.3);
CHECK(draft_depth_stat(st, 1, DRAFT_DEPTH_TOP) < 0); /* another round's state */
st.status = 0;
CHECK(draft_depth_stat(st, 2, DRAFT_DEPTH_TOP) < 0);
}
TEST(adaptive_chain_depth_keeps_the_target_law) {
const int trials = 300000;
g_sharp = 2.0;
struct C { int lag, stat; double stop; const char* name; };
const C cs[] = { { 0, DRAFT_DEPTH_TOP, 0.36, "top lag0" }, { 1, DRAFT_DEPTH_TOP, 0.40, "top lag1" },
{ 0, DRAFT_DEPTH_PATH, 0.12, "path lag0" }, { 1, DRAFT_DEPTH_PATH, 0.22, "path lag1" } };
for (const C& c : cs) {
const DraftDepthCfg cfg = depth_cfg(4, c.lag, c.stat, c.stop);
const DepthRun tv = run_depth(cfg, true, trials, 0.6);
const DepthRun rw = run_depth(cfg, false, trials, 0.6);
std::printf(" depth %-9s stop %.2f: trav z3=%.2f z1=%.2f acc=%.3f | rowwise z3=%.2f z1=%.2f acc=%.3f | "
"depth %.2f shares %.2f %.2f %.2f %.2f\n", c.name, c.stop, tv.z3, tv.z1, tv.mean_acc,
rw.z3, rw.z1, rw.mean_acc, tv.mean_depth, tv.share[1], tv.share[2], tv.share[3], tv.share[4]);
CHECK(tv.z3 < 4.5); CHECK(tv.z1 < 4.5);
CHECK(rw.z3 < 4.5); CHECK(rw.z1 < 4.5);
/* the rule really stops chains at different depths, and the walk gains from the longer ones */
int used = 0;
for (int d = 1; d <= 4; ++d) used += tv.share[d] > 0.10;
CHECK(used >= 2);
CHECK(tv.mean_acc >= rw.mean_acc);
}
/* a far head and a min of two rounds */
const DepthRun far = run_depth(depth_cfg(4, 1, DRAFT_DEPTH_TOP, 0.5, 2), true, 200000, 2.0);
CHECK(far.z3 < 4.5); CHECK(far.z1 < 4.5);
g_sharp = 1.0;
}
TEST(a_depth_rule_that_reads_the_admitted_draft_or_the_target_is_not_exact) {
const int trials = 100000;
g_sharp = 2.0;
const DraftDepthCfg cfg = depth_cfg(4, 0, DRAFT_DEPTH_TOP, 0.0);
for (int trav = 0; trav < 2; ++trav) {
const DepthRun own = run_depth(cfg, trav != 0, trials, 0.6, 1, 0.30);
const DepthRun peek = run_depth(cfg, trav != 0, trials, 0.6, 2, 0);
std::printf(" depth controls (%s): own-mass z3=%.1f z1=%.1f depth %.2f | target peek z3=%.1f z1=%.1f depth %.2f\n",
trav ? "traversal" : "rowwise", own.z3, own.z1, own.mean_depth, peek.z3, peek.z1,
peek.mean_depth);
CHECK(own.z3 > 20.0 || own.z1 > 20.0);
CHECK(peek.z3 > 20.0 || peek.z1 > 20.0);
}
g_sharp = 1.0;
}
RAD_TEST_MAIN()
+146
View File
@@ -8070,6 +8070,152 @@ TEST(a_wider_state_index_row_is_the_managers_row_on_every_build) {
CHECK(checked > 100);
}
/* ================================================================== --draft-depth adaptive
*
* The engine's adaptive chain (core/sched/draft_depth.h) gives every sequence its own number of
* drafts each step: the drafter ran for the deepest row, expect_draft names each row's depth, the
* step issued ahead verifies exactly that many (1 + d rows, the card's ADV_TRUNK taking the first d
* proposals of the slot) and set_draft publishes the same d. That step must be the step the
* scheduler builds after the commit from a d-token proposal, row for row, on every step of a run
* whose depths change from step to step and differ between the sequences of one step. */
namespace {
int depth_at(uint64_t seq, int t, int n_spec) {
return 1 + (int)(ahead_mix(seq * 31 + 7, (uint64_t)t) % (uint32_t)n_spec);
}
void depth_publish(Fixture& f, const std::vector<uint64_t>& ids, int n_spec, int t) {
for (uint64_t id : ids) {
int32_t d[8];
const int dn = depth_at(id, t, n_spec);
for (int j = 0; j < dn; ++j) d[j] = ahead_draft(id, t, j);
CHECK_OK(f.sch.set_draft(id, d, dn));
}
}
AheadRun depth_reference(int n_spec, int steps) {
Fixture f;
std::vector<uint64_t> ids;
ahead_setup(f, n_spec, ids);
AheadRun run;
const RadBatch* b = f.sch.step();
if (!b) return run;
run.steps.push_back(read_rows(b, f.sch.plan(), f.kv));
for (int t = 0; t < steps; ++t) {
CHECK_OK(ahead_commit(f, t));
depth_publish(f, ids, n_spec, t + 1);
b = f.sch.step();
if (!b) break;
run.steps.push_back(read_rows(b, f.sch.plan(), f.kv));
}
ahead_finish(f, ids, run);
return run;
}
AheadRun depth_pipelined(int n_spec, int steps, std::vector<int>* rows_seen) {
Fixture f;
std::vector<uint64_t> ids;
ahead_setup(f, n_spec, ids);
AheadRun run;
std::vector<StepSlot> slots((size_t)f.cfg.max_seqs);
const RadBatch* b = f.sch.step();
if (!b) return run;
run.steps.push_back(read_rows(b, f.sch.plan(), f.kv));
for (int t = 0; t < steps; ++t) {
const StepPlan& p = f.sch.plan();
std::vector<int32_t> 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;
/* The head ran every round (the deepest row's); this row's chain is its first d. */
for (int j = 0; j < n_spec; ++j)
slots[(size_t)e.card].tok[1 + j] = ahead_draft(e.seq, t + 1, j);
f.sch.expect_draft(e.seq, depth_at(e.seq, t + 1, n_spec));
}
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);
for (int64_t i = 0; i < nb->n_seq; ++i) rows_seen->push_back(nb->q_lens[i]);
}
CHECK_OK(ahead_commit(f, t));
depth_publish(f, ids, n_spec, t + 1);
if (nb) {
f.sch.promote_next();
b = nb;
++run.ahead;
} else {
b = f.sch.step();
if (!b) break;
}
run.steps.push_back(read_rows(b, f.sch.plan(), f.kv));
}
ahead_finish(f, ids, run);
return run;
}
} /* namespace */
TEST(a_step_issued_ahead_with_each_rows_own_draft_depth_equals_the_step_built_after_the_commit) {
const int n_spec = 5, T = 32;
std::vector<int> rows;
const AheadRun a = depth_reference(n_spec, T);
const AheadRun b = depth_pipelined(n_spec, T, &rows);
CHECK_EQ(b.ahead, T);
CHECK_EQ(b.card_host_disagree, 0);
REQUIRE_EQ(a.steps.size(), b.steps.size());
REQUIRE_EQ(a.steps.size(), (size_t)T + 1);
for (size_t t = 0; t < a.steps.size(); ++t) {
const bool same = a.steps[t] == b.steps[t];
CHECK(same);
if (!same) std::fprintf(stderr, " adaptive depth: step %zu differs\n", t);
CHECK_EQ(a.steps[t].bad_slots, 0);
CHECK_EQ(b.steps[t].bad_slots, 0);
}
CHECK(a.out == b.out);
/* every depth 1 .. n_spec was verified, as 1 + d rows */
for (int d = 1; d <= n_spec; ++d)
CHECK(std::count(rows.begin(), rows.end(), d + 1) > 0);
CHECK_EQ(std::count_if(rows.begin(), rows.end(), [&](int r) { return r < 2 || r > n_spec + 1; }),
(std::ptrdiff_t)0);
REQUIRE_EQ(a.held.size(), b.held.size());
for (size_t i = 0; i < a.held.size(); ++i) {
CHECK(b.held[i] >= a.held[i]);
CHECK(b.held[i] <= a.held[i] + 1);
}
}
/* The count a step issued ahead was planned with is binding: a proposal of another length for it is
* refused (the card verifies the planned rows), under a fixed window as under any other. */
TEST(a_draft_of_another_depth_than_the_step_issued_ahead_was_planned_with_is_refused) {
Fixture f;
std::vector<uint64_t> ids;
ahead_setup(f, 5, ids);
const RadBatch* b = f.sch.step();
REQUIRE(b != nullptr);
for (uint64_t id : ids) f.sch.expect_draft(id, 2);
const RadBatch* nb = f.sch.step_next();
REQUIRE(nb != nullptr);
for (int64_t i = 0; i < nb->n_seq; ++i) CHECK_EQ(nb->q_lens[i], 3);
CHECK_OK(ahead_commit(f, 0));
const int32_t d[5] = { 11, 12, 13, 14, 15 };
CHECK_EQ(f.sch.set_draft(ids[0], d, 3), RAD_E_STATE);
CHECK_EQ(f.sch.set_draft(ids[0], d, 1), RAD_E_STATE);
CHECK_OK(f.sch.set_draft(ids[0], d, 2));
}
RAD_TEST_MAIN()
/* ================================================================== the plan without a span */