Skip to content

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++

spec: DraftPolicy measures a lookup window size again when its cost is stale (#1252)

Niko122148d2e6cabcf4
diff --git a/include/strata/spec/draft_policy.hpp b/include/strata/spec/draft_policy.hpp
index c4981e924..09c06d74d 100644
--- a/include/strata/spec/draft_policy.hpp
+++ b/include/strata/spec/draft_policy.hpp
@@ -49,6 +49,7 @@ public:
private:
static int bucket(int match);
double mtp_tokens(int t) const;
+ bool stale(int t) const; // a size not measured for a long stretch of rounds
int max_t_;
double margin_;
@@ -56,6 +57,8 @@ private:
std::array<double, kMaxT + 1> mtp_tok_{}, mtp_n_{}; // tokens committed by MTP windows of that size
std::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)
+ double rounds_ = 0; // rounds observed so far
+ std::array<double, kMaxT + 1> last_{}; // the round at which each size was last measured
};
} // namespace strata::spec
diff --git a/src/spec/draft_policy.cpp b/src/spec/draft_policy.cpp
index 858f4a563..9b9eb0a83 100644
--- a/src/spec/draft_policy.cpp
+++ b/src/spec/draft_policy.cpp
@@ -18,6 +18,9 @@ constexpr double kDecay = 0.97; // lookup counts: older windows fade
constexpr double kPriorQ[DraftPolicy::kBuckets] = {0.75, 0.88, 0.93, 0.96};
constexpr double kPriorN = 4.0;
constexpr int kProbes = 3; // a lookup window size is tried this often before its guessed cost can veto it
+// A size not measured for this many rounds is tried again when a confident lookup could use it: its cost was set in
+// one stretch (a prompt's cache refill, say) and, priced high, it would never be measured again (#1252)
+constexpr double kStaleRounds = 300.0;
} // namespace
@@ -47,6 +50,8 @@ double DraftPolicy::cost_ms(int t) const {
return den > 0 ? num / den : kShape[t];
}
+bool DraftPolicy::stale(int t) const { return cost_n_[t] > 0 && rounds_ - last_[t] > kStaleRounds; }
+
double DraftPolicy::mtp_tokens(int t) const {
if (mtp_n_[t] > 0) return mtp_tok_[t];
return 1.0 + 0.7 * (t - 1); // before any MTP window of this size: a typical acceptance
@@ -66,15 +71,17 @@ DraftPolicy::Pick DraftPolicy::choose(int t_mtp, int lookup_k, int match) const
const double r = e / cost_ms(k + 1);
if (r > best) { best = r; best_t = k + 1; }
}
+ const int t_full = std::min(lookup_k, max_t_ - 1) + 1;
if (best_t > 0 && best > base * (1.0 + margin_)) {
p.lookup = true;
p.t = best_t;
+ // a wider size whose cost has gone stale is re-measured, so one slow stretch cannot price it out for good
+ if (best_t < t_full && q >= 0.85 && stale(t_full)) p.t = t_full;
return p;
}
// 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)
- const int t_full = std::min(lookup_k, max_t_ - 1) + 1;
- if (t_full > p.t && cost_n_[t_full] < kProbes && q >= 0.85) {
+ if (t_full > p.t && (cost_n_[t_full] < kProbes || stale(t_full)) && q >= 0.85) {
p.lookup = true;
p.t = t_full;
}
@@ -86,7 +93,9 @@ void DraftPolicy::observe(bool lookup, int t, int accepted, int match, double ro
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;
+ last_[t] = rounds_;
}
+ rounds_ += 1.0;
if (lookup) {
const int b = bucket(match);
ok_[b] = kDecay * ok_[b] + accepted;
@@ -122,7 +131,7 @@ int DraftPolicy::chain(int t_mtp, double p_mtp, int k_avail, int match) const {
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;
+ if ((cost_n_[t_full] < kProbes || stale(t_full)) && p_mtp * c >= 0.6) return kmax;
return 0;
}
@@ -131,7 +140,9 @@ void DraftPolicy::observe_chain(int t_mtp, int k, int accepted, int match, doubl
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;
+ last_[t] = rounds_;
}
+ rounds_ += 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;
diff --git a/src/spec/draft_policy_test.cpp b/src/spec/draft_policy_test.cpp
index 3a7a3e610..923fc79dd 100644
--- a/src/spec/draft_policy_test.cpp
+++ b/src/spec/draft_policy_test.cpp
@@ -58,6 +58,21 @@ int main() {
for (int i = 0; i < 3; ++i) p.observe(true, 6, 5, 40, 3.0 * cost(6)); // it turns out very expensive
check(!p.choose(4, 5, 40).lookup, "after the probes, the measured cost decides");
}
+ {
+ // #1252: a wide size priced high in one slow stretch is measured again after a while
+ DraftPolicy p(6);
+ for (int i = 0; i < 50; ++i) p.observe(false, 4, 3, 0, cost(4)); // a strong MTP
+ for (int t = 2; t <= 6; ++t) for (int i = 0; i < 4; ++i) p.observe(false, t, t - 1, 0, cost(t));
+ for (int i = 0; i < 6; ++i) p.observe(true, 6, 5, 40, 4.0 * cost(6)); // the wide size, in a slow stretch
+ for (int i = 0; i < 30; ++i) p.observe(true, 4, 3, 40, cost(4)); // and the short window's rate is fine
+ bool saw6 = false;
+ for (int i = 0; i < 400 && !saw6; ++i) {
+ const DraftPolicy::Pick k = p.choose(4, 5, 40);
+ if (k.lookup && k.t == 6) { saw6 = true; p.observe(true, 6, 5, 40, cost(6)); }
+ else p.observe(k.lookup, k.t, k.t - 1, 40, cost(k.t));
+ }
+ check(saw6, "a stale wide size is tried again within a few hundred rounds");
+ }
{
// --lookup-chain: rows cheap (UMA-like) and the chain always accepted -> chained; rows dear or the chain
// always rejected -> not