Archived
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:
1 parent
d0fd4aa1bb
commit
e78d484e60
13 files changed
+1054
-4
No files matched your search
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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()
|
||||
@@ -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 */
|
||||
|
||||
Reference in new issue
Block a user