From 5f49ec459972c6337e89b47c17e500ac8d4901cf Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 21 Oct 2026 14:46:56 +0200 Subject: [PATCH 1/6] spec: STRATA_SPEC_COST keeps an MTP draft only while the next row pays at the measured row cost (step-by-step chain rule in MtpDrafter, DraftPolicy::keep_draft) --- include/strata/core/mtp.hpp | 6 ++++++ include/strata/spec/draft_policy.hpp | 20 +++++++++++++------- src/core/mtp.cpp | 5 +++-- src/program/generate.cpp | 28 ++++++++++++++++++++++------ src/spec/draft_policy.cpp | 27 ++++++++++----------------- 5 files changed, 54 insertions(+), 32 deletions(-) diff --git a/include/strata/core/mtp.hpp b/include/strata/core/mtp.hpp index 7216f015..d97d26bd 100644 --- a/include/strata/core/mtp.hpp +++ b/include/strata/core/mtp.hpp @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -105,6 +106,10 @@ public: float* probs = nullptr, float min_p = 0.0f, int* n_drafts = nullptr); /// The first round: one cell (`cell`) from `R_row` (device) and `token` -> T-1 drafts. + /// STRATA_SPEC_COST: called after each draft step with (index, probability) instead of the `min_p` test; false ends the chain. + /// Also called once for the last step of a chain that reached its length (so the rule sees every draft). Empty: min_p decides. + void set_chain_rule(std::function f) { chain_rule_ = std::move(f); } + bool draft_first(int T, const float* R_row, int32_t token, int64_t cell, int32_t* drafts, std::string& err, float* probs = nullptr, float min_p = 0.0f, int* n_drafts = nullptr); @@ -117,6 +122,7 @@ public: /// window's commit (`tail`) and the window's pick at its last accepted row (`next`, the next window's row 0). void set_draft_history(const int32_t* tail, int64_t n_tail, int32_t next); void set_ple_session(SessionState* s) { ple_ss_ = s; } + std::function chain_rule_; bool coupled() const { return coupled_active_; } /// PROBABILISTIC DRAFT ACCEPTANCE (core/spec_prob.hpp, STRATA_SPEC_PROB=1): this request's drafts are SAMPLED from /// q and each draft j's distribution is in spec_q() + j * kSpecQStride (ids, -1 terminated; then float bits), diff --git a/include/strata/spec/draft_policy.hpp b/include/strata/spec/draft_policy.hpp index 245fa4b1..d91543d8 100644 --- a/include/strata/spec/draft_policy.hpp +++ b/include/strata/spec/draft_policy.hpp @@ -46,13 +46,19 @@ public: double lookup_rate(int match) const; // current q for a match length double cost_ms(int t) const; // measured or scaled round time of a window of t tokens - /// COST-AWARE MTP WINDOW (STRATA_SPEC_COST=1 / --spec-cost): the window of 1..cap rows with the best expected committed - /// tokens per ms, from the MTP chain's draft probabilities p[0..n) (n = drafts computed; the chain is drafted in full): - /// E(T) = 1 + q_1 + .. + q_{T-1}, q_j = prod_{i<=j} a(p_i), a() = the acceptance measured for drafts of that probability - /// cost(T) = the measured round time of that window size (cost_ms, kept non-decreasing) - /// A wider window is taken only if its E/cost beats the narrower best by `margin`. Only chooses which rows are verified. - int best_rows(const float* p, int n, int cap) const; - /// After a round of the MTP window T whose drafts were p[0..T-1): `accepted` of them held. Teaches a(). + /// COST-AWARE MTP WINDOW (STRATA_SPEC_COST=1): the MTP chain is drafted one step at a time and keeps going only while the + /// next row is expected to pay: keep draft j (a window of j + 2 rows instead of j + 1) when + /// (E + q_j) / cost(j + 2) >= (1 + margin) * E / cost(j + 1) + /// with E = 1 + q_0 + .. + q_{j-1} the expected tokens of the window so far, q_j = prod_{i<=j} a(p_i), a() the acceptance measured for + /// MTP drafts of that probability (draft_rate), cost() the measured round time of that window size (cost_ms: it includes the draft). + /// It only chooses which rows are verified; the output is unchanged. + struct ChainState { + double q = 1.0, e = 1.0; + int kept = 0; ///< drafts kept so far = the window's rows - 1 + }; + /// Draft `idx` (0-based, probability p) was just computed: does it join the window? idx == 0 starts a new chain. + bool keep_draft(ChainState& st, int idx, float p, int cap) const; + /// After a round of the MTP window T whose drafts were p[0..T-1): `accepted` of them held. Teaches draft_rate(). void observe_probs(const float* p, int n_window_drafts, int accepted); double draft_rate(double p) const; // acceptance of an MTP draft that came with probability p diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp index 58627029..f4b82b33 100644 --- a/src/core/mtp.cpp +++ b/src/core/mtp.cpp @@ -1620,7 +1620,7 @@ bool MtpDrafter::draft(int T, const int32_t* tokens, int64_t p, int a, int32_t* }; int n = 0; - if (min_p <= 0.0f && max_steps > 0) { + if (min_p <= 0.0f && !chain_rule_ && max_steps > 0) { if (cudaGraphLaunch(cp ? round_exec_c_[T] : round_exec_[T], cs_) != cudaSuccess) { err = std::string("mtp draft: ") + cudaGetErrorString(cudaGetLastError()); return false; @@ -1686,7 +1686,7 @@ bool MtpDrafter::draft(int T, const int32_t* tokens, int64_t p, int a, int32_t* float pj = ((volatile float*) h_prob_)[0]; if (probs) probs[0] = pj; n = 1; - for (int j = 1; j < max_steps && pj >= min_p; ++j) { + for (int j = 1; j < max_steps && (chain_rule_ ? chain_rule_(j - 1, pj) : pj >= min_p); ++j) { if (dt) cudaEventRecord(g_dt.eb[j], cs_); tl = Clock::now(); if (cudaGraphLaunch(cp ? step_exec_c_[j] : step_exec_[j], cs_) != cudaSuccess) { @@ -1705,6 +1705,7 @@ bool MtpDrafter::draft(int T, const int32_t* tokens, int64_t p, int a, int32_t* if (probs) probs[j] = pj; ++n; } + if (chain_rule_ && n >= max_steps) (void) chain_rule_(n - 1, pj); // the last step of a full chain prefetch_ple(drafts[n - 1]); const Clock::time_point t_pre_sync = Clock::now(); if (cudaStreamSynchronize(cs_) != cudaSuccess) { diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 19bfa2c5..d62fd406 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -11032,6 +11032,14 @@ int main(int argc, char** argv) { strata::spec::SuffixDrafter sfx(std::max(1, o.suffix_draft), 64, (size_t) o.max_context + 4096); strata::spec::DraftPolicy policy(S); // MTP or lookup window, learned over the whole process const bool spec_cost = [] { const char* v = std::getenv("STRATA_SPEC_COST"); return v != nullptr && v[0] == '1'; }(); // cost-aware MTP window (A/B arm) + strata::spec::DraftPolicy::ChainState chain_st; + int chain_rows = 1; // the window the cost-aware rule kept in the MTP chain just drafted + if (spec_cost && use_mtp) + mtp.set_chain_rule([&](int idx, float pj) { + const bool k = policy.keep_draft(chain_st, idx, pj, S_mtp); + chain_rows = 1 + chain_st.kept; + return k; + }); // The vision path (--vision): GENI carries images. The file is one // or more strata-vision records (int32 'SVE1', n, nx, ny, n_embd, then n x n_embd floats) in prompt order; // each image's rows go to its run of <|image_pad|> tokens, whose M-RoPE positions are mtmd's: t = p, @@ -14726,7 +14734,7 @@ int main(int argc, char** argv) { while (!pl_ran && !cancelled && produced_n < max_new) { int T = use_mtp ? S_mtp : 1; // no --mtp: one token a round unless a lookup draft fires if (use_mtp && spec_cost && !first_window) { - T = policy.best_rows(dprob.data(), S_mtp - 1, S_mtp); + T = std::clamp(chain_rows, 1, S_mtp); } else if (use_mtp && req_spec_min_p > 0.0) { T = 1; while (T < S_mtp && dprob[(size_t) T - 1] >= (float) req_spec_min_p) ++T; @@ -15011,7 +15019,7 @@ int main(int argc, char** argv) { if (use_mtp && hist_n > 0 && mtp.coupled() && !eos && produced_n < max_new) mtp.set_draft_history(consumed.data(), (int64_t) consumed.size(), outv[(size_t) a]); const bool drafted = !use_mtp || eos || produced_n >= max_new || - mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), spec_cost ? 0.0f : (float) req_spec_min_p); + mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), (float) req_spec_min_p); { const Clock::time_point tw3 = Clock::now(); auto msd = [](Clock::time_point a0, Clock::time_point b0) { return std::chrono::duration(b0 - a0).count(); }; @@ -16208,7 +16216,7 @@ int main(int argc, char** argv) { // STRATA_SPEC_TRACE= (bench only, off by default): one line per round, "T a sfx ms p1 p2 ..." (window rows, drafts accepted, // lookup window, round ms, the MTP draft probabilities), for offline window-length policy studies // STRATA_SPEC_COST=1 (A/B arm, bench): the MTP window by expected tokens per ms at this machine's measured row costs - // (DraftPolicy::best_rows) instead of "draft probability >= --spec-min-p"; the chain is drafted in full + // (DraftPolicy::keep_draft, step by step) instead of "draft probability >= --spec-min-p" const bool spec_cost = [] { const char* v = std::getenv("STRATA_SPEC_COST"); return v != nullptr && v[0] == '1'; }(); std::vector dwin((size_t) o.spec, 0.0f); // the draft probabilities this round's window was chosen from std::FILE* spec_trace = nullptr; @@ -16219,7 +16227,7 @@ int main(int argc, char** argv) { // generated token and the MTP's first cell); otherwise the token loop already did that. bool first_window = native_pack; if (use_mtp && !first_window && - !mtp.draft_first(o.spec, ss.R, x, p - 1, drafts.data(), err, dprob.data(), spec_cost ? 0.0f : (float) o.spec_min_p)) { + !mtp.draft_first(o.spec, ss.R, x, p - 1, drafts.data(), err, dprob.data(), (float) o.spec_min_p)) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } @@ -16237,6 +16245,14 @@ int main(int argc, char** argv) { if (use_mtp && S_mtp < o.spec) mtp.set_max_drafts(S_mtp - 1); strata::spec::SuffixDrafter sfx(std::max(1, o.suffix_draft), 64, (size_t) o.max_context + 4096); strata::spec::DraftPolicy policy(o.spec); // MTP or lookup window (see draft_policy.hpp) + strata::spec::DraftPolicy::ChainState chain_st; + int chain_rows = 1; // STRATA_SPEC_COST: the window the cost-aware rule kept in the MTP chain just drafted + if (spec_cost && use_mtp) + mtp.set_chain_rule([&](int idx, float pj) { + const bool k = policy.keep_draft(chain_st, idx, pj, S_mtp); + chain_rows = 1 + chain_st.kept; + return k; + }); std::vector sbuf((size_t) o.spec, 0); int64_t sfx_windows = 0, sfx_drafts = 0, sfx_ok = 0; int64_t chain_windows = 0, chain_drafts = 0, chain_ok = 0; // --lookup-chain's own counts @@ -16282,7 +16298,7 @@ int main(int argc, char** argv) { strata::core::progress_at("spec loop: choosing the window", (int64_t) produced.size()); int T = S_mtp; if (use_mtp && spec_cost && !first_window) { - T = policy.best_rows(dprob.data(), S_mtp - 1, S_mtp); + T = std::clamp(chain_rows, 1, S_mtp); } else if (use_mtp && o.spec_min_p > 0.0) { T = 1; while (T < S_mtp && dprob[(size_t) T - 1] >= (float) o.spec_min_p) ++T; @@ -16421,7 +16437,7 @@ int main(int argc, char** argv) { break; } const bool drafted = !use_mtp || (int64_t) produced.size() >= max_new || - mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), spec_cost ? 0.0f : (float) o.spec_min_p); + mtp.draft(T, outv.data(), p, a, drafts.data(), err, dprob.data(), (float) o.spec_min_p); if (adapt_thr.joinable()) adapt_thr.join(); if (!adapt_ok) return 1; if (!drafted) { diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cpp index 8ec78ea5..3359b5fa 100644 --- a/src/spec/draft_policy.cpp +++ b/src/spec/draft_policy.cpp @@ -132,24 +132,17 @@ void DraftPolicy::observe_probs(const float* p, int n, int accepted) { } } -int DraftPolicy::best_rows(const float* p, int n, int cap) const { - cap = std::clamp(cap, 1, max_t_); - const int m = std::min(n, cap - 1); - double q = 1.0, e = 1.0, floor_cost = cost_ms(1); - double best_v = e / floor_cost; - int best_t = 1; - for (int j = 0; j < m; ++j) { - q *= draft_rate(p[j]); - e += q; - const int t = j + 2; - floor_cost = std::max(floor_cost, cost_ms(t)); - const double v = e / floor_cost; - if (v > best_v * (1.0 + margin_)) { - best_v = v; - best_t = t; - } +bool DraftPolicy::keep_draft(ChainState& st, int idx, float p, int cap) const { + if (idx == 0) st = ChainState{}; + st.q *= draft_rate(p); + if (idx + 2 > std::clamp(cap, 1, max_t_)) return false; // no row left for it + const double c0 = cost_ms(idx + 1), c1 = std::max(c0, cost_ms(idx + 2)); // a wider window never costs less + const bool keep = (st.e + st.q) / c1 >= (1.0 + margin_ / 3.0) * st.e / c0; + if (keep) { + st.e += st.q; + st.kept = idx + 1; } - return best_t; + return keep; } double DraftPolicy::chain_rate(int match) const { From d232721db616d7d621dc0cae1af3b4c264946052 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 21 Oct 2026 15:03:51 +0200 Subject: [PATCH 2/6] spec: cost-aware chain rule keeps the best width seen and looks one or two drafts ahead (the row cost is not convex); unmeasured widths are probed a few times --- include/strata/spec/draft_policy.hpp | 17 +++++++++------ src/program/generate.cpp | 4 ++-- src/spec/draft_policy.cpp | 32 ++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/include/strata/spec/draft_policy.hpp b/include/strata/spec/draft_policy.hpp index d91543d8..ed83abf0 100644 --- a/include/strata/spec/draft_policy.hpp +++ b/include/strata/spec/draft_policy.hpp @@ -46,17 +46,20 @@ public: double lookup_rate(int match) const; // current q for a match length double cost_ms(int t) const; // measured or scaled round time of a window of t tokens - /// COST-AWARE MTP WINDOW (STRATA_SPEC_COST=1): the MTP chain is drafted one step at a time and keeps going only while the - /// next row is expected to pay: keep draft j (a window of j + 2 rows instead of j + 1) when - /// (E + q_j) / cost(j + 2) >= (1 + margin) * E / cost(j + 1) - /// with E = 1 + q_0 + .. + q_{j-1} the expected tokens of the window so far, q_j = prod_{i<=j} a(p_i), a() the acceptance measured for - /// MTP drafts of that probability (draft_rate), cost() the measured round time of that window size (cost_ms: it includes the draft). + /// COST-AWARE MTP WINDOW (STRATA_SPEC_COST=1): the MTP chain is drafted one step at a time; after draft j (probability p_j) + /// E_j = 1 + q_0 + .. + q_j, q_i = prod_{k<=i} a(p_k) a() = the acceptance measured for MTP drafts of that probability + /// is the expected tokens of the window of j + 2 rows, worth E_j / cost(j + 2) tokens per ms (cost() = the measured round time of + /// that window size, which includes the draft). The window is the best of the widths seen; the chain goes on only while one or two + /// more drafts could still beat it (the cost is not convex: on a card with experts in RAM a window of 3 costs about a window of 2). /// It only chooses which rows are verified; the output is unchanged. struct ChainState { double q = 1.0, e = 1.0; - int kept = 0; ///< drafts kept so far = the window's rows - 1 + double best_v = 0.0; + int best_rows = 1; ///< the window to verify: 1 + the drafts kept + int kept = 0; ///< best_rows - 1 }; - /// Draft `idx` (0-based, probability p) was just computed: does it join the window? idx == 0 starts a new chain. + /// Draft `idx` (0-based, probability p) was just computed: update `st` and say whether to compute draft idx + 1. + /// idx == 0 starts a new chain. `cap` = the widest window (rows). bool keep_draft(ChainState& st, int idx, float p, int cap) const; /// After a round of the MTP window T whose drafts were p[0..T-1): `accepted` of them held. Teaches draft_rate(). void observe_probs(const float* p, int n_window_drafts, int accepted); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index d62fd406..653b9aad 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -11037,7 +11037,7 @@ int main(int argc, char** argv) { if (spec_cost && use_mtp) mtp.set_chain_rule([&](int idx, float pj) { const bool k = policy.keep_draft(chain_st, idx, pj, S_mtp); - chain_rows = 1 + chain_st.kept; + chain_rows = chain_st.best_rows; return k; }); // The vision path (--vision): GENI carries images. The file is one @@ -16250,7 +16250,7 @@ int main(int argc, char** argv) { if (spec_cost && use_mtp) mtp.set_chain_rule([&](int idx, float pj) { const bool k = policy.keep_draft(chain_st, idx, pj, S_mtp); - chain_rows = 1 + chain_st.kept; + chain_rows = chain_st.best_rows; return k; }); std::vector sbuf((size_t) o.spec, 0); diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cpp index 3359b5fa..124e3fca 100644 --- a/src/spec/draft_policy.cpp +++ b/src/spec/draft_policy.cpp @@ -133,16 +133,30 @@ void DraftPolicy::observe_probs(const float* p, int n, int accepted) { } bool DraftPolicy::keep_draft(ChainState& st, int idx, float p, int cap) const { - if (idx == 0) st = ChainState{}; - st.q *= draft_rate(p); - if (idx + 2 > std::clamp(cap, 1, max_t_)) return false; // no row left for it - const double c0 = cost_ms(idx + 1), c1 = std::max(c0, cost_ms(idx + 2)); // a wider window never costs less - const bool keep = (st.e + st.q) / c1 >= (1.0 + margin_ / 3.0) * st.e / c0; - if (keep) { - st.e += st.q; - st.kept = idx + 1; + cap = std::clamp(cap, 1, max_t_); + if (idx == 0) { + st = ChainState{}; + st.best_v = 1.0 / cost_ms(1); } - return keep; + const double a = draft_rate(p); + st.q *= a; + st.e += st.q; + const int rows = idx + 2; + if (rows <= cap) { + const double v = st.e / cost_ms(rows); + if (v > st.best_v * (1.0 + margin_ / 3.0)) { + st.best_v = v; + st.best_rows = rows; + } + // a width not measured yet is tried a few times when the chain is likely to hold that far + if (cost_n_[rows] < kProbes && st.q >= 0.15) st.best_rows = rows; + } + st.kept = st.best_rows - 1; + if (rows + 1 > cap) return false; // no row left for another draft + // one or two more drafts, each as likely to hold as this one did, could they beat the best width so far? + const double v1 = (st.e + st.q * a) / cost_ms(rows + 1); + const double v2 = rows + 2 <= cap ? (st.e + st.q * a + st.q * a * a) / cost_ms(rows + 2) : 0.0; + return std::max(v1, v2) > st.best_v * (1.0 + margin_ / 3.0) || (cost_n_[rows + 1] < kProbes && st.q * a >= 0.15); } double DraftPolicy::chain_rate(int match) const { From d45cd9d3cf7aa35ef4e5076aec1e545e63fc9167 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 21 Oct 2026 15:35:07 +0200 Subject: [PATCH 3/6] spec: STRATA_SPEC_COST_TABLE seeds the row costs (a per-card table), the CLI prints the learned table at the end --- include/strata/spec/draft_policy.hpp | 6 ++++++ src/program/generate.cpp | 11 +++++++++++ src/spec/draft_policy.cpp | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/include/strata/spec/draft_policy.hpp b/include/strata/spec/draft_policy.hpp index ed83abf0..2408de59 100644 --- a/include/strata/spec/draft_policy.hpp +++ b/include/strata/spec/draft_policy.hpp @@ -16,6 +16,7 @@ #pragma once #include +#include namespace strata::spec { @@ -45,6 +46,11 @@ public: double chain_rate(int match) const; double lookup_rate(int match) const; // current q for a match length double cost_ms(int t) const; // measured or scaled round time of a window of t tokens + /// A per-card row-cost table (round ms of the windows of 1..n rows, the draft included) counted as `weight` earlier rounds each: + /// the start of a process that already knows the card (STRATA_SPEC_COST_TABLE; tuning time), replaced by what it then measures. + void seed_costs(const double* ms, int n, double weight); + /// The measured round cost of every window size seen: "T1:39.8(n=120) T2:..." for the log / the next process's table. + std::string cost_table_text() const; /// COST-AWARE MTP WINDOW (STRATA_SPEC_COST=1): the MTP chain is drafted one step at a time; after draft j (probability p_j) /// E_j = 1 + q_0 + .. + q_j, q_i = prod_{k<=i} a(p_k) a() = the acceptance measured for MTP drafts of that probability diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 653b9aad..3f2cda8f 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -16247,6 +16247,16 @@ int main(int argc, char** argv) { strata::spec::DraftPolicy policy(o.spec); // MTP or lookup window (see draft_policy.hpp) strata::spec::DraftPolicy::ChainState chain_st; int chain_rows = 1; // STRATA_SPEC_COST: the window the cost-aware rule kept in the MTP chain just drafted + if (const char* tab = std::getenv("STRATA_SPEC_COST_TABLE"); spec_cost && tab != nullptr && tab[0] != '\0') { + double ms[strata::spec::DraftPolicy::kMaxT] = {}; + int n = 0; + for (const char* q = tab; *q != '\0' && n < strata::spec::DraftPolicy::kMaxT;) { + ms[n++] = std::atof(q); + while (*q != '\0' && *q != ',') ++q; + if (*q == ',') ++q; + } + policy.seed_costs(ms, n, 6.0); + } if (spec_cost && use_mtp) mtp.set_chain_rule([&](int idx, float pj) { const bool k = policy.keep_draft(chain_st, idx, pj, S_mtp); @@ -16489,6 +16499,7 @@ int main(int argc, char** argv) { if (o.lookup_chain > 0) std::printf("%-24s %lld windows, chained drafts accepted %lld of %lld\n", "lookup chain", (long long) chain_windows, (long long) chain_ok, (long long) chain_drafts); + if (spec_cost) std::printf("%-24s %s\n", "spec cost table", policy.cost_table_text().c_str()); if (!t_cycle.empty()) { std::printf("%-24s", "window cost (ms/round)"); for (size_t i = 1; i < t_cycle_ms.size(); ++i) diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cpp index 124e3fca..16d997b1 100644 --- a/src/spec/draft_policy.cpp +++ b/src/spec/draft_policy.cpp @@ -2,6 +2,7 @@ #include "strata/spec/draft_policy.hpp" #include +#include namespace strata::spec { namespace { @@ -51,6 +52,25 @@ double DraftPolicy::cost_ms(int t) const { return den > 0 ? num / den : kShape[t]; } +void DraftPolicy::seed_costs(const double* ms, int n, double weight) { + for (int t = 1; t <= std::min(n, kMaxT); ++t) + if (ms[t - 1] > 0.0 && cost_n_[t] == 0) { + cost_[t] = ms[t - 1]; + cost_n_[t] = weight; + } +} + +std::string DraftPolicy::cost_table_text() const { + std::string out; + char b[64]; + for (int t = 1; t <= kMaxT; ++t) + if (cost_n_[t] > 0) { + std::snprintf(b, sizeof b, "%sT%d:%.2f(n=%.0f)", out.empty() ? "" : " ", t, cost_[t], cost_n_[t]); + out += b; + } + return out; +} + bool DraftPolicy::stale(int t) const { return cost_n_[t] > 0 && rounds_ - last_[t] > kStaleRounds; } double DraftPolicy::mtp_tokens(int t) const { From e0d79b02f9eb477676f845f3f43b729d39e8f6b9 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 21 Oct 2026 16:04:10 +0200 Subject: [PATCH 4/6] spec: with STRATA_SPEC_COST a round far above the known cost of its width (the first use captures a graph) counts as a tenth of a sample --- include/strata/spec/draft_policy.hpp | 5 +++++ src/program/generate.cpp | 2 ++ src/spec/draft_policy.cpp | 9 +++++++++ 3 files changed, 16 insertions(+) diff --git a/include/strata/spec/draft_policy.hpp b/include/strata/spec/draft_policy.hpp index 2408de59..0254fb71 100644 --- a/include/strata/spec/draft_policy.hpp +++ b/include/strata/spec/draft_policy.hpp @@ -51,6 +51,9 @@ public: void seed_costs(const double* ms, int n, double weight); /// The measured round cost of every window size seen: "T1:39.8(n=120) T2:..." for the log / the next process's table. std::string cost_table_text() const; + /// STRATA_SPEC_COST: a round time far above what is known for that width (the first use of a width captures its graph; a stalled + /// round) counts as a tenth of a sample, so one capture cannot price a width out of use. Off: the cost EMA as it always was. + void set_robust(bool on) { robust_ = on; } /// COST-AWARE MTP WINDOW (STRATA_SPEC_COST=1): the MTP chain is drafted one step at a time; after draft j (probability p_j) /// E_j = 1 + q_0 + .. + q_j, q_i = prod_{k<=i} a(p_k) a() = the acceptance measured for MTP drafts of that probability @@ -83,6 +86,8 @@ private: std::array ok_{}, bad_{}; // lookup drafts accepted / windows cut short, decayed std::array cok_{}, cbad_{}; // chained lookup drafts, the same (reached rounds only) double rounds_ = 0; // rounds observed so far + bool robust_ = false; + double clamp_sample(int t, double ms) const; static constexpr int kPBuckets = 8; std::array pa_ok_{}, pa_n_{}; // MTP drafts reached / accepted by probability bucket (decayed) std::array last_{}; // the round at which each size was last measured diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 3f2cda8f..fc83195b 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -11034,6 +11034,7 @@ int main(int argc, char** argv) { const bool spec_cost = [] { const char* v = std::getenv("STRATA_SPEC_COST"); return v != nullptr && v[0] == '1'; }(); // cost-aware MTP window (A/B arm) strata::spec::DraftPolicy::ChainState chain_st; int chain_rows = 1; // the window the cost-aware rule kept in the MTP chain just drafted + policy.set_robust(spec_cost); if (spec_cost && use_mtp) mtp.set_chain_rule([&](int idx, float pj) { const bool k = policy.keep_draft(chain_st, idx, pj, S_mtp); @@ -16257,6 +16258,7 @@ int main(int argc, char** argv) { } policy.seed_costs(ms, n, 6.0); } + policy.set_robust(spec_cost); if (spec_cost && use_mtp) mtp.set_chain_rule([&](int idx, float pj) { const bool k = policy.keep_draft(chain_st, idx, pj, S_mtp); diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cpp index 16d997b1..1f845edf 100644 --- a/src/spec/draft_policy.cpp +++ b/src/spec/draft_policy.cpp @@ -52,6 +52,13 @@ double DraftPolicy::cost_ms(int t) const { return den > 0 ? num / den : kShape[t]; } +double DraftPolicy::clamp_sample(int t, double ms) const { + if (!robust_) return ms; + const double known = cost_ms(t); // the measured value, or the scaled guess from the other widths + if (cost_n_[t] > 0 || known > 0.0) return std::min(ms, 2.0 * known); + return ms; +} + void DraftPolicy::seed_costs(const double* ms, int n, double weight) { for (int t = 1; t <= std::min(n, kMaxT); ++t) if (ms[t - 1] > 0.0 && cost_n_[t] == 0) { @@ -112,6 +119,7 @@ DraftPolicy::Pick DraftPolicy::choose(int t_mtp, int lookup_k, int match) const void DraftPolicy::observe(bool lookup, int t, int accepted, int match, double round_ms) { t = std::clamp(t, 1, kMaxT); if (round_ms > 0) { + round_ms = clamp_sample(t, round_ms); cost_[t] = cost_n_[t] > 0 ? (1.0 - kCostAlpha) * cost_[t] + kCostAlpha * round_ms : round_ms; cost_n_[t] += 1.0; last_[t] = rounds_; @@ -214,6 +222,7 @@ int DraftPolicy::chain(int t_mtp, double p_mtp, int k_avail, int match) const { void DraftPolicy::observe_chain(int t_mtp, int k, int accepted, int match, double round_ms) { const int t = std::clamp(t_mtp + k, 1, kMaxT); if (round_ms > 0) { + round_ms = clamp_sample(t, round_ms); cost_[t] = cost_n_[t] > 0 ? (1.0 - kCostAlpha) * cost_[t] + kCostAlpha * round_ms : round_ms; cost_n_[t] += 1.0; last_[t] = rounds_; From 48ec4a59acc9f5693fd24796f49fd586819f58bf Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 21 Oct 2026 16:10:56 +0200 Subject: [PATCH 5/6] spec: cost-aware window: the first round of a width is replaced by the guess from the other widths, a running mean for the first samples --- src/spec/draft_policy.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cpp index 1f845edf..4651e9c2 100644 --- a/src/spec/draft_policy.cpp +++ b/src/spec/draft_policy.cpp @@ -55,8 +55,13 @@ double DraftPolicy::cost_ms(int t) const { double DraftPolicy::clamp_sample(int t, double ms) const { if (!robust_) return ms; const double known = cost_ms(t); // the measured value, or the scaled guess from the other widths - if (cost_n_[t] > 0 || known > 0.0) return std::min(ms, 2.0 * known); - return ms; + if (cost_n_[t] == 0) { + // the first use of a width captures its graph: that round says nothing about its cost, the guess from the other widths stands in + for (int u = 1; u <= kMaxT; ++u) + if (cost_n_[u] > 0) return known; + return ms; + } + return std::min(ms, 2.0 * known); } void DraftPolicy::seed_costs(const double* ms, int n, double weight) { @@ -120,7 +125,8 @@ void DraftPolicy::observe(bool lookup, int t, int accepted, int match, double ro t = std::clamp(t, 1, kMaxT); if (round_ms > 0) { round_ms = clamp_sample(t, round_ms); - cost_[t] = cost_n_[t] > 0 ? (1.0 - kCostAlpha) * cost_[t] + kCostAlpha * round_ms : round_ms; + const double alpha = robust_ ? std::max(kCostAlpha, 1.0 / (cost_n_[t] + 1.0)) : kCostAlpha; // a running mean for the first samples + cost_[t] = cost_n_[t] > 0 ? (1.0 - alpha) * cost_[t] + alpha * round_ms : round_ms; cost_n_[t] += 1.0; last_[t] = rounds_; } @@ -223,7 +229,8 @@ void DraftPolicy::observe_chain(int t_mtp, int k, int accepted, int match, doubl const int t = std::clamp(t_mtp + k, 1, kMaxT); if (round_ms > 0) { round_ms = clamp_sample(t, round_ms); - cost_[t] = cost_n_[t] > 0 ? (1.0 - kCostAlpha) * cost_[t] + kCostAlpha * round_ms : round_ms; + const double alpha = robust_ ? std::max(kCostAlpha, 1.0 / (cost_n_[t] + 1.0)) : kCostAlpha; // a running mean for the first samples + cost_[t] = cost_n_[t] > 0 ? (1.0 - alpha) * cost_[t] + alpha * round_ms : round_ms; cost_n_[t] += 1.0; last_[t] = rounds_; } From b1ebc3d7ca4084d8b2e4750dc118e898d0afb9da Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 22 Oct 2026 07:24:46 +0200 Subject: [PATCH 6/6] spec: the cost-aware window keeps one row-cost table per context length (< 6K, < 24K, longer), seedable per bucket --- include/strata/spec/draft_policy.hpp | 11 +++- src/program/generate.cpp | 19 ++++-- src/spec/draft_policy.cpp | 86 +++++++++++++++++++--------- 3 files changed, 79 insertions(+), 37 deletions(-) diff --git a/include/strata/spec/draft_policy.hpp b/include/strata/spec/draft_policy.hpp index 0254fb71..88970c8b 100644 --- a/include/strata/spec/draft_policy.hpp +++ b/include/strata/spec/draft_policy.hpp @@ -24,6 +24,7 @@ class DraftPolicy { public: static constexpr int kMaxT = 8; static constexpr int kBuckets = 4; + static constexpr int kCtxBuckets = 3; // STRATA_SPEC_COST: row costs per context length (< 6K, < 24K, longer) explicit DraftPolicy(int max_t, double margin = 0.03); @@ -48,7 +49,10 @@ public: double cost_ms(int t) const; // measured or scaled round time of a window of t tokens /// A per-card row-cost table (round ms of the windows of 1..n rows, the draft included) counted as `weight` earlier rounds each: /// the start of a process that already knows the card (STRATA_SPEC_COST_TABLE; tuning time), replaced by what it then measures. - void seed_costs(const double* ms, int n, double weight); + void seed_costs(const double* ms, int n, double weight, int bucket = 0); + /// STRATA_SPEC_COST: the context position of the next round; selects the row-cost table of its length (a row of attention costs + /// more at 16K than at 1K). A bucket with no measurement yet starts from the nearest one that has. A no-op without set_robust(true). + void set_context(long long pos); /// The measured round cost of every window size seen: "T1:39.8(n=120) T2:..." for the log / the next process's table. std::string cost_table_text() const; /// STRATA_SPEC_COST: a round time far above what is known for that width (the first use of a width captures its graph; a stalled @@ -81,7 +85,8 @@ private: int max_t_; double margin_; - std::array cost_{}, cost_n_{}; // round ms by window size + std::array, kCtxBuckets> cost_{}, cost_n_{}; // round ms by context bucket and window size + int bk_ = 0; // the current context bucket std::array mtp_tok_{}, mtp_n_{}; // tokens committed by MTP windows of that size std::array ok_{}, bad_{}; // lookup drafts accepted / windows cut short, decayed std::array cok_{}, cbad_{}; // chained lookup drafts, the same (reached rounds only) @@ -90,7 +95,7 @@ private: double clamp_sample(int t, double ms) const; static constexpr int kPBuckets = 8; std::array pa_ok_{}, pa_n_{}; // MTP drafts reached / accepted by probability bucket (decayed) - std::array last_{}; // the round at which each size was last measured + std::array, kCtxBuckets> last_{}; // the round at which each size was last measured }; } // namespace strata::spec diff --git a/src/program/generate.cpp b/src/program/generate.cpp index fc83195b..c099b1bd 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -14734,6 +14734,7 @@ int main(int argc, char** argv) { } while (!pl_ran && !cancelled && produced_n < max_new) { int T = use_mtp ? S_mtp : 1; // no --mtp: one token a round unless a lookup draft fires + policy.set_context(p); if (use_mtp && spec_cost && !first_window) { T = std::clamp(chain_rows, 1, S_mtp); } else if (use_mtp && req_spec_min_p > 0.0) { @@ -16249,14 +16250,19 @@ int main(int argc, char** argv) { strata::spec::DraftPolicy::ChainState chain_st; int chain_rows = 1; // STRATA_SPEC_COST: the window the cost-aware rule kept in the MTP chain just drafted if (const char* tab = std::getenv("STRATA_SPEC_COST_TABLE"); spec_cost && tab != nullptr && tab[0] != '\0') { + // "T1,T2,..;T1,T2,..;T1,..": the row costs of the 3 context buckets (< 6K, < 24K, longer); one list seeds the first double ms[strata::spec::DraftPolicy::kMaxT] = {}; - int n = 0; - for (const char* q = tab; *q != '\0' && n < strata::spec::DraftPolicy::kMaxT;) { - ms[n++] = std::atof(q); - while (*q != '\0' && *q != ',') ++q; - if (*q == ',') ++q; + int n = 0, bucket = 0; + for (const char* q = tab; *q != '\0' && bucket < strata::spec::DraftPolicy::kCtxBuckets;) { + if (n < strata::spec::DraftPolicy::kMaxT) ms[n++] = std::atof(q); + while (*q != '\0' && *q != ',' && *q != ';') ++q; + const bool end_list = *q == ';' || *q == '\0'; + if (*q != '\0') ++q; + if (end_list) { + policy.seed_costs(ms, n, 6.0, bucket++); + n = 0; + } } - policy.seed_costs(ms, n, 6.0); } policy.set_robust(spec_cost); if (spec_cost && use_mtp) @@ -16309,6 +16315,7 @@ int main(int argc, char** argv) { const Clock::time_point t0 = Clock::now(); strata::core::progress_at("spec loop: choosing the window", (int64_t) produced.size()); int T = S_mtp; + policy.set_context(p); if (use_mtp && spec_cost && !first_window) { T = std::clamp(chain_rows, 1, S_mtp); } else if (use_mtp && o.spec_min_p > 0.0) { diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cpp index 4651e9c2..ffd9bbbf 100644 --- a/src/spec/draft_policy.cpp +++ b/src/spec/draft_policy.cpp @@ -40,13 +40,13 @@ double DraftPolicy::lookup_rate(int match) const { double DraftPolicy::cost_ms(int t) const { t = std::clamp(t, 1, kMaxT); - if (cost_n_[t] > 0) return cost_[t]; + if (cost_n_[bk_][t] > 0) return cost_[bk_][t]; // scale from the measured sizes, weighting each by how often it was seen double num = 0.0, den = 0.0; for (int u = 1; u <= kMaxT; ++u) - if (cost_n_[u] > 0) { - const double w = std::min(cost_n_[u], 20.0); - num += w * cost_[u] * kShape[t] / kShape[u]; + if (cost_n_[bk_][u] > 0) { + const double w = std::min(cost_n_[bk_][u], 20.0); + num += w * cost_[bk_][u] * kShape[t] / kShape[u]; den += w; } return den > 0 ? num / den : kShape[t]; @@ -55,35 +55,65 @@ double DraftPolicy::cost_ms(int t) const { double DraftPolicy::clamp_sample(int t, double ms) const { if (!robust_) return ms; const double known = cost_ms(t); // the measured value, or the scaled guess from the other widths - if (cost_n_[t] == 0) { + if (cost_n_[bk_][t] == 0) { // the first use of a width captures its graph: that round says nothing about its cost, the guess from the other widths stands in for (int u = 1; u <= kMaxT; ++u) - if (cost_n_[u] > 0) return known; + if (cost_n_[bk_][u] > 0) return known; return ms; } return std::min(ms, 2.0 * known); } -void DraftPolicy::seed_costs(const double* ms, int n, double weight) { +void DraftPolicy::seed_costs(const double* ms, int n, double weight, int bucket) { + bucket = std::clamp(bucket, 0, kCtxBuckets - 1); for (int t = 1; t <= std::min(n, kMaxT); ++t) - if (ms[t - 1] > 0.0 && cost_n_[t] == 0) { - cost_[t] = ms[t - 1]; - cost_n_[t] = weight; + if (ms[t - 1] > 0.0 && cost_n_[bucket][t] == 0) { + cost_[bucket][t] = ms[t - 1]; + cost_n_[bucket][t] = weight; } } +void DraftPolicy::set_context(long long pos) { + if (!robust_) return; + const int nb = pos < 6000 ? 0 : pos < 24000 ? 1 : 2; + if (nb == bk_) return; + bool has = false; + for (int t = 1; t <= kMaxT; ++t) has = has || cost_n_[nb][t] > 0; + if (!has) { // nothing measured at this length yet: the nearest bucket that has data is the prior (one sample's worth) + for (int d = 1; d < kCtxBuckets && !has; ++d) + for (const int src : {nb - d, nb + d}) { + if (src < 0 || src >= kCtxBuckets || has) continue; + for (int t = 1; t <= kMaxT; ++t) + if (cost_n_[src][t] > 0) { + cost_[nb][t] = cost_[src][t]; + cost_n_[nb][t] = 1.0; + last_[nb][t] = rounds_; + has = true; + } + } + } + bk_ = nb; +} + std::string DraftPolicy::cost_table_text() const { std::string out; char b[64]; - for (int t = 1; t <= kMaxT; ++t) - if (cost_n_[t] > 0) { - std::snprintf(b, sizeof b, "%sT%d:%.2f(n=%.0f)", out.empty() ? "" : " ", t, cost_[t], cost_n_[t]); - out += b; - } + for (int k = 0; k < kCtxBuckets; ++k) { + bool any = false; + for (int t = 1; t <= kMaxT; ++t) any = any || cost_n_[k][t] > 0; + if (!any) continue; + out += (out.empty() ? "" : " | "); + out += k == 0 ? "ctx<6K:" : k == 1 ? "ctx<24K:" : "ctx>=24K:"; + for (int t = 1; t <= kMaxT; ++t) + if (cost_n_[k][t] > 0) { + std::snprintf(b, sizeof b, " T%d:%.2f(n=%.0f)", t, cost_[k][t], cost_n_[k][t]); + out += b; + } + } return out; } -bool DraftPolicy::stale(int t) const { return cost_n_[t] > 0 && rounds_ - last_[t] > kStaleRounds; } +bool DraftPolicy::stale(int t) const { return cost_n_[bk_][t] > 0 && rounds_ - last_[bk_][t] > kStaleRounds; } double DraftPolicy::mtp_tokens(int t) const { if (mtp_n_[t] > 0) return mtp_tok_[t]; @@ -114,7 +144,7 @@ DraftPolicy::Pick DraftPolicy::choose(int t_mtp, int lookup_k, int match) const } // a guessed cost can keep the policy from ever measuring a size: the first few times a confident lookup would // need a size not measured yet, it is tried (verification keeps the output; only the one round's speed is at stake) - if (t_full > p.t && (cost_n_[t_full] < kProbes || stale(t_full)) && q >= 0.85) { + if (t_full > p.t && (cost_n_[bk_][t_full] < kProbes || stale(t_full)) && q >= 0.85) { p.lookup = true; p.t = t_full; } @@ -125,10 +155,10 @@ void DraftPolicy::observe(bool lookup, int t, int accepted, int match, double ro t = std::clamp(t, 1, kMaxT); if (round_ms > 0) { round_ms = clamp_sample(t, round_ms); - const double alpha = robust_ ? std::max(kCostAlpha, 1.0 / (cost_n_[t] + 1.0)) : kCostAlpha; // a running mean for the first samples - cost_[t] = cost_n_[t] > 0 ? (1.0 - alpha) * cost_[t] + alpha * round_ms : round_ms; - cost_n_[t] += 1.0; - last_[t] = rounds_; + const double alpha = robust_ ? std::max(kCostAlpha, 1.0 / (cost_n_[bk_][t] + 1.0)) : kCostAlpha; // a running mean for the first samples + 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; if (lookup) { @@ -183,14 +213,14 @@ bool DraftPolicy::keep_draft(ChainState& st, int idx, float p, int cap) const { st.best_rows = rows; } // a width not measured yet is tried a few times when the chain is likely to hold that far - if (cost_n_[rows] < kProbes && st.q >= 0.15) st.best_rows = rows; + if (cost_n_[bk_][rows] < kProbes && st.q >= 0.15) st.best_rows = rows; } st.kept = st.best_rows - 1; if (rows + 1 > cap) return false; // no row left for another draft // one or two more drafts, each as likely to hold as this one did, could they beat the best width so far? const double v1 = (st.e + st.q * a) / cost_ms(rows + 1); const double v2 = rows + 2 <= cap ? (st.e + st.q * a + st.q * a * a) / cost_ms(rows + 2) : 0.0; - return std::max(v1, v2) > st.best_v * (1.0 + margin_ / 3.0) || (cost_n_[rows + 1] < kProbes && st.q * a >= 0.15); + return std::max(v1, v2) > st.best_v * (1.0 + margin_ / 3.0) || (cost_n_[bk_][rows + 1] < kProbes && st.q * a >= 0.15); } double DraftPolicy::chain_rate(int match) const { @@ -221,7 +251,7 @@ int DraftPolicy::chain(int t_mtp, double p_mtp, int k_avail, int match) const { return best_k; } // as choose(): a size whose cost is only guessed is tried a few times when the continuation looks likely - if ((cost_n_[t_full] < kProbes || stale(t_full)) && p_mtp * c >= 0.6) return kmax; + if ((cost_n_[bk_][t_full] < kProbes || stale(t_full)) && p_mtp * c >= 0.6) return kmax; return 0; } @@ -229,10 +259,10 @@ void DraftPolicy::observe_chain(int t_mtp, int k, int accepted, int match, doubl const int t = std::clamp(t_mtp + k, 1, kMaxT); if (round_ms > 0) { round_ms = clamp_sample(t, round_ms); - const double alpha = robust_ ? std::max(kCostAlpha, 1.0 / (cost_n_[t] + 1.0)) : kCostAlpha; // a running mean for the first samples - cost_[t] = cost_n_[t] > 0 ? (1.0 - alpha) * cost_[t] + alpha * round_ms : round_ms; - cost_n_[t] += 1.0; - last_[t] = rounds_; + const double alpha = robust_ ? std::max(kCostAlpha, 1.0 / (cost_n_[bk_][t] + 1.0)) : kCostAlpha; // a running mean for the first samples + 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; // the MTP part is an ordinary MTP window as far as its own drafts go