Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
--lookup-chain: the draft policy keeps only the chained tokens that pay
Niko12215e0c43baccb7
diff --git a/include/strata/spec/draft_policy.hpp b/include/strata/spec/draft_policy.hppindex c77b86966..c4981e924 100644--- a/include/strata/spec/draft_policy.hpp+++ b/include/strata/spec/draft_policy.hpp@@ -35,6 +35,14 @@ public:/// 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);+ /// --lookup-chain: how many of `k_avail` lookup tokens to append after an MTP window of `t_mtp` tokens whose+ /// drafts all hold with probability `p_mtp` (estimated from the draft layer's own probabilities): the k with the+ /// best expected tokens per ms, E(t_mtp) + p_mtp (c + c^2 + .. + c^k) at the cost of t_mtp + k, if it beats the MTP+ /// window alone by `margin`; c = the acceptance of chained drafts by match length (their own counts). 0 = none.+ int chain(int t_mtp, double p_mtp, int k_avail, int match) const;+ /// After a chained round: the MTP window's size, the chained tokens, the window's accepted drafts, its time.+ void observe_chain(int t_mtp, int k, int accepted, int match, double round_ms);+ double chain_rate(int match) const;double lookup_rate(int match) const; // current q for a match lengthdouble cost_ms(int t) const; // measured or scaled round time of a window of t tokens@@ -47,6 +55,7 @@ private:std::array<double, kMaxT + 1> cost_{}, cost_n_{}; // round ms by window sizestd::array<double, kMaxT + 1> mtp_tok_{}, mtp_n_{}; // tokens committed by MTP windows of that sizestd::array<double, kBuckets> ok_{}, bad_{}; // lookup drafts accepted / windows cut short, decayed+ std::array<double, kBuckets> cok_{}, cbad_{}; // chained lookup drafts, the same (reached rounds only)};} // namespace strata::specdiff --git a/src/program/generate.cpp b/src/program/generate.cppindex a47a94f59..2a46bb06a 100644--- a/src/program/generate.cpp+++ b/src/program/generate.cpp@@ -5481,6 +5481,7 @@ int main(int argc, char** argv) {int64_t t2_rej[8] = {}, t2_hit[8] = {}; // STRATA_MTP_TOP2: rejections inside the MTP drafts by depth, runner-up hitsstd::vector<int32_t> cbuf((size_t) S, 0), ctail;strata::spec::PromptLookupSource lookup_src(sfx);+ static const bool chain_fixed = std::getenv("STRATA_LOOKUP_CHAIN_FIXED") != nullptr; // no policy gateint64_t draft_offered = 0, draft_accepted = 0;// what the session holds once this request is done: the prompt read so far, then every committed tokenstd::vector<int32_t> consumed;@@ -5528,13 +5529,19 @@ int main(int argc, char** argv) {}}// --lookup-chain: what followed an earlier occurrence of the context + the MTP's drafts, after them- int chain_n = 0;+ int chain_n = 0, cm = 0;if (o.lookup_chain > 0 && !first_window && !from_sfx && T < S) {- int cm = 0, csrc = -1;+ int csrc = -1;const int nt = chain_tail(sfx, drafts.data(), T - 1, ctail);chain_n = strata::spec::propose_from_sources(lookup_src, ctail.data(), nt, T - 1,std::min(o.lookup_chain, S - T), o.lookup_chain_min,cbuf.data(), &cm, &csrc);+ // the policy keeps the chained tokens that pay at this machine's measured window costs+ if (chain_n > 0 && !chain_fixed) {+ double p_mtp = 1.0;+ for (int i = 0; i < T - 1; ++i) p_mtp *= dprob[(size_t) i];+ chain_n = policy.chain(T, p_mtp, chain_n, cm);+ }}const int T_mtp = T;T += chain_n;@@ -5621,9 +5628,12 @@ int main(int argc, char** argv) {std::printf("ERR %s\n", err.c_str());return 1;}- if (timed_round && !eos && chain_n == 0) // a chained window is neither an MTP nor a lookup window+ if (timed_round && !eos && chain_n == 0)policy.observe(from_sfx, T, a, sfx_match,std::chrono::duration<double, std::milli>(Clock::now() - round0).count());+ else if (timed_round && !eos) // a chained window: its MTP part, its chain, its cost+ policy.observe_chain(T_mtp, chain_n, a, cm,+ std::chrono::duration<double, std::milli>(Clock::now() - round0).count());if (eos) { finish = "stop"; break; }if (stop_req.load()) { finish = "cancel"; break; }x = outv[(size_t) a];@@ -6384,13 +6394,18 @@ int main(int argc, char** argv) {}}// --lookup-chain: what followed an earlier occurrence of the context + the MTP's drafts, after them- int chain_n = 0;+ int chain_n = 0, cm = 0;if (o.lookup_chain > 0 && use_mtp && !first_window && !from_sfx && T < o.spec) {- int cm = 0, csrc = -1;+ int csrc = -1;const int nt = chain_tail(sfx, drafts.data(), T - 1, ctail);chain_n = strata::spec::propose_from_sources(lookup_src, ctail.data(), nt, T - 1,std::min(o.lookup_chain, o.spec - T), o.lookup_chain_min,cbuf.data(), &cm, &csrc);+ if (chain_n > 0 && std::getenv("STRATA_LOOKUP_CHAIN_FIXED") == nullptr) {+ double p_mtp = 1.0;+ for (int i = 0; i < T - 1; ++i) p_mtp *= dprob[(size_t) i];+ chain_n = policy.chain(T, p_mtp, chain_n, cm);+ }}const int T_mtp = T;T += chain_n;@@ -6481,6 +6496,7 @@ int main(int argc, char** argv) {const double round_ms = std::chrono::duration<double, std::milli>(Clock::now() - t0).count();total_ms += round_ms;if (timed_round && chain_n == 0) policy.observe(from_sfx, T, a, sfx_match, round_ms);+ else if (timed_round) policy.observe_chain(T_mtp, chain_n, a, cm, round_ms);if (rounds % 64 == 0)std::fprintf(stderr, "strata generate: position %lld, %lld tokens, %lld rounds\n", (long long) p,(long long) produced.size(), (long long) rounds);diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cppindex 1ac771985..858f4a563 100644--- a/src/spec/draft_policy.cpp+++ b/src/spec/draft_policy.cpp@@ -98,4 +98,49 @@ void DraftPolicy::observe(bool lookup, int t, int accepted, int match, double ro}}+double DraftPolicy::chain_rate(int match) const {+ // prior: a chained draft is a lookup continuing the MTP's drafts - weaker evidence than a plain match of that length+ constexpr double kChainPrior[kBuckets] = {0.45, 0.65, 0.8, 0.9};+ const int b = bucket(match);+ return (cok_[b] + kPriorN * kChainPrior[b]) / (cok_[b] + cbad_[b] + kPriorN);+}++int DraftPolicy::chain(int t_mtp, double p_mtp, int k_avail, int match) const {+ t_mtp = std::clamp(t_mtp, 1, max_t_);+ const int kmax = std::min(k_avail, max_t_ - t_mtp);+ if (kmax <= 0) return 0;+ const double e0 = mtp_tokens(t_mtp), base = e0 / cost_ms(t_mtp), c = chain_rate(match);+ p_mtp = std::clamp(p_mtp, 0.0, 1.0);+ double gain = 0.0, ci = 1.0, best = 0.0;+ int best_k = 0;+ for (int k = 1; k <= kmax; ++k) {+ ci *= c;+ gain += ci;+ const double r = (e0 + p_mtp * gain) / cost_ms(t_mtp + k);+ if (r > best) { best = r; best_k = k; }+ }+ if (best_k > 0 && best > base * (1.0 + margin_)) return best_k;+ // as choose(): a size whose cost is only guessed is tried a few times when the continuation looks likely+ const int t_full = t_mtp + kmax;+ if (cost_n_[t_full] < kProbes && p_mtp * c >= 0.6) return kmax;+ return 0;+}++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) {+ cost_[t] = cost_n_[t] > 0 ? (1.0 - kCostAlpha) * cost_[t] + kCostAlpha * round_ms : round_ms;+ cost_n_[t] += 1.0;+ }+ // the MTP part is an ordinary MTP window as far as its own drafts go+ const int mtp_acc = std::min(accepted, t_mtp - 1);+ const double got = mtp_acc + 1.0;+ mtp_tok_[t_mtp] = mtp_n_[t_mtp] > 0 ? (1.0 - kTokAlpha) * mtp_tok_[t_mtp] + kTokAlpha * got : got;+ mtp_n_[t_mtp] += 1.0;+ if (accepted < t_mtp - 1) return; // the chain was never reached: it says nothing about the lookup+ const int b = bucket(match), ok = accepted - (t_mtp - 1);+ cok_[b] = kDecay * cok_[b] + ok;+ cbad_[b] = kDecay * cbad_[b] + (ok < k ? 1.0 : 0.0);+}+} // namespace strata::specdiff --git a/src/spec/draft_policy_test.cpp b/src/spec/draft_policy_test.cppindex e04b3de82..3a7a3e610 100644--- a/src/spec/draft_policy_test.cpp+++ b/src/spec/draft_policy_test.cpp@@ -58,6 +58,23 @@ int main() {for (int i = 0; i < 3; ++i) p.observe(true, 6, 5, 40, 3.0 * cost(6)); // it turns out very expensivecheck(!p.choose(4, 5, 40).lookup, "after the probes, the measured cost decides");}+ {+ // --lookup-chain: rows cheap (UMA-like) and the chain always accepted -> chained; rows dear or the chain+ // always rejected -> not+ auto cheap = [](int t) { return 40.0 + 2.0 * (t - 1); };+ DraftPolicy p(8);+ for (int t = 2; t <= 8; ++t)+ for (int i = 0; i < 5; ++i) p.observe(false, t, 1, 0, cheap(t));+ for (int i = 0; i < 30; ++i) p.observe(false, 4, 2, 0, cheap(4));+ for (int i = 0; i < 30; ++i) p.observe_chain(4, 3, 6, 12, cheap(7)); // every chained token accepted+ check(p.chain(4, 0.9, 3, 12) == 3, "chain: cheap rows, always accepted -> all 3 chained");+ DraftPolicy q(8);+ for (int t = 2; t <= 8; ++t)+ for (int i = 0; i < 5; ++i) q.observe(false, t, 1, 0, cost(t));+ for (int i = 0; i < 30; ++i) q.observe_chain(4, 3, 3, 4, cost(7)); // reached, never accepted+ check(q.chain(4, 0.9, 3, 4) == 0, "chain: dear rows, never accepted -> none");+ check(q.chain(4, 0.9, 0, 4) == 0 && q.chain(8, 1.0, 3, 40) == 0, "chain: nothing proposed / no room -> none");+ }std::printf(g_fail ? "FAIL\n" : "PASS\n");return g_fail ? 1 : 0;}