A profitable shorter chained lookup returned before the stale full-size check, so the full chain could stay excluded for good although the policy re-probes stale sizes in the other selection paths (#1252). Check a confident stale full size before returning the profitable shorter chain. The 300-round policy, the EMA, the header and plain lookup are unchanged; a regression test covers the bounded re-probe spacing and the MTP confidence gate.
162 lines
6.9 KiB
C++
162 lines
6.9 KiB
C++
// src/spec/draft_policy.cpp - see include/strata/spec/draft_policy.hpp.
|
|
#include "strata/spec/draft_policy.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
namespace strata::spec {
|
|
namespace {
|
|
|
|
// The shape of a round's cost by window size, relative to one token, used only for sizes not measured yet (the
|
|
// measured round times replace it). Between the RTX 5070's measured curves: +10 ms per token with every missed expert
|
|
// on the CPU (bench/results/2026-09-27-spec/window-cost), flatter with the default CPU/DMA split.
|
|
constexpr double kShape[DraftPolicy::kMaxT + 1] = {0.0, 1.0, 1.35, 1.7, 2.05, 2.45, 2.85, 3.25, 3.6};
|
|
constexpr double kCostAlpha = 0.1; // EMA weight of a new round time
|
|
constexpr double kTokAlpha = 0.05; // EMA weight of a new MTP window outcome
|
|
constexpr double kDecay = 0.97; // lookup counts: older windows fade
|
|
// Before a bucket has data: the longer the match, the likelier its continuation (llama.cpp's lookup decoding gates
|
|
// on the same thing); worth 4 observations, so a few real windows override it
|
|
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
|
|
|
|
DraftPolicy::DraftPolicy(int max_t, double margin)
|
|
: max_t_(std::clamp(max_t, 1, kMaxT)), margin_(margin) {}
|
|
|
|
int DraftPolicy::bucket(int match) {
|
|
return match < 6 ? 0 : match < 12 ? 1 : match < 24 ? 2 : 3;
|
|
}
|
|
|
|
double DraftPolicy::lookup_rate(int match) const {
|
|
const int b = bucket(match);
|
|
return (ok_[b] + kPriorN * kPriorQ[b]) / (ok_[b] + bad_[b] + kPriorN);
|
|
}
|
|
|
|
double DraftPolicy::cost_ms(int t) const {
|
|
t = std::clamp(t, 1, kMaxT);
|
|
if (cost_n_[t] > 0) return cost_[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];
|
|
den += w;
|
|
}
|
|
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
|
|
}
|
|
|
|
DraftPolicy::Pick DraftPolicy::choose(int t_mtp, int lookup_k, int match) const {
|
|
Pick p;
|
|
p.t = std::clamp(t_mtp, 1, max_t_);
|
|
if (lookup_k <= 0) return p;
|
|
const double base = mtp_tokens(p.t) / cost_ms(p.t);
|
|
const double q = lookup_rate(match);
|
|
double e = 1.0, qi = 1.0, best = 0.0;
|
|
int best_t = 0;
|
|
for (int k = 1; k <= std::min(lookup_k, max_t_ - 1); ++k) {
|
|
qi *= q;
|
|
e += qi;
|
|
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)
|
|
if (t_full > p.t && (cost_n_[t_full] < kProbes || stale(t_full)) && q >= 0.85) {
|
|
p.lookup = true;
|
|
p.t = t_full;
|
|
}
|
|
return p;
|
|
}
|
|
|
|
void DraftPolicy::observe(bool lookup, int t, int accepted, int match, double round_ms) {
|
|
t = std::clamp(t, 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;
|
|
last_[t] = rounds_;
|
|
}
|
|
rounds_ += 1.0;
|
|
if (lookup) {
|
|
const int b = bucket(match);
|
|
ok_[b] = kDecay * ok_[b] + accepted;
|
|
bad_[b] = kDecay * bad_[b] + (accepted < t - 1 ? 1.0 : 0.0);
|
|
} else {
|
|
const double got = accepted + 1.0;
|
|
mtp_tok_[t] = mtp_n_[t] > 0 ? (1.0 - kTokAlpha) * mtp_tok_[t] + kTokAlpha * got : got;
|
|
mtp_n_[t] += 1.0;
|
|
}
|
|
}
|
|
|
|
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; }
|
|
}
|
|
const int t_full = t_mtp + kmax;
|
|
if (best_k > 0 && best > base * (1.0 + margin_)) {
|
|
// As choose(): a profitable shorter chain must not keep a stale full size from being measured again.
|
|
if (best_k < kmax && p_mtp * c >= 0.6 && stale(t_full)) return kmax;
|
|
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;
|
|
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;
|
|
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;
|
|
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::spec
|