From c43347d0945ae1fbebbfcb43e332d54989d9c272 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 13 Nov 2026 02:25:47 +0100 Subject: [PATCH] spec: a learning stretch times window sizes past its proposals with filler rows Sizes 5 and 6 of the Radeon 780M and 4 and 5 of the M5 Max were never reached by a lookup proposal in a prose learning stretch, so their table entries were filled in (780M: 174.9 and 206.1 ms against 160 and 178 measured by live costs; the lookup of 5 rows was then priced out and the frozen table lost 1.9-2.5 % on code and JSON to live costs). - choose() takes pad_cap: during learning, when the caller allows it (a deterministic proposal, never a probabilistic window), the window of the smallest size with under 30 samples is run even when the proposal is shorter; the rows past the proposal repeat its last token, which the verifier rejects. Pick::pad says how many rows are filler. - A window run this way goes to the learner and to the cost of its size (observe_forced) and to no lookup or MTP statistic. - The forced-round budget is 200 per stretch (40 per size). - Unit test draft_policy_test: 108 of 108 (5 new: padded window, filler count, forced round feeds no statistic, pad_cap of one draft). --- include/strata/spec/draft_policy.hpp | 17 +++++++++++------ src/program/generate.cpp | 17 ++++++++++++++--- src/spec/draft_policy.cpp | 22 ++++++++++++++++++---- src/spec/draft_policy_test.cpp | 16 ++++++++++++++++ 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/include/strata/spec/draft_policy.hpp b/include/strata/spec/draft_policy.hpp index a958803a38..cfbf2dcbc4 100644 --- a/include/strata/spec/draft_policy.hpp +++ b/include/strata/spec/draft_policy.hpp @@ -128,11 +128,16 @@ public: struct Pick { bool lookup = false; int t = 1; // window size (1 + drafts) + int pad = 0; // a learning stretch's window longer than the proposal: this many rows past it are filler (the caller fills them) }; /// `t_mtp`: the MTP's window; `lookup_k`: the lookup proposal's length (0 = none); `match`: its match length. - Pick choose(int t_mtp, int lookup_k, int match) const; + /// pad_cap > 0 (learning, a deterministic proposal): a window may be longer than the proposal, up to pad_cap drafts, its extra rows filler + /// that the verifier will most likely reject; only a learning stretch asks for one (see explore below). + Pick choose(int t_mtp, int lookup_k, int match, int pad_cap = 0) const; /// After the round: the window it used, the drafts accepted, and the round's time (verify + commit + draft). void observe(bool lookup, int t, int accepted, int match, double round_ms); + /// The round of a window run for its size's sake (Pick::pad > 0): its time goes to the learner and the cost of that size, its outcome to no statistic. + void observe_forced(int t, double round_ms); /// Gated proposals, inside one generation: a round's committed outcome only, never its time. `t` is the /// lookup window chosen before the draws, or the configured MTP window; both are fixed by the history. A /// lookup window counts as a probe of its size, so a size whose cost is still guessed is tried kProbes times, @@ -179,15 +184,15 @@ public: /// Learning a fixed table: every timed round also goes to learner() (raw round time, in its context bucket). Off by default. void set_learning(bool on) { learning_ = on; } bool learning() const { return learning_; } - /// Measuring the sizes the policy can pick (learning only): after kExploreAfter timed rounds, a lookup proposal long enough for a size - /// that has fewer than kExploreN samples in the current context bucket takes that size's window instead (the smallest such size), at - /// most kExplorePerSize times per size and kExploreBudget times in a stretch. The tokens of a learning stretch were never repeatable + /// Measuring the sizes the policy can pick (learning only): after kExploreAfter timed rounds, a lookup proposal (padded with filler rows up to + /// pad_cap when the caller allows it, else long enough as it is) takes the window of a size that has fewer than kExploreN samples in the + /// current context bucket instead (the smallest such size), at most kExplorePerSize times per size and kExploreBudget times in a stretch. The tokens of a learning stretch were never repeatable /// (the windows follow the live costs); a window's size can change the batch the verifier runs, and with it the tokens. A size that /// no proposal ever reaches keeps its filled-in cost (CostLearner::table). static constexpr int kExploreAfter = 300; static constexpr int kExploreN = 30; static constexpr int kExplorePerSize = 40; - static constexpr int kExploreBudget = 150; + static constexpr int kExploreBudget = 200; int forced_rounds() const { return forced_total_; } /// Start the learning stretch again: the learner's samples and the forced-round counts. void reset_learning(); @@ -244,7 +249,7 @@ private: bool fixed_ = false; // set_fixed_costs: no round time changes a cost bool learning_ = false; // set_learning: round times also go to learner_ double busy_age_ = -1.0; // set_busy_age - int explore_size(int lookup_k) const; // the size a learning stretch measures next, 0 = none (choose() counts it) + int explore_size(int lookup_k, int pad_cap) const; // the size a learning stretch measures next, 0 = none (choose() counts it) mutable int forced_total_ = 0; // windows taken for their size's sake, this stretch mutable std::array forced_{}; // ... by size CostLearner learner_; diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 7578c0b38b..33a2f7efb1 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -19485,6 +19485,7 @@ int main(int argc, char** argv) { policy.set_context(p); const bool merged = first_window && merge_T > 0; // the prompt's last tokens and the first token bool from_sfx = false; + int sfx_pad = 0; // a learning stretch's lookup window longer than the proposal: the filler rows (DraftPolicy::Pick::pad) int sfx_match = 0; bool dfr_lookup = false; int dfr_T = 0, dfr_match = 0; @@ -19540,9 +19541,17 @@ int main(int argc, char** argv) { // chooses, and the lookup is taken without asking whether the MTP's first guess agrees const bool probabilistic = use_mtp && !req_df2 && mtp.prob(); // DFlash2's candidates are not draws from the MTP's q if (k > 0 && (probabilistic || !draft_on || sbuf[0] == drafts[0])) { + // a learning stretch may run a longer window than the proposal to time that size: the extra rows repeat the proposal's last + // token, which the verifier rejects (a deterministic proposal only: a probabilistic window keeps its own draws) const strata::spec::DraftPolicy::Pick pk = probabilistic - ? source_policy.choose(S_mtp, k, sfx_match) : policy.choose(T, k, sfx_match); - if (pk.lookup && !trace_nopick) { T = pk.t; from_sfx = true; } + ? source_policy.choose(S_mtp, k, sfx_match) : policy.choose(T, k, sfx_match, dc_learning ? S_look - 1 : 0); + if (pk.lookup && !trace_nopick && pk.t - 1 <= (int) sbuf.size()) { + T = pk.t; from_sfx = true; + if (pk.pad > 0) { + sfx_pad = pk.pad; + for (int i = k; i < k + pk.pad; ++i) sbuf[(size_t) i] = sbuf[(size_t) k - 1]; + } + } if (pk.lookup && !trace_nopick && o.lookup_max_t > 0) { // --lookup-max-t: a size whose window graph fits const int t = graph_fits(T); if (t < 0) { std::printf("ERR %s\n", err.c_str()); return 1; } @@ -19987,7 +19996,9 @@ int main(int argc, char** argv) { } if (spec_cost && use_mtp && timed_round && !from_sfx) policy.observe_probs(dwin_s.data(), T_mtp - 1, a); if (dc_learning) policy.set_busy_age(std::chrono::duration(Clock::now() - dc_busy_since).count()); - if (timed_round && !eos && chain_n == 0) + if (timed_round && !eos && chain_n == 0 && sfx_pad > 0 && from_sfx) // a window run for its size's sake: its time only + policy.observe_forced(T, std::chrono::duration(Clock::now() - round0).count()); + else if (timed_round && !eos && chain_n == 0) policy.observe(from_sfx, T, a, sfx_match, std::chrono::duration(Clock::now() - round0).count()); else if (timed_round && !eos) // a chained window: its MTP part, its chain, its cost diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cpp index 99c10b4dd5..7c1b92f4a3 100644 --- a/src/spec/draft_policy.cpp +++ b/src/spec/draft_policy.cpp @@ -458,15 +458,16 @@ double DraftPolicy::mtp_tokens(int t) const { return 1.0 + 0.7 * (t - 1); // before any MTP window of this size: a typical acceptance } -DraftPolicy::Pick DraftPolicy::choose(int t_mtp, int lookup_k, int match) const { +DraftPolicy::Pick DraftPolicy::choose(int t_mtp, int lookup_k, int match, int pad_cap) const { Pick p; p.t = std::clamp(t_mtp, 1, max_t_); if (lookup_k <= 0) return p; if (learning_ && !fixed_) { // a learning stretch measures the sizes it has too few rounds of, when a proposal is long enough for one - const int s = explore_size(lookup_k); + const int s = explore_size(lookup_k, pad_cap); if (s > 0) { p.lookup = true; p.t = s; + p.pad = std::max(0, s - 1 - lookup_k); return p; } } @@ -498,9 +499,9 @@ DraftPolicy::Pick DraftPolicy::choose(int t_mtp, int lookup_k, int match) const return p; } -int DraftPolicy::explore_size(int lookup_k) const { +int DraftPolicy::explore_size(int lookup_k, int pad_cap) const { if (learner_.rounds() + learner_.late_rounds() < kExploreAfter || forced_total_ >= kExploreBudget) return 0; - for (int s = 2; s <= std::min(lookup_k + 1, max_t_); ++s) { + for (int s = 2; s <= std::min(std::max(lookup_k, pad_cap) + 1, max_t_); ++s) { if (!allowed(s) || forced_[(size_t) s] >= kExplorePerSize || learner_.samples(bk_, s) >= kExploreN) continue; ++forced_[(size_t) s]; ++forced_total_; @@ -509,6 +510,19 @@ int DraftPolicy::explore_size(int lookup_k) const { return 0; } +void DraftPolicy::observe_forced(int t, double round_ms) { + t = std::clamp(t, 1, kMaxT); + if (learning_ && round_ms > 0) learner_.add(bk_, t, round_ms, busy_age_); + if (round_ms > 0 && !fixed_) { + round_ms = clamp_sample(t, round_ms); + const double alpha = robust_ ? std::max(kCostAlpha, 1.0 / (cost_n_[bk_][t] + 1.0)) : kCostAlpha; + cost_[bk_][t] = cost_n_[bk_][t] > 0 ? (1.0 - alpha) * cost_[bk_][t] + alpha * round_ms : round_ms; + cost_n_[bk_][t] += 1.0; + last_[bk_][t] = rounds_; + } + rounds_ += 1.0; +} + void DraftPolicy::reset_learning() { learner_.reset(); forced_total_ = 0; diff --git a/src/spec/draft_policy_test.cpp b/src/spec/draft_policy_test.cpp index 624f3d89b8..f0a9e62dbb 100644 --- a/src/spec/draft_policy_test.cpp +++ b/src/spec/draft_policy_test.cpp @@ -458,6 +458,7 @@ int main() { check(f1.lookup && f1.t == 2 && y.forced_rounds() == 1, "explore: after the first rounds the smallest size with few samples is taken, if a proposal reaches it"); const DraftPolicy::Pick f2 = y.choose(1, 1, 3); check(y.forced_rounds() == 2 && f2.lookup && f2.t == 2, "explore: a proposal of one draft reaches 2 rows only"); + y.set_busy_age(100.0); // late rounds: no size gets a fresh sample, so the per-size cap (40) and then the budget end the forcing int per[DraftPolicy::kMaxT + 1] = {}; for (int i = 0; i < 700; ++i) { const DraftPolicy::Pick pk = y.choose(1, 7, 3); @@ -495,6 +496,21 @@ int main() { const std::vector wt = w.learner().table(0, 20, 6); check(w.learner().samples(0, 4) == 0 && wt.size() == 6 && wt[3] > wt[2] && wt[5] > wt[3], "explore: a size no proposal reaches is filled in above the measured ones"); (void) worst; + // a padded window: a short proposal, a size past it that has no samples, the caller allows filler rows + DraftPolicy g(8); + g.set_learning(true); + feed(g, 400); + for (int i = 0; i < 40; ++i) g.observe_forced(2, 95.0); // size 2 measured (3 is fed already) + const DraftPolicy::Pick np = g.choose(1, 1, 3); + check(g.forced_rounds() == 0 || !np.lookup || np.pad == 0, "pad: without pad_cap a proposal of one draft never makes a longer window"); + const DraftPolicy::Pick gp = g.choose(1, 1, 3, 7); + check(gp.lookup && gp.t == 4 && gp.pad == 2 && g.forced_rounds() == 1, "pad: with pad_cap the smallest unmeasured size is run, 2 filler rows past the proposal"); + const double q0 = g.lookup_rate(3); + for (int i = 0; i < 30; ++i) g.observe_forced(4, 143.0); + check(g.lookup_rate(3) == q0 && g.learner().samples(0, 4) == 30, "pad: a forced round feeds the learner and no lookup statistic"); + const DraftPolicy::Pick g5 = g.choose(1, 1, 3, 7); + check(g5.lookup && g5.t == 5 && g5.pad == 3, "pad: the next unmeasured size follows once one has its samples"); + check(g.choose(1, 1, 3, 1).pad == 0, "pad: a pad_cap of one draft makes no filler"); } std::printf(g_fail ? "FAIL\n" : "PASS\n"); return g_fail ? 1 : 0;